7890f826ce7f061d31e0009660d01da18e5f83db
   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#include "sha1-lookup.h"
   8
   9#define CUTOFF_DATE_SLOP 86400 /* one day */
  10
  11typedef struct rev_name {
  12        const char *tip_name;
  13        unsigned long taggerdate;
  14        int generation;
  15        int distance;
  16} rev_name;
  17
  18static long cutoff = LONG_MAX;
  19
  20/* How many generations are maximally preferred over _one_ merge traversal? */
  21#define MERGE_TRAVERSAL_WEIGHT 65535
  22
  23static int is_better_name(struct rev_name *name,
  24                          const char *tip_name,
  25                          unsigned long taggerdate,
  26                          int generation,
  27                          int distance)
  28{
  29        return (name->taggerdate > taggerdate ||
  30                (name->taggerdate == taggerdate &&
  31                 name->distance > distance));
  32}
  33
  34static void name_rev(struct commit *commit,
  35                const char *tip_name, unsigned long taggerdate,
  36                int generation, int distance,
  37                int deref)
  38{
  39        struct rev_name *name = (struct rev_name *)commit->util;
  40        struct commit_list *parents;
  41        int parent_number = 1;
  42
  43        parse_commit(commit);
  44
  45        if (commit->date < cutoff)
  46                return;
  47
  48        if (deref) {
  49                tip_name = xstrfmt("%s^0", tip_name);
  50
  51                if (generation)
  52                        die("generation: %d, but deref?", generation);
  53        }
  54
  55        if (name == NULL) {
  56                name = xmalloc(sizeof(rev_name));
  57                commit->util = name;
  58                goto copy_data;
  59        } else if (is_better_name(name, tip_name, taggerdate,
  60                                  generation, distance)) {
  61copy_data:
  62                name->tip_name = tip_name;
  63                name->taggerdate = taggerdate;
  64                name->generation = generation;
  65                name->distance = distance;
  66        } else
  67                return;
  68
  69        for (parents = commit->parents;
  70                        parents;
  71                        parents = parents->next, parent_number++) {
  72                if (parent_number > 1) {
  73                        size_t len;
  74                        char *new_name;
  75
  76                        strip_suffix(tip_name, "^0", &len);
  77                        if (generation > 0)
  78                                new_name = xstrfmt("%.*s~%d^%d", (int)len, tip_name,
  79                                                   generation, parent_number);
  80                        else
  81                                new_name = xstrfmt("%.*s^%d", (int)len, tip_name,
  82                                                   parent_number);
  83
  84                        name_rev(parents->item, new_name, taggerdate, 0,
  85                                distance + MERGE_TRAVERSAL_WEIGHT, 0);
  86                } else {
  87                        name_rev(parents->item, tip_name, taggerdate,
  88                                generation + 1, distance + 1, 0);
  89                }
  90        }
  91}
  92
  93static int subpath_matches(const char *path, const char *filter)
  94{
  95        const char *subpath = path;
  96
  97        while (subpath) {
  98                if (!wildmatch(filter, subpath, 0, NULL))
  99                        return subpath - path;
 100                subpath = strchr(subpath, '/');
 101                if (subpath)
 102                        subpath++;
 103        }
 104        return -1;
 105}
 106
 107static const char *name_ref_abbrev(const char *refname, int shorten_unambiguous)
 108{
 109        if (shorten_unambiguous)
 110                refname = shorten_unambiguous_ref(refname, 0);
 111        else if (starts_with(refname, "refs/heads/"))
 112                refname = refname + 11;
 113        else if (starts_with(refname, "refs/"))
 114                refname = refname + 5;
 115        return refname;
 116}
 117
 118struct name_ref_data {
 119        int tags_only;
 120        int name_only;
 121        struct string_list ref_filters;
 122        struct string_list exclude_filters;
 123};
 124
 125static struct tip_table {
 126        struct tip_table_entry {
 127                unsigned char sha1[20];
 128                const char *refname;
 129        } *table;
 130        int nr;
 131        int alloc;
 132        int sorted;
 133} tip_table;
 134
 135static void add_to_tip_table(const unsigned char *sha1, const char *refname,
 136                             int shorten_unambiguous)
 137{
 138        refname = name_ref_abbrev(refname, shorten_unambiguous);
 139
 140        ALLOC_GROW(tip_table.table, tip_table.nr + 1, tip_table.alloc);
 141        hashcpy(tip_table.table[tip_table.nr].sha1, sha1);
 142        tip_table.table[tip_table.nr].refname = xstrdup(refname);
 143        tip_table.nr++;
 144        tip_table.sorted = 0;
 145}
 146
 147static int tipcmp(const void *a_, const void *b_)
 148{
 149        const struct tip_table_entry *a = a_, *b = b_;
 150        return hashcmp(a->sha1, b->sha1);
 151}
 152
 153static int name_ref(const char *path, const struct object_id *oid, int flags, void *cb_data)
 154{
 155        struct object *o = parse_object(oid->hash);
 156        struct name_ref_data *data = cb_data;
 157        int can_abbreviate_output = data->tags_only && data->name_only;
 158        int deref = 0;
 159        unsigned long taggerdate = ULONG_MAX;
 160
 161        if (data->tags_only && !starts_with(path, "refs/tags/"))
 162                return 0;
 163
 164        if (data->exclude_filters.nr) {
 165                struct string_list_item *item;
 166
 167                for_each_string_list_item(item, &data->exclude_filters) {
 168                        if (subpath_matches(path, item->string) >= 0)
 169                                return 0;
 170                }
 171        }
 172
 173        if (data->ref_filters.nr) {
 174                struct string_list_item *item;
 175                int matched = 0;
 176
 177                /* See if any of the patterns match. */
 178                for_each_string_list_item(item, &data->ref_filters) {
 179                        /*
 180                         * Check all patterns even after finding a match, so
 181                         * that we can see if a match with a subpath exists.
 182                         * When a user asked for 'refs/tags/v*' and 'v1.*',
 183                         * both of which match, the user is showing her
 184                         * willingness to accept a shortened output by having
 185                         * the 'v1.*' in the acceptable refnames, so we
 186                         * shouldn't stop when seeing 'refs/tags/v1.4' matches
 187                         * 'refs/tags/v*'.  We should show it as 'v1.4'.
 188                         */
 189                        switch (subpath_matches(path, item->string)) {
 190                        case -1: /* did not match */
 191                                break;
 192                        case 0: /* matched fully */
 193                                matched = 1;
 194                                break;
 195                        default: /* matched subpath */
 196                                matched = 1;
 197                                can_abbreviate_output = 1;
 198                                break;
 199                        }
 200                }
 201
 202                /* If none of the patterns matched, stop now */
 203                if (!matched)
 204                        return 0;
 205        }
 206
 207        add_to_tip_table(oid->hash, path, can_abbreviate_output);
 208
 209        while (o && o->type == OBJ_TAG) {
 210                struct tag *t = (struct tag *) o;
 211                if (!t->tagged)
 212                        break; /* broken repository */
 213                o = parse_object(t->tagged->oid.hash);
 214                deref = 1;
 215                taggerdate = t->date;
 216        }
 217        if (o && o->type == OBJ_COMMIT) {
 218                struct commit *commit = (struct commit *)o;
 219
 220                path = name_ref_abbrev(path, can_abbreviate_output);
 221                name_rev(commit, xstrdup(path), taggerdate, 0, 0, deref);
 222        }
 223        return 0;
 224}
 225
 226static const unsigned char *nth_tip_table_ent(size_t ix, void *table_)
 227{
 228        struct tip_table_entry *table = table_;
 229        return table[ix].sha1;
 230}
 231
 232static const char *get_exact_ref_match(const struct object *o)
 233{
 234        int found;
 235
 236        if (!tip_table.table || !tip_table.nr)
 237                return NULL;
 238
 239        if (!tip_table.sorted) {
 240                QSORT(tip_table.table, tip_table.nr, tipcmp);
 241                tip_table.sorted = 1;
 242        }
 243
 244        found = sha1_pos(o->oid.hash, tip_table.table, tip_table.nr,
 245                         nth_tip_table_ent);
 246        if (0 <= found)
 247                return tip_table.table[found].refname;
 248        return NULL;
 249}
 250
 251/* returns a static buffer */
 252static const char *get_rev_name(const struct object *o)
 253{
 254        static char buffer[1024];
 255        struct rev_name *n;
 256        struct commit *c;
 257
 258        if (o->type != OBJ_COMMIT)
 259                return get_exact_ref_match(o);
 260        c = (struct commit *) o;
 261        n = c->util;
 262        if (!n)
 263                return NULL;
 264
 265        if (!n->generation)
 266                return n->tip_name;
 267        else {
 268                int len = strlen(n->tip_name);
 269                if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
 270                        len -= 2;
 271                snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
 272                                n->generation);
 273
 274                return buffer;
 275        }
 276}
 277
 278static void show_name(const struct object *obj,
 279                      const char *caller_name,
 280                      int always, int allow_undefined, int name_only)
 281{
 282        const char *name;
 283        const struct object_id *oid = &obj->oid;
 284
 285        if (!name_only)
 286                printf("%s ", caller_name ? caller_name : oid_to_hex(oid));
 287        name = get_rev_name(obj);
 288        if (name)
 289                printf("%s\n", name);
 290        else if (allow_undefined)
 291                printf("undefined\n");
 292        else if (always)
 293                printf("%s\n", find_unique_abbrev(oid->hash, DEFAULT_ABBREV));
 294        else
 295                die("cannot describe '%s'", oid_to_hex(oid));
 296}
 297
 298static char const * const name_rev_usage[] = {
 299        N_("git name-rev [<options>] <commit>..."),
 300        N_("git name-rev [<options>] --all"),
 301        N_("git name-rev [<options>] --stdin"),
 302        NULL
 303};
 304
 305static void name_rev_line(char *p, struct name_ref_data *data)
 306{
 307        int forty = 0;
 308        char *p_start;
 309        for (p_start = p; *p; p++) {
 310#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
 311                if (!ishex(*p))
 312                        forty = 0;
 313                else if (++forty == 40 &&
 314                         !ishex(*(p+1))) {
 315                        unsigned char sha1[40];
 316                        const char *name = NULL;
 317                        char c = *(p+1);
 318                        int p_len = p - p_start + 1;
 319
 320                        forty = 0;
 321
 322                        *(p+1) = 0;
 323                        if (!get_sha1(p - 39, sha1)) {
 324                                struct object *o =
 325                                        lookup_object(sha1);
 326                                if (o)
 327                                        name = get_rev_name(o);
 328                        }
 329                        *(p+1) = c;
 330
 331                        if (!name)
 332                                continue;
 333
 334                        if (data->name_only)
 335                                printf("%.*s%s", p_len - 40, p_start, name);
 336                        else
 337                                printf("%.*s (%s)", p_len, p_start, name);
 338                        p_start = p + 1;
 339                }
 340        }
 341
 342        /* flush */
 343        if (p_start != p)
 344                fwrite(p_start, p - p_start, 1, stdout);
 345}
 346
 347int cmd_name_rev(int argc, const char **argv, const char *prefix)
 348{
 349        struct object_array revs = OBJECT_ARRAY_INIT;
 350        int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0, peel_tag = 0;
 351        struct name_ref_data data = { 0, 0, STRING_LIST_INIT_NODUP, STRING_LIST_INIT_NODUP };
 352        struct option opts[] = {
 353                OPT_BOOL(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
 354                OPT_BOOL(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
 355                OPT_STRING_LIST(0, "refs", &data.ref_filters, N_("pattern"),
 356                                   N_("only use refs matching <pattern>")),
 357                OPT_STRING_LIST(0, "exclude", &data.exclude_filters, N_("pattern"),
 358                                   N_("ignore refs matching <pattern>")),
 359                OPT_GROUP(""),
 360                OPT_BOOL(0, "all", &all, N_("list all commits reachable from all refs")),
 361                OPT_BOOL(0, "stdin", &transform_stdin, N_("read from stdin")),
 362                OPT_BOOL(0, "undefined", &allow_undefined, N_("allow to print `undefined` names (default)")),
 363                OPT_BOOL(0, "always",     &always,
 364                           N_("show abbreviated commit object as fallback")),
 365                {
 366                        /* A Hidden OPT_BOOL */
 367                        OPTION_SET_INT, 0, "peel-tag", &peel_tag, NULL,
 368                        N_("dereference tags in the input (internal use)"),
 369                        PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, 1,
 370                },
 371                OPT_END(),
 372        };
 373
 374        git_config(git_default_config, NULL);
 375        argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
 376        if (all + transform_stdin + !!argc > 1) {
 377                error("Specify either a list, or --all, not both!");
 378                usage_with_options(name_rev_usage, opts);
 379        }
 380        if (all || transform_stdin)
 381                cutoff = 0;
 382
 383        for (; argc; argc--, argv++) {
 384                unsigned char sha1[20];
 385                struct object *object;
 386                struct commit *commit;
 387
 388                if (get_sha1(*argv, sha1)) {
 389                        fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
 390                                        *argv);
 391                        continue;
 392                }
 393
 394                commit = NULL;
 395                object = parse_object(sha1);
 396                if (object) {
 397                        struct object *peeled = deref_tag(object, *argv, 0);
 398                        if (peeled && peeled->type == OBJ_COMMIT)
 399                                commit = (struct commit *)peeled;
 400                }
 401
 402                if (!object) {
 403                        fprintf(stderr, "Could not get object for %s. Skipping.\n",
 404                                        *argv);
 405                        continue;
 406                }
 407
 408                if (commit) {
 409                        if (cutoff > commit->date)
 410                                cutoff = commit->date;
 411                }
 412
 413                if (peel_tag) {
 414                        if (!commit) {
 415                                fprintf(stderr, "Could not get commit for %s. Skipping.\n",
 416                                        *argv);
 417                                continue;
 418                        }
 419                        object = (struct object *)commit;
 420                }
 421                add_object_array(object, *argv, &revs);
 422        }
 423
 424        if (cutoff)
 425                cutoff = cutoff - CUTOFF_DATE_SLOP;
 426        for_each_ref(name_ref, &data);
 427
 428        if (transform_stdin) {
 429                char buffer[2048];
 430
 431                while (!feof(stdin)) {
 432                        char *p = fgets(buffer, sizeof(buffer), stdin);
 433                        if (!p)
 434                                break;
 435                        name_rev_line(p, &data);
 436                }
 437        } else if (all) {
 438                int i, max;
 439
 440                max = get_max_object_index();
 441                for (i = 0; i < max; i++) {
 442                        struct object *obj = get_indexed_object(i);
 443                        if (!obj || obj->type != OBJ_COMMIT)
 444                                continue;
 445                        show_name(obj, NULL,
 446                                  always, allow_undefined, data.name_only);
 447                }
 448        } else {
 449                int i;
 450                for (i = 0; i < revs.nr; i++)
 451                        show_name(revs.objects[i].item, revs.objects[i].name,
 452                                  always, allow_undefined, data.name_only);
 453        }
 454
 455        return 0;
 456}