summaryrefslogtreecommitdiff
path: root/shallow.c
diff options
context:
space:
mode:
authorTaylor Blau <me@ttaylorr.com>2020-04-23 00:25:45 (GMT)
committerJunio C Hamano <gitster@pobox.com>2020-04-24 20:56:39 (GMT)
commit37b9dcabfc48b0cbce638140279878dac37aec73 (patch)
tree687775f0a523a5571625cfc01080eabe937b91c3 /shallow.c
parent8a8da49728b82a54e0f76c6cceba65be18095493 (diff)
downloadgit-37b9dcabfc48b0cbce638140279878dac37aec73.zip
git-37b9dcabfc48b0cbce638140279878dac37aec73.tar.gz
git-37b9dcabfc48b0cbce638140279878dac37aec73.tar.bz2
shallow.c: use '{commit,rollback}_shallow_file'
In bd0b42aed3 (fetch-pack: do not take shallow lock unnecessarily, 2019-01-10), the author noted that 'is_repository_shallow' produces visible side-effect(s) by setting 'is_shallow' and 'shallow_stat'. This is a problem for e.g., fetching with '--update-shallow' in a shallow repository with 'fetch.writeCommitGraph' enabled, since the update to '.git/shallow' will cause Git to think that the repository isn't shallow when it is, thereby circumventing the commit-graph compatibility check. This causes problems in shallow repositories with at least shallow refs that have at least one ancestor (since the client won't have those objects, and therefore can't take the reachability closure over commits when writing a commit-graph). Address this by introducing thin wrappers over 'commit_lock_file' and 'rollback_lock_file' for use specifically when the lock is held over '.git/shallow'. These wrappers (appropriately called 'commit_shallow_file' and 'rollback_shallow_file') call into their respective functions in 'lockfile.h', but additionally reset validity checks used by the shallow machinery. Replace each instance of 'commit_lock_file' and 'rollback_lock_file' with 'commit_shallow_file' and 'rollback_shallow_file' when the lock being held is over the '.git/shallow' file. As a result, 'prune_shallow' can now only be called once (since 'check_shallow_file_for_update' will die after calling 'reset_repository_shallow'). But, this is OK since we only call 'prune_shallow' at most once per process. Helped-by: Jonathan Tan <jonathantanmy@google.com> Helped-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Taylor Blau <me@ttaylorr.com> Reviewed-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'shallow.c')
-rw-r--r--shallow.c30
1 files changed, 21 insertions, 9 deletions
diff --git a/shallow.c b/shallow.c
index 7fd04af..5010a6c 100644
--- a/shallow.c
+++ b/shallow.c
@@ -40,13 +40,6 @@ int register_shallow(struct repository *r, const struct object_id *oid)
int is_repository_shallow(struct repository *r)
{
- /*
- * NEEDSWORK: This function updates
- * r->parsed_objects->{is_shallow,shallow_stat} as a side effect but
- * there is no corresponding function to clear them when the shallow
- * file is updated.
- */
-
FILE *fp;
char buf[1024];
const char *path = r->parsed_objects->alternate_shallow_file;
@@ -79,6 +72,25 @@ int is_repository_shallow(struct repository *r)
return r->parsed_objects->is_shallow;
}
+static void reset_repository_shallow(struct repository *r)
+{
+ r->parsed_objects->is_shallow = -1;
+ stat_validity_clear(r->parsed_objects->shallow_stat);
+}
+
+int commit_shallow_file(struct repository *r, struct lock_file *lk)
+{
+ int res = commit_lock_file(lk);
+ reset_repository_shallow(r);
+ return res;
+}
+
+void rollback_shallow_file(struct repository *r, struct lock_file *lk)
+{
+ rollback_lock_file(lk);
+ reset_repository_shallow(r);
+}
+
/*
* TODO: use "int" elemtype instead of "int *" when/if commit-slab
* supports a "valid" flag.
@@ -410,10 +422,10 @@ void prune_shallow(unsigned options)
if (write_in_full(fd, sb.buf, sb.len) < 0)
die_errno("failed to write to %s",
get_lock_file_path(&shallow_lock));
- commit_lock_file(&shallow_lock);
+ commit_shallow_file(the_repository, &shallow_lock);
} else {
unlink(git_path_shallow(the_repository));
- rollback_lock_file(&shallow_lock);
+ rollback_shallow_file(the_repository, &shallow_lock);
}
strbuf_release(&sb);
}