Fix in-place editing functions in convert.c
[gitweb.git] / builtin-name-rev.c
index 1b06b4afaa4850c7b508bf1813c2f97d96b1f7a1..03083e94776234b280316978cd3f638262f41398 100644 (file)
@@ -4,19 +4,24 @@
 #include "tag.h"
 #include "refs.h"
 
+#define CUTOFF_DATE_SLOP 86400 /* one day */
+
 static const char name_rev_usage[] =
-       "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
+       "git-name-rev [--tags | --refs=<pattern>] ( --all | --stdin | committish [committish...] )\n";
 
 typedef struct rev_name {
        const char *tip_name;
-       int merge_traversals;
        int generation;
+       int distance;
 } rev_name;
 
 static long cutoff = LONG_MAX;
 
+/* How many generations are maximally preferred over _one_ merge traversal? */
+#define MERGE_TRAVERSAL_WEIGHT 65535
+
 static void name_rev(struct commit *commit,
-               const char *tip_name, int merge_traversals, int generation,
+               const char *tip_name, int generation, int distance,
                int deref)
 {
        struct rev_name *name = (struct rev_name *)commit->util;
@@ -43,13 +48,11 @@ static void name_rev(struct commit *commit,
                name = xmalloc(sizeof(rev_name));
                commit->util = name;
                goto copy_data;
-       } else if (name->merge_traversals > merge_traversals ||
-                       (name->merge_traversals == merge_traversals &&
-                        name->generation > generation)) {
+       } else if (name->distance > distance) {
 copy_data:
                name->tip_name = tip_name;
-               name->merge_traversals = merge_traversals;
                name->generation = generation;
+               name->distance = distance;
        } else
                return;
 
@@ -58,7 +61,10 @@ static void name_rev(struct commit *commit,
                        parents = parents->next, parent_number++) {
                if (parent_number > 1) {
                        int len = strlen(tip_name);
-                       char *new_name = xmalloc(len + 8);
+                       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;
@@ -69,22 +75,31 @@ static void name_rev(struct commit *commit,
                                sprintf(new_name, "%.*s^%d", len, tip_name,
                                                parent_number);
 
-                       name_rev(parents->item, new_name,
-                               merge_traversals + 1 , 0, 0);
+                       name_rev(parents->item, new_name, 0,
+                               distance + MERGE_TRAVERSAL_WEIGHT, 0);
                } else {
-                       name_rev(parents->item, tip_name, merge_traversals,
-                               generation + 1, 0);
+                       name_rev(parents->item, tip_name, generation + 1,
+                               distance + 1, 0);
                }
        }
 }
 
+struct name_ref_data {
+       int tags_only;
+       int name_only;
+       const char *ref_filter;
+};
+
 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
 {
        struct object *o = parse_object(sha1);
-       int tags_only = *(int*)cb_data;
+       struct name_ref_data *data = cb_data;
        int deref = 0;
 
-       if (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))
                return 0;
 
        while (o && o->type == OBJ_TAG) {
@@ -97,9 +112,13 @@ 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 (data->tags_only
+                   && data->name_only
+                   && !prefixcmp(path, "refs/tags/"))
+                       path = path + 10;
+               else if (!prefixcmp(path, "refs/"))
                        path = path + 5;
 
                name_rev(commit, xstrdup(path), 0, 0, deref);
@@ -138,7 +157,7 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
 {
        struct object_array revs = { 0, 0, NULL };
        int as_is = 0, all = 0, transform_stdin = 0;
-       int tags_only = 0;
+       struct name_ref_data data = { 0, 0, NULL };
 
        git_config(git_default_config);
 
@@ -154,8 +173,14 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
                        if (!strcmp(*argv, "--")) {
                                as_is = 1;
                                continue;
+                       } else if (!strcmp(*argv, "--name-only")) {
+                               data.name_only = 1;
+                               continue;
                        } else if (!strcmp(*argv, "--tags")) {
-                               tags_only = 1;
+                               data.tags_only = 1;
+                               continue;
+                       } else  if (!prefixcmp(*argv, "--refs=")) {
+                               data.ref_filter = *argv + 7;
                                continue;
                        } else if (!strcmp(*argv, "--all")) {
                                if (argc > 1)
@@ -194,7 +219,9 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
                add_object_array((struct object *)commit, *argv, &revs);
        }
 
-       for_each_ref(name_ref, &tags_only);
+       if (cutoff)
+               cutoff = cutoff - CUTOFF_DATE_SLOP;
+       for_each_ref(name_ref, &data);
 
        if (transform_stdin) {
                char buffer[2048];
@@ -249,16 +276,18 @@ int cmd_name_rev(int argc, const char **argv, const char *prefix)
                        struct object * obj = get_indexed_object(i);
                        if (!obj)
                                continue;
-                       printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
+                       if (!data.name_only)
+                               printf("%s ", sha1_to_hex(obj->sha1));
+                       printf("%s\n", get_rev_name(obj));
                }
        } else {
                int i;
-               for (i = 0; i < revs.nr; i++)
-                       printf("%s %s\n",
-                               revs.objects[i].name,
-                               get_rev_name(revs.objects[i].item));
+               for (i = 0; i < revs.nr; i++) {
+                       if (!data.name_only)
+                               printf("%s ", revs.objects[i].name);
+                       printf("%s\n", get_rev_name(revs.objects[i].item));
+               }
        }
 
        return 0;
 }
-