builtin-name-rev.con commit sha1-lookup: make selection of 'middle' less aggressive (12ecb01)
   1#include "builtin.h"
   2#include "cache.h"
   3#include "commit.h"
   4#include "tag.h"
   5#include "refs.h"
   6#include "parse-options.h"
   7
   8#define CUTOFF_DATE_SLOP 86400 /* one day */
   9
  10typedef struct rev_name {
  11        const char *tip_name;
  12        int generation;
  13        int distance;
  14} rev_name;
  15
  16static long cutoff = LONG_MAX;
  17
  18/* How many generations are maximally preferred over _one_ merge traversal? */
  19#define MERGE_TRAVERSAL_WEIGHT 65535
  20
  21static void name_rev(struct commit *commit,
  22                const char *tip_name, int generation, int distance,
  23                int deref)
  24{
  25        struct rev_name *name = (struct rev_name *)commit->util;
  26        struct commit_list *parents;
  27        int parent_number = 1;
  28
  29        if (!commit->object.parsed)
  30                parse_commit(commit);
  31
  32        if (commit->date < cutoff)
  33                return;
  34
  35        if (deref) {
  36                char *new_name = xmalloc(strlen(tip_name)+3);
  37                strcpy(new_name, tip_name);
  38                strcat(new_name, "^0");
  39                tip_name = new_name;
  40
  41                if (generation)
  42                        die("generation: %d, but deref?", generation);
  43        }
  44
  45        if (name == NULL) {
  46                name = xmalloc(sizeof(rev_name));
  47                commit->util = name;
  48                goto copy_data;
  49        } else if (name->distance > distance) {
  50copy_data:
  51                name->tip_name = tip_name;
  52                name->generation = generation;
  53                name->distance = distance;
  54        } else
  55                return;
  56
  57        for (parents = commit->parents;
  58                        parents;
  59                        parents = parents->next, parent_number++) {
  60                if (parent_number > 1) {
  61                        int len = strlen(tip_name);
  62                        char *new_name = xmalloc(len +
  63                                1 + decimal_length(generation) +  /* ~<n> */
  64                                1 + 2 +                           /* ^NN */
  65                                1);
  66
  67                        if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
  68                                len -= 2;
  69                        if (generation > 0)
  70                                sprintf(new_name, "%.*s~%d^%d", len, tip_name,
  71                                                generation, parent_number);
  72                        else
  73                                sprintf(new_name, "%.*s^%d", len, tip_name,
  74                                                parent_number);
  75
  76                        name_rev(parents->item, new_name, 0,
  77                                distance + MERGE_TRAVERSAL_WEIGHT, 0);
  78                } else {
  79                        name_rev(parents->item, tip_name, generation + 1,
  80                                distance + 1, 0);
  81                }
  82        }
  83}
  84
  85struct name_ref_data {
  86        int tags_only;
  87        int name_only;
  88        const char *ref_filter;
  89};
  90
  91static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
  92{
  93        struct object *o = parse_object(sha1);
  94        struct name_ref_data *data = cb_data;
  95        int deref = 0;
  96
  97        if (data->tags_only && prefixcmp(path, "refs/tags/"))
  98                return 0;
  99
 100        if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
 101                return 0;
 102
 103        while (o && o->type == OBJ_TAG) {
 104                struct tag *t = (struct tag *) o;
 105                if (!t->tagged)
 106                        break; /* broken repository */
 107                o = parse_object(t->tagged->sha1);
 108                deref = 1;
 109        }
 110        if (o && o->type == OBJ_COMMIT) {
 111                struct commit *commit = (struct commit *)o;
 112
 113                if (!prefixcmp(path, "refs/heads/"))
 114                        path = path + 11;
 115                else if (data->tags_only
 116                    && data->name_only
 117                    && !prefixcmp(path, "refs/tags/"))
 118                        path = path + 10;
 119                else if (!prefixcmp(path, "refs/"))
 120                        path = path + 5;
 121
 122                name_rev(commit, xstrdup(path), 0, 0, deref);
 123        }
 124        return 0;
 125}
 126
 127/* returns a static buffer */
 128static const char *get_rev_name(const struct object *o)
 129{
 130        static char buffer[1024];
 131        struct rev_name *n;
 132        struct commit *c;
 133
 134        if (o->type != OBJ_COMMIT)
 135                return NULL;
 136        c = (struct commit *) o;
 137        n = c->util;
 138        if (!n)
 139                return NULL;
 140
 141        if (!n->generation)
 142                return n->tip_name;
 143        else {
 144                int len = strlen(n->tip_name);
 145                if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
 146                        len -= 2;
 147                snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
 148                                n->generation);
 149
 150                return buffer;
 151        }
 152}
 153
 154static void show_name(const struct object *obj,
 155                      const char *caller_name,
 156                      int always, int allow_undefined, int name_only)
 157{
 158        const char *name;
 159        const unsigned char *sha1 = obj->sha1;
 160
 161        if (!name_only)
 162                printf("%s ", caller_name ? caller_name : sha1_to_hex(sha1));
 163        name = get_rev_name(obj);
 164        if (name)
 165                printf("%s\n", name);
 166        else if (allow_undefined)
 167                printf("undefined\n");
 168        else if (always)
 169                printf("%s\n", find_unique_abbrev(sha1, DEFAULT_ABBREV));
 170        else
 171                die("cannot describe '%s'", sha1_to_hex(sha1));
 172}
 173
 174static char const * const name_rev_usage[] = {
 175        "git-name-rev [options] ( --all | --stdin | <commit>... )",
 176        NULL
 177};
 178
 179int cmd_name_rev(int argc, const char **argv, const char *prefix)
 180{
 181        struct object_array revs = { 0, 0, NULL };
 182        int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0;
 183        struct name_ref_data data = { 0, 0, NULL };
 184        struct option opts[] = {
 185                OPT_BOOLEAN(0, "name-only", &data.name_only, "print only names (no SHA-1)"),
 186                OPT_BOOLEAN(0, "tags", &data.tags_only, "only use tags to name the commits"),
 187                OPT_STRING(0, "refs", &data.ref_filter, "pattern",
 188                                   "only use refs matching <pattern>"),
 189                OPT_GROUP(""),
 190                OPT_BOOLEAN(0, "all", &all, "list all commits reachable from all refs"),
 191                OPT_BOOLEAN(0, "stdin", &transform_stdin, "read from stdin"),
 192                OPT_BOOLEAN(0, "undefined", &allow_undefined, "allow to print `undefined` names"),
 193                OPT_BOOLEAN(0, "always",     &always,
 194                           "show abbreviated commit object as fallback"),
 195                OPT_END(),
 196        };
 197
 198        git_config(git_default_config);
 199        argc = parse_options(argc, argv, opts, name_rev_usage, 0);
 200        if (!!all + !!transform_stdin + !!argc > 1) {
 201                error("Specify either a list, or --all, not both!");
 202                usage_with_options(name_rev_usage, opts);
 203        }
 204        if (all || transform_stdin)
 205                cutoff = 0;
 206
 207        for (; argc; argc--, argv++) {
 208                unsigned char sha1[20];
 209                struct object *o;
 210                struct commit *commit;
 211
 212                if (get_sha1(*argv, sha1)) {
 213                        fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
 214                                        *argv);
 215                        continue;
 216                }
 217
 218                o = deref_tag(parse_object(sha1), *argv, 0);
 219                if (!o || o->type != OBJ_COMMIT) {
 220                        fprintf(stderr, "Could not get commit for %s. Skipping.\n",
 221                                        *argv);
 222                        continue;
 223                }
 224
 225                commit = (struct commit *)o;
 226                if (cutoff > commit->date)
 227                        cutoff = commit->date;
 228                add_object_array((struct object *)commit, *argv, &revs);
 229        }
 230
 231        if (cutoff)
 232                cutoff = cutoff - CUTOFF_DATE_SLOP;
 233        for_each_ref(name_ref, &data);
 234
 235        if (transform_stdin) {
 236                char buffer[2048];
 237                char *p, *p_start;
 238
 239                while (!feof(stdin)) {
 240                        int forty = 0;
 241                        p = fgets(buffer, sizeof(buffer), stdin);
 242                        if (!p)
 243                                break;
 244
 245                        for (p_start = p; *p; p++) {
 246#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
 247                                if (!ishex(*p))
 248                                        forty = 0;
 249                                else if (++forty == 40 &&
 250                                                !ishex(*(p+1))) {
 251                                        unsigned char sha1[40];
 252                                        const char *name = NULL;
 253                                        char c = *(p+1);
 254
 255                                        forty = 0;
 256
 257                                        *(p+1) = 0;
 258                                        if (!get_sha1(p - 39, sha1)) {
 259                                                struct object *o =
 260                                                        lookup_object(sha1);
 261                                                if (o)
 262                                                        name = get_rev_name(o);
 263                                        }
 264                                        *(p+1) = c;
 265
 266                                        if (!name)
 267                                                continue;
 268
 269                                        fwrite(p_start, p - p_start + 1, 1, stdout);
 270                                        printf(" (%s)", name);
 271                                        p_start = p + 1;
 272                                }
 273                        }
 274
 275                        /* flush */
 276                        if (p_start != p)
 277                                fwrite(p_start, p - p_start, 1, stdout);
 278                }
 279        } else if (all) {
 280                int i, max;
 281
 282                max = get_max_object_index();
 283                for (i = 0; i < max; i++)
 284                        show_name(get_indexed_object(i), NULL,
 285                                  always, allow_undefined, data.name_only);
 286        } else {
 287                int i;
 288                for (i = 0; i < revs.nr; i++)
 289                        show_name(revs.objects[i].item, revs.objects[i].name,
 290                                  always, allow_undefined, data.name_only);
 291        }
 292
 293        return 0;
 294}