convert refresh_index to take struct pathspec
[git] / builtin / name-rev.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "refs.h"
6 #include "parse-options.h"
7
8 #define CUTOFF_DATE_SLOP 86400 /* one day */
9
10 typedef struct rev_name {
11         const char *tip_name;
12         int generation;
13         int distance;
14 } rev_name;
15
16 static long cutoff = LONG_MAX;
17
18 /* How many generations are maximally preferred over _one_ merge traversal? */
19 #define MERGE_TRAVERSAL_WEIGHT 65535
20
21 static void name_rev(struct commit *commit,
22                 const char *tip_name, int generation, int distance,
23                 int deref)
24 {
25         struct rev_name *name = (struct rev_name *)commit->util;
26         struct commit_list *parents;
27         int parent_number = 1;
28
29         if (!commit->object.parsed)
30                 parse_commit(commit);
31
32         if (commit->date < cutoff)
33                 return;
34
35         if (deref) {
36                 char *new_name = xmalloc(strlen(tip_name)+3);
37                 strcpy(new_name, tip_name);
38                 strcat(new_name, "^0");
39                 tip_name = new_name;
40
41                 if (generation)
42                         die("generation: %d, but deref?", generation);
43         }
44
45         if (name == NULL) {
46                 name = xmalloc(sizeof(rev_name));
47                 commit->util = name;
48                 goto copy_data;
49         } else if (name->distance > distance) {
50 copy_data:
51                 name->tip_name = tip_name;
52                 name->generation = generation;
53                 name->distance = distance;
54         } else
55                 return;
56
57         for (parents = commit->parents;
58                         parents;
59                         parents = parents->next, parent_number++) {
60                 if (parent_number > 1) {
61                         int len = strlen(tip_name);
62                         char *new_name = xmalloc(len +
63                                 1 + decimal_length(generation) +  /* ~<n> */
64                                 1 + 2 +                           /* ^NN */
65                                 1);
66
67                         if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
68                                 len -= 2;
69                         if (generation > 0)
70                                 sprintf(new_name, "%.*s~%d^%d", len, tip_name,
71                                                 generation, parent_number);
72                         else
73                                 sprintf(new_name, "%.*s^%d", len, tip_name,
74                                                 parent_number);
75
76                         name_rev(parents->item, new_name, 0,
77                                 distance + MERGE_TRAVERSAL_WEIGHT, 0);
78                 } else {
79                         name_rev(parents->item, tip_name, generation + 1,
80                                 distance + 1, 0);
81                 }
82         }
83 }
84
85 static int subpath_matches(const char *path, const char *filter)
86 {
87         const char *subpath = path;
88
89         while (subpath) {
90                 if (!fnmatch(filter, subpath, 0))
91                         return subpath - path;
92                 subpath = strchr(subpath, '/');
93                 if (subpath)
94                         subpath++;
95         }
96         return -1;
97 }
98
99 struct name_ref_data {
100         int tags_only;
101         int name_only;
102         const char *ref_filter;
103 };
104
105 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
106 {
107         struct object *o = parse_object(sha1);
108         struct name_ref_data *data = cb_data;
109         int can_abbreviate_output = data->tags_only && data->name_only;
110         int deref = 0;
111
112         if (data->tags_only && prefixcmp(path, "refs/tags/"))
113                 return 0;
114
115         if (data->ref_filter) {
116                 switch (subpath_matches(path, data->ref_filter)) {
117                 case -1: /* did not match */
118                         return 0;
119                 case 0:  /* matched fully */
120                         break;
121                 default: /* matched subpath */
122                         can_abbreviate_output = 1;
123                         break;
124                 }
125         }
126
127         while (o && o->type == OBJ_TAG) {
128                 struct tag *t = (struct tag *) o;
129                 if (!t->tagged)
130                         break; /* broken repository */
131                 o = parse_object(t->tagged->sha1);
132                 deref = 1;
133         }
134         if (o && o->type == OBJ_COMMIT) {
135                 struct commit *commit = (struct commit *)o;
136
137                 if (can_abbreviate_output)
138                         path = shorten_unambiguous_ref(path, 0);
139                 else if (!prefixcmp(path, "refs/heads/"))
140                         path = path + 11;
141                 else if (!prefixcmp(path, "refs/"))
142                         path = path + 5;
143
144                 name_rev(commit, xstrdup(path), 0, 0, deref);
145         }
146         return 0;
147 }
148
149 /* returns a static buffer */
150 static const char *get_rev_name(const struct object *o)
151 {
152         static char buffer[1024];
153         struct rev_name *n;
154         struct commit *c;
155
156         if (o->type != OBJ_COMMIT)
157                 return NULL;
158         c = (struct commit *) o;
159         n = c->util;
160         if (!n)
161                 return NULL;
162
163         if (!n->generation)
164                 return n->tip_name;
165         else {
166                 int len = strlen(n->tip_name);
167                 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
168                         len -= 2;
169                 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
170                                 n->generation);
171
172                 return buffer;
173         }
174 }
175
176 static void show_name(const struct object *obj,
177                       const char *caller_name,
178                       int always, int allow_undefined, int name_only)
179 {
180         const char *name;
181         const unsigned char *sha1 = obj->sha1;
182
183         if (!name_only)
184                 printf("%s ", caller_name ? caller_name : sha1_to_hex(sha1));
185         name = get_rev_name(obj);
186         if (name)
187                 printf("%s\n", name);
188         else if (allow_undefined)
189                 printf("undefined\n");
190         else if (always)
191                 printf("%s\n", find_unique_abbrev(sha1, DEFAULT_ABBREV));
192         else
193                 die("cannot describe '%s'", sha1_to_hex(sha1));
194 }
195
196 static char const * const name_rev_usage[] = {
197         N_("git name-rev [options] <commit>..."),
198         N_("git name-rev [options] --all"),
199         N_("git name-rev [options] --stdin"),
200         NULL
201 };
202
203 static void name_rev_line(char *p, struct name_ref_data *data)
204 {
205         int forty = 0;
206         char *p_start;
207         for (p_start = p; *p; p++) {
208 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
209                 if (!ishex(*p))
210                         forty = 0;
211                 else if (++forty == 40 &&
212                          !ishex(*(p+1))) {
213                         unsigned char sha1[40];
214                         const char *name = NULL;
215                         char c = *(p+1);
216                         int p_len = p - p_start + 1;
217
218                         forty = 0;
219
220                         *(p+1) = 0;
221                         if (!get_sha1(p - 39, sha1)) {
222                                 struct object *o =
223                                         lookup_object(sha1);
224                                 if (o)
225                                         name = get_rev_name(o);
226                         }
227                         *(p+1) = c;
228
229                         if (!name)
230                                 continue;
231
232                         if (data->name_only)
233                                 printf("%.*s%s", p_len - 40, p_start, name);
234                         else
235                                 printf("%.*s (%s)", p_len, p_start, name);
236                         p_start = p + 1;
237                 }
238         }
239
240         /* flush */
241         if (p_start != p)
242                 fwrite(p_start, p - p_start, 1, stdout);
243 }
244
245 int cmd_name_rev(int argc, const char **argv, const char *prefix)
246 {
247         struct object_array revs = OBJECT_ARRAY_INIT;
248         int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0;
249         struct name_ref_data data = { 0, 0, NULL };
250         struct option opts[] = {
251                 OPT_BOOLEAN(0, "name-only", &data.name_only, N_("print only names (no SHA-1)")),
252                 OPT_BOOLEAN(0, "tags", &data.tags_only, N_("only use tags to name the commits")),
253                 OPT_STRING(0, "refs", &data.ref_filter, N_("pattern"),
254                                    N_("only use refs matching <pattern>")),
255                 OPT_GROUP(""),
256                 OPT_BOOLEAN(0, "all", &all, N_("list all commits reachable from all refs")),
257                 OPT_BOOLEAN(0, "stdin", &transform_stdin, N_("read from stdin")),
258                 OPT_BOOLEAN(0, "undefined", &allow_undefined, N_("allow to print `undefined` names")),
259                 OPT_BOOLEAN(0, "always",     &always,
260                            N_("show abbreviated commit object as fallback")),
261                 OPT_END(),
262         };
263
264         git_config(git_default_config, NULL);
265         argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
266         if (!!all + !!transform_stdin + !!argc > 1) {
267                 error("Specify either a list, or --all, not both!");
268                 usage_with_options(name_rev_usage, opts);
269         }
270         if (all || transform_stdin)
271                 cutoff = 0;
272
273         for (; argc; argc--, argv++) {
274                 unsigned char sha1[20];
275                 struct object *o;
276                 struct commit *commit;
277
278                 if (get_sha1(*argv, sha1)) {
279                         fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
280                                         *argv);
281                         continue;
282                 }
283
284                 o = deref_tag(parse_object(sha1), *argv, 0);
285                 if (!o || o->type != OBJ_COMMIT) {
286                         fprintf(stderr, "Could not get commit for %s. Skipping.\n",
287                                         *argv);
288                         continue;
289                 }
290
291                 commit = (struct commit *)o;
292                 if (cutoff > commit->date)
293                         cutoff = commit->date;
294                 add_object_array((struct object *)commit, *argv, &revs);
295         }
296
297         if (cutoff)
298                 cutoff = cutoff - CUTOFF_DATE_SLOP;
299         for_each_ref(name_ref, &data);
300
301         if (transform_stdin) {
302                 char buffer[2048];
303
304                 while (!feof(stdin)) {
305                         char *p = fgets(buffer, sizeof(buffer), stdin);
306                         if (!p)
307                                 break;
308                         name_rev_line(p, &data);
309                 }
310         } else if (all) {
311                 int i, max;
312
313                 max = get_max_object_index();
314                 for (i = 0; i < max; i++) {
315                         struct object *obj = get_indexed_object(i);
316                         if (!obj || obj->type != OBJ_COMMIT)
317                                 continue;
318                         show_name(obj, NULL,
319                                   always, allow_undefined, data.name_only);
320                 }
321         } else {
322                 int i;
323                 for (i = 0; i < revs.nr; i++)
324                         show_name(revs.objects[i].item, revs.objects[i].name,
325                                   always, allow_undefined, data.name_only);
326         }
327
328         return 0;
329 }