Merge branch 'maint' of git://linux-nfs.org/~bfields/git into maint
[gitweb.git] / builtin-name-rev.c
index 36f1ba6d384d963c3ef3af1c82aff360f2409995..ef1638590722017ad2cacca7ce30098fd0392bd5 100644 (file)
@@ -57,13 +57,20 @@ static void name_rev(struct commit *commit,
                        parents;
                        parents = parents->next, parent_number++) {
                if (parent_number > 1) {
-                       char *new_name = xmalloc(strlen(tip_name)+8);
-
+                       int len = strlen(tip_name);
+                       char *new_name = xmalloc(len +
+                               1 + decimal_length(generation) +  /* ~<n> */
+                               1 + 2 +                           /* ^NN */
+                               1);
+
+                       if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
+                               len -= 2;
                        if (generation > 0)
-                               sprintf(new_name, "%s~%d^%d", tip_name,
+                               sprintf(new_name, "%.*s~%d^%d", len, tip_name,
                                                generation, parent_number);
                        else
-                               sprintf(new_name, "%s^%d", tip_name, parent_number);
+                               sprintf(new_name, "%.*s^%d", len, tip_name,
+                                               parent_number);
 
                        name_rev(parents->item, new_name,
                                merge_traversals + 1 , 0, 0);
@@ -85,7 +92,7 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
        struct name_ref_data *data = cb_data;
        int deref = 0;
 
-       if (data->tags_only && strncmp(path, "refs/tags/", 10))
+       if (data->tags_only && prefixcmp(path, "refs/tags/"))
                return 0;
 
        if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
@@ -101,9 +108,9 @@ static int name_ref(const char *path, const unsigned char *sha1, int flags, void
        if (o && o->type == OBJ_COMMIT) {
                struct commit *commit = (struct commit *)o;
 
-               if (!strncmp(path, "refs/heads/", 11))
+               if (!prefixcmp(path, "refs/heads/"))
                        path = path + 11;
-               else if (!strncmp(path, "refs/", 5))
+               else if (!prefixcmp(path, "refs/"))
                        path = path + 5;
 
                name_rev(commit, xstrdup(path), 0, 0, deref);
@@ -127,10 +134,15 @@ static const char* get_rev_name(struct object *o)
 
        if (!n->generation)
                return n->tip_name;
-
-       snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
-
-       return buffer;
+       else {
+               int len = strlen(n->tip_name);
+               if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
+                       len -= 2;
+               snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
+                               n->generation);
+
+               return buffer;
+       }
 }
 
 int cmd_name_rev(int argc, const char **argv, const char *prefix)
@@ -156,7 +168,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
                        } else if (!strcmp(*argv, "--tags")) {
                                data.tags_only = 1;
                                continue;
-                       } else  if (!strncmp(*argv, "--refs=", 7)) {
+                       } else  if (!prefixcmp(*argv, "--refs=")) {
                                data.ref_filter = *argv + 7;
                                continue;
                        } else if (!strcmp(*argv, "--all")) {