Merge branch 'maint'
[git/git.git] / builtin-name-rev.c
CommitLineData
d6b64ed0 1#include "builtin.h"
bd321bcc
JS
2#include "cache.h"
3#include "commit.h"
4#include "tag.h"
5#include "refs.h"
6
c075aea5
JH
7#define CUTOFF_DATE_SLOP 86400 /* one day */
8
bd321bcc 9static const char name_rev_usage[] =
2afc29aa 10 "git-name-rev [--tags | --refs=<pattern>] ( --all | --stdin | committish [committish...] )\n";
bd321bcc
JS
11
12typedef struct rev_name {
13 const char *tip_name;
14 int merge_traversals;
15 int generation;
16} rev_name;
17
18static long cutoff = LONG_MAX;
19
20static void name_rev(struct commit *commit,
21 const char *tip_name, int merge_traversals, int generation,
22 int deref)
23{
d3ff6f55 24 struct rev_name *name = (struct rev_name *)commit->util;
bd321bcc 25 struct commit_list *parents;
f2e6f1c9 26 int parent_number = 1;
bd321bcc
JS
27
28 if (!commit->object.parsed)
29 parse_commit(commit);
30
31 if (commit->date < cutoff)
32 return;
33
34 if (deref) {
35 char *new_name = xmalloc(strlen(tip_name)+3);
36 strcpy(new_name, tip_name);
37 strcat(new_name, "^0");
38 tip_name = new_name;
39
40 if (generation)
41 die("generation: %d, but deref?", generation);
42 }
43
44 if (name == NULL) {
45 name = xmalloc(sizeof(rev_name));
d3ff6f55 46 commit->util = name;
bd321bcc
JS
47 goto copy_data;
48 } else if (name->merge_traversals > merge_traversals ||
49 (name->merge_traversals == merge_traversals &&
50 name->generation > generation)) {
51copy_data:
52 name->tip_name = tip_name;
53 name->merge_traversals = merge_traversals;
54 name->generation = generation;
55 } else
56 return;
57
58 for (parents = commit->parents;
59 parents;
60 parents = parents->next, parent_number++) {
f2e6f1c9 61 if (parent_number > 1) {
59d3f541 62 int len = strlen(tip_name);
cf606e3d
AW
63 char *new_name = xmalloc(len +
64 1 + decimal_length(generation) + /* ~<n> */
65 1 + 2 + /* ^NN */
66 1);
bd321bcc 67
59d3f541
JS
68 if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
69 len -= 2;
bd321bcc 70 if (generation > 0)
59d3f541 71 sprintf(new_name, "%.*s~%d^%d", len, tip_name,
bd321bcc
JS
72 generation, parent_number);
73 else
59d3f541
JS
74 sprintf(new_name, "%.*s^%d", len, tip_name,
75 parent_number);
bd321bcc
JS
76
77 name_rev(parents->item, new_name,
78 merge_traversals + 1 , 0, 0);
79 } else {
80 name_rev(parents->item, tip_name, merge_traversals,
81 generation + 1, 0);
82 }
83 }
84}
85
2afc29aa
JS
86struct name_ref_data {
87 int tags_only;
23615708 88 int name_only;
2afc29aa
JS
89 const char *ref_filter;
90};
91
8da19775 92static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
bd321bcc
JS
93{
94 struct object *o = parse_object(sha1);
2afc29aa 95 struct name_ref_data *data = cb_data;
bd321bcc
JS
96 int deref = 0;
97
cc44c765 98 if (data->tags_only && prefixcmp(path, "refs/tags/"))
2afc29aa
JS
99 return 0;
100
101 if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
bd321bcc
JS
102 return 0;
103
1974632c 104 while (o && o->type == OBJ_TAG) {
bd321bcc
JS
105 struct tag *t = (struct tag *) o;
106 if (!t->tagged)
107 break; /* broken repository */
108 o = parse_object(t->tagged->sha1);
109 deref = 1;
110 }
1974632c 111 if (o && o->type == OBJ_COMMIT) {
bd321bcc 112 struct commit *commit = (struct commit *)o;
bd321bcc 113
cc44c765 114 if (!prefixcmp(path, "refs/heads/"))
2c817df2 115 path = path + 11;
23615708
SP
116 else if (data->tags_only
117 && data->name_only
118 && !prefixcmp(path, "refs/tags/"))
119 path = path + 10;
cc44c765 120 else if (!prefixcmp(path, "refs/"))
2c817df2 121 path = path + 5;
bd321bcc 122
9befac47 123 name_rev(commit, xstrdup(path), 0, 0, deref);
bd321bcc
JS
124 }
125 return 0;
126}
127
128/* returns a static buffer */
129static const char* get_rev_name(struct object *o)
130{
131 static char buffer[1024];
d3ff6f55
LT
132 struct rev_name *n;
133 struct commit *c;
134
1974632c 135 if (o->type != OBJ_COMMIT)
d3ff6f55
LT
136 return "undefined";
137 c = (struct commit *) o;
138 n = c->util;
bd321bcc
JS
139 if (!n)
140 return "undefined";
141
142 if (!n->generation)
143 return n->tip_name;
59d3f541
JS
144 else {
145 int len = strlen(n->tip_name);
146 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
147 len -= 2;
148 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
149 n->generation);
150
151 return buffer;
152 }
bd321bcc 153}
1f1e895f 154
d6b64ed0 155int cmd_name_rev(int argc, const char **argv, const char *prefix)
bd321bcc 156{
1f1e895f 157 struct object_array revs = { 0, 0, NULL };
bd321bcc 158 int as_is = 0, all = 0, transform_stdin = 0;
23615708 159 struct name_ref_data data = { 0, 0, NULL };
bd321bcc 160
84a9b58c 161 git_config(git_default_config);
bd321bcc
JS
162
163 if (argc < 2)
164 usage(name_rev_usage);
165
166 for (--argc, ++argv; argc; --argc, ++argv) {
167 unsigned char sha1[20];
168 struct object *o;
169 struct commit *commit;
170
171 if (!as_is && (*argv)[0] == '-') {
172 if (!strcmp(*argv, "--")) {
173 as_is = 1;
174 continue;
23615708
SP
175 } else if (!strcmp(*argv, "--name-only")) {
176 data.name_only = 1;
177 continue;
bd321bcc 178 } else if (!strcmp(*argv, "--tags")) {
2afc29aa
JS
179 data.tags_only = 1;
180 continue;
cc44c765 181 } else if (!prefixcmp(*argv, "--refs=")) {
2afc29aa 182 data.ref_filter = *argv + 7;
bd321bcc
JS
183 continue;
184 } else if (!strcmp(*argv, "--all")) {
185 if (argc > 1)
186 die("Specify either a list, or --all, not both!");
187 all = 1;
188 cutoff = 0;
189 continue;
190 } else if (!strcmp(*argv, "--stdin")) {
191 if (argc > 1)
192 die("Specify either a list, or --stdin, not both!");
193 transform_stdin = 1;
194 cutoff = 0;
195 continue;
196 }
197 usage(name_rev_usage);
198 }
199
200 if (get_sha1(*argv, sha1)) {
201 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
202 *argv);
203 continue;
204 }
205
9534f40b 206 o = deref_tag(parse_object(sha1), *argv, 0);
1974632c 207 if (!o || o->type != OBJ_COMMIT) {
bd321bcc
JS
208 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
209 *argv);
210 continue;
211 }
212
213 commit = (struct commit *)o;
214
215 if (cutoff > commit->date)
216 cutoff = commit->date;
217
1f1e895f 218 add_object_array((struct object *)commit, *argv, &revs);
bd321bcc
JS
219 }
220
c075aea5
JH
221 if (cutoff)
222 cutoff = cutoff - CUTOFF_DATE_SLOP;
2afc29aa 223 for_each_ref(name_ref, &data);
bd321bcc
JS
224
225 if (transform_stdin) {
226 char buffer[2048];
227 char *p, *p_start;
228
229 while (!feof(stdin)) {
230 int forty = 0;
231 p = fgets(buffer, sizeof(buffer), stdin);
232 if (!p)
233 break;
234
235 for (p_start = p; *p; p++) {
236#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
237 if (!ishex(*p))
238 forty = 0;
239 else if (++forty == 40 &&
240 !ishex(*(p+1))) {
241 unsigned char sha1[40];
242 const char *name = "undefined";
243 char c = *(p+1);
244
245 forty = 0;
246
247 *(p+1) = 0;
248 if (!get_sha1(p - 39, sha1)) {
249 struct object *o =
250 lookup_object(sha1);
251 if (o)
252 name = get_rev_name(o);
253 }
254 *(p+1) = c;
255
256 if (!strcmp(name, "undefined"))
257 continue;
258
2d76d0d1
JH
259 fwrite(p_start, p - p_start + 1, 1,
260 stdout);
261 printf(" (%s)", name);
bd321bcc
JS
262 p_start = p + 1;
263 }
264 }
265
266 /* flush */
267 if (p_start != p)
268 fwrite(p_start, p - p_start, 1, stdout);
269 }
270 } else if (all) {
fc046a75 271 int i, max;
bd321bcc 272
fc046a75
LT
273 max = get_max_object_index();
274 for (i = 0; i < max; i++) {
275 struct object * obj = get_indexed_object(i);
276 if (!obj)
277 continue;
23615708
SP
278 if (!data.name_only)
279 printf("%s ", sha1_to_hex(obj->sha1));
280 printf("%s\n", get_rev_name(obj));
fc046a75 281 }
1f1e895f
LT
282 } else {
283 int i;
23615708
SP
284 for (i = 0; i < revs.nr; i++) {
285 if (!data.name_only)
286 printf("%s ", revs.objects[i].name);
287 printf("%s\n", get_rev_name(revs.objects[i].item));
288 }
1f1e895f 289 }
bd321bcc
JS
290
291 return 0;
292}
293