Merge branch 'gl/cleanup' into gl/cleanup-next
[git] / builtin-name-rev.c
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
8 static const char name_rev_usage[] =
9         "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
10
11 typedef struct rev_name {
12         const char *tip_name;
13         int merge_traversals;
14         int generation;
15 } rev_name;
16
17 static long cutoff = LONG_MAX;
18
19 static 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)) {
50 copy_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
78 static int tags_only;
79
80 static int name_ref(const char *path, const unsigned char *sha1)
81 {
82         struct object *o = parse_object(sha1);
83         int deref = 0;
84
85         if (tags_only && strncmp(path, "refs/tags/", 10))
86                 return 0;
87
88         while (o && o->type == OBJ_TAG) {
89                 struct tag *t = (struct tag *) o;
90                 if (!t->tagged)
91                         break; /* broken repository */
92                 o = parse_object(t->tagged->sha1);
93                 deref = 1;
94         }
95         if (o && o->type == OBJ_COMMIT) {
96                 struct commit *commit = (struct commit *)o;
97
98                 if (!strncmp(path, "refs/heads/", 11))
99                         path = path + 11;
100                 else if (!strncmp(path, "refs/", 5))
101                         path = path + 5;
102
103                 name_rev(commit, strdup(path), 0, 0, deref);
104         }
105         return 0;
106 }
107
108 /* returns a static buffer */
109 static const char* get_rev_name(struct object *o)
110 {
111         static char buffer[1024];
112         struct rev_name *n;
113         struct commit *c;
114
115         if (o->type != OBJ_COMMIT)
116                 return "undefined";
117         c = (struct commit *) o;
118         n = c->util;
119         if (!n)
120                 return "undefined";
121
122         if (!n->generation)
123                 return n->tip_name;
124
125         snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
126
127         return buffer;
128 }
129
130 int cmd_name_rev(int argc, const char **argv, const char *prefix)
131 {
132         struct object_array revs = { 0, 0, NULL };
133         int as_is = 0, all = 0, transform_stdin = 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);
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