Skip to content

Commit 2b4ff3d

Browse files
committed
Merge branch 'tb/reset-shallow'
Fix in-core inconsistency after fetching into a shallow repository that broke the code to write out commit-graph. * tb/reset-shallow: shallow.c: use '{commit,rollback}_shallow_file' t5537: use test_write_lines and indented heredocs for readability
2 parents cc0c732 + 37b9dca commit 2b4ff3d

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

builtin/receive-pack.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,12 +893,12 @@ static int update_shallow_ref(struct command *cmd, struct shallow_info *si)
893893
opt.env = tmp_objdir_env(tmp_objdir);
894894
setup_alternate_shallow(&shallow_lock, &opt.shallow_file, &extra);
895895
if (check_connected(command_singleton_iterator, cmd, &opt)) {
896-
rollback_lock_file(&shallow_lock);
896+
rollback_shallow_file(the_repository, &shallow_lock);
897897
oid_array_clear(&extra);
898898
return -1;
899899
}
900900

901-
commit_lock_file(&shallow_lock);
901+
commit_shallow_file(the_repository, &shallow_lock);
902902

903903
/*
904904
* Make sure setup_alternate_shallow() for the next ref does

commit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ struct oid_array;
249249
struct ref;
250250
int register_shallow(struct repository *r, const struct object_id *oid);
251251
int unregister_shallow(const struct object_id *oid);
252+
int commit_shallow_file(struct repository *r, struct lock_file *lk);
253+
void rollback_shallow_file(struct repository *r, struct lock_file *lk);
252254
int for_each_commit_graft(each_commit_graft_fn, void *);
253255
int is_repository_shallow(struct repository *r);
254256
struct commit_list *get_shallow_commits(struct object_array *heads,

fetch-pack.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,9 +1629,9 @@ static void update_shallow(struct fetch_pack_args *args,
16291629
if (args->deepen && alternate_shallow_file) {
16301630
if (*alternate_shallow_file == '\0') { /* --unshallow */
16311631
unlink_or_warn(git_path_shallow(the_repository));
1632-
rollback_lock_file(&shallow_lock);
1632+
rollback_shallow_file(the_repository, &shallow_lock);
16331633
} else
1634-
commit_lock_file(&shallow_lock);
1634+
commit_shallow_file(the_repository, &shallow_lock);
16351635
alternate_shallow_file = NULL;
16361636
return;
16371637
}
@@ -1655,7 +1655,7 @@ static void update_shallow(struct fetch_pack_args *args,
16551655
setup_alternate_shallow(&shallow_lock,
16561656
&alternate_shallow_file,
16571657
&extra);
1658-
commit_lock_file(&shallow_lock);
1658+
commit_shallow_file(the_repository, &shallow_lock);
16591659
alternate_shallow_file = NULL;
16601660
}
16611661
oid_array_clear(&extra);
@@ -1693,7 +1693,7 @@ static void update_shallow(struct fetch_pack_args *args,
16931693
setup_alternate_shallow(&shallow_lock,
16941694
&alternate_shallow_file,
16951695
&extra);
1696-
commit_lock_file(&shallow_lock);
1696+
commit_shallow_file(the_repository, &shallow_lock);
16971697
oid_array_clear(&extra);
16981698
oid_array_clear(&ref);
16991699
alternate_shallow_file = NULL;
@@ -1785,7 +1785,7 @@ struct ref *fetch_pack(struct fetch_pack_args *args,
17851785
error(_("remote did not send all necessary objects"));
17861786
free_refs(ref_cpy);
17871787
ref_cpy = NULL;
1788-
rollback_lock_file(&shallow_lock);
1788+
rollback_shallow_file(the_repository, &shallow_lock);
17891789
goto cleanup;
17901790
}
17911791
args->connectivity_checked = 1;

shallow.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,6 @@ int register_shallow(struct repository *r, const struct object_id *oid)
4040

4141
int is_repository_shallow(struct repository *r)
4242
{
43-
/*
44-
* NEEDSWORK: This function updates
45-
* r->parsed_objects->{is_shallow,shallow_stat} as a side effect but
46-
* there is no corresponding function to clear them when the shallow
47-
* file is updated.
48-
*/
49-
5043
FILE *fp;
5144
char buf[1024];
5245
const char *path = r->parsed_objects->alternate_shallow_file;
@@ -79,6 +72,25 @@ int is_repository_shallow(struct repository *r)
7972
return r->parsed_objects->is_shallow;
8073
}
8174

75+
static void reset_repository_shallow(struct repository *r)
76+
{
77+
r->parsed_objects->is_shallow = -1;
78+
stat_validity_clear(r->parsed_objects->shallow_stat);
79+
}
80+
81+
int commit_shallow_file(struct repository *r, struct lock_file *lk)
82+
{
83+
int res = commit_lock_file(lk);
84+
reset_repository_shallow(r);
85+
return res;
86+
}
87+
88+
void rollback_shallow_file(struct repository *r, struct lock_file *lk)
89+
{
90+
rollback_lock_file(lk);
91+
reset_repository_shallow(r);
92+
}
93+
8294
/*
8395
* TODO: use "int" elemtype instead of "int *" when/if commit-slab
8496
* supports a "valid" flag.
@@ -410,10 +422,10 @@ void prune_shallow(unsigned options)
410422
if (write_in_full(fd, sb.buf, sb.len) < 0)
411423
die_errno("failed to write to %s",
412424
get_lock_file_path(&shallow_lock));
413-
commit_lock_file(&shallow_lock);
425+
commit_shallow_file(the_repository, &shallow_lock);
414426
} else {
415427
unlink(git_path_shallow(the_repository));
416-
rollback_lock_file(&shallow_lock);
428+
rollback_shallow_file(the_repository, &shallow_lock);
417429
}
418430
strbuf_release(&sb);
419431
}

t/t5537-fetch-shallow.sh

Lines changed: 46 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ test_expect_success 'setup' '
1616
commit 3 &&
1717
commit 4 &&
1818
git config --global transfer.fsckObjects true &&
19-
test_oid_cache <<-EOF
19+
test_oid_cache <<-\EOF
2020
perl sha1:s/0034shallow %s/0036unshallow %s/
2121
perl sha256:s/004cshallow %s/004eunshallow %s/
2222
EOF
@@ -25,10 +25,7 @@ test_expect_success 'setup' '
2525
test_expect_success 'setup shallow clone' '
2626
git clone --no-local --depth=2 .git shallow &&
2727
git --git-dir=shallow/.git log --format=%s >actual &&
28-
cat <<EOF >expect &&
29-
4
30-
3
31-
EOF
28+
test_write_lines 4 3 >expect &&
3229
test_cmp expect actual
3330
'
3431

@@ -38,10 +35,7 @@ test_expect_success 'clone from shallow clone' '
3835
cd shallow2 &&
3936
git fsck &&
4037
git log --format=%s >actual &&
41-
cat <<EOF >expect &&
42-
4
43-
3
44-
EOF
38+
test_write_lines 4 3 >expect &&
4539
test_cmp expect actual
4640
)
4741
'
@@ -56,11 +50,7 @@ test_expect_success 'fetch from shallow clone' '
5650
git fetch &&
5751
git fsck &&
5852
git log --format=%s origin/master >actual &&
59-
cat <<EOF >expect &&
60-
5
61-
4
62-
3
63-
EOF
53+
test_write_lines 5 4 3 >expect &&
6454
test_cmp expect actual
6555
)
6656
'
@@ -75,10 +65,7 @@ test_expect_success 'fetch --depth from shallow clone' '
7565
git fetch --depth=2 &&
7666
git fsck &&
7767
git log --format=%s origin/master >actual &&
78-
cat <<EOF >expect &&
79-
6
80-
5
81-
EOF
68+
test_write_lines 6 5 >expect &&
8269
test_cmp expect actual
8370
)
8471
'
@@ -89,12 +76,7 @@ test_expect_success 'fetch --unshallow from shallow clone' '
8976
git fetch --unshallow &&
9077
git fsck &&
9178
git log --format=%s origin/master >actual &&
92-
cat <<EOF >expect &&
93-
6
94-
5
95-
4
96-
3
97-
EOF
79+
test_write_lines 6 5 4 3 >expect &&
9880
test_cmp expect actual
9981
)
10082
'
@@ -111,15 +93,10 @@ test_expect_success 'fetch something upstream has but hidden by clients shallow
11193
git fetch ../.git +refs/heads/master:refs/remotes/top/master &&
11294
git fsck &&
11395
git log --format=%s top/master >actual &&
114-
cat <<EOF >expect &&
115-
add-1-back
116-
4
117-
3
118-
EOF
96+
test_write_lines add-1-back 4 3 >expect &&
11997
test_cmp expect actual
12098
) &&
12199
git --git-dir=shallow2/.git cat-file blob $(echo 1|git hash-object --stdin) >/dev/null
122-
123100
'
124101

125102
test_expect_success 'fetch that requires changes in .git/shallow is filtered' '
@@ -133,14 +110,10 @@ test_expect_success 'fetch that requires changes in .git/shallow is filtered' '
133110
cd notshallow &&
134111
git fetch ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
135112
git for-each-ref --format="%(refname)" >actual.refs &&
136-
cat <<EOF >expect.refs &&
137-
refs/remotes/shallow/no-shallow
138-
EOF
113+
echo refs/remotes/shallow/no-shallow >expect.refs &&
139114
test_cmp expect.refs actual.refs &&
140115
git log --format=%s shallow/no-shallow >actual &&
141-
cat <<EOF >expect &&
142-
no-shallow
143-
EOF
116+
echo no-shallow >expect &&
144117
test_cmp expect actual
145118
)
146119
'
@@ -158,21 +131,44 @@ test_expect_success 'fetch --update-shallow' '
158131
git fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
159132
git fsck &&
160133
git for-each-ref --sort=refname --format="%(refname)" >actual.refs &&
161-
cat <<EOF >expect.refs &&
162-
refs/remotes/shallow/master
163-
refs/remotes/shallow/no-shallow
164-
refs/tags/heavy-tag
165-
refs/tags/light-tag
166-
EOF
134+
cat <<-\EOF >expect.refs &&
135+
refs/remotes/shallow/master
136+
refs/remotes/shallow/no-shallow
137+
refs/tags/heavy-tag
138+
refs/tags/light-tag
139+
EOF
140+
test_cmp expect.refs actual.refs &&
141+
git log --format=%s shallow/master >actual &&
142+
test_write_lines 7 6 5 4 3 >expect &&
143+
test_cmp expect actual
144+
)
145+
'
146+
147+
test_expect_success 'fetch --update-shallow (with fetch.writeCommitGraph)' '
148+
(
149+
cd shallow &&
150+
git checkout master &&
151+
commit 8 &&
152+
git tag -m foo heavy-tag-for-graph HEAD^ &&
153+
git tag light-tag-for-graph HEAD^:tracked
154+
) &&
155+
test_config -C notshallow fetch.writeCommitGraph true &&
156+
(
157+
cd notshallow &&
158+
git fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/* &&
159+
git fsck &&
160+
git for-each-ref --sort=refname --format="%(refname)" >actual.refs &&
161+
cat <<-EOF >expect.refs &&
162+
refs/remotes/shallow/master
163+
refs/remotes/shallow/no-shallow
164+
refs/tags/heavy-tag
165+
refs/tags/heavy-tag-for-graph
166+
refs/tags/light-tag
167+
refs/tags/light-tag-for-graph
168+
EOF
167169
test_cmp expect.refs actual.refs &&
168170
git log --format=%s shallow/master >actual &&
169-
cat <<EOF >expect &&
170-
7
171-
6
172-
5
173-
4
174-
3
175-
EOF
171+
test_write_lines 8 7 6 5 4 3 >expect &&
176172
test_cmp expect actual
177173
)
178174
'
@@ -183,10 +179,7 @@ test_expect_success POSIXPERM,SANITY 'shallow fetch from a read-only repo' '
183179
find read-only.git -print | xargs chmod -w &&
184180
git clone --no-local --depth=2 read-only.git from-read-only &&
185181
git --git-dir=from-read-only/.git log --format=%s >actual &&
186-
cat >expect <<EOF &&
187-
add-1-back
188-
4
189-
EOF
182+
test_write_lines add-1-back 4 >expect &&
190183
test_cmp expect actual
191184
'
192185

0 commit comments

Comments
 (0)