Thread (25 messages) flat view 25 messages, 4 authors, 2026-02-15
STALE210d

Revision v2 of 5 in this series.

Revisions (5)
  1. v1 [diff vs current]
  2. v2 current
  3. v3 [diff vs current]
  4. v4 [diff vs current]
  5. v5 [diff vs current]

[PATCH v2 0/2] shallow: handling fetch relative-deepen

From: Samo Pogačnik via GitGitGadget <hidden>
Date: 2026-01-09 22:23:48

When a shallowed repository gets deepened beyond the beginning of a merged
branch, we may endup with some shallows, that are behind the reachable ones.
Added test 'fetching deepen beyond merged branch' exposes that behaviour.

On the other hand, it seems that equivalent absolute depth driven fetches
result in all the correct shallows. That led to this proposal, which unifies
absolute and relative deepening in a way that the same get_shallow_commits()
call is used in both cases. The difference is only that depth is adapted for
relative deepening by measuring equivalent depth of current local shallow
commits in the current remote repo. Thus a new function get_shallows_depth()
has been added and the function get_reachable_list() became redundant /
removed.

The get_shallows_depth() function also shares the logic of the
get_shallow_commits() function, but it focuses on counting depth of each
existing shallow commit. The minimum result is stored as
'data->deepen_relative', which is set not to be zero for relative deepening
anyway. That way we can allways summ 'data->deepen_relative' and 'depth'
values, because 'data->deepen_relative' is always 0 in absolute deepening.

Samo Pogačnik (2):
  shallow: free local object_array allocations
  shallow: handling fetch relative-deepen

 shallow.c             | 45 +++++++++++++++++--------
 shallow.h             |  1 +
 t/t5500-fetch-pack.sh | 23 +++++++++++++
 upload-pack.c         | 76 +++++--------------------------------------
 4 files changed, 64 insertions(+), 81 deletions(-)


base-commit: f0ef5b6d9bcc258e4cbef93839d1b7465d5212b9
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-2121%2Fspog%2Ffix-fetch-deepen-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-2121/spog/fix-fetch-deepen-v2
Pull-Request: https://github.com/git/git/pull/2121

Range-diff vs v1:

 1:  277c8616a9 ! 1:  f8a8d077cd shallow: free local object_array allocations
     @@ Commit message
          does not free its dynamic elements before the function returns.
          As a result elements remain allocated and their reference forgotten.
      
     +    Also note, that test 'fetching deepen beyond merged branch' added by
     +    'shallow: handling fetch relative-deepen' patch fails without this
     +    correction in linux-leaks and linux-reftable-leaks test runs.
     +
          Signed-off-by: Samo Pogačnik [off-list ref]
      
       ## shallow.c ##
 2:  b352a33c90 ! 2:  ba1f80105f shallow: handling fetch relative-deepen
     @@ Commit message
          shallow: handling fetch relative-deepen
      
          When a shallowed repository gets deepened beyond the beginning of a
     -    merged branch, we may endup with some shallows, that are behind the
     -    reachable ones. Added test 'fetching deepen beyond merged branch'
     -    exposes that behaviour.
     +    merged branch, we may end up with some shallows that are hidden behind
     +    the reachable shallow commits. Added test 'fetching deepen beyond
     +    merged branch' exposes that behaviour.
     +
     +    An example showing the problem based on added test:
     +    0. Whole initial git repo to be cloned from
     +    Graph:
     +    *   033585d (HEAD -> main) Merge branch 'branch'
     +    |\
     +    | * 984f8b1 (branch) five
     +    | * ecb578a four
     +    |/
     +    * 0cb5d20 three
     +    * 2b4e70d two
     +    * 61ba98b one
     +
     +    1. Initial shallow clone --depth=3 (all good)
     +    Shallows:
     +    2b4e70da2a10e1d3231a0ae2df396024735601f1
     +    ecb578a3cf37198d122ae5df7efed9abaca17144
     +    Graph:
     +    *   033585d (HEAD -> main) Merge branch 'branch'
     +    |\
     +    | * 984f8b1 five
     +    | * ecb578a (grafted) four
     +    * 0cb5d20 three
     +    * 2b4e70d (grafted) two
     +
     +    2. Deepen shallow clone with fetch --deepen=1 (NOT OK)
     +    Shallows:
     +    0cb5d204f4ef96ed241feb0f2088c9f4794ba758
     +    61ba98be443fd51c542eb66585a1f6d7e15fcdae
     +    Graph:
     +    *   033585d (HEAD -> main) Merge branch 'branch'
     +    |\
     +    | * 984f8b1 five
     +    | * ecb578a four
     +    |/
     +    * 0cb5d20 (grafted) three
     +    ---
     +    Note that second shallow commit 61ba98be443fd51c542eb66585a1f6d7e15fcdae
     +    is not reachable.
      
          On the other hand, it seems that equivalent absolute depth driven
          fetches result in all the correct shallows. That led to this proposal,
     @@ Commit message
          repo. Thus a new function get_shallows_depth() has been added and the
          function get_reachable_list() became redundant / removed.
      
     +    Same example showing the corrected second step:
     +    2. Deepen shallow clone with fetch --deepen=1 (all good)
     +    Shallow:
     +    61ba98be443fd51c542eb66585a1f6d7e15fcdae
     +    Graph:
     +    *   033585d (HEAD -> main) Merge branch 'branch'
     +    |\
     +    | * 984f8b1 five
     +    | * ecb578a four
     +    |/
     +    * 0cb5d20 three
     +    * 2b4e70d two
     +    * 61ba98b (grafted) one
     +
          The get_shallows_depth() function also shares the logic of the
          get_shallow_commits() function, but it focuses on counting depth of
          each existing shallow commit. The minimum result is stored as
     @@ Commit message
      
          Signed-off-by: Samo Pogačnik [off-list ref]
      
     + ## shallow.c ##
     +@@ shallow.c: static void free_depth_in_slab(int **ptr)
     + {
     + 	FREE_AND_NULL(*ptr);
     + }
     +-struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
     +-		int shallow_flag, int not_shallow_flag)
     ++struct commit_list *get_shallow_commits(struct object_array *heads,
     ++					struct object_array *shallows, int *deepen_relative,
     ++					int depth, int shallow_flag, int not_shallow_flag)
     + {
     +-	size_t i = 0;
     +-	int cur_depth = 0;
     ++	size_t i = 0, j;
     ++	int cur_depth = 0, cur_depth_shallow = 0;
     + 	struct commit_list *result = NULL;
     + 	struct object_array stack = OBJECT_ARRAY_INIT;
     + 	struct commit *commit = NULL;
     +@@ shallow.c: struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
     + 		}
     + 		parse_commit_or_die(commit);
     + 		cur_depth++;
     +-		if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
     +-		    (is_repository_shallow(the_repository) && !commit->parents &&
     +-		     (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
     +-		     graft->nr_parent < 0)) {
     +-			commit_list_insert(commit, &result);
     +-			commit->object.flags |= shallow_flag;
     +-			commit = NULL;
     +-			continue;
     ++		if (shallows) {
     ++			for (j = 0; j < shallows->nr; j++)
     ++				if (oideq(&commit->object.oid, &shallows->objects[j].item->oid))
     ++					if ((!cur_depth_shallow) || (cur_depth < cur_depth_shallow))
     ++						cur_depth_shallow = cur_depth;
     ++
     ++			if ((is_repository_shallow(the_repository) && !commit->parents &&
     ++			     (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
     ++			     graft->nr_parent < 0)) {
     ++				commit = NULL;
     ++				continue;
     ++			}
     ++		} else {
     ++			if ((depth != INFINITE_DEPTH && cur_depth >= depth) ||
     ++			    (is_repository_shallow(the_repository) && !commit->parents &&
     ++			     (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
     ++			     graft->nr_parent < 0)) {
     ++				commit_list_insert(commit, &result);
     ++				commit->object.flags |= shallow_flag;
     ++				commit = NULL;
     ++				continue;
     ++			}
     ++			commit->object.flags |= not_shallow_flag;
     + 		}
     +-		commit->object.flags |= not_shallow_flag;
     + 		for (p = commit->parents, commit = NULL; p; p = p->next) {
     + 			int **depth_slot = commit_depth_at(&depths, p->item);
     + 			if (!*depth_slot) {
     +@@ shallow.c: struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
     + 	}
     + 	deep_clear_commit_depth(&depths, free_depth_in_slab);
     + 	object_array_clear(&stack);
     +-
     ++	if (shallows && deepen_relative)
     ++		*deepen_relative = cur_depth_shallow;
     + 	return result;
     + }
     + 
     +
     + ## shallow.h ##
     +@@ shallow.h: int commit_shallow_file(struct repository *r, struct shallow_lock *lk);
     + void rollback_shallow_file(struct repository *r, struct shallow_lock *lk);
     + 
     + struct commit_list *get_shallow_commits(struct object_array *heads,
     ++					struct object_array *shallows, int *deepen_relative,
     + 					int depth, int shallow_flag, int not_shallow_flag);
     + struct commit_list *get_shallow_commits_by_rev_list(struct strvec *argv,
     + 						    int shallow_flag, int not_shallow_flag);
     +
       ## t/t5500-fetch-pack.sh ##
      @@ t/t5500-fetch-pack.sh: test_expect_success 'fetching deepen' '
       	)
     @@ t/t5500-fetch-pack.sh: test_expect_success 'fetching deepen' '
      +		cd - &&
      +		git clone --bare --depth 3 "file://$(pwd)/shallow-deepen-merged" deepen.git &&
      +		git -C deepen.git fetch origin --deepen=1 &&
     -+		echo "Shallow:" && cat deepen.git/shallow &&
      +		git -C deepen.git rev-list --all >actual &&
     -+		echo "All rev-lis:" && cat actual &&
     -+		for commit in $(sed "/^$/d" deepen.git/shallow); do
     ++		for commit in $(sed "/^$/d" deepen.git/shallow)
     ++		do
      +			test_grep "$commit" actual || exit 1
      +		done
      +	)
     @@ t/t5500-fetch-pack.sh: test_expect_success 'fetching deepen' '
       	rm -rf server client &&
      
       ## upload-pack.c ##
     -@@
     - #include "json-writer.h"
     - #include "strmap.h"
     - #include "promisor-remote.h"
     -+#include "tag.h"
     - 
     - /* Remember to update object flag allocation in object.h */
     - #define THEY_HAVE	(1u << 11)
      @@ upload-pack.c: error:
       	return -1;
       }
       
      -static int get_reachable_list(struct upload_pack_data *data,
      -			      struct object_array *reachable)
     -+define_commit_slab(commit_depth, int *);
     -+static void free_depth_in_slab(int **ptr)
     ++static void get_shallows_depth(struct upload_pack_data *data)
       {
      -	struct child_process cmd = CHILD_PROCESS_INIT;
      -	int i;
     @@ upload-pack.c: error:
      -		o = lookup_object(the_repository, &oid);
      -		if (o && o->type == OBJ_COMMIT) {
      -			o->flags &= ~TMP_MARK;
     -+	FREE_AND_NULL(*ptr);
     -+}
     -+static void get_shallows_depth(struct upload_pack_data *data)
     -+{
     -+	size_t i = 0, j;
     -+	int cur_depth = 0, cur_depth_shallow = 0;
     -+	struct object_array stack = OBJECT_ARRAY_INIT;
     -+	struct commit *commit = NULL;
     -+	struct commit_graft *graft;
     -+	struct commit_depth depths;
     -+	struct object_array *heads = &data->want_obj;
     -+	struct object_array *shallows = &data->shallows;
     -+
     -+	init_commit_depth(&depths);
     -+	while (commit || i < heads->nr || stack.nr) {
     -+		struct commit_list *p;
     -+		if (!commit) {
     -+			if (i < heads->nr) {
     -+				int **depth_slot;
     -+				commit = (struct commit *)
     -+					deref_tag(the_repository,
     -+						  heads->objects[i++].item,
     -+						  NULL, 0);
     -+				if (!commit || commit->object.type != OBJ_COMMIT) {
     -+					commit = NULL;
     -+					continue;
     -+				}
     -+				depth_slot = commit_depth_at(&depths, commit);
     -+				if (!*depth_slot)
     -+					*depth_slot = xmalloc(sizeof(int));
     -+				**depth_slot = 0;
     -+				cur_depth = 0;
     -+			} else {
     -+				commit = (struct commit *)
     -+					object_array_pop(&stack);
     -+				cur_depth = **commit_depth_at(&depths, commit);
     -+			}
     - 		}
     +-		}
      -	}
      -	for (i = get_max_object_index(the_repository); 0 < i; i--) {
      -		o = get_indexed_object(the_repository, i - 1);
     @@ upload-pack.c: error:
      -		    (o->flags & TMP_MARK)) {
      -			add_object_array(o, NULL, reachable);
      -				o->flags &= ~TMP_MARK;
     -+		parse_commit_or_die(commit);
     -+		cur_depth++;
     -+		for (j = 0; j < shallows->nr; j++)
     -+			if (oideq(&commit->object.oid, &shallows->objects[j].item->oid))
     -+				if ((!cur_depth_shallow) || (cur_depth < cur_depth_shallow))
     -+					cur_depth_shallow = cur_depth;
     -+
     -+		if ((is_repository_shallow(the_repository) && !commit->parents &&
     -+		     (graft = lookup_commit_graft(the_repository, &commit->object.oid)) != NULL &&
     -+		     graft->nr_parent < 0)) {
     -+			commit = NULL;
     -+			continue;
     -+		}
     -+		for (p = commit->parents, commit = NULL; p; p = p->next) {
     -+			int **depth_slot = commit_depth_at(&depths, p->item);
     -+			if (!*depth_slot) {
     -+				*depth_slot = xmalloc(sizeof(int));
     -+				**depth_slot = cur_depth;
     -+			} else {
     -+				if (cur_depth >= **depth_slot)
     -+					continue;
     -+				**depth_slot = cur_depth;
     -+			}
     -+			if (p->next)
     -+				add_object_array(&p->item->object,
     -+						NULL, &stack);
     -+			else {
     -+				commit = p->item;
     -+				cur_depth = **commit_depth_at(&depths, commit);
     -+			}
     - 		}
     - 	}
     +-		}
     +-	}
      -	close(cmd.out);
      -
      -	if (finish_command(&cmd)) {
     @@ upload-pack.c: error:
      -out:
      -	child_process_clear(&cmd);
      -	return ret;
     -+	deep_clear_commit_depth(&depths, free_depth_in_slab);
     -+	object_array_clear(&stack);
     -+	data->deepen_relative = cur_depth_shallow;
     ++	get_shallow_commits(&data->want_obj, &data->shallows,
     ++			    &data->deepen_relative, 0,
     ++			    SHALLOW, NOT_SHALLOW);
       }
       
       static int has_unreachable(struct object_array *src, enum allow_uor allow_uor)
     @@ upload-pack.c: static void deepen(struct upload_pack_data *data, int depth)
      +		if (data->deepen_relative)
      +			get_shallows_depth(data);
      +
     -+		result = get_shallow_commits(&data->want_obj,
     ++		result = get_shallow_commits(&data->want_obj, NULL, NULL,
      +					     data->deepen_relative + depth,
       					     SHALLOW, NOT_SHALLOW);
       		send_shallow(data, result);

-- 
gitgitgadget
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help