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