blob: 06a38ac8c10126085e6477c205d450da089faae2 [file] [log] [blame]
Matthias Kestenholzd6b64ed2006-08-03 17:24:35 +02001#include "builtin.h"
Johannes Schindelinbd321bc2005-10-26 15:10:20 +02002#include "cache.h"
3#include "commit.h"
4#include "tag.h"
5#include "refs.h"
Pierre Habouzitedefb1a2007-10-15 22:57:59 +02006#include "parse-options.h"
Johannes Schindelinbd321bc2005-10-26 15:10:20 +02007
Junio C Hamanoc075aea2007-05-24 12:20:42 -07008#define CUTOFF_DATE_SLOP 86400 /* one day */
9
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020010typedef struct rev_name {
11 const char *tip_name;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020012 int generation;
Johannes Schindelinac076c22007-08-27 12:37:33 +010013 int distance;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020014} rev_name;
15
16static long cutoff = LONG_MAX;
17
Johannes Schindelinac076c22007-08-27 12:37:33 +010018/* How many generations are maximally preferred over _one_ merge traversal? */
19#define MERGE_TRAVERSAL_WEIGHT 65535
20
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020021static void name_rev(struct commit *commit,
Johannes Schindelinac076c22007-08-27 12:37:33 +010022 const char *tip_name, int generation, int distance,
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020023 int deref)
24{
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -070025 struct rev_name *name = (struct rev_name *)commit->util;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020026 struct commit_list *parents;
Junio C Hamanof2e6f1c2005-11-28 20:51:44 -080027 int parent_number = 1;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020028
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));
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -070047 commit->util = name;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020048 goto copy_data;
Johannes Schindelinac076c22007-08-27 12:37:33 +010049 } else if (name->distance > distance) {
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020050copy_data:
51 name->tip_name = tip_name;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020052 name->generation = generation;
Johannes Schindelinac076c22007-08-27 12:37:33 +010053 name->distance = distance;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020054 } else
55 return;
56
57 for (parents = commit->parents;
58 parents;
59 parents = parents->next, parent_number++) {
Junio C Hamanof2e6f1c2005-11-28 20:51:44 -080060 if (parent_number > 1) {
Johannes Schindelin59d3f542007-02-20 01:08:48 +010061 int len = strlen(tip_name);
Andy Whitcroftcf606e32007-05-15 17:33:25 +010062 char *new_name = xmalloc(len +
63 1 + decimal_length(generation) + /* ~<n> */
64 1 + 2 + /* ^NN */
65 1);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020066
Johannes Schindelin59d3f542007-02-20 01:08:48 +010067 if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
68 len -= 2;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020069 if (generation > 0)
Johannes Schindelin59d3f542007-02-20 01:08:48 +010070 sprintf(new_name, "%.*s~%d^%d", len, tip_name,
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020071 generation, parent_number);
72 else
Johannes Schindelin59d3f542007-02-20 01:08:48 +010073 sprintf(new_name, "%.*s^%d", len, tip_name,
74 parent_number);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020075
Johannes Schindelinac076c22007-08-27 12:37:33 +010076 name_rev(parents->item, new_name, 0,
77 distance + MERGE_TRAVERSAL_WEIGHT, 0);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020078 } else {
Johannes Schindelinac076c22007-08-27 12:37:33 +010079 name_rev(parents->item, tip_name, generation + 1,
80 distance + 1, 0);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020081 }
82 }
83}
84
Johannes Schindelin2afc29a2007-02-17 19:22:35 +010085struct name_ref_data {
86 int tags_only;
Shawn O. Pearce23615702007-05-21 03:20:25 -040087 int name_only;
Johannes Schindelin2afc29a2007-02-17 19:22:35 +010088 const char *ref_filter;
89};
90
Junio C Hamano8da19772006-09-20 22:02:01 -070091static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020092{
93 struct object *o = parse_object(sha1);
Johannes Schindelin2afc29a2007-02-17 19:22:35 +010094 struct name_ref_data *data = cb_data;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +020095 int deref = 0;
96
Junio C Hamanocc44c762007-02-20 01:53:29 -080097 if (data->tags_only && prefixcmp(path, "refs/tags/"))
Johannes Schindelin2afc29a2007-02-17 19:22:35 +010098 return 0;
99
100 if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200101 return 0;
102
Linus Torvalds19746322006-07-11 20:45:31 -0700103 while (o && o->type == OBJ_TAG) {
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200104 struct tag *t = (struct tag *) o;
105 if (!t->tagged)
106 break; /* broken repository */
107 o = parse_object(t->tagged->sha1);
108 deref = 1;
109 }
Linus Torvalds19746322006-07-11 20:45:31 -0700110 if (o && o->type == OBJ_COMMIT) {
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200111 struct commit *commit = (struct commit *)o;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200112
Junio C Hamanocc44c762007-02-20 01:53:29 -0800113 if (!prefixcmp(path, "refs/heads/"))
Junio C Hamano2c817df2006-01-11 14:20:09 -0800114 path = path + 11;
Shawn O. Pearce23615702007-05-21 03:20:25 -0400115 else if (data->tags_only
116 && data->name_only
117 && !prefixcmp(path, "refs/tags/"))
118 path = path + 10;
Junio C Hamanocc44c762007-02-20 01:53:29 -0800119 else if (!prefixcmp(path, "refs/"))
Junio C Hamano2c817df2006-01-11 14:20:09 -0800120 path = path + 5;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200121
Shawn Pearce9befac42006-09-02 00:16:31 -0400122 name_rev(commit, xstrdup(path), 0, 0, deref);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200123 }
124 return 0;
125}
126
127/* returns a static buffer */
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800128static const char *get_rev_name(const struct object *o)
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200129{
130 static char buffer[1024];
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700131 struct rev_name *n;
132 struct commit *c;
133
Linus Torvalds19746322006-07-11 20:45:31 -0700134 if (o->type != OBJ_COMMIT)
Pierre Habouzita2cf9f42007-12-24 12:18:22 +0100135 return NULL;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700136 c = (struct commit *) o;
137 n = c->util;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200138 if (!n)
Pierre Habouzita2cf9f42007-12-24 12:18:22 +0100139 return NULL;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200140
141 if (!n->generation)
142 return n->tip_name;
Johannes Schindelin59d3f542007-02-20 01:08:48 +0100143 else {
144 int len = strlen(n->tip_name);
145 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
146 len -= 2;
147 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
148 n->generation);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200149
Johannes Schindelin59d3f542007-02-20 01:08:48 +0100150 return buffer;
151 }
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200152}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700153
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800154static void show_name(const struct object *obj,
155 const char *caller_name,
156 int always, int allow_undefined, int name_only)
157{
158 const char *name;
159 const unsigned char *sha1 = obj->sha1;
160
161 if (!name_only)
162 printf("%s ", caller_name ? caller_name : sha1_to_hex(sha1));
163 name = get_rev_name(obj);
164 if (name)
165 printf("%s\n", name);
166 else if (allow_undefined)
167 printf("undefined\n");
168 else if (always)
169 printf("%s\n", find_unique_abbrev(sha1, DEFAULT_ABBREV));
170 else
171 die("cannot describe '%s'", sha1_to_hex(sha1));
172}
173
Pierre Habouzitedefb1a2007-10-15 22:57:59 +0200174static char const * const name_rev_usage[] = {
Stephan Beyer1b1dd232008-07-13 15:36:15 +0200175 "git name-rev [options] ( --all | --stdin | <commit>... )",
Pierre Habouzitedefb1a2007-10-15 22:57:59 +0200176 NULL
177};
178
Junio C Hamanoe8b55fa2008-08-02 11:04:22 -0700179static void name_rev_line(char *p, struct name_ref_data *data)
180{
181 int forty = 0;
182 char *p_start;
183 for (p_start = p; *p; p++) {
184#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
185 if (!ishex(*p))
186 forty = 0;
187 else if (++forty == 40 &&
188 !ishex(*(p+1))) {
189 unsigned char sha1[40];
190 const char *name = NULL;
191 char c = *(p+1);
René Scharfe7c5b1672008-08-03 15:44:33 +0200192 int p_len = p - p_start + 1;
Junio C Hamanoe8b55fa2008-08-02 11:04:22 -0700193
194 forty = 0;
195
196 *(p+1) = 0;
197 if (!get_sha1(p - 39, sha1)) {
198 struct object *o =
199 lookup_object(sha1);
200 if (o)
201 name = get_rev_name(o);
202 }
203 *(p+1) = c;
204
205 if (!name)
206 continue;
207
René Scharfe7c5b1672008-08-03 15:44:33 +0200208 if (data->name_only)
209 printf("%.*s%s", p_len - 40, p_start, name);
210 else
211 printf("%.*s (%s)", p_len, p_start, name);
Junio C Hamanoe8b55fa2008-08-02 11:04:22 -0700212 p_start = p + 1;
213 }
214 }
215
216 /* flush */
217 if (p_start != p)
218 fwrite(p_start, p - p_start, 1, stdout);
219}
220
Matthias Kestenholzd6b64ed2006-08-03 17:24:35 +0200221int cmd_name_rev(int argc, const char **argv, const char *prefix)
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200222{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700223 struct object_array revs = { 0, 0, NULL };
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800224 int all = 0, transform_stdin = 0, allow_undefined = 1, always = 0;
Shawn O. Pearce23615702007-05-21 03:20:25 -0400225 struct name_ref_data data = { 0, 0, NULL };
Pierre Habouzitedefb1a2007-10-15 22:57:59 +0200226 struct option opts[] = {
227 OPT_BOOLEAN(0, "name-only", &data.name_only, "print only names (no SHA-1)"),
228 OPT_BOOLEAN(0, "tags", &data.tags_only, "only use tags to name the commits"),
229 OPT_STRING(0, "refs", &data.ref_filter, "pattern",
230 "only use refs matching <pattern>"),
231 OPT_GROUP(""),
232 OPT_BOOLEAN(0, "all", &all, "list all commits reachable from all refs"),
233 OPT_BOOLEAN(0, "stdin", &transform_stdin, "read from stdin"),
Pierre Habouzita2cf9f42007-12-24 12:18:22 +0100234 OPT_BOOLEAN(0, "undefined", &allow_undefined, "allow to print `undefined` names"),
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800235 OPT_BOOLEAN(0, "always", &always,
236 "show abbreviated commit object as fallback"),
Pierre Habouzitedefb1a2007-10-15 22:57:59 +0200237 OPT_END(),
238 };
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200239
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100240 git_config(git_default_config, NULL);
Stephen Boyd37782922009-05-23 11:53:12 -0700241 argc = parse_options(argc, argv, prefix, opts, name_rev_usage, 0);
Pierre Habouzitedefb1a2007-10-15 22:57:59 +0200242 if (!!all + !!transform_stdin + !!argc > 1) {
243 error("Specify either a list, or --all, not both!");
244 usage_with_options(name_rev_usage, opts);
245 }
246 if (all || transform_stdin)
247 cutoff = 0;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200248
Pierre Habouzitedefb1a2007-10-15 22:57:59 +0200249 for (; argc; argc--, argv++) {
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200250 unsigned char sha1[20];
251 struct object *o;
252 struct commit *commit;
253
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200254 if (get_sha1(*argv, sha1)) {
255 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
256 *argv);
257 continue;
258 }
259
Junio C Hamano9534f402005-11-02 15:19:13 -0800260 o = deref_tag(parse_object(sha1), *argv, 0);
Linus Torvalds19746322006-07-11 20:45:31 -0700261 if (!o || o->type != OBJ_COMMIT) {
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200262 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
263 *argv);
264 continue;
265 }
266
267 commit = (struct commit *)o;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200268 if (cutoff > commit->date)
269 cutoff = commit->date;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700270 add_object_array((struct object *)commit, *argv, &revs);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200271 }
272
Junio C Hamanoc075aea2007-05-24 12:20:42 -0700273 if (cutoff)
274 cutoff = cutoff - CUTOFF_DATE_SLOP;
Johannes Schindelin2afc29a2007-02-17 19:22:35 +0100275 for_each_ref(name_ref, &data);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200276
277 if (transform_stdin) {
278 char buffer[2048];
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200279
280 while (!feof(stdin)) {
Junio C Hamanoe8b55fa2008-08-02 11:04:22 -0700281 char *p = fgets(buffer, sizeof(buffer), stdin);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200282 if (!p)
283 break;
Junio C Hamanoe8b55fa2008-08-02 11:04:22 -0700284 name_rev_line(p, &data);
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200285 }
286 } else if (all) {
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700287 int i, max;
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200288
Linus Torvaldsfc046a72006-06-29 21:38:55 -0700289 max = get_max_object_index();
Björn Steinbrinka83123d2008-06-06 01:31:55 +0200290 for (i = 0; i < max; i++) {
291 struct object *obj = get_indexed_object(i);
292 if (!obj)
293 continue;
294 show_name(obj, NULL,
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800295 always, allow_undefined, data.name_only);
Björn Steinbrinka83123d2008-06-06 01:31:55 +0200296 }
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700297 } else {
298 int i;
Junio C Hamanoda2478d2008-03-02 08:51:57 -0800299 for (i = 0; i < revs.nr; i++)
300 show_name(revs.objects[i].item, revs.objects[i].name,
301 always, allow_undefined, data.name_only);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700302 }
Johannes Schindelinbd321bc2005-10-26 15:10:20 +0200303
304 return 0;
305}