blob: 974f3403abe76288dc98797e3ceac21a070828ec [file] [log] [blame]
Junio C Hamanof76412e2005-08-21 02:51:10 -07001#include "cache.h"
2#include "commit.h"
3#include "refs.h"
Peter Eriksen51ce34b2006-05-23 14:15:35 +02004#include "builtin.h"
Markus Heidelbergab07ba22009-04-22 23:41:25 +02005#include "color.h"
René Scharfec949b002015-10-31 20:06:45 +01006#include "argv-array.h"
Stephen Boyd57343652009-05-21 00:33:18 -07007#include "parse-options.h"
Junio C Hamanof76412e2005-08-21 02:51:10 -07008
Stephen Boyd57343652009-05-21 00:33:18 -07009static const char* show_branch_usage[] = {
Junio C Hamanobb831db2015-02-11 13:44:19 -080010 N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
Ralf Thielowd6589d12015-01-20 20:30:28 +010011 " [--current] [--color[=<when>] | --no-color] [--sparse]\n"
12 " [--more=<n> | --list | --independent | --merge-base]\n"
13 " [--no-name | --sha1-name] [--topics] [(<rev> | <glob>)...]"),
Alex Henrie9c9b4f22015-01-13 00:44:47 -070014 N_("git show-branch (-g | --reflog)[=<n>[,<base>]] [--list] [<ref>]"),
Stephen Boyd57343652009-05-21 00:33:18 -070015 NULL
16};
Junio C Hamanof76412e2005-08-21 02:51:10 -070017
Markus Heidelbergab07ba22009-04-22 23:41:25 +020018static int showbranch_use_color = -1;
Markus Heidelbergab07ba22009-04-22 23:41:25 +020019
René Scharfec949b002015-10-31 20:06:45 +010020static struct argv_array default_args = ARGV_ARRAY_INIT;
Junio C Hamanoc2bdd6a2006-01-09 13:29:23 -080021
Junio C Hamanof76412e2005-08-21 02:51:10 -070022#define UNINTERESTING 01
23
24#define REV_SHIFT 2
Linus Torvalds885a86a2006-06-14 16:45:13 -070025#define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
Junio C Hamanof76412e2005-08-21 02:51:10 -070026
Junio C Hamano7e3fe902006-12-14 15:58:56 -080027#define DEFAULT_REFLOG 4
28
Markus Heidelbergab07ba22009-04-22 23:41:25 +020029static const char *get_color_code(int idx)
30{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070031 if (want_color(showbranch_use_color))
Dan McGee7cd52b52011-04-05 00:40:23 -050032 return column_colors_ansi[idx % column_colors_ansi_max];
Markus Heidelbergab07ba22009-04-22 23:41:25 +020033 return "";
34}
35
36static const char *get_color_reset_code(void)
37{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -070038 if (want_color(showbranch_use_color))
Markus Heidelbergab07ba22009-04-22 23:41:25 +020039 return GIT_COLOR_RESET;
40 return "";
41}
42
Junio C Hamanof76412e2005-08-21 02:51:10 -070043static struct commit *interesting(struct commit_list *list)
44{
45 while (list) {
46 struct commit *commit = list->item;
47 list = list->next;
48 if (commit->object.flags & UNINTERESTING)
49 continue;
50 return commit;
51 }
52 return NULL;
53}
54
Junio C Hamanof76412e2005-08-21 02:51:10 -070055struct commit_name {
Junio C Hamano8e5dd222005-08-29 17:19:47 -070056 const char *head_name; /* which head's ancestor? */
57 int generation; /* how many parents away from head_name */
Junio C Hamanof76412e2005-08-21 02:51:10 -070058};
59
Junio C Hamano8e5dd222005-08-29 17:19:47 -070060/* Name the commit as nth generation ancestor of head_name;
Junio C Hamanof76412e2005-08-21 02:51:10 -070061 * we count only the first-parent relationship for naming purposes.
62 */
Junio C Hamano8e5dd222005-08-29 17:19:47 -070063static void name_commit(struct commit *commit, const char *head_name, int nth)
Junio C Hamanof76412e2005-08-21 02:51:10 -070064{
65 struct commit_name *name;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -070066 if (!commit->util)
67 commit->util = xmalloc(sizeof(struct commit_name));
68 name = commit->util;
Junio C Hamano8e5dd222005-08-29 17:19:47 -070069 name->head_name = head_name;
Junio C Hamanof76412e2005-08-21 02:51:10 -070070 name->generation = nth;
71}
72
73/* Parent is the first parent of the commit. We may name it
Junio C Hamano8e5dd222005-08-29 17:19:47 -070074 * as (n+1)th generation ancestor of the same head_name as
Junio C Hamanobcaf60b2005-12-08 14:10:02 -080075 * commit is nth generation ancestor of, if that generation
Junio C Hamanof76412e2005-08-21 02:51:10 -070076 * number is better than the name it already has.
77 */
78static void name_parent(struct commit *commit, struct commit *parent)
79{
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -070080 struct commit_name *commit_name = commit->util;
81 struct commit_name *parent_name = parent->util;
Junio C Hamanof76412e2005-08-21 02:51:10 -070082 if (!commit_name)
83 return;
84 if (!parent_name ||
85 commit_name->generation + 1 < parent_name->generation)
Junio C Hamano8e5dd222005-08-29 17:19:47 -070086 name_commit(parent, commit_name->head_name,
Junio C Hamanof76412e2005-08-21 02:51:10 -070087 commit_name->generation + 1);
88}
89
Junio C Hamano8e5dd222005-08-29 17:19:47 -070090static int name_first_parent_chain(struct commit *c)
91{
92 int i = 0;
93 while (c) {
94 struct commit *p;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -070095 if (!c->util)
Junio C Hamano8e5dd222005-08-29 17:19:47 -070096 break;
97 if (!c->parents)
98 break;
99 p = c->parents->item;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700100 if (!p->util) {
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700101 name_parent(c, p);
102 i++;
103 }
Alexandre Julliardf8263c52006-07-23 19:51:04 +0200104 else
105 break;
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700106 c = p;
107 }
108 return i;
109}
110
111static void name_commits(struct commit_list *list,
112 struct commit **rev,
113 char **ref_name,
114 int num_rev)
115{
116 struct commit_list *cl;
117 struct commit *c;
118 int i;
119
120 /* First give names to the given heads */
121 for (cl = list; cl; cl = cl->next) {
122 c = cl->item;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700123 if (c->util)
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700124 continue;
125 for (i = 0; i < num_rev; i++) {
126 if (rev[i] == c) {
127 name_commit(c, ref_name[i], 0);
128 break;
129 }
130 }
131 }
132
133 /* Then commits on the first parent ancestry chain */
134 do {
135 i = 0;
136 for (cl = list; cl; cl = cl->next) {
137 i += name_first_parent_chain(cl->item);
138 }
139 } while (i);
140
141 /* Finally, any unnamed commits */
142 do {
143 i = 0;
144 for (cl = list; cl; cl = cl->next) {
145 struct commit_list *parents;
146 struct commit_name *n;
147 int nth;
148 c = cl->item;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700149 if (!c->util)
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700150 continue;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700151 n = c->util;
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700152 parents = c->parents;
153 nth = 0;
154 while (parents) {
155 struct commit *p = parents->item;
Jeff Kingaaa07e32013-04-05 17:15:50 -0400156 struct strbuf newname = STRBUF_INIT;
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700157 parents = parents->next;
158 nth++;
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700159 if (p->util)
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700160 continue;
Junio C Hamanofbaf8342005-09-24 23:33:02 -0700161 switch (n->generation) {
162 case 0:
Jeff Kingaaa07e32013-04-05 17:15:50 -0400163 strbuf_addstr(&newname, n->head_name);
Junio C Hamanofbaf8342005-09-24 23:33:02 -0700164 break;
165 case 1:
Jeff Kingaaa07e32013-04-05 17:15:50 -0400166 strbuf_addf(&newname, "%s^", n->head_name);
Junio C Hamanofbaf8342005-09-24 23:33:02 -0700167 break;
168 default:
Jeff Kingaaa07e32013-04-05 17:15:50 -0400169 strbuf_addf(&newname, "%s~%d",
170 n->head_name, n->generation);
Junio C Hamano013f2762005-10-11 15:22:48 -0700171 break;
Junio C Hamanofbaf8342005-09-24 23:33:02 -0700172 }
Junio C Hamano013f2762005-10-11 15:22:48 -0700173 if (nth == 1)
Jeff Kingaaa07e32013-04-05 17:15:50 -0400174 strbuf_addch(&newname, '^');
Junio C Hamano013f2762005-10-11 15:22:48 -0700175 else
Jeff Kingaaa07e32013-04-05 17:15:50 -0400176 strbuf_addf(&newname, "^%d", nth);
177 name_commit(p, strbuf_detach(&newname, NULL), 0);
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700178 i++;
179 name_first_parent_chain(p);
180 }
181 }
182 } while (i);
183}
184
Junio C Hamanof76412e2005-08-21 02:51:10 -0700185static int mark_seen(struct commit *commit, struct commit_list **seen_p)
186{
187 if (!commit->object.flags) {
Junio C Hamano26a8ad22006-07-16 00:00:09 -0700188 commit_list_insert(commit, seen_p);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700189 return 1;
190 }
191 return 0;
192}
193
194static void join_revs(struct commit_list **list_p,
195 struct commit_list **seen_p,
196 int num_rev, int extra)
197{
198 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
199 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
200
201 while (*list_p) {
202 struct commit_list *parents;
Junio C Hamano9ce70282005-11-09 23:36:15 -0800203 int still_interesting = !!interesting(*list_p);
René Scharfee510ab82015-10-24 18:21:31 +0200204 struct commit *commit = pop_commit(list_p);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700205 int flags = commit->object.flags & all_mask;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700206
Junio C Hamano9ce70282005-11-09 23:36:15 -0800207 if (!still_interesting && extra <= 0)
Junio C Hamanof76412e2005-08-21 02:51:10 -0700208 break;
209
210 mark_seen(commit, seen_p);
211 if ((flags & all_revs) == all_revs)
212 flags |= UNINTERESTING;
213 parents = commit->parents;
214
215 while (parents) {
216 struct commit *p = parents->item;
217 int this_flag = p->object.flags;
218 parents = parents->next;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700219 if ((this_flag & flags) == flags)
220 continue;
Jeff King00640532013-10-24 04:53:01 -0400221 parse_commit(p);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700222 if (mark_seen(p, seen_p) && !still_interesting)
223 extra--;
224 p->object.flags |= flags;
Thiago Farina47e44ed2010-11-26 23:58:14 -0200225 commit_list_insert_by_date(p, list_p);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700226 }
227 }
Junio C Hamano6b209d42005-11-10 15:47:58 -0800228
229 /*
230 * Postprocess to complete well-poisoning.
231 *
232 * At this point we have all the commits we have seen in
Junio C Hamano26a8ad22006-07-16 00:00:09 -0700233 * seen_p list. Mark anything that can be reached from
234 * uninteresting commits not interesting.
Junio C Hamano6b209d42005-11-10 15:47:58 -0800235 */
236 for (;;) {
237 int changed = 0;
238 struct commit_list *s;
239 for (s = *seen_p; s; s = s->next) {
240 struct commit *c = s->item;
241 struct commit_list *parents;
242
243 if (((c->object.flags & all_revs) != all_revs) &&
244 !(c->object.flags & UNINTERESTING))
245 continue;
246
247 /* The current commit is either a merge base or
248 * already uninteresting one. Mark its parents
249 * as uninteresting commits _only_ if they are
250 * already parsed. No reason to find new ones
251 * here.
252 */
253 parents = c->parents;
254 while (parents) {
255 struct commit *p = parents->item;
256 parents = parents->next;
257 if (!(p->object.flags & UNINTERESTING)) {
258 p->object.flags |= UNINTERESTING;
259 changed = 1;
260 }
261 }
262 }
263 if (!changed)
264 break;
265 }
Junio C Hamanof76412e2005-08-21 02:51:10 -0700266}
267
Junio C Hamano013f2762005-10-11 15:22:48 -0700268static void show_one_commit(struct commit *commit, int no_name)
Junio C Hamanof76412e2005-08-21 02:51:10 -0700269{
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500270 struct strbuf pretty = STRBUF_INIT;
Junio C Hamano80583c02007-06-11 00:34:54 -0700271 const char *pretty_str = "(unavailable)";
Linus Torvaldsd3ff6f52006-06-17 18:26:18 -0700272 struct commit_name *name = commit->util;
Junio C Hamano80583c02007-06-11 00:34:54 -0700273
274 if (commit->object.parsed) {
Jeff King8b8a5372011-05-26 18:27:24 -0400275 pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
Pierre Habouzit674d1722007-09-10 12:35:06 +0200276 pretty_str = pretty.buf;
Junio C Hamano80583c02007-06-11 00:34:54 -0700277 }
Christian Couder59556542013-11-30 21:55:40 +0100278 if (starts_with(pretty_str, "[PATCH] "))
Junio C Hamano80583c02007-06-11 00:34:54 -0700279 pretty_str += 8;
Junio C Hamano013f2762005-10-11 15:22:48 -0700280
281 if (!no_name) {
282 if (name && name->head_name) {
283 printf("[%s", name->head_name);
284 if (name->generation) {
285 if (name->generation == 1)
286 printf("^");
287 else
288 printf("~%d", name->generation);
289 }
290 printf("] ");
291 }
292 else
293 printf("[%s] ",
brian m. carlsoned1c9972015-11-10 02:22:29 +0000294 find_unique_abbrev(commit->object.oid.hash,
Tay Ray Chuanbd7440f2010-05-24 16:50:44 +0800295 DEFAULT_ABBREV));
Junio C Hamanof76412e2005-08-21 02:51:10 -0700296 }
Junio C Hamano80583c02007-06-11 00:34:54 -0700297 puts(pretty_str);
Pierre Habouzit674d1722007-09-10 12:35:06 +0200298 strbuf_release(&pretty);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700299}
300
301static char *ref_name[MAX_REVS + 1];
302static int ref_name_cnt;
303
Junio C Hamanobb5ebed2005-12-23 12:47:18 -0800304static const char *find_digit_prefix(const char *s, int *v)
305{
306 const char *p;
307 int ver;
308 char ch;
309
310 for (p = s, ver = 0;
311 '0' <= (ch = *p) && ch <= '9';
312 p++)
313 ver = ver * 10 + ch - '0';
314 *v = ver;
315 return p;
316}
317
318
319static int version_cmp(const char *a, const char *b)
320{
321 while (1) {
322 int va, vb;
323
324 a = find_digit_prefix(a, &va);
325 b = find_digit_prefix(b, &vb);
326 if (va != vb)
327 return va - vb;
328
329 while (1) {
330 int ca = *a;
331 int cb = *b;
332 if ('0' <= ca && ca <= '9')
333 ca = 0;
334 if ('0' <= cb && cb <= '9')
335 cb = 0;
336 if (ca != cb)
337 return ca - cb;
338 if (!ca)
339 break;
340 a++;
341 b++;
342 }
343 if (!*a && !*b)
344 return 0;
345 }
346}
347
Junio C Hamano628894b2005-08-24 23:26:20 -0700348static int compare_ref_name(const void *a_, const void *b_)
349{
350 const char * const*a = a_, * const*b = b_;
Junio C Hamanobb5ebed2005-12-23 12:47:18 -0800351 return version_cmp(*a, *b);
Junio C Hamano628894b2005-08-24 23:26:20 -0700352}
353
354static void sort_ref_range(int bottom, int top)
355{
René Scharfe7e65c752016-10-01 18:19:48 +0200356 QSORT(ref_name + bottom, top - bottom, compare_ref_name);
Junio C Hamano628894b2005-08-24 23:26:20 -0700357}
358
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000359static int append_ref(const char *refname, const struct object_id *oid,
Junio C Hamano76a44c52007-01-19 01:20:23 -0800360 int allow_dups)
Junio C Hamanof76412e2005-08-21 02:51:10 -0700361{
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000362 struct commit *commit = lookup_commit_reference_gently(oid->hash, 1);
Junio C Hamanobb5ebed2005-12-23 12:47:18 -0800363 int i;
364
Junio C Hamanof76412e2005-08-21 02:51:10 -0700365 if (!commit)
366 return 0;
Junio C Hamanobb5ebed2005-12-23 12:47:18 -0800367
Junio C Hamano76a44c52007-01-19 01:20:23 -0800368 if (!allow_dups) {
369 /* Avoid adding the same thing twice */
370 for (i = 0; i < ref_name_cnt; i++)
371 if (!strcmp(refname, ref_name[i]))
372 return 0;
373 }
Junio C Hamano79778e42005-10-23 01:18:42 -0700374 if (MAX_REVS <= ref_name_cnt) {
Vasco Almeida205d1342016-09-15 14:59:07 +0000375 warning(Q_("ignoring %s; cannot handle more than %d ref",
376 "ignoring %s; cannot handle more than %d refs",
377 MAX_REVS), refname, MAX_REVS);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700378 return 0;
379 }
Shawn Pearce9befac42006-09-02 00:16:31 -0400380 ref_name[ref_name_cnt++] = xstrdup(refname);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700381 ref_name[ref_name_cnt] = NULL;
382 return 0;
383}
384
Michael Haggerty2e253a42015-05-25 18:38:46 +0000385static int append_head_ref(const char *refname, const struct object_id *oid,
386 int flag, void *cb_data)
Junio C Hamanof76412e2005-08-21 02:51:10 -0700387{
Michael Haggerty2e253a42015-05-25 18:38:46 +0000388 struct object_id tmp;
Junio C Hamano92421502005-11-21 00:43:12 -0800389 int ofs = 11;
Christian Couder59556542013-11-30 21:55:40 +0100390 if (!starts_with(refname, "refs/heads/"))
Junio C Hamanof76412e2005-08-21 02:51:10 -0700391 return 0;
Junio C Hamano92421502005-11-21 00:43:12 -0800392 /* If both heads/foo and tags/foo exists, get_sha1 would
393 * get confused.
394 */
Michael Haggerty2e253a42015-05-25 18:38:46 +0000395 if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid))
Junio C Hamano92421502005-11-21 00:43:12 -0800396 ofs = 5;
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000397 return append_ref(refname + ofs, oid, 0);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700398}
399
Michael Haggerty2e253a42015-05-25 18:38:46 +0000400static int append_remote_ref(const char *refname, const struct object_id *oid,
401 int flag, void *cb_data)
Brian Gernhardtf49006b2006-12-22 08:58:39 -0500402{
Michael Haggerty2e253a42015-05-25 18:38:46 +0000403 struct object_id tmp;
Brian Gernhardtf49006b2006-12-22 08:58:39 -0500404 int ofs = 13;
Christian Couder59556542013-11-30 21:55:40 +0100405 if (!starts_with(refname, "refs/remotes/"))
Brian Gernhardtf49006b2006-12-22 08:58:39 -0500406 return 0;
407 /* If both heads/foo and tags/foo exists, get_sha1 would
408 * get confused.
409 */
Michael Haggerty2e253a42015-05-25 18:38:46 +0000410 if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid))
Brian Gernhardtf49006b2006-12-22 08:58:39 -0500411 ofs = 5;
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000412 return append_ref(refname + ofs, oid, 0);
Brian Gernhardtf49006b2006-12-22 08:58:39 -0500413}
414
Michael Haggerty2e253a42015-05-25 18:38:46 +0000415static int append_tag_ref(const char *refname, const struct object_id *oid,
416 int flag, void *cb_data)
Junio C Hamanof76412e2005-08-21 02:51:10 -0700417{
Christian Couder59556542013-11-30 21:55:40 +0100418 if (!starts_with(refname, "refs/tags/"))
Junio C Hamanof76412e2005-08-21 02:51:10 -0700419 return 0;
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000420 return append_ref(refname + 5, oid, 0);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700421}
422
Junio C Hamano287f8602005-12-04 15:58:50 -0800423static const char *match_ref_pattern = NULL;
424static int match_ref_slash = 0;
425static int count_slash(const char *s)
426{
427 int cnt = 0;
428 while (*s)
429 if (*s++ == '/')
430 cnt++;
431 return cnt;
432}
433
Michael Haggertya00595f2015-05-25 18:38:45 +0000434static int append_matching_ref(const char *refname, const struct object_id *oid,
435 int flag, void *cb_data)
Junio C Hamano287f8602005-12-04 15:58:50 -0800436{
437 /* we want to allow pattern hold/<asterisk> to show all
438 * branches under refs/heads/hold/, and v0.99.9? to show
439 * refs/tags/v0.99.9a and friends.
440 */
441 const char *tail;
442 int slash = count_slash(refname);
443 for (tail = refname; *tail && match_ref_slash < slash; )
444 if (*tail++ == '/')
445 slash--;
446 if (!*tail)
447 return 0;
Nguyễn Thái Ngọc Duyeb078942014-02-15 09:01:46 +0700448 if (wildmatch(match_ref_pattern, tail, 0, NULL))
Junio C Hamano287f8602005-12-04 15:58:50 -0800449 return 0;
Christian Couder59556542013-11-30 21:55:40 +0100450 if (starts_with(refname, "refs/heads/"))
Michael Haggerty2e253a42015-05-25 18:38:46 +0000451 return append_head_ref(refname, oid, flag, cb_data);
Christian Couder59556542013-11-30 21:55:40 +0100452 if (starts_with(refname, "refs/tags/"))
Michael Haggerty2e253a42015-05-25 18:38:46 +0000453 return append_tag_ref(refname, oid, flag, cb_data);
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000454 return append_ref(refname, oid, 0);
Junio C Hamano287f8602005-12-04 15:58:50 -0800455}
456
Brian Gernhardtf49006b2006-12-22 08:58:39 -0500457static void snarf_refs(int head, int remotes)
Junio C Hamanof76412e2005-08-21 02:51:10 -0700458{
Junio C Hamano628894b2005-08-24 23:26:20 -0700459 if (head) {
460 int orig_cnt = ref_name_cnt;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +0000461
Michael Haggerty2e253a42015-05-25 18:38:46 +0000462 for_each_ref(append_head_ref, NULL);
Junio C Hamano628894b2005-08-24 23:26:20 -0700463 sort_ref_range(orig_cnt, ref_name_cnt);
464 }
Brian Gernhardtf49006b2006-12-22 08:58:39 -0500465 if (remotes) {
Junio C Hamano628894b2005-08-24 23:26:20 -0700466 int orig_cnt = ref_name_cnt;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +0000467
Michael Haggerty2e253a42015-05-25 18:38:46 +0000468 for_each_ref(append_remote_ref, NULL);
Junio C Hamano628894b2005-08-24 23:26:20 -0700469 sort_ref_range(orig_cnt, ref_name_cnt);
470 }
Junio C Hamanof76412e2005-08-21 02:51:10 -0700471}
472
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700473static int rev_is_head(char *head, int headlen, char *name,
Junio C Hamanof76412e2005-08-21 02:51:10 -0700474 unsigned char *head_sha1, unsigned char *sha1)
475{
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700476 if ((!head[0]) ||
David Rientjesa89fccd2006-08-17 11:54:57 -0700477 (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
Junio C Hamanof76412e2005-08-21 02:51:10 -0700478 return 0;
Christian Couder59556542013-11-30 21:55:40 +0100479 if (starts_with(head, "refs/heads/"))
Johannes Schindelinafdcec72006-09-22 00:07:01 +0200480 head += 11;
Christian Couder59556542013-11-30 21:55:40 +0100481 if (starts_with(name, "refs/heads/"))
Johannes Schindelinafdcec72006-09-22 00:07:01 +0200482 name += 11;
Christian Couder59556542013-11-30 21:55:40 +0100483 else if (starts_with(name, "heads/"))
Johannes Schindelinafdcec72006-09-22 00:07:01 +0200484 name += 6;
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700485 return !strcmp(head, name);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700486}
487
488static int show_merge_base(struct commit_list *seen, int num_rev)
489{
490 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
491 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
Junio C Hamano2f0f8b72005-09-08 12:15:52 -0700492 int exit_status = 1;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700493
494 while (seen) {
René Scharfee510ab82015-10-24 18:21:31 +0200495 struct commit *commit = pop_commit(&seen);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700496 int flags = commit->object.flags & all_mask;
497 if (!(flags & UNINTERESTING) &&
498 ((flags & all_revs) == all_revs)) {
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000499 puts(oid_to_hex(&commit->object.oid));
Junio C Hamano2f0f8b72005-09-08 12:15:52 -0700500 exit_status = 0;
501 commit->object.flags |= UNINTERESTING;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700502 }
503 }
Junio C Hamano2f0f8b72005-09-08 12:15:52 -0700504 return exit_status;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700505}
506
Junio C Hamano1f8af482005-09-09 15:40:45 -0700507static int show_independent(struct commit **rev,
508 int num_rev,
509 char **ref_name,
510 unsigned int *rev_mask)
511{
512 int i;
513
514 for (i = 0; i < num_rev; i++) {
515 struct commit *commit = rev[i];
516 unsigned int flag = rev_mask[i];
517
518 if (commit->object.flags == flag)
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000519 puts(oid_to_hex(&commit->object.oid));
Junio C Hamano1f8af482005-09-09 15:40:45 -0700520 commit->object.flags |= UNINTERESTING;
521 }
522 return 0;
523}
524
Junio C Hamano287f8602005-12-04 15:58:50 -0800525static void append_one_rev(const char *av)
526{
Michael Haggerty7a456c12015-05-25 18:38:47 +0000527 struct object_id revkey;
528 if (!get_sha1(av, revkey.hash)) {
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000529 append_ref(av, &revkey, 0);
Junio C Hamano287f8602005-12-04 15:58:50 -0800530 return;
531 }
Junio C Hamano87758f92006-01-11 00:20:25 -0800532 if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) {
Junio C Hamano287f8602005-12-04 15:58:50 -0800533 /* glob style match */
534 int saved_matches = ref_name_cnt;
Michael Haggerty2b2a5be2015-05-25 18:38:28 +0000535
Junio C Hamano287f8602005-12-04 15:58:50 -0800536 match_ref_pattern = av;
537 match_ref_slash = count_slash(av);
Michael Haggertya00595f2015-05-25 18:38:45 +0000538 for_each_ref(append_matching_ref, NULL);
Junio C Hamano287f8602005-12-04 15:58:50 -0800539 if (saved_matches == ref_name_cnt &&
540 ref_name_cnt < MAX_REVS)
Vasco Almeida8a78d462016-09-15 14:59:06 +0000541 error(_("no matching refs with %s"), av);
René Scharfe7e65c752016-10-01 18:19:48 +0200542 sort_ref_range(saved_matches, ref_name_cnt);
Junio C Hamano287f8602005-12-04 15:58:50 -0800543 return;
544 }
545 die("bad sha1 reference %s", av);
546}
547
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100548static int git_show_branch_config(const char *var, const char *value, void *cb)
Junio C Hamanoc2bdd6a2006-01-09 13:29:23 -0800549{
550 if (!strcmp(var, "showbranch.default")) {
Junio C Hamano4f9c4122008-02-11 10:51:03 -0800551 if (!value)
552 return config_error_nonbool(var);
Junio C Hamano3af1cae2009-06-08 23:26:44 -0700553 /*
554 * default_arg is now passed to parse_options(), so we need to
Junio C Hamano9517e6b2010-02-03 21:23:18 -0800555 * mimic the real argv a bit better.
Junio C Hamano3af1cae2009-06-08 23:26:44 -0700556 */
René Scharfec949b002015-10-31 20:06:45 +0100557 if (!default_args.argc)
558 argv_array_push(&default_args, "show-branch");
559 argv_array_push(&default_args, value);
Junio C Hamanoc2bdd6a2006-01-09 13:29:23 -0800560 return 0;
561 }
562
Markus Heidelbergab07ba22009-04-22 23:41:25 +0200563 if (!strcmp(var, "color.showbranch")) {
Jeff Kinge269eb72011-08-17 22:03:48 -0700564 showbranch_use_color = git_config_colorbool(var, value);
Markus Heidelbergab07ba22009-04-22 23:41:25 +0200565 return 0;
566 }
567
568 return git_color_default_config(var, value, cb);
Junio C Hamanoc2bdd6a2006-01-09 13:29:23 -0800569}
570
Junio C Hamanoc24fe422006-05-01 17:12:26 -0700571static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
572{
573 /* If the commit is tip of the named branches, do not
574 * omit it.
575 * Otherwise, if it is a merge that is reachable from only one
576 * tip, it is not that interesting.
577 */
578 int i, flag, count;
579 for (i = 0; i < n; i++)
580 if (rev[i] == commit)
581 return 0;
582 flag = commit->object.flags;
583 for (i = count = 0; i < n; i++) {
584 if (flag & (1u << (i + REV_SHIFT)))
585 count++;
586 }
587 if (count == 1)
588 return 1;
589 return 0;
590}
591
Stephen Boyd57343652009-05-21 00:33:18 -0700592static int reflog = 0;
593
594static int parse_reflog_param(const struct option *opt, const char *arg,
595 int unset)
Junio C Hamano76a44c52007-01-19 01:20:23 -0800596{
597 char *ep;
Stephen Boyd57343652009-05-21 00:33:18 -0700598 const char **base = (const char **)opt->value;
599 if (!arg)
600 arg = "";
601 reflog = strtoul(arg, &ep, 10);
Junio C Hamano76a44c52007-01-19 01:20:23 -0800602 if (*ep == ',')
603 *base = ep + 1;
604 else if (*ep)
Stephen Boyd57343652009-05-21 00:33:18 -0700605 return error("unrecognized reflog param '%s'", arg);
Junio C Hamano76a44c52007-01-19 01:20:23 -0800606 else
607 *base = NULL;
Stephen Boyd57343652009-05-21 00:33:18 -0700608 if (reflog <= 0)
609 reflog = DEFAULT_REFLOG;
610 return 0;
Junio C Hamano76a44c52007-01-19 01:20:23 -0800611}
612
Linus Torvaldsa633fca2006-07-28 22:44:25 -0700613int cmd_show_branch(int ac, const char **av, const char *prefix)
Junio C Hamanof76412e2005-08-21 02:51:10 -0700614{
615 struct commit *rev[MAX_REVS], *commit;
Junio C Hamano76a44c52007-01-19 01:20:23 -0800616 char *reflog_msg[MAX_REVS];
Junio C Hamanof76412e2005-08-21 02:51:10 -0700617 struct commit_list *list = NULL, *seen = NULL;
Junio C Hamano1f8af482005-09-09 15:40:45 -0700618 unsigned int rev_mask[MAX_REVS];
Junio C Hamanof76412e2005-08-21 02:51:10 -0700619 int num_rev, i, extra = 0;
Brian Gernhardtf49006b2006-12-22 08:58:39 -0500620 int all_heads = 0, all_remotes = 0;
Junio C Hamano6b209d42005-11-10 15:47:58 -0800621 int all_mask, all_revs;
Junio C Hamano08f704f2013-06-06 16:07:14 -0700622 enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER;
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700623 char head[128];
624 const char *head_p;
625 int head_len;
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000626 struct object_id head_oid;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700627 int merge_base = 0;
Junio C Hamano1f8af482005-09-09 15:40:45 -0700628 int independent = 0;
Junio C Hamano013f2762005-10-11 15:22:48 -0700629 int no_name = 0;
630 int sha1_name = 0;
Junio C Hamano6b209d42005-11-10 15:47:58 -0800631 int shown_merge_point = 0;
Junio C Hamano1aa68d62006-01-11 00:16:42 -0800632 int with_current_branch = 0;
Junio C Hamanoebedc312006-01-11 14:02:38 -0800633 int head_at = -1;
Junio C Hamanod320a542006-03-02 17:14:00 -0800634 int topics = 0;
Junio C Hamanoc24fe422006-05-01 17:12:26 -0700635 int dense = 1;
Junio C Hamano76a44c52007-01-19 01:20:23 -0800636 const char *reflog_base = NULL;
Stephen Boyd57343652009-05-21 00:33:18 -0700637 struct option builtin_show_branch_options[] = {
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200638 OPT_BOOL('a', "all", &all_heads,
639 N_("show remote-tracking and local branches")),
640 OPT_BOOL('r', "remotes", &all_remotes,
641 N_("show remote-tracking branches")),
Mark Lodato73e9da02010-02-16 23:55:58 -0500642 OPT__COLOR(&showbranch_use_color,
Nguyễn Thái Ngọc Duyd780bef2012-08-20 19:32:44 +0700643 N_("color '*!+-' corresponding to the branch")),
644 { OPTION_INTEGER, 0, "more", &extra, N_("n"),
645 N_("show <n> more commits after the common ancestor"),
Stephen Boyde169b972009-06-07 16:39:15 -0700646 PARSE_OPT_OPTARG, NULL, (intptr_t)1 },
Nguyễn Thái Ngọc Duyd780bef2012-08-20 19:32:44 +0700647 OPT_SET_INT(0, "list", &extra, N_("synonym to more=-1"), -1),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200648 OPT_BOOL(0, "no-name", &no_name, N_("suppress naming strings")),
649 OPT_BOOL(0, "current", &with_current_branch,
650 N_("include the current branch")),
651 OPT_BOOL(0, "sha1-name", &sha1_name,
652 N_("name commits with their object names")),
653 OPT_BOOL(0, "merge-base", &merge_base,
654 N_("show possible merge bases")),
655 OPT_BOOL(0, "independent", &independent,
Nguyễn Thái Ngọc Duyd780bef2012-08-20 19:32:44 +0700656 N_("show refs unreachable from any other ref")),
Junio C Hamano08f704f2013-06-06 16:07:14 -0700657 OPT_SET_INT(0, "topo-order", &sort_order,
658 N_("show commits in topological order"),
659 REV_SORT_IN_GRAPH_ORDER),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200660 OPT_BOOL(0, "topics", &topics,
661 N_("show only commits not on the first branch")),
Stephen Boyd57343652009-05-21 00:33:18 -0700662 OPT_SET_INT(0, "sparse", &dense,
Nguyễn Thái Ngọc Duyd780bef2012-08-20 19:32:44 +0700663 N_("show merges reachable from only one tip"), 0),
Junio C Hamano08f704f2013-06-06 16:07:14 -0700664 OPT_SET_INT(0, "date-order", &sort_order,
Thomas Rast465cf8c2013-07-18 14:26:56 +0200665 N_("topologically sort, maintaining date order "
Junio C Hamano4ca8ae72013-07-22 11:23:30 -0700666 "where possible"),
Junio C Hamano08f704f2013-06-06 16:07:14 -0700667 REV_SORT_BY_COMMIT_DATE),
Nguyễn Thái Ngọc Duyd780bef2012-08-20 19:32:44 +0700668 { OPTION_CALLBACK, 'g', "reflog", &reflog_base, N_("<n>[,<base>]"),
669 N_("show <n> most recent ref-log entries starting at "
670 "base"),
Stephen Boyd57343652009-05-21 00:33:18 -0700671 PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP,
672 parse_reflog_param },
673 OPT_END()
674 };
Junio C Hamanof76412e2005-08-21 02:51:10 -0700675
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100676 git_config(git_show_branch_config, NULL);
Junio C Hamano076b2322005-08-30 16:59:37 -0700677
Junio C Hamanoc2bdd6a2006-01-09 13:29:23 -0800678 /* If nothing is specified, try the default first */
René Scharfec949b002015-10-31 20:06:45 +0100679 if (ac == 1 && default_args.argc) {
680 ac = default_args.argc;
681 av = default_args.argv;
Junio C Hamanoc2bdd6a2006-01-09 13:29:23 -0800682 }
683
Stephen Boyd37782922009-05-23 11:53:12 -0700684 ac = parse_options(ac, av, prefix, builtin_show_branch_options,
Stephen Boyd57343652009-05-21 00:33:18 -0700685 show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION);
686 if (all_heads)
687 all_remotes = 1;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700688
Junio C Hamanodf373ea2007-01-25 22:14:45 -0800689 if (extra || reflog) {
Junio C Hamano76a44c52007-01-19 01:20:23 -0800690 /* "listing" mode is incompatible with
691 * independent nor merge-base modes.
692 */
693 if (independent || merge_base)
Stephen Boyd57343652009-05-21 00:33:18 -0700694 usage_with_options(show_branch_usage,
695 builtin_show_branch_options);
Junio C Hamanodf373ea2007-01-25 22:14:45 -0800696 if (reflog && ((0 < extra) || all_heads || all_remotes))
Junio C Hamano76a44c52007-01-19 01:20:23 -0800697 /*
698 * Asking for --more in reflog mode does not
Junio C Hamanob15af072007-01-19 22:51:49 -0800699 * make sense. --list is Ok.
700 *
701 * Also --all and --remotes do not make sense either.
Junio C Hamano76a44c52007-01-19 01:20:23 -0800702 */
Vasco Almeida8a78d462016-09-15 14:59:06 +0000703 die(_("--reflog is incompatible with --all, --remotes, "
704 "--independent or --merge-base"));
Junio C Hamano76a44c52007-01-19 01:20:23 -0800705 }
Junio C Hamano1f8af482005-09-09 15:40:45 -0700706
Junio C Hamanobb5ebed2005-12-23 12:47:18 -0800707 /* If nothing is specified, show all branches by default */
Mike Hommey539d09c2015-03-31 07:12:23 +0900708 if (ac <= topics && all_heads + all_remotes == 0)
Junio C Hamanobb5ebed2005-12-23 12:47:18 -0800709 all_heads = 1;
710
Junio C Hamano7e3fe902006-12-14 15:58:56 -0800711 if (reflog) {
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000712 struct object_id oid;
Junio C Hamano76a44c52007-01-19 01:20:23 -0800713 char *ref;
714 int base = 0;
David Aguilarc41a87d2014-09-18 20:45:37 -0700715 unsigned int flags = 0;
Junio C Hamanodf373ea2007-01-25 22:14:45 -0800716
717 if (ac == 0) {
718 static const char *fake_av[2];
Junio C Hamano632ac9f2007-02-03 23:31:47 -0800719
Ronnie Sahlberg7695d112014-07-15 12:59:36 -0700720 fake_av[0] = resolve_refdup("HEAD",
721 RESOLVE_REF_READING,
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000722 oid.hash, NULL);
Junio C Hamanodf373ea2007-01-25 22:14:45 -0800723 fake_av[1] = NULL;
724 av = fake_av;
725 ac = 1;
Jeff King7cd17e82015-09-24 17:02:54 -0400726 if (!*av)
Vasco Almeida8a78d462016-09-15 14:59:06 +0000727 die(_("no branches given, and HEAD is not valid"));
Junio C Hamanodf373ea2007-01-25 22:14:45 -0800728 }
Junio C Hamano76a44c52007-01-19 01:20:23 -0800729 if (ac != 1)
Vasco Almeida8a78d462016-09-15 14:59:06 +0000730 die(_("--reflog option needs one branch name"));
Junio C Hamanodf373ea2007-01-25 22:14:45 -0800731
Junio C Hamanob15af072007-01-19 22:51:49 -0800732 if (MAX_REVS < reflog)
Vasco Almeida205d1342016-09-15 14:59:07 +0000733 die(Q_("only %d entry can be shown at one time.",
734 "only %d entries can be shown at one time.",
735 MAX_REVS), MAX_REVS);
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000736 if (!dwim_ref(*av, strlen(*av), oid.hash, &ref))
Vasco Almeida8a78d462016-09-15 14:59:06 +0000737 die(_("no such ref %s"), *av);
Junio C Hamano76a44c52007-01-19 01:20:23 -0800738
739 /* Has the base been specified? */
740 if (reflog_base) {
741 char *ep;
742 base = strtoul(reflog_base, &ep, 10);
743 if (*ep) {
744 /* Ah, that is a date spec... */
745 unsigned long at;
746 at = approxidate(reflog_base);
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000747 read_ref_at(ref, flags, at, -1, oid.hash, NULL,
Junio C Hamano76a44c52007-01-19 01:20:23 -0800748 NULL, NULL, &base);
749 }
750 }
751
Junio C Hamano7e3fe902006-12-14 15:58:56 -0800752 for (i = 0; i < reflog; i++) {
Jeff King28310182014-06-19 17:24:33 -0400753 char *logmsg;
Jeff King78f23bd2015-08-19 14:12:48 -0400754 char *nth_desc;
Shawn O. Pearce3a556022007-03-06 20:44:17 -0500755 const char *msg;
Junio C Hamano76a44c52007-01-19 01:20:23 -0800756 unsigned long timestamp;
757 int tz;
758
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000759 if (read_ref_at(ref, flags, 0, base+i, oid.hash, &logmsg,
Junio C Hamano76a44c52007-01-19 01:20:23 -0800760 &timestamp, &tz, NULL)) {
761 reflog = i;
762 break;
763 }
764 msg = strchr(logmsg, '\t');
765 if (!msg)
766 msg = "(none)";
767 else
768 msg++;
Jeff King28310182014-06-19 17:24:33 -0400769 reflog_msg[i] = xstrfmt("(%s) %s",
Jeff Kinga5481a62015-06-25 12:55:02 -0400770 show_date(timestamp, tz,
771 DATE_MODE(RELATIVE)),
Jeff King28310182014-06-19 17:24:33 -0400772 msg);
Junio C Hamano76a44c52007-01-19 01:20:23 -0800773 free(logmsg);
Jeff King78f23bd2015-08-19 14:12:48 -0400774
775 nth_desc = xstrfmt("%s@{%d}", *av, base+i);
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000776 append_ref(nth_desc, &oid, 1);
Jeff King78f23bd2015-08-19 14:12:48 -0400777 free(nth_desc);
Junio C Hamano7e3fe902006-12-14 15:58:56 -0800778 }
Jeff King28b35632014-07-24 00:41:11 -0400779 free(ref);
Junio C Hamano7e3fe902006-12-14 15:58:56 -0800780 }
781 else {
782 while (0 < ac) {
783 append_one_rev(*av);
784 ac--; av++;
785 }
Mike Hommey539d09c2015-03-31 07:12:23 +0900786 if (all_heads + all_remotes)
787 snarf_refs(all_heads, all_remotes);
Junio C Hamanobb5ebed2005-12-23 12:47:18 -0800788 }
Junio C Hamanof76412e2005-08-21 02:51:10 -0700789
Ronnie Sahlberg7695d112014-07-15 12:59:36 -0700790 head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING,
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000791 head_oid.hash, NULL);
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700792 if (head_p) {
793 head_len = strlen(head_p);
794 memcpy(head, head_p, head_len + 1);
Junio C Hamano1aa68d62006-01-11 00:16:42 -0800795 }
796 else {
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700797 head_len = 0;
798 head[0] = 0;
Junio C Hamano1aa68d62006-01-11 00:16:42 -0800799 }
800
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700801 if (with_current_branch && head_p) {
Junio C Hamano1aa68d62006-01-11 00:16:42 -0800802 int has_head = 0;
803 for (i = 0; !has_head && i < ref_name_cnt; i++) {
804 /* We are only interested in adding the branch
805 * HEAD points at.
806 */
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700807 if (rev_is_head(head,
808 head_len,
Junio C Hamano1aa68d62006-01-11 00:16:42 -0800809 ref_name[i],
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000810 head_oid.hash, NULL))
Junio C Hamano1aa68d62006-01-11 00:16:42 -0800811 has_head++;
812 }
813 if (!has_head) {
Christian Couder59556542013-11-30 21:55:40 +0100814 int offset = starts_with(head, "refs/heads/") ? 11 : 0;
Junio C Hamanof8fcb572008-05-26 15:09:51 -0700815 append_one_rev(head + offset);
Junio C Hamano1aa68d62006-01-11 00:16:42 -0800816 }
817 }
818
Junio C Hamano287f8602005-12-04 15:58:50 -0800819 if (!ref_name_cnt) {
820 fprintf(stderr, "No revs to be shown.\n");
821 exit(0);
822 }
Junio C Hamanof76412e2005-08-21 02:51:10 -0700823
824 for (num_rev = 0; ref_name[num_rev]; num_rev++) {
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000825 struct object_id revkey;
Junio C Hamano1f8af482005-09-09 15:40:45 -0700826 unsigned int flag = 1u << (num_rev + REV_SHIFT);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700827
828 if (MAX_REVS <= num_rev)
Vasco Almeida205d1342016-09-15 14:59:07 +0000829 die(Q_("cannot handle more than %d rev.",
830 "cannot handle more than %d revs.",
831 MAX_REVS), MAX_REVS);
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000832 if (get_sha1(ref_name[num_rev], revkey.hash))
Vasco Almeida8a78d462016-09-15 14:59:06 +0000833 die(_("'%s' is not a valid ref."), ref_name[num_rev]);
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000834 commit = lookup_commit_reference(revkey.hash);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700835 if (!commit)
Vasco Almeida8a78d462016-09-15 14:59:06 +0000836 die(_("cannot find commit %s (%s)"),
Michael Haggerty96062b52015-05-25 18:38:49 +0000837 ref_name[num_rev], oid_to_hex(&revkey));
Junio C Hamanof76412e2005-08-21 02:51:10 -0700838 parse_commit(commit);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700839 mark_seen(commit, &seen);
840
841 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
842 * and so on. REV_SHIFT bits from bit 0 are used for
843 * internal bookkeeping.
844 */
Junio C Hamano1f8af482005-09-09 15:40:45 -0700845 commit->object.flags |= flag;
846 if (commit->object.flags == flag)
Thiago Farina47e44ed2010-11-26 23:58:14 -0200847 commit_list_insert_by_date(commit, &list);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700848 rev[num_rev] = commit;
849 }
Junio C Hamano1f8af482005-09-09 15:40:45 -0700850 for (i = 0; i < num_rev; i++)
851 rev_mask[i] = rev[i]->object.flags;
852
853 if (0 <= extra)
854 join_revs(&list, &seen, num_rev, extra);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700855
Thiago Farina47e44ed2010-11-26 23:58:14 -0200856 commit_list_sort_by_date(&seen);
Junio C Hamano26a8ad22006-07-16 00:00:09 -0700857
Junio C Hamanof76412e2005-08-21 02:51:10 -0700858 if (merge_base)
859 return show_merge_base(seen, num_rev);
860
Junio C Hamano1f8af482005-09-09 15:40:45 -0700861 if (independent)
862 return show_independent(rev, num_rev, ref_name, rev_mask);
863
864 /* Show list; --more=-1 means list-only */
Junio C Hamanoc9d023b2005-09-10 18:24:46 -0700865 if (1 < num_rev || extra < 0) {
Junio C Hamanof76412e2005-08-21 02:51:10 -0700866 for (i = 0; i < num_rev; i++) {
867 int j;
Linus Torvaldsed378ec2006-09-11 20:17:35 -0700868 int is_head = rev_is_head(head,
869 head_len,
Junio C Hamanof76412e2005-08-21 02:51:10 -0700870 ref_name[i],
Michael Haggertyd1516bf2015-05-25 18:38:48 +0000871 head_oid.hash,
brian m. carlsoned1c9972015-11-10 02:22:29 +0000872 rev[i]->object.oid.hash);
Junio C Hamano1f8af482005-09-09 15:40:45 -0700873 if (extra < 0)
874 printf("%c [%s] ",
875 is_head ? '*' : ' ', ref_name[i]);
876 else {
877 for (j = 0; j < i; j++)
878 putchar(' ');
Markus Heidelbergab07ba22009-04-22 23:41:25 +0200879 printf("%s%c%s [%s] ",
Dan McGee7cd52b52011-04-05 00:40:23 -0500880 get_color_code(i),
Markus Heidelbergab07ba22009-04-22 23:41:25 +0200881 is_head ? '*' : '!',
882 get_color_reset_code(), ref_name[i]);
Junio C Hamano1f8af482005-09-09 15:40:45 -0700883 }
Junio C Hamano76a44c52007-01-19 01:20:23 -0800884
885 if (!reflog) {
886 /* header lines never need name */
887 show_one_commit(rev[i], 1);
888 }
889 else
890 puts(reflog_msg[i]);
891
Junio C Hamanoebedc312006-01-11 14:02:38 -0800892 if (is_head)
893 head_at = i;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700894 }
Junio C Hamano1f8af482005-09-09 15:40:45 -0700895 if (0 <= extra) {
896 for (i = 0; i < num_rev; i++)
897 putchar('-');
898 putchar('\n');
899 }
Junio C Hamanof5e375c2005-08-22 23:16:46 -0700900 }
Junio C Hamano1f8af482005-09-09 15:40:45 -0700901 if (extra < 0)
902 exit(0);
Junio C Hamanof5e375c2005-08-22 23:16:46 -0700903
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700904 /* Sort topologically */
Junio C Hamano08f704f2013-06-06 16:07:14 -0700905 sort_in_topological_order(&seen, sort_order);
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700906
907 /* Give names to commits */
Junio C Hamano013f2762005-10-11 15:22:48 -0700908 if (!sha1_name && !no_name)
909 name_commits(seen, rev, ref_name, num_rev);
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700910
911 all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
912 all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700913
Junio C Hamanof76412e2005-08-21 02:51:10 -0700914 while (seen) {
René Scharfee510ab82015-10-24 18:21:31 +0200915 struct commit *commit = pop_commit(&seen);
Junio C Hamanof76412e2005-08-21 02:51:10 -0700916 int this_flag = commit->object.flags;
Junio C Hamanof794c232006-03-03 14:34:40 -0800917 int is_merge_point = ((this_flag & all_revs) == all_revs);
Junio C Hamanof5e375c2005-08-22 23:16:46 -0700918
Junio C Hamanof794c232006-03-03 14:34:40 -0800919 shown_merge_point |= is_merge_point;
Junio C Hamano8e5dd222005-08-29 17:19:47 -0700920
Junio C Hamanof5e375c2005-08-22 23:16:46 -0700921 if (1 < num_rev) {
Junio C Hamanoc24fe422006-05-01 17:12:26 -0700922 int is_merge = !!(commit->parents &&
923 commit->parents->next);
Junio C Hamanof794c232006-03-03 14:34:40 -0800924 if (topics &&
925 !is_merge_point &&
926 (this_flag & (1u << REV_SHIFT)))
927 continue;
Junio C Hamanoc24fe422006-05-01 17:12:26 -0700928 if (dense && is_merge &&
929 omit_in_dense(commit, rev, num_rev))
930 continue;
Junio C Hamanoebedc312006-01-11 14:02:38 -0800931 for (i = 0; i < num_rev; i++) {
932 int mark;
933 if (!(this_flag & (1u << (i + REV_SHIFT))))
934 mark = ' ';
935 else if (is_merge)
936 mark = '-';
937 else if (i == head_at)
938 mark = '*';
939 else
940 mark = '+';
Markus Heidelbergab07ba22009-04-22 23:41:25 +0200941 printf("%s%c%s",
Dan McGee7cd52b52011-04-05 00:40:23 -0500942 get_color_code(i),
Markus Heidelbergab07ba22009-04-22 23:41:25 +0200943 mark, get_color_reset_code());
Junio C Hamanoebedc312006-01-11 14:02:38 -0800944 }
Junio C Hamanof5e375c2005-08-22 23:16:46 -0700945 putchar(' ');
946 }
Junio C Hamano013f2762005-10-11 15:22:48 -0700947 show_one_commit(commit, no_name);
Junio C Hamano6b209d42005-11-10 15:47:58 -0800948
949 if (shown_merge_point && --extra < 0)
950 break;
Junio C Hamanof76412e2005-08-21 02:51:10 -0700951 }
952 return 0;
953}