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