Skip to content

Commit 732232a

Browse files
spearceJunio C Hamano
authored and
Junio C Hamano
committed
Force writing ref if it doesn't exist.
Normally we try to skip writing a ref if its value hasn't changed but in the special case that the ref doesn't exist but the new value is going to be 0{40} then force writing the ref anyway. Signed-off-by: Shawn O. Pearce <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent c22a7f0 commit 732232a

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

refs.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,7 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
288288
const unsigned char *old_sha1, int mustexist)
289289
{
290290
struct ref_lock *lock;
291+
struct stat st;
291292

292293
lock = xcalloc(1, sizeof(struct ref_lock));
293294
lock->lock_fd = -1;
@@ -303,6 +304,7 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
303304
lock->ref_file = strdup(path);
304305
lock->lock_file = strdup(mkpath("%s.lock", lock->ref_file));
305306
lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen));
307+
lock->force_write = !lstat(lock->ref_file, &st) || errno == ENOENT;
306308

307309
if (safe_create_leading_directories(lock->lock_file))
308310
die("unable to create directory for %s", lock->lock_file);
@@ -405,7 +407,7 @@ int write_ref_sha1(struct ref_lock *lock,
405407

406408
if (!lock)
407409
return -1;
408-
if (!memcmp(lock->old_sha1, sha1, 20)) {
410+
if (!lock->force_write && !memcmp(lock->old_sha1, sha1, 20)) {
409411
unlock_ref(lock);
410412
return 0;
411413
}

refs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ struct ref_lock {
77
char *log_file;
88
unsigned char old_sha1[20];
99
int lock_fd;
10+
int force_write;
1011
};
1112

1213
/*

0 commit comments

Comments
 (0)