add logref support to git-symbolic-ref
authorNicolas Pitre <nico@cam.org>
Fri, 26 Jan 2007 22:26:10 +0000 (17:26 -0500)
committerJunio C Hamano <junkio@cox.net>
Sun, 28 Jan 2007 10:16:46 +0000 (02:16 -0800)
Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Junio C Hamano <junkio@cox.net>
Documentation/git-symbolic-ref.txt
builtin-branch.c
builtin-init-db.c
builtin-symbolic-ref.c
cache.h
refs.c

index f93b79a85e1259f789551a94ab422a846cf4cf65..a88f7228605ee35002573123f3dbe63c3e405dc6 100644 (file)
@@ -7,7 +7,7 @@ git-symbolic-ref - Read and modify symbolic refs
 
 SYNOPSIS
 --------
-'git-symbolic-ref' [-q] <name> [<ref>]
+'git-symbolic-ref' [-q] [-m <reason>] <name> [<ref>]
 
 DESCRIPTION
 -----------
@@ -31,6 +31,10 @@ OPTIONS
        symbolic ref but a detached HEAD; instead exit with
        non-zero status silently.
 
+-m::
+       Update the reflog for <name> with <reason>.  This is valid only
+       when creating or updating a symbolic ref.
+
 NOTES
 -----
 In the past, `.git/HEAD` was a symbolic link pointing at
index d60690bb084ef83ce99a56453ae9dfa56da12807..76f174fd89b6841ba395d25cb089d2c615f5dd37 100644 (file)
@@ -381,7 +381,8 @@ static void rename_branch(const char *oldname, const char *newname, int force)
        if (rename_ref(oldref, newref, logmsg))
                die("Branch rename failed");
 
-       if (!strcmp(oldname, head) && create_symref("HEAD", newref))
+       /* no need to pass logmsg here as HEAD didn't really move */
+       if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
                die("Branch renamed to %s, but HEAD is not updated!", newname);
 }
 
index 186548938193ac125d9d9f89080cec640d0a46e0..12e43d0db4df0c40fe6375b38691684efcddad18 100644 (file)
@@ -231,7 +231,7 @@ static int create_default_files(const char *git_dir, const char *template_path)
        strcpy(path + len, "HEAD");
        reinit = !read_ref("HEAD", sha1);
        if (!reinit) {
-               if (create_symref("HEAD", "refs/heads/master") < 0)
+               if (create_symref("HEAD", "refs/heads/master", NULL) < 0)
                        exit(1);
        }
 
index 227c9d4a6274871b52420221d9d088bb3ac5554f..d41b40640b96e7d5568174c7d16088c29d361d42 100644 (file)
@@ -3,7 +3,7 @@
 #include "refs.h"
 
 static const char git_symbolic_ref_usage[] =
-"git-symbolic-ref [-q] name [ref]";
+"git-symbolic-ref [-q] [-m <reason>] name [ref]";
 
 static void check_symref(const char *HEAD, int quiet)
 {
@@ -25,6 +25,7 @@ static void check_symref(const char *HEAD, int quiet)
 int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
 {
        int quiet = 0;
+       const char *msg = NULL;
 
        git_config(git_default_config);
 
@@ -34,6 +35,17 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
                        break;
                else if (!strcmp("-q", arg))
                        quiet = 1;
+               else if (!strcmp("-m", arg)) {
+                       argc--;
+                       argv++;
+                       if (argc <= 1)
+                               break;
+                       msg = argv[1];
+                       if (!*msg)
+                               die("Refusing to perform update with empty message");
+                       if (strchr(msg, '\n'))
+                               die("Refusing to perform update with \\n in message");
+               }
                else if (!strcmp("--", arg)) {
                        argc--;
                        argv++;
@@ -50,7 +62,7 @@ int cmd_symbolic_ref(int argc, const char **argv, const char *prefix)
                check_symref(argv[1], quiet);
                break;
        case 3:
-               create_symref(argv[1], argv[2]);
+               create_symref(argv[1], argv[2], msg);
                break;
        default:
                usage(git_symbolic_ref_usage);
diff --git a/cache.h b/cache.h
index 9873ee97d93f7e4b6809262469f228c53339d080..201704bacf5e7c03ccab9b81b01f94cf50fe943e 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -302,7 +302,7 @@ extern int read_ref(const char *filename, unsigned char *sha1);
 extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);
 extern int dwim_ref(const char *str, int len, unsigned char *sha1, char **ref);
 
-extern int create_symref(const char *ref, const char *refs_heads_master);
+extern int create_symref(const char *ref, const char *refs_heads_master, const char *logmsg);
 extern int validate_headref(const char *ref);
 
 extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
diff --git a/refs.c b/refs.c
index ec6fe29ebb671924e4b0bb27a5d2c6d2288561c0..591318501f6ba072c4888ed7cf9e5b34d26fb32f 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -986,18 +986,23 @@ int write_ref_sha1(struct ref_lock *lock,
        return 0;
 }
 
-int create_symref(const char *ref_target, const char *refs_heads_master)
+int create_symref(const char *ref_target, const char *refs_heads_master,
+                 const char *logmsg)
 {
        const char *lockpath;
        char ref[1000];
        int fd, len, written;
        const char *git_HEAD = git_path("%s", ref_target);
+       unsigned char old_sha1[20], new_sha1[20];
+
+       if (logmsg && read_ref(ref_target, old_sha1))
+               hashclr(old_sha1);
 
 #ifndef NO_SYMLINK_HEAD
        if (prefer_symlink_refs) {
                unlink(git_HEAD);
                if (!symlink(refs_heads_master, git_HEAD))
-                       return 0;
+                       goto done;
                fprintf(stderr, "no symlink - falling back to symbolic ref\n");
        }
 #endif
@@ -1030,6 +1035,11 @@ int create_symref(const char *ref_target, const char *refs_heads_master)
                error("Unable to fix permissions on %s", lockpath);
                return -4;
        }
+
+       done:
+       if (logmsg && !read_ref(refs_heads_master, new_sha1))
+               log_ref_write(ref_target, old_sha1, new_sha1, logmsg);
+
        return 0;
 }