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