upload-pack: fix off-by-one depth calculation in shallow clone

get_shallow_commits() is used to determine the cut points at a given depth (i.e. the number of commits in a chain that the user likes to get). However we count current depth up to the commit "commit" but we do the cutting at its parents (i.e. current depth + 1). This makes upload-pack always return one commit more than requested. This patch fixes it. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Nguyễn Thái Ngọc Duy committed Jan 11, 2013 at 16:05 UTC 682c7d2f1a2d1a5443777237450505738af2ff1a
2 files changed +26 -7
shallow.c
+7 -1
@@ -72,8 +72,14 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
72 }
73 if (parse_commit(commit))
74 die("invalid commit");
75 - commit->object.flags |= not_shallow_flag;
75 cur_depth++;
76 + if (cur_depth >= depth) {
77 + commit_list_insert(commit, &result);
78 + commit->object.flags |= shallow_flag;
79 + commit = NULL;
80 + continue;
81 + }
82 + commit->object.flags |= not_shallow_flag;
83 for (p = commit->parents, commit = NULL; p; p = p->next) {
84 if (!p->item->util) {
85 int *pointer = xmalloc(sizeof(int));
t/t5500-fetch-pack.sh
+19 -6
@@ -130,16 +130,25 @@ test_expect_success 'single given branch clone' '
130 test_must_fail git --git-dir=branch-a/.git rev-parse origin/B
131 '
132
133 +test_expect_success 'clone shallow depth 1' '
134 + git clone --no-single-branch --depth 1 "file://$(pwd)/." shallow0 &&
135 + test "`git --git-dir=shallow0/.git rev-list --count HEAD`" = 1
136 +'
137 +
138 test_expect_success 'clone shallow' '
139 git clone --no-single-branch --depth 2 "file://$(pwd)/." shallow
140 '
141
142 +test_expect_success 'clone shallow depth count' '
143 + test "`git --git-dir=shallow/.git rev-list --count HEAD`" = 2
144 +'
145 +
146 test_expect_success 'clone shallow object count' '
147 (
148 cd shallow &&
149 git count-objects -v
150 ) > count.shallow &&
142 - grep "^in-pack: 18" count.shallow
151 + grep "^in-pack: 12" count.shallow
152 '
153
154 test_expect_success 'clone shallow object count (part 2)' '
@@ -256,12 +265,16 @@ test_expect_success 'additional simple shallow deepenings' '
265 )
266 '
267
268 +test_expect_success 'clone shallow depth count' '
269 + test "`git --git-dir=shallow/.git rev-list --count HEAD`" = 11
270 +'
271 +
272 test_expect_success 'clone shallow object count' '
273 (
274 cd shallow &&
275 git count-objects -v
276 ) > count.shallow &&
264 - grep "^count: 52" count.shallow
277 + grep "^count: 55" count.shallow
278 '
279
280 test_expect_success 'fetch --no-shallow on full repo' '
@@ -293,7 +306,7 @@ test_expect_success 'clone shallow object count' '
306 cd shallow2 &&
307 git count-objects -v
308 ) > count.shallow2 &&
296 - grep "^in-pack: 6" count.shallow2
309 + grep "^in-pack: 3" count.shallow2
310 '
311
312 test_expect_success 'clone shallow with --branch' '
@@ -301,7 +314,7 @@ test_expect_success 'clone shallow with --branch' '
314 '
315
316 test_expect_success 'clone shallow object count' '
304 - echo "in-pack: 6" > count3.expected &&
317 + echo "in-pack: 3" > count3.expected &&
318 GIT_DIR=shallow3/.git git count-objects -v |
319 grep "^in-pack" > count3.actual &&
320 test_cmp count3.expected count3.actual
@@ -330,7 +343,7 @@ EOF
343 GIT_DIR=shallow6/.git git tag -l >taglist.actual &&
344 test_cmp taglist.expected taglist.actual &&
345
333 - echo "in-pack: 7" > count6.expected &&
346 + echo "in-pack: 4" > count6.expected &&
347 GIT_DIR=shallow6/.git git count-objects -v |
348 grep "^in-pack" > count6.actual &&
349 test_cmp count6.expected count6.actual
@@ -345,7 +358,7 @@ EOF
358 GIT_DIR=shallow7/.git git tag -l >taglist.actual &&
359 test_cmp taglist.expected taglist.actual &&
360
348 - echo "in-pack: 7" > count7.expected &&
361 + echo "in-pack: 4" > count7.expected &&
362 GIT_DIR=shallow7/.git git count-objects -v |
363 grep "^in-pack" > count7.actual &&
364 test_cmp count7.expected count7.actual