mirror of
https://github.com/git/git
synced 2024-08-27 11:39:22 +00:00
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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
5f95c9f850
commit
0cc77c386c
24
shallow.c
24
shallow.c
|
@ -10,7 +10,7 @@
|
||||||
#include "commit-slab.h"
|
#include "commit-slab.h"
|
||||||
|
|
||||||
static int is_shallow = -1;
|
static int is_shallow = -1;
|
||||||
static struct stat shallow_stat;
|
static struct stat_validity shallow_stat;
|
||||||
static char *alternate_shallow_file;
|
static char *alternate_shallow_file;
|
||||||
|
|
||||||
void set_alternate_shallow_file(const char *path, int override)
|
void set_alternate_shallow_file(const char *path, int override)
|
||||||
|
@ -52,12 +52,12 @@ int is_repository_shallow(void)
|
||||||
* shallow file should be used. We could just open it and it
|
* shallow file should be used. We could just open it and it
|
||||||
* will likely fail. But let's do an explicit check instead.
|
* will likely fail. But let's do an explicit check instead.
|
||||||
*/
|
*/
|
||||||
if (!*path ||
|
if (!*path || (fp = fopen(path, "r")) == NULL) {
|
||||||
stat(path, &shallow_stat) ||
|
stat_validity_clear(&shallow_stat);
|
||||||
(fp = fopen(path, "r")) == NULL) {
|
|
||||||
is_shallow = 0;
|
is_shallow = 0;
|
||||||
return is_shallow;
|
return is_shallow;
|
||||||
}
|
}
|
||||||
|
stat_validity_update(&shallow_stat, fileno(fp));
|
||||||
is_shallow = 1;
|
is_shallow = 1;
|
||||||
|
|
||||||
while (fgets(buf, sizeof(buf), fp)) {
|
while (fgets(buf, sizeof(buf), fp)) {
|
||||||
|
@ -137,21 +137,11 @@ struct commit_list *get_shallow_commits(struct object_array *heads, int depth,
|
||||||
|
|
||||||
void check_shallow_file_for_update(void)
|
void check_shallow_file_for_update(void)
|
||||||
{
|
{
|
||||||
struct stat st;
|
if (is_shallow == -1)
|
||||||
|
|
||||||
if (!is_shallow)
|
|
||||||
return;
|
|
||||||
else if (is_shallow == -1)
|
|
||||||
die("BUG: shallow must be initialized by now");
|
die("BUG: shallow must be initialized by now");
|
||||||
|
|
||||||
if (stat(git_path("shallow"), &st))
|
if (!stat_validity_check(&shallow_stat, git_path("shallow")))
|
||||||
die("shallow file was removed during fetch");
|
die("shallow file has changed since we read it");
|
||||||
else if (st.st_mtime != shallow_stat.st_mtime
|
|
||||||
#ifdef USE_NSEC
|
|
||||||
|| ST_MTIME_NSEC(st) != ST_MTIME_NSEC(shallow_stat)
|
|
||||||
#endif
|
|
||||||
)
|
|
||||||
die("shallow file was changed during fetch");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#define SEEN_ONLY 1
|
#define SEEN_ONLY 1
|
||||||
|
|
Loading…
Reference in a new issue