name-rev.con commit Remove "refs" field from "struct object" (3e4339e)
   1#include <stdlib.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] ( --all | --stdin | commitish [commitish...] )\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                        char *new_name = xmalloc(strlen(tip_name)+8);
  61
  62                        if (generation > 0)
  63                                sprintf(new_name, "%s~%d^%d", tip_name,
  64                                                generation, parent_number);
  65                        else
  66                                sprintf(new_name, "%s^%d", tip_name, parent_number);
  67
  68                        name_rev(parents->item, new_name,
  69                                merge_traversals + 1 , 0, 0);
  70                } else {
  71                        name_rev(parents->item, tip_name, merge_traversals,
  72                                generation + 1, 0);
  73                }
  74        }
  75}
  76
  77static int tags_only = 0;
  78
  79static int name_ref(const char *path, const unsigned char *sha1)
  80{
  81        struct object *o = parse_object(sha1);
  82        int deref = 0;
  83
  84        if (tags_only && strncmp(path, "refs/tags/", 10))
  85                return 0;
  86
  87        while (o && o->type == TYPE_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 == TYPE_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, strdup(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 != TYPE_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 main(int argc, char **argv)
 130{
 131        struct object_list *revs = NULL;
 132        struct object_list **walker = &revs;
 133        int as_is = 0, all = 0, transform_stdin = 0;
 134
 135        setup_git_directory();
 136        git_config(git_default_config);
 137
 138        if (argc < 2)
 139                usage(name_rev_usage);
 140
 141        for (--argc, ++argv; argc; --argc, ++argv) {
 142                unsigned char sha1[20];
 143                struct object *o;
 144                struct commit *commit;
 145
 146                if (!as_is && (*argv)[0] == '-') {
 147                        if (!strcmp(*argv, "--")) {
 148                                as_is = 1;
 149                                continue;
 150                        } else if (!strcmp(*argv, "--tags")) {
 151                                tags_only = 1;
 152                                continue;
 153                        } else if (!strcmp(*argv, "--all")) {
 154                                if (argc > 1)
 155                                        die("Specify either a list, or --all, not both!");
 156                                all = 1;
 157                                cutoff = 0;
 158                                continue;
 159                        } else if (!strcmp(*argv, "--stdin")) {
 160                                if (argc > 1)
 161                                        die("Specify either a list, or --stdin, not both!");
 162                                transform_stdin = 1;
 163                                cutoff = 0;
 164                                continue;
 165                        }
 166                        usage(name_rev_usage);
 167                }
 168
 169                if (get_sha1(*argv, sha1)) {
 170                        fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
 171                                        *argv);
 172                        continue;
 173                }
 174
 175                o = deref_tag(parse_object(sha1), *argv, 0);
 176                if (!o || o->type != TYPE_COMMIT) {
 177                        fprintf(stderr, "Could not get commit for %s. Skipping.\n",
 178                                        *argv);
 179                        continue;
 180                }
 181
 182                commit = (struct commit *)o;
 183
 184                if (cutoff > commit->date)
 185                        cutoff = commit->date;
 186
 187                object_list_append((struct object *)commit, walker);
 188                (*walker)->name = *argv;
 189                walker = &((*walker)->next);
 190        }
 191
 192        for_each_ref(name_ref);
 193
 194        if (transform_stdin) {
 195                char buffer[2048];
 196                char *p, *p_start;
 197
 198                while (!feof(stdin)) {
 199                        int forty = 0;
 200                        p = fgets(buffer, sizeof(buffer), stdin);
 201                        if (!p)
 202                                break;
 203
 204                        for (p_start = p; *p; p++) {
 205#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
 206                                if (!ishex(*p))
 207                                        forty = 0;
 208                                else if (++forty == 40 &&
 209                                                !ishex(*(p+1))) {
 210                                        unsigned char sha1[40];
 211                                        const char *name = "undefined";
 212                                        char c = *(p+1);
 213
 214                                        forty = 0;
 215
 216                                        *(p+1) = 0;
 217                                        if (!get_sha1(p - 39, sha1)) {
 218                                                struct object *o =
 219                                                        lookup_object(sha1);
 220                                                if (o)
 221                                                        name = get_rev_name(o);
 222                                        }
 223                                        *(p+1) = c;
 224
 225                                        if (!strcmp(name, "undefined"))
 226                                                continue;
 227
 228                                        fwrite(p_start, p - p_start + 1, 1,
 229                                               stdout);
 230                                        printf(" (%s)", name);
 231                                        p_start = p + 1;
 232                                }
 233                        }
 234
 235                        /* flush */
 236                        if (p_start != p)
 237                                fwrite(p_start, p - p_start, 1, stdout);
 238                }
 239        } else if (all) {
 240                int i;
 241
 242                for (i = 0; i < obj_allocs; i++)
 243                        if (objs[i])
 244                                printf("%s %s\n", sha1_to_hex(objs[i]->sha1),
 245                                                get_rev_name(objs[i]));
 246        } else
 247                for ( ; revs; revs = revs->next)
 248                        printf("%s %s\n", revs->name, get_rev_name(revs->item));
 249
 250        return 0;
 251}
 252