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 <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn Pearce committed
May 19, 2006 at 03:29 UTC
732232a123e1e61e38babb1c572722bb8a189ba3
2 files changed
+4
-1
refs.c
+3
-1
@@ -288,6 +288,7 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
288
const unsigned char *old_sha1, int mustexist)
289
{
290
struct ref_lock *lock;
291
+ struct stat st;
292
293
lock = xcalloc(1, sizeof(struct ref_lock));
294
lock->lock_fd = -1;
@@ -303,6 +304,7 @@ static struct ref_lock* lock_ref_sha1_basic(const char *path,
304
lock->ref_file = strdup(path);
305
lock->lock_file = strdup(mkpath("%s.lock", lock->ref_file));
306
lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen));
307
+ lock->force_write = !lstat(lock->ref_file, &st) || errno == ENOENT;
308
309
if (safe_create_leading_directories(lock->lock_file))
310
die("unable to create directory for %s", lock->lock_file);
@@ -405,7 +407,7 @@ int write_ref_sha1(struct ref_lock *lock,
407
408
if (!lock)
409
return -1;
408
- if (!memcmp(lock->old_sha1, sha1, 20)) {
410
+ if (!lock->force_write && !memcmp(lock->old_sha1, sha1, 20)) {
411
unlock_ref(lock);
412
return 0;
413
}
refs.h
+1
@@ -7,6 +7,7 @@ struct ref_lock {
7
char *log_file;
8
unsigned char old_sha1[20];
9
int lock_fd;
10
+ int force_write;
11
};
12
13
/*