aboutsummaryrefslogtreecommitdiff
path: root/shallow.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2026-03-04 10:52:59 -0800
committerJunio C Hamano <gitster@pobox.com>2026-03-04 10:52:59 -0800
commitfa383b96c7211a415152dffaf6b272bb09116f06 (patch)
treed0bafbc0b0e2b8843b1b4d942922c6ccbf49f6df /shallow.c
parent22c9b6bd93ef8975deebd2a4439aa428ab22118b (diff)
parent3ef68ff40ebf75bba9c4b05f50197190ff1abda2 (diff)
downloadgit-fa383b96c7211a415152dffaf6b272bb09116f06.tar.xz
Merge branch 'sp/shallow-deepen-relative-fix'
"git fetch --deepen" that tries to go beyond merged branch used to get confused where the updated shallow points are, which has been corrected. * sp/shallow-deepen-relative-fix: shallow: handling fetch relative-deepen shallow: free local object_array allocations
Diffstat (limited to 'shallow.c')
-rw-r--r--shallow.c73
1 files changed, 61 insertions, 12 deletions
diff --git a/shallow.c b/shallow.c
index 0409b1354c..7a3dd56795 100644
--- a/shallow.c
+++ b/shallow.c
@@ -130,11 +130,24 @@ 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)
+/*
+ * 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;
- int cur_depth = 0;
+ int cur_depth = 0, cur_depth_shallow = 0;
struct commit_list *result = NULL;
struct object_array stack = OBJECT_ARRAY_INIT;
struct commit *commit = NULL;
@@ -168,16 +181,30 @@ 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 (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)
+ 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) {
@@ -198,10 +225,32 @@ 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 && 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);