7 static const char name_rev_usage[] =
8 "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\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->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));
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 name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
79 struct object *o = parse_object(sha1);
80 int tags_only = *(int*)cb_data;
83 if (tags_only && strncmp(path, "refs/tags/", 10))
86 while (o && o->type == OBJ_TAG) {
87 struct tag *t = (struct tag *) o;
89 break; /* broken repository */
90 o = parse_object(t->tagged->sha1);
93 if (o && o->type == OBJ_COMMIT) {
94 struct commit *commit = (struct commit *)o;
96 if (!strncmp(path, "refs/heads/", 11))
98 else if (!strncmp(path, "refs/", 5))
101 name_rev(commit, xstrdup(path), 0, 0, deref);
106 /* returns a static buffer */
107 static const char* get_rev_name(struct object *o)
109 static char buffer[1024];
113 if (o->type != OBJ_COMMIT)
115 c = (struct commit *) o;
123 snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
128 int cmd_name_rev(int argc, const char **argv, const char *prefix)
130 struct object_array revs = { 0, 0, NULL };
131 int as_is = 0, all = 0, transform_stdin = 0;
134 git_config(git_default_config);
137 usage(name_rev_usage);
139 for (--argc, ++argv; argc; --argc, ++argv) {
140 unsigned char sha1[20];
142 struct commit *commit;
144 if (!as_is && (*argv)[0] == '-') {
145 if (!strcmp(*argv, "--")) {
148 } else if (!strcmp(*argv, "--tags")) {
151 } else if (!strcmp(*argv, "--all")) {
153 die("Specify either a list, or --all, not both!");
157 } else if (!strcmp(*argv, "--stdin")) {
159 die("Specify either a list, or --stdin, not both!");
164 usage(name_rev_usage);
167 if (get_sha1(*argv, sha1)) {
168 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
173 o = deref_tag(parse_object(sha1), *argv, 0);
174 if (!o || o->type != OBJ_COMMIT) {
175 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
180 commit = (struct commit *)o;
182 if (cutoff > commit->date)
183 cutoff = commit->date;
185 add_object_array((struct object *)commit, *argv, &revs);
188 for_each_ref(name_ref, &tags_only);
190 if (transform_stdin) {
194 while (!feof(stdin)) {
196 p = fgets(buffer, sizeof(buffer), stdin);
200 for (p_start = p; *p; p++) {
201 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
204 else if (++forty == 40 &&
206 unsigned char sha1[40];
207 const char *name = "undefined";
213 if (!get_sha1(p - 39, sha1)) {
217 name = get_rev_name(o);
221 if (!strcmp(name, "undefined"))
224 fwrite(p_start, p - p_start + 1, 1,
226 printf(" (%s)", name);
233 fwrite(p_start, p - p_start, 1, stdout);
238 max = get_max_object_index();
239 for (i = 0; i < max; i++) {
240 struct object * obj = get_indexed_object(i);
243 printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
247 for (i = 0; i < revs.nr; i++)
249 revs.objects[i].name,
250 get_rev_name(revs.objects[i].item));