Skip to content

Commit 9809814

Browse files
jonathantanmygitster
authored andcommitted
commit,shallow: unparse commits if grafts changed
When a commit is parsed, it pretends to have a different (possibly empty) list of parents if there is graft information for that commit. But there is a bug that could occur when a commit is parsed, the graft information is updated (for example, when a shallow file is rewritten), and the same commit is subsequently used: the parents of the commit do not conform to the updated graft information, but the information at the time of parsing. This is usually not an issue, as a commit is usually introduced into the repository at the same time as its graft information. That means that when we try to parse that commit, we already have its graft information. But it is an issue when fetching a shallow point directly into a repository with submodules. The function assign_shallow_commits_to_refs() parses all sought objects (including the shallow point, which we are directly fetching). In update_shallow() in fetch-pack.c, assign_shallow_commits_to_refs() is called before commit_shallow_file(), which means that the shallow point would have been parsed before graft information is updated. Once a commit is parsed, it is no longer sensitive to any graft information updates. This parsed commit is subsequently used when we do a revision walk to search for submodules to fetch, meaning that the commit is considered to have parents even though it is a shallow point (and therefore should be treated as having no parents). Therefore, whenever graft information is updated, mark the commits that were previously grafts and the commits that are newly grafts as unparsed. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2668e36 commit 9809814

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

commit.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,17 @@ int commit_graft_pos(struct repository *r, const struct object_id *oid)
120120
commit_graft_oid_access);
121121
}
122122

123+
static void unparse_commit(struct repository *r, const struct object_id *oid)
124+
{
125+
struct commit *c = lookup_commit(r, oid);
126+
127+
if (!c->object.parsed)
128+
return;
129+
free_commit_list(c->parents);
130+
c->parents = NULL;
131+
c->object.parsed = 0;
132+
}
133+
123134
int register_commit_graft(struct repository *r, struct commit_graft *graft,
124135
int ignore_dups)
125136
{
@@ -145,6 +156,7 @@ int register_commit_graft(struct repository *r, struct commit_graft *graft,
145156
(r->parsed_objects->grafts_nr - pos - 1) *
146157
sizeof(*r->parsed_objects->grafts));
147158
r->parsed_objects->grafts[pos] = graft;
159+
unparse_commit(r, &graft->oid);
148160
return 0;
149161
}
150162

@@ -253,8 +265,10 @@ void reset_commit_grafts(struct repository *r)
253265
{
254266
int i;
255267

256-
for (i = 0; i < r->parsed_objects->grafts_nr; i++)
268+
for (i = 0; i < r->parsed_objects->grafts_nr; i++) {
269+
unparse_commit(r, &r->parsed_objects->grafts[i]->oid);
257270
free(r->parsed_objects->grafts[i]);
271+
}
258272
r->parsed_objects->grafts_nr = 0;
259273
r->parsed_objects->commit_graft_prepared = 0;
260274
}

shallow.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ int commit_shallow_file(struct repository *r, struct shallow_lock *lk)
9797
{
9898
int res = commit_lock_file(&lk->lock);
9999
reset_repository_shallow(r);
100+
101+
/*
102+
* Update in-memory data structures with the new shallow information,
103+
* including unparsing all commits that now have grafts.
104+
*/
105+
is_repository_shallow(r);
106+
100107
return res;
101108
}
102109

t/t5537-fetch-shallow.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,15 @@ test_expect_success 'fetch --update-shallow into a repo with submodules' '
170170
git -C repo-with-sub fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/*
171171
'
172172

173+
test_expect_success 'fetch --update-shallow a commit that is also a shallow point into a repo with submodules' '
174+
git init repo-with-unreachable-upstream-shallow &&
175+
git -C repo-with-unreachable-upstream-shallow submodule add ../a-submodule a-submodule &&
176+
git -C repo-with-unreachable-upstream-shallow commit -m "added submodule" &&
177+
178+
SHALLOW=$(cat shallow/.git/shallow) &&
179+
git -C repo-with-unreachable-upstream-shallow fetch --update-shallow ../shallow/.git "$SHALLOW":refs/heads/a-shallow
180+
'
181+
173182
test_expect_success 'fetch --update-shallow (with fetch.writeCommitGraph)' '
174183
(
175184
cd shallow &&

0 commit comments

Comments
 (0)