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

From: "Samo Pogačnik via GitGitGadget" <[email-masked]>
Date: Sun, 15 Feb 2026 20:11:54 +0000

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             | 73 ++++++++++++++++++++++++++++++++++++-------
 shallow.h             |  2 ++
 t/t5500-fetch-pack.sh | 23 ++++++++++++++
 upload-pack.c         | 72 ++----------------------------------------
 4 files changed, 88 insertions(+), 82 deletions(-)

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

Range-diff vs v4:

 1:  f8a8d077cd = 1:  f8a8d077cd shallow: free local object_array allocations
 2:  e9b20ae06f ! 2:  8d48ba9cd1 shallow: handling fetch relative-deepen
     @@ Commit message
      
          Signed-off-by: Samo Pogačnik <[email-masked]>
      
     +    Fixing v4
     +
     +    Fixing v4 again
     +
       ## shallow.c ##
      @@ shallow.c: static void free_depth_in_slab(int **ptr)
       {
     @@ shallow.c: static void free_depth_in_slab(int **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)
     ++/*
     ++ * This is a common internal function that can either return a list of
     ++ * shallow commits or calculate the current maximum depth of a shallow
     ++ * repository, depending on the input parameters.
     ++ *
     ++ * Depth calculation is triggered by passing the `shallows` parameter.
     ++ * In this case, the computed depth is stored in `max_cur_depth` (if it is
     ++ * provided), and the function returns NULL.
     ++ *
     ++ * Otherwise, `max_cur_depth` remains unchanged and the function returns
     ++ * a list of shallow commits.
     ++ */
     ++static struct commit_list *get_shallows_or_depth(struct object_array *heads,
     ++				struct object_array *shallows, int *max_cur_depth,
     ++				int depth, int shallow_flag, int not_shallow_flag)
       {
     --	size_t i = 0;
     + 	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;
     @@ shallow.c: struct commit_list *get_shallow_commits(struct object_array *heads, i
      -			commit = NULL;
      -			continue;
      +		if (shallows) {
     -+			for (j = 0; j < shallows->nr; j++)
     ++			for (size_t 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))
     ++					if (!cur_depth_shallow || cur_depth < cur_depth_shallow)
      +						cur_depth_shallow = cur_depth;
      +
      +			if ((is_repository_shallow(the_repository) && !commit->parents &&
     @@ shallow.c: struct commit_list *get_shallow_commits(struct object_array *heads, i
       			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;
     + 
     ++	if (shallows && max_cur_depth)
     ++		*max_cur_depth = cur_depth_shallow;
       	return result;
       }
       
     ++int get_shallows_depth(struct object_array *heads, struct object_array *shallows)
     ++{
     ++	int max_cur_depth = 0;
     ++	get_shallows_or_depth(heads, shallows, &max_cur_depth, 0, 0, 0);
     ++	return max_cur_depth;
     ++
     ++}
     ++
     ++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)
     ++{
     ++	if (shallows && deepen_relative) {
     ++		depth += get_shallows_depth(heads, shallows);
     ++	}
     ++	return get_shallows_or_depth(heads, NULL, NULL,
     ++				     depth, shallow_flag, not_shallow_flag);
     ++}
     ++
     + static void show_commit(struct commit *commit, void *data)
     + {
     + 	commit_list_insert(commit, data);
      
       ## shallow.h ##
      @@ shallow.h: int commit_shallow_file(struct repository *r, struct shallow_lock *lk);
     + /* rollback $GIT_DIR/shallow and reset stat-validity checks */
       void rollback_shallow_file(struct repository *r, struct shallow_lock *lk);
       
     ++int get_shallows_depth(struct object_array *heads, struct object_array *shallows);
       struct commit_list *get_shallow_commits(struct object_array *heads,
     -+					struct object_array *shallows, int *deepen_relative,
     ++					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);
     @@ upload-pack.c: error:
       
      -static int get_reachable_list(struct upload_pack_data *data,
      -			      struct object_array *reachable)
     -+static void get_shallows_depth(struct upload_pack_data *data)
     - {
     +-{
      -	struct child_process cmd = CHILD_PROCESS_INIT;
      -	int i;
      -	struct object *o;
     @@ upload-pack.c: error:
      -out:
      -	child_process_clear(&cmd);
      -	return ret;
     -+	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)
     + {
     + 	struct child_process cmd = CHILD_PROCESS_INIT;
      @@ upload-pack.c: static void deepen(struct upload_pack_data *data, int depth)
       			struct object *object = data->shallows.objects[i].item;
       			object->flags |= NOT_SHALLOW;
     @@ upload-pack.c: static void deepen(struct upload_pack_data *data, int depth)
       		struct commit_list *result;
       
      -		result = get_shallow_commits(&data->want_obj, depth,
     -+		if (data->deepen_relative)
     -+			get_shallows_depth(data);
     -+
     -+		result = get_shallow_commits(&data->want_obj, NULL, NULL,
     -+					     data->deepen_relative + depth,
     ++		result = get_shallow_commits(&data->want_obj, &data->shallows,
     ++					     data->deepen_relative, depth,
       					     SHALLOW, NOT_SHALLOW);
       		send_shallow(data, result);
       		free_commit_list(result);

-- 
gitgitgadget