Skip to content

Commit 2a69ff0

Browse files
jonathantanmygitster
authored andcommitted
shallow: reset commit grafts when shallow is reset
When reset_repository_shallow() is called, Git clears its cache of shallow information, so that if shallow information is re-requested, Git will read fresh data from disk instead of reusing its stale cached data. However, the cache of commit grafts is not likewise cleared, even though there are commit grafts created from shallow information. This means that if on-disk shallow information were to be updated and then a commit-graft-using codepath were run (for example, a revision walk), Git would be using stale commit graft information. This can be seen from the test in this patch, in which Git performs a revision walk (to check for changed submodules) after a fetch with --update-shallow. Therefore, clear the cache of commit grafts whenever reset_repository_shallow() is called. Signed-off-by: Jonathan Tan <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 74cc1aa commit 2a69ff0

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

commit.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,16 @@ int for_each_commit_graft(each_commit_graft_fn fn, void *cb_data)
249249
return ret;
250250
}
251251

252+
void reset_commit_grafts(struct repository *r)
253+
{
254+
int i;
255+
256+
for (i = 0; i < r->parsed_objects->grafts_nr; i++)
257+
free(r->parsed_objects->grafts[i]);
258+
r->parsed_objects->grafts_nr = 0;
259+
r->parsed_objects->commit_graft_prepared = 0;
260+
}
261+
252262
struct commit_buffer {
253263
void *buffer;
254264
unsigned long size;

commit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ int commit_graft_pos(struct repository *r, const struct object_id *oid);
249249
int register_commit_graft(struct repository *r, struct commit_graft *, int);
250250
void prepare_commit_graft(struct repository *r);
251251
struct commit_graft *lookup_commit_graft(struct repository *r, const struct object_id *oid);
252+
void reset_commit_grafts(struct repository *r);
252253

253254
struct commit *get_fork_point(const char *refname, struct commit *commit);
254255

shallow.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ static void reset_repository_shallow(struct repository *r)
9090
{
9191
r->parsed_objects->is_shallow = -1;
9292
stat_validity_clear(r->parsed_objects->shallow_stat);
93+
reset_commit_grafts(r);
9394
}
9495

9596
int commit_shallow_file(struct repository *r, struct shallow_lock *lk)

submodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "parse-options.h"
2323
#include "object-store.h"
2424
#include "commit-reach.h"
25+
#include "shallow.h"
2526

2627
static int config_update_recurse_submodules = RECURSE_SUBMODULES_OFF;
2728
static int initialized_fetch_ref_tips;

t/t5537-fetch-shallow.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,15 @@ test_expect_success 'fetch --update-shallow' '
161161
)
162162
'
163163

164+
test_expect_success 'fetch --update-shallow into a repo with submodules' '
165+
git init a-submodule &&
166+
test_commit -C a-submodule foo &&
167+
git init repo-with-sub &&
168+
git -C repo-with-sub submodule add ../a-submodule a-submodule &&
169+
git -C repo-with-sub commit -m "added submodule" &&
170+
git -C repo-with-sub fetch --update-shallow ../shallow/.git refs/heads/*:refs/remotes/shallow/*
171+
'
172+
164173
test_expect_success 'fetch --update-shallow (with fetch.writeCommitGraph)' '
165174
(
166175
cd shallow &&

0 commit comments

Comments
 (0)