Skip to content

Commit 682c7d2

Browse files
pcloudsgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4dcb167 commit 682c7d2

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

shallow.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
7272
}
7373
if (parse_commit(commit))
7474
die("invalid commit");
75-
commit->object.flags |= not_shallow_flag;
7675
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;
7783
for (p = commit->parents, commit = NULL; p; p = p->next) {
7884
if (!p->item->util) {
7985
int *pointer = xmalloc(sizeof(int));

t/t5500-fetch-pack.sh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,16 +130,25 @@ test_expect_success 'single given branch clone' '
130130
test_must_fail git --git-dir=branch-a/.git rev-parse origin/B
131131
'
132132

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+
133138
test_expect_success 'clone shallow' '
134139
git clone --no-single-branch --depth 2 "file://$(pwd)/." shallow
135140
'
136141

142+
test_expect_success 'clone shallow depth count' '
143+
test "`git --git-dir=shallow/.git rev-list --count HEAD`" = 2
144+
'
145+
137146
test_expect_success 'clone shallow object count' '
138147
(
139148
cd shallow &&
140149
git count-objects -v
141150
) > count.shallow &&
142-
grep "^in-pack: 18" count.shallow
151+
grep "^in-pack: 12" count.shallow
143152
'
144153

145154
test_expect_success 'clone shallow object count (part 2)' '
@@ -256,12 +265,16 @@ test_expect_success 'additional simple shallow deepenings' '
256265
)
257266
'
258267

268+
test_expect_success 'clone shallow depth count' '
269+
test "`git --git-dir=shallow/.git rev-list --count HEAD`" = 11
270+
'
271+
259272
test_expect_success 'clone shallow object count' '
260273
(
261274
cd shallow &&
262275
git count-objects -v
263276
) > count.shallow &&
264-
grep "^count: 52" count.shallow
277+
grep "^count: 55" count.shallow
265278
'
266279

267280
test_expect_success 'fetch --no-shallow on full repo' '
@@ -293,15 +306,15 @@ test_expect_success 'clone shallow object count' '
293306
cd shallow2 &&
294307
git count-objects -v
295308
) > count.shallow2 &&
296-
grep "^in-pack: 6" count.shallow2
309+
grep "^in-pack: 3" count.shallow2
297310
'
298311

299312
test_expect_success 'clone shallow with --branch' '
300313
git clone --depth 1 --branch A "file://$(pwd)/." shallow3
301314
'
302315

303316
test_expect_success 'clone shallow object count' '
304-
echo "in-pack: 6" > count3.expected &&
317+
echo "in-pack: 3" > count3.expected &&
305318
GIT_DIR=shallow3/.git git count-objects -v |
306319
grep "^in-pack" > count3.actual &&
307320
test_cmp count3.expected count3.actual
@@ -330,7 +343,7 @@ EOF
330343
GIT_DIR=shallow6/.git git tag -l >taglist.actual &&
331344
test_cmp taglist.expected taglist.actual &&
332345
333-
echo "in-pack: 7" > count6.expected &&
346+
echo "in-pack: 4" > count6.expected &&
334347
GIT_DIR=shallow6/.git git count-objects -v |
335348
grep "^in-pack" > count6.actual &&
336349
test_cmp count6.expected count6.actual
@@ -345,7 +358,7 @@ EOF
345358
GIT_DIR=shallow7/.git git tag -l >taglist.actual &&
346359
test_cmp taglist.expected taglist.actual &&
347360
348-
echo "in-pack: 7" > count7.expected &&
361+
echo "in-pack: 4" > count7.expected &&
349362
GIT_DIR=shallow7/.git git count-objects -v |
350363
grep "^in-pack" > count7.actual &&
351364
test_cmp count7.expected count7.actual

0 commit comments

Comments
 (0)