Code

make reflog filename independent from struct ref_lock
authorNicolas Pitre <nico@cam.org>
Fri, 26 Jan 2007 22:26:05 +0000 (17:26 -0500)
committerJunio C Hamano <junkio@cox.net>
Sun, 28 Jan 2007 10:16:46 +0000 (02:16 -0800)
This allows for ref_log_write() to be used in a more flexible way,
and is needed for future changes.

This is only code reorg with no behavior change.

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
builtin-reflog.c
refs.c
refs.h

index b443ed9ef6936294e827a2700af80484f8a98506..b6612a90ed7a049290291f484609c0efa911c1a7 100644 (file)
@@ -242,7 +242,7 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
        struct cmd_reflog_expire_cb *cmd = cb_data;
        struct expire_reflog_cb cb;
        struct ref_lock *lock;
-       char *newlog_path = NULL;
+       char *log_file, *newlog_path = NULL;
        int status = 0;
 
        if (strncmp(ref, "refs/", 5))
@@ -255,7 +255,8 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
        lock = lock_ref_sha1(ref + 5, sha1);
        if (!lock)
                return error("cannot lock ref '%s'", ref);
-       if (!file_exists(lock->log_file))
+       log_file = xstrdup(git_path("logs/%s", ref));
+       if (!file_exists(log_file))
                goto finish;
        if (!cmd->dry_run) {
                newlog_path = xstrdup(git_path("logs/%s.lock", ref));
@@ -271,13 +272,14 @@ static int expire_reflog(const char *ref, const unsigned char *sha1, int unused,
                if (fclose(cb.newlog))
                        status |= error("%s: %s", strerror(errno),
                                        newlog_path);
-               if (rename(newlog_path, lock->log_file)) {
+               if (rename(newlog_path, log_file)) {
                        status |= error("cannot rename %s to %s",
-                                       newlog_path, lock->log_file);
+                                       newlog_path, log_file);
                        unlink(newlog_path);
                }
        }
        free(newlog_path);
+       free(log_file);
        unlock_ref(lock);
        return status;
 }
diff --git a/refs.c b/refs.c
index 12e46b8bbefb35e5d402b3c8ef20dd9e4263e81a..3b295f380662aab7fc247b6f9fed9e4884758cf5 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -680,7 +680,6 @@ static struct ref_lock *lock_ref_sha1_basic(const char *ref, const unsigned char
        lock->lk = xcalloc(1, sizeof(struct lock_file));
 
        lock->ref_name = xstrdup(ref);
-       lock->log_file = xstrdup(git_path("logs/%s", ref));
        ref_file = git_path("%s", ref);
        lock->force_write = lstat(ref_file, &st) && errno == ENOENT;
 
@@ -776,10 +775,10 @@ int delete_ref(const char *refname, unsigned char *sha1)
         */
        ret |= repack_without_ref(refname);
 
-       err = unlink(lock->log_file);
+       err = unlink(git_path("logs/%s", lock->ref_name));
        if (err && errno != ENOENT)
                fprintf(stderr, "warning: unlink(%s) failed: %s",
-                       lock->log_file, strerror(errno));
+                       git_path("logs/%s", lock->ref_name), strerror(errno));
        invalidate_cached_refs();
        unlock_ref(lock);
        return ret;
@@ -920,47 +919,48 @@ void unlock_ref(struct ref_lock *lock)
                        rollback_lock_file(lock->lk);
        }
        free(lock->ref_name);
-       free(lock->log_file);
        free(lock);
 }
 
-static int log_ref_write(struct ref_lock *lock,
-       const unsigned char *sha1, const char *msg)
+static int log_ref_write(const char *ref_name, const unsigned char *old_sha1,
+                        const unsigned char *new_sha1, const char *msg)
 {
        int logfd, written, oflags = O_APPEND | O_WRONLY;
        unsigned maxlen, len;
        int msglen;
-       char *logrec;
+       char *log_file, *logrec;
        const char *committer;
 
        if (log_all_ref_updates < 0)
                log_all_ref_updates = !is_bare_repository();
 
+       log_file = git_path("logs/%s", ref_name);
+
        if (log_all_ref_updates &&
-           (!strncmp(lock->ref_name, "refs/heads/", 11) ||
-            !strncmp(lock->ref_name, "refs/remotes/", 13))) {
-               if (safe_create_leading_directories(lock->log_file) < 0)
+           (!strncmp(ref_name, "refs/heads/", 11) ||
+            !strncmp(ref_name, "refs/remotes/", 13))) {
+               if (safe_create_leading_directories(log_file) < 0)
                        return error("unable to create directory for %s",
-                               lock->log_file);
+                                    log_file);
                oflags |= O_CREAT;
        }
 
-       logfd = open(lock->log_file, oflags, 0666);
+       logfd = open(log_file, oflags, 0666);
        if (logfd < 0) {
                if (!(oflags & O_CREAT) && errno == ENOENT)
                        return 0;
 
                if ((oflags & O_CREAT) && errno == EISDIR) {
-                       if (remove_empty_directories(lock->log_file)) {
+                       if (remove_empty_directories(log_file)) {
                                return error("There are still logs under '%s'",
-                                            lock->log_file);
+                                            log_file);
                        }
-                       logfd = open(lock->log_file, oflags, 0666);
+                       logfd = open(log_file, oflags, 0666);
                }
 
                if (logfd < 0)
                        return error("Unable to append to %s: %s",
-                                    lock->log_file, strerror(errno));
+                                    log_file, strerror(errno));
        }
 
        msglen = 0;
@@ -982,8 +982,8 @@ static int log_ref_write(struct ref_lock *lock,
        maxlen = strlen(committer) + msglen + 100;
        logrec = xmalloc(maxlen);
        len = sprintf(logrec, "%s %s %s\n",
-                     sha1_to_hex(lock->old_sha1),
-                     sha1_to_hex(sha1),
+                     sha1_to_hex(old_sha1),
+                     sha1_to_hex(new_sha1),
                      committer);
        if (msglen)
                len += sprintf(logrec + len - 1, "\t%.*s\n", msglen, msg) - 1;
@@ -991,7 +991,7 @@ static int log_ref_write(struct ref_lock *lock,
        free(logrec);
        close(logfd);
        if (written != len)
-               return error("Unable to append to %s", lock->log_file);
+               return error("Unable to append to %s", log_file);
        return 0;
 }
 
@@ -1014,7 +1014,7 @@ int write_ref_sha1(struct ref_lock *lock,
                return -1;
        }
        invalidate_cached_refs();
-       if (log_ref_write(lock, sha1, logmsg) < 0) {
+       if (log_ref_write(lock->ref_name, lock->old_sha1, sha1, logmsg) < 0) {
                unlock_ref(lock);
                return -1;
        }
diff --git a/refs.h b/refs.h
index 33450f13e781b5ab60956082c7d8b2dc31f64286..2d2ba149abbb69c796307113b7de917d579b5b5c 100644 (file)
--- a/refs.h
+++ b/refs.h
@@ -3,7 +3,6 @@
 
 struct ref_lock {
        char *ref_name;
-       char *log_file;
        struct lock_file *lk;
        unsigned char old_sha1[20];
        int lock_fd;