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