8 static const char name_rev_usage[] =
9 "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
11 typedef struct rev_name {
17 static long cutoff = LONG_MAX;
19 static void name_rev(struct commit *commit,
20 const char *tip_name, int merge_traversals, int generation,
23 struct rev_name *name = (struct rev_name *)commit->util;
24 struct commit_list *parents;
25 int parent_number = 1;
27 if (!commit->object.parsed)
30 if (commit->date < cutoff)
34 char *new_name = xmalloc(strlen(tip_name)+3);
35 strcpy(new_name, tip_name);
36 strcat(new_name, "^0");
40 die("generation: %d, but deref?", generation);
44 name = xmalloc(sizeof(rev_name));
47 } else if (name->merge_traversals > merge_traversals ||
48 (name->merge_traversals == merge_traversals &&
49 name->generation > generation)) {
51 name->tip_name = tip_name;
52 name->merge_traversals = merge_traversals;
53 name->generation = generation;
57 for (parents = commit->parents;
59 parents = parents->next, parent_number++) {
60 if (parent_number > 1) {
61 char *new_name = xmalloc(strlen(tip_name)+8);
64 sprintf(new_name, "%s~%d^%d", tip_name,
65 generation, parent_number);
67 sprintf(new_name, "%s^%d", tip_name, parent_number);
69 name_rev(parents->item, new_name,
70 merge_traversals + 1 , 0, 0);
72 name_rev(parents->item, tip_name, merge_traversals,
78 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
80 struct object *o = parse_object(sha1);
81 int tags_only = *(int*)cb_data;
84 if (tags_only && strncmp(path, "refs/tags/", 10))
87 while (o && o->type == OBJ_TAG) {
88 struct tag *t = (struct tag *) o;
90 break; /* broken repository */
91 o = parse_object(t->tagged->sha1);
94 if (o && o->type == OBJ_COMMIT) {
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, xstrdup(path), 0, 0, deref);
107 /* returns a static buffer */
108 static const char* get_rev_name(struct object *o)
110 static char buffer[1024];
114 if (o->type != OBJ_COMMIT)
116 c = (struct commit *) o;
124 snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
129 int cmd_name_rev(int argc, const char **argv, const char *prefix)
131 struct object_array revs = { 0, 0, NULL };
132 int as_is = 0, all = 0, transform_stdin = 0;
135 git_config(git_default_config);
138 usage(name_rev_usage);
140 for (--argc, ++argv; argc; --argc, ++argv) {
141 unsigned char sha1[20];
143 struct commit *commit;
145 if (!as_is && (*argv)[0] == '-') {
146 if (!strcmp(*argv, "--")) {
149 } else if (!strcmp(*argv, "--tags")) {
152 } else if (!strcmp(*argv, "--all")) {
154 die("Specify either a list, or --all, not both!");
158 } else if (!strcmp(*argv, "--stdin")) {
160 die("Specify either a list, or --stdin, not both!");
165 usage(name_rev_usage);
168 if (get_sha1(*argv, sha1)) {
169 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
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",
181 commit = (struct commit *)o;
183 if (cutoff > commit->date)
184 cutoff = commit->date;
186 add_object_array((struct object *)commit, *argv, &revs);
189 for_each_ref(name_ref, &tags_only);
191 if (transform_stdin) {
195 while (!feof(stdin)) {
197 p = fgets(buffer, sizeof(buffer), stdin);
201 for (p_start = p; *p; p++) {
202 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
205 else if (++forty == 40 &&
207 unsigned char sha1[40];
208 const char *name = "undefined";
214 if (!get_sha1(p - 39, sha1)) {
218 name = get_rev_name(o);
222 if (!strcmp(name, "undefined"))
225 fwrite(p_start, p - p_start + 1, 1,
227 printf(" (%s)", name);
234 fwrite(p_start, p - p_start, 1, stdout);
239 max = get_max_object_index();
240 for (i = 0; i < max; i++) {
241 struct object * obj = get_indexed_object(i);
244 printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
248 for (i = 0; i < revs.nr; i++)
250 revs.objects[i].name,
251 get_rev_name(revs.objects[i].item));