Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 0cc77c3

Browse files
peffgitster
authored andcommitted
shallow: use stat_validity to check for up-to-date file
When we are about to write the shallow file, we check that it has not changed since we last read it. Instead of hand-rolling this, we can use stat_validity. This is built around the index stat-check, so it is more robust than just checking the mtime, as we do now (it uses the same check as we do for index files). The new code also handles the case of a shallow file appearing unexpectedly. With the current code, two simultaneous processes making us shallow (e.g., two "git fetch --depth=1" running at the same time in a non-shallow repository) can race to overwrite each other. As a bonus, we also remove a race in determining the stat information of what we read (we stat and then open, leaving a race window; instead we should open and then fstat the descriptor). Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5f95c9f commit 0cc77c3

File tree

1 file changed

+7
-17
lines changed

1 file changed

+7
-17
lines changed

shallow.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "commit-slab.h"
1111

1212
static int is_shallow = -1;
13-
static struct stat shallow_stat;
13+
static struct stat_validity shallow_stat;
1414
static char *alternate_shallow_file;
1515

1616
void set_alternate_shallow_file(const char *path, int override)
@@ -52,12 +52,12 @@ int is_repository_shallow(void)
5252
* shallow file should be used. We could just open it and it
5353
* will likely fail. But let's do an explicit check instead.
5454
*/
55-
if (!*path ||
56-
stat(path, &shallow_stat) ||
57-
(fp = fopen(path, "r")) == NULL) {
55+
if (!*path || (fp = fopen(path, "r")) == NULL) {
56+
stat_validity_clear(&shallow_stat);
5857
is_shallow = 0;
5958
return is_shallow;
6059
}
60+
stat_validity_update(&shallow_stat, fileno(fp));
6161
is_shallow = 1;
6262

6363
while (fgets(buf, sizeof(buf), fp)) {
@@ -137,21 +137,11 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
137137

138138
void check_shallow_file_for_update(void)
139139
{
140-
struct stat st;
141-
142-
if (!is_shallow)
143-
return;
144-
else if (is_shallow == -1)
140+
if (is_shallow == -1)
145141
die("BUG: shallow must be initialized by now");
146142

147-
if (stat(git_path("shallow"), &st))
148-
die("shallow file was removed during fetch");
149-
else if (st.st_mtime != shallow_stat.st_mtime
150-
#ifdef USE_NSEC
151-
|| ST_MTIME_NSEC(st) != ST_MTIME_NSEC(shallow_stat)
152-
#endif
153-
)
154-
die("shallow file was changed during fetch");
143+
if (!stat_validity_check(&shallow_stat, git_path("shallow")))
144+
die("shallow file has changed since we read it");
155145
}
156146

157147
#define SEEN_ONLY 1

0 commit comments

Comments
 (0)