7 static const char name_rev_usage[] =
8 "git-name-rev [--tags] ( --all | --stdin | commitish [commitish...] )\n";
10 typedef struct rev_name {
16 static long cutoff = LONG_MAX;
18 static void name_rev(struct commit *commit,
19 const char *tip_name, int merge_traversals, int generation,
22 struct rev_name *name = (struct rev_name *)commit->object.util;
23 struct commit_list *parents;
24 int parent_number = 1;
26 if (!commit->object.parsed)
29 if (commit->date < cutoff)
33 char *new_name = xmalloc(strlen(tip_name)+3);
34 strcpy(new_name, tip_name);
35 strcat(new_name, "^0");
39 die("generation: %d, but deref?", generation);
43 name = xmalloc(sizeof(rev_name));
44 commit->object.util = name;
46 } else if (name->merge_traversals > merge_traversals ||
47 (name->merge_traversals == merge_traversals &&
48 name->generation > generation)) {
50 name->tip_name = tip_name;
51 name->merge_traversals = merge_traversals;
52 name->generation = generation;
56 for (parents = commit->parents;
58 parents = parents->next, parent_number++) {
59 if (parent_number > 1) {
60 char *new_name = xmalloc(strlen(tip_name)+8);
63 sprintf(new_name, "%s~%d^%d", tip_name,
64 generation, parent_number);
66 sprintf(new_name, "%s^%d", tip_name, parent_number);
68 name_rev(parents->item, new_name,
69 merge_traversals + 1 , 0, 0);
71 name_rev(parents->item, tip_name, merge_traversals,
77 static int tags_only = 0;
79 static int name_ref(const char *path, const unsigned char *sha1)
81 struct object *o = parse_object(sha1);
84 if (tags_only && strncmp(path, "refs/tags/", 10))
87 while (o && o->type == tag_type) {
88 struct tag *t = (struct tag *) o;
90 break; /* broken repository */
91 o = parse_object(t->tagged->sha1);
94 if (o && o->type == commit_type) {
95 struct commit *commit = (struct commit *)o;
97 if (!strncmp(path, "refs/heads/", 11))
99 else if (!strncmp(path, "refs/", 5))
102 name_rev(commit, strdup(path), 0, 0, deref);
107 /* returns a static buffer */
108 static const char* get_rev_name(struct object *o)
110 static char buffer[1024];
111 struct rev_name *n = (struct rev_name *)o->util;
118 snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
123 int main(int argc, char **argv)
125 struct object_list *revs = NULL;
126 struct object_list **walker = &revs;
127 int as_is = 0, all = 0, transform_stdin = 0;
129 setup_git_directory();
132 usage(name_rev_usage);
134 for (--argc, ++argv; argc; --argc, ++argv) {
135 unsigned char sha1[20];
137 struct commit *commit;
139 if (!as_is && (*argv)[0] == '-') {
140 if (!strcmp(*argv, "--")) {
143 } else if (!strcmp(*argv, "--tags")) {
146 } else if (!strcmp(*argv, "--all")) {
148 die("Specify either a list, or --all, not both!");
152 } else if (!strcmp(*argv, "--stdin")) {
154 die("Specify either a list, or --stdin, not both!");
159 usage(name_rev_usage);
162 if (get_sha1(*argv, sha1)) {
163 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
168 o = deref_tag(parse_object(sha1), *argv, 0);
169 if (!o || o->type != commit_type) {
170 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
175 commit = (struct commit *)o;
177 if (cutoff > commit->date)
178 cutoff = commit->date;
180 object_list_append((struct object *)commit, walker);
181 (*walker)->name = *argv;
182 walker = &((*walker)->next);
185 for_each_ref(name_ref);
187 if (transform_stdin) {
191 while (!feof(stdin)) {
193 p = fgets(buffer, sizeof(buffer), stdin);
197 for (p_start = p; *p; p++) {
198 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
201 else if (++forty == 40 &&
203 unsigned char sha1[40];
204 const char *name = "undefined";
210 if (!get_sha1(p - 39, sha1)) {
214 name = get_rev_name(o);
218 if (!strcmp(name, "undefined"))
221 fwrite(p_start, p - p_start + 1, 1,
223 printf(" (%s)", name);
230 fwrite(p_start, p - p_start, 1, stdout);
235 for (i = 0; i < nr_objs; i++)
236 printf("%s %s\n", sha1_to_hex(objs[i]->sha1),
237 get_rev_name(objs[i]));
239 for ( ; revs; revs = revs->next)
240 printf("%s %s\n", revs->name, get_rev_name(revs->item));