lock_ref_sha1(): do not sometimes error() and sometimes die().
This cleans up the error path in the function so it does not die() itself sometimes while signalling an error with NULL some other times which was inconsistent and confusing. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 30, 2006 at 14:14 UTC
5cc3cef997503e7543d927dbe23daca891131168
1 file changed
+18
-8
refs.c
+18
-8
@@ -561,6 +561,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
561
const char *orig_ref = ref;
562
struct ref_lock *lock;
563
struct stat st;
564
+ int last_errno = 0;
565
int mustexist = (old_sha1 && !is_null_sha1(old_sha1));
566
567
lock = xcalloc(1, sizeof(struct ref_lock));
@@ -574,17 +575,18 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
575
* to remain.
576
*/
577
ref_file = git_path("%s", orig_ref);
577
- if (remove_empty_directories(ref_file))
578
- die("there are still refs under '%s'", orig_ref);
578
+ if (remove_empty_directories(ref_file)) {
579
+ last_errno = errno;
580
+ error("there are still refs under '%s'", orig_ref);
581
+ goto error_return;
582
+ }
583
ref = resolve_ref(orig_ref, lock->old_sha1, mustexist, NULL);
584
}
585
if (!ref) {
582
- int last_errno = errno;
586
+ last_errno = errno;
587
error("unable to resolve reference %s: %s",
588
orig_ref, strerror(errno));
585
- unlock_ref(lock);
586
- errno = last_errno;
587
- return NULL;
589
+ goto error_return;
590
}
591
lock->lk = xcalloc(1, sizeof(struct lock_file));
592
@@ -593,11 +595,19 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
595
ref_file = git_path("%s", ref);
596
lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
597
596
- if (safe_create_leading_directories(ref_file))
597
- die("unable to create directory for %s", ref_file);
598
+ if (safe_create_leading_directories(ref_file)) {
599
+ last_errno = errno;
600
+ error("unable to create directory for %s", ref_file);
601
+ goto error_return;
602
+ }
603
lock->lock_fd = hold_lock_file_for_update(lock->lk, ref_file, 1);
604
605
return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
606
+
607
+ error_return:
608
+ unlock_ref(lock);
609
+ errno = last_errno;
610
+ return NULL;
611
}
612
613
struct ref_lock *lock_ref_sha1(const char *ref, const unsigned char *old_sha1)