Replace uses of strdup with xstrdup.
[gitweb.git] / refs.c
diff --git a/refs.c b/refs.c
index 2d9c1dc5d385b9bfcce293ba6d2c21018b9ae0af..5e653141ceb8183cfb7922c3ad89fd1a0110647a 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -29,7 +29,7 @@ const char *resolve_ref(const char *path, unsigned char *sha1, int reading)
                if (lstat(path, &st) < 0) {
                        if (reading || errno != ENOENT)
                                return NULL;
-                       memset(sha1, 0, 20);
+                       hashclr(sha1);
                        return path;
                }
 
@@ -147,7 +147,7 @@ static int do_for_each_ref(const char *base, int (*fn)(const char *path, const u
                        namelen = strlen(de->d_name);
                        if (namelen > 255)
                                continue;
-                       if (namelen>5 && !strcmp(de->d_name+namelen-5,".lock"))
+                       if (has_extension(de->d_name, ".lock"))
                                continue;
                        memcpy(path + baselen, de->d_name, namelen+1);
                        if (stat(git_path("%s", path), &st) < 0)
@@ -281,7 +281,7 @@ static struct ref_lock *verify_lock(struct ref_lock *lock,
                unlock_ref(lock);
                return NULL;
        }
-       if (memcmp(lock->old_sha1, old_sha1, 20)) {
+       if (hashcmp(lock->old_sha1, old_sha1)) {
                error("Ref %s is at %s but expected %s", lock->ref_file,
                        sha1_to_hex(lock->old_sha1), sha1_to_hex(old_sha1));
                unlock_ref(lock);
@@ -294,6 +294,7 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path,
        int plen,
        const unsigned char *old_sha1, int mustexist)
 {
+       const char *orig_path = path;
        struct ref_lock *lock;
        struct stat st;
 
@@ -303,24 +304,22 @@ static struct ref_lock *lock_ref_sha1_basic(const char *path,
        plen = strlen(path) - plen;
        path = resolve_ref(path, lock->old_sha1, mustexist);
        if (!path) {
+               int last_errno = errno;
+               error("unable to resolve reference %s: %s",
+                       orig_path, strerror(errno));
                unlock_ref(lock);
+               errno = last_errno;
                return NULL;
        }
        lock->lk = xcalloc(1, sizeof(struct lock_file));
 
-       lock->ref_file = strdup(path);
-       lock->log_file = strdup(git_path("logs/%s", lock->ref_file + plen));
+       lock->ref_file = xstrdup(path);
+       lock->log_file = xstrdup(git_path("logs/%s", lock->ref_file + plen));
        lock->force_write = lstat(lock->ref_file, &st) && errno == ENOENT;
 
        if (safe_create_leading_directories(lock->ref_file))
                die("unable to create directory for %s", lock->ref_file);
-       lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file);
-       if (lock->lock_fd < 0) {
-               error("Couldn't open lock file %s: %s",
-                     lock->lk->filename, strerror(errno));
-               unlock_ref(lock);
-               return NULL;
-       }
+       lock->lock_fd = hold_lock_file_for_update(lock->lk, lock->ref_file, 1);
 
        return old_sha1 ? verify_lock(lock, old_sha1, mustexist) : lock;
 }
@@ -349,10 +348,8 @@ void unlock_ref(struct ref_lock *lock)
                if (lock->lk)
                        rollback_lock_file(lock->lk);
        }
-       if (lock->ref_file)
-               free(lock->ref_file);
-       if (lock->log_file)
-               free(lock->log_file);
+       free(lock->ref_file);
+       free(lock->log_file);
        free(lock);
 }
 
@@ -379,7 +376,6 @@ static int log_ref_write(struct ref_lock *lock,
                        lock->log_file, strerror(errno));
        }
 
-       setup_ident();
        committer = git_committer_info(1);
        if (msg) {
                maxlen = strlen(committer) + strlen(msg) + 2*40 + 5;
@@ -413,7 +409,7 @@ int write_ref_sha1(struct ref_lock *lock,
 
        if (!lock)
                return -1;
-       if (!lock->force_write && !memcmp(lock->old_sha1, sha1, 20)) {
+       if (!lock->force_write && !hashcmp(lock->old_sha1, sha1)) {
                unlock_ref(lock);
                return 0;
        }
@@ -477,7 +473,7 @@ int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
                                        die("Log %s is corrupt.", logfile);
                                if (get_sha1_hex(rec + 41, sha1))
                                        die("Log %s is corrupt.", logfile);
-                               if (memcmp(logged_sha1, sha1, 20)) {
+                               if (hashcmp(logged_sha1, sha1)) {
                                        tz = strtoul(tz_c, NULL, 10);
                                        fprintf(stderr,
                                                "warning: Log %s has gap after %s.\n",
@@ -491,7 +487,7 @@ int read_ref_at(const char *ref, unsigned long at_time, unsigned char *sha1)
                        else {
                                if (get_sha1_hex(rec + 41, logged_sha1))
                                        die("Log %s is corrupt.", logfile);
-                               if (memcmp(logged_sha1, sha1, 20)) {
+                               if (hashcmp(logged_sha1, sha1)) {
                                        tz = strtoul(tz_c, NULL, 10);
                                        fprintf(stderr,
                                                "warning: Log %s unexpectedly ended on %s.\n",