Remove TYPE_* constant macros and use object_type enums consistently.
[git] / name-rev.c
1 #include <stdlib.h>
2 #include "cache.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "refs.h"
6
7 static const char name_rev_usage[] =
8         "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
9
10 typedef struct rev_name {
11         const char *tip_name;
12         int merge_traversals;
13         int generation;
14 } rev_name;
15
16 static long cutoff = LONG_MAX;
17
18 static 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)) {
49 copy_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
77 static int tags_only = 0;
78
79 static 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 == OBJ_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 == OBJ_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 */
108 static 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 != OBJ_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
129 int main(int argc, char **argv)
130 {
131         struct object_array revs = { 0, 0, NULL };
132         int as_is = 0, all = 0, transform_stdin = 0;
133
134         setup_git_directory();
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