]> git.ipfire.org Git - thirdparty/git.git/blobdiff - builtin-reflog.c
GIT-VERSION-FILE: check ./version first.
[thirdparty/git.git] / builtin-reflog.c
index b443ed9ef6936294e827a2700af80484f8a98506..65b845b447b745eecd2a45e47a5c2226588dcfd1 100644 (file)
@@ -13,7 +13,7 @@
  */
 
 static const char reflog_expire_usage[] =
-"git-reflog expire [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
+"git-reflog (show|expire) [--verbose] [--dry-run] [--stale-fix] [--expire=<time>] [--expire-unreachable=<time>] [--all] <refs>...";
 
 static unsigned long default_reflog_expire;
 static unsigned long default_reflog_expire_unreachable;
@@ -242,20 +242,18 @@ 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))
-               return error("not a ref '%s'", ref);
-
        memset(&cb, 0, sizeof(cb));
        /* we take the lock for the ref itself to prevent it from
         * getting updated.
         */
-       lock = lock_ref_sha1(ref + 5, sha1);
+       lock = lock_any_ref_for_update(ref, 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 +269,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;
 }
@@ -351,7 +350,7 @@ static int cmd_reflog_expire(int argc, const char **argv, const char *prefix)
        }
 
        if (do_all)
-               status |= for_each_ref(expire_reflog, &cb);
+               status |= for_each_reflog(expire_reflog, &cb);
        while (i < argc) {
                const char *ref = argv[i++];
                unsigned char sha1[20];
@@ -373,10 +372,16 @@ static const char reflog_usage[] =
 
 int cmd_reflog(int argc, const char **argv, const char *prefix)
 {
-       if (argc < 2)
-               usage(reflog_usage);
-       else if (!strcmp(argv[1], "expire"))
+       /* With no command, we default to showing it. */
+       if (argc < 2 || *argv[1] == '-')
+               return cmd_log_reflog(argc, argv, prefix);
+
+       if (!strcmp(argv[1], "show"))
+               return cmd_log_reflog(argc - 1, argv + 1, prefix);
+
+       if (!strcmp(argv[1], "expire"))
                return cmd_reflog_expire(argc - 1, argv + 1, prefix);
-       else
-               usage(reflog_usage);
+
+       /* Not a recognized reflog command..*/
+       usage(reflog_usage);
 }