show-branch.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. #include "cache.h"
  2. #include "config.h"
  3. #include "pretty.h"
  4. #include "refs.h"
  5. #include "builtin.h"
  6. #include "color.h"
  7. #include "strvec.h"
  8. #include "parse-options.h"
  9. #include "dir.h"
  10. #include "commit-slab.h"
  11. static const char* show_branch_usage[] = {
  12. N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
  13. " [--current] [--color[=<when>] | --no-color] [--sparse]\n"
  14. " [--more=<n> | --list | --independent | --merge-base]\n"
  15. " [--no-name | --sha1-name] [--topics] [(<rev> | <glob>)...]"),
  16. N_("git show-branch (-g | --reflog)[=<n>[,<base>]] [--list] [<ref>]"),
  17. NULL
  18. };
  19. static int showbranch_use_color = -1;
  20. static struct strvec default_args = STRVEC_INIT;
  21. /*
  22. * TODO: convert this use of commit->object.flags to commit-slab
  23. * instead to store a pointer to ref name directly. Then use the same
  24. * UNINTERESTING definition from revision.h here.
  25. */
  26. #define UNINTERESTING 01
  27. #define REV_SHIFT 2
  28. #define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */
  29. #define DEFAULT_REFLOG 4
  30. static const char *get_color_code(int idx)
  31. {
  32. if (want_color(showbranch_use_color))
  33. return column_colors_ansi[idx % column_colors_ansi_max];
  34. return "";
  35. }
  36. static const char *get_color_reset_code(void)
  37. {
  38. if (want_color(showbranch_use_color))
  39. return GIT_COLOR_RESET;
  40. return "";
  41. }
  42. static struct commit *interesting(struct commit_list *list)
  43. {
  44. while (list) {
  45. struct commit *commit = list->item;
  46. list = list->next;
  47. if (commit->object.flags & UNINTERESTING)
  48. continue;
  49. return commit;
  50. }
  51. return NULL;
  52. }
  53. struct commit_name {
  54. const char *head_name; /* which head's ancestor? */
  55. int generation; /* how many parents away from head_name */
  56. };
  57. define_commit_slab(commit_name_slab, struct commit_name *);
  58. static struct commit_name_slab name_slab;
  59. static struct commit_name *commit_to_name(struct commit *commit)
  60. {
  61. return *commit_name_slab_at(&name_slab, commit);
  62. }
  63. /* Name the commit as nth generation ancestor of head_name;
  64. * we count only the first-parent relationship for naming purposes.
  65. */
  66. static void name_commit(struct commit *commit, const char *head_name, int nth)
  67. {
  68. struct commit_name *name;
  69. name = *commit_name_slab_at(&name_slab, commit);
  70. if (!name) {
  71. name = xmalloc(sizeof(*name));
  72. *commit_name_slab_at(&name_slab, commit) = name;
  73. }
  74. name->head_name = head_name;
  75. name->generation = nth;
  76. }
  77. /* Parent is the first parent of the commit. We may name it
  78. * as (n+1)th generation ancestor of the same head_name as
  79. * commit is nth generation ancestor of, if that generation
  80. * number is better than the name it already has.
  81. */
  82. static void name_parent(struct commit *commit, struct commit *parent)
  83. {
  84. struct commit_name *commit_name = commit_to_name(commit);
  85. struct commit_name *parent_name = commit_to_name(parent);
  86. if (!commit_name)
  87. return;
  88. if (!parent_name ||
  89. commit_name->generation + 1 < parent_name->generation)
  90. name_commit(parent, commit_name->head_name,
  91. commit_name->generation + 1);
  92. }
  93. static int name_first_parent_chain(struct commit *c)
  94. {
  95. int i = 0;
  96. while (c) {
  97. struct commit *p;
  98. if (!commit_to_name(c))
  99. break;
  100. if (!c->parents)
  101. break;
  102. p = c->parents->item;
  103. if (!commit_to_name(p)) {
  104. name_parent(c, p);
  105. i++;
  106. }
  107. else
  108. break;
  109. c = p;
  110. }
  111. return i;
  112. }
  113. static void name_commits(struct commit_list *list,
  114. struct commit **rev,
  115. char **ref_name,
  116. int num_rev)
  117. {
  118. struct commit_list *cl;
  119. struct commit *c;
  120. int i;
  121. /* First give names to the given heads */
  122. for (cl = list; cl; cl = cl->next) {
  123. c = cl->item;
  124. if (commit_to_name(c))
  125. continue;
  126. for (i = 0; i < num_rev; i++) {
  127. if (rev[i] == c) {
  128. name_commit(c, ref_name[i], 0);
  129. break;
  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. /* Finally, any unnamed commits */
  141. do {
  142. i = 0;
  143. for (cl = list; cl; cl = cl->next) {
  144. struct commit_list *parents;
  145. struct commit_name *n;
  146. int nth;
  147. c = cl->item;
  148. if (!commit_to_name(c))
  149. continue;
  150. n = commit_to_name(c);
  151. parents = c->parents;
  152. nth = 0;
  153. while (parents) {
  154. struct commit *p = parents->item;
  155. struct strbuf newname = STRBUF_INIT;
  156. parents = parents->next;
  157. nth++;
  158. if (commit_to_name(p))
  159. continue;
  160. switch (n->generation) {
  161. case 0:
  162. strbuf_addstr(&newname, n->head_name);
  163. break;
  164. case 1:
  165. strbuf_addf(&newname, "%s^", n->head_name);
  166. break;
  167. default:
  168. strbuf_addf(&newname, "%s~%d",
  169. n->head_name, n->generation);
  170. break;
  171. }
  172. if (nth == 1)
  173. strbuf_addch(&newname, '^');
  174. else
  175. strbuf_addf(&newname, "^%d", nth);
  176. name_commit(p, strbuf_detach(&newname, NULL), 0);
  177. i++;
  178. name_first_parent_chain(p);
  179. }
  180. }
  181. } while (i);
  182. }
  183. static int mark_seen(struct commit *commit, struct commit_list **seen_p)
  184. {
  185. if (!commit->object.flags) {
  186. commit_list_insert(commit, seen_p);
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. static void join_revs(struct commit_list **list_p,
  192. struct commit_list **seen_p,
  193. int num_rev, int extra)
  194. {
  195. int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
  196. int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
  197. while (*list_p) {
  198. struct commit_list *parents;
  199. int still_interesting = !!interesting(*list_p);
  200. struct commit *commit = pop_commit(list_p);
  201. int flags = commit->object.flags & all_mask;
  202. if (!still_interesting && extra <= 0)
  203. break;
  204. mark_seen(commit, seen_p);
  205. if ((flags & all_revs) == all_revs)
  206. flags |= UNINTERESTING;
  207. parents = commit->parents;
  208. while (parents) {
  209. struct commit *p = parents->item;
  210. int this_flag = p->object.flags;
  211. parents = parents->next;
  212. if ((this_flag & flags) == flags)
  213. continue;
  214. parse_commit(p);
  215. if (mark_seen(p, seen_p) && !still_interesting)
  216. extra--;
  217. p->object.flags |= flags;
  218. commit_list_insert_by_date(p, list_p);
  219. }
  220. }
  221. /*
  222. * Postprocess to complete well-poisoning.
  223. *
  224. * At this point we have all the commits we have seen in
  225. * seen_p list. Mark anything that can be reached from
  226. * uninteresting commits not interesting.
  227. */
  228. for (;;) {
  229. int changed = 0;
  230. struct commit_list *s;
  231. for (s = *seen_p; s; s = s->next) {
  232. struct commit *c = s->item;
  233. struct commit_list *parents;
  234. if (((c->object.flags & all_revs) != all_revs) &&
  235. !(c->object.flags & UNINTERESTING))
  236. continue;
  237. /* The current commit is either a merge base or
  238. * already uninteresting one. Mark its parents
  239. * as uninteresting commits _only_ if they are
  240. * already parsed. No reason to find new ones
  241. * here.
  242. */
  243. parents = c->parents;
  244. while (parents) {
  245. struct commit *p = parents->item;
  246. parents = parents->next;
  247. if (!(p->object.flags & UNINTERESTING)) {
  248. p->object.flags |= UNINTERESTING;
  249. changed = 1;
  250. }
  251. }
  252. }
  253. if (!changed)
  254. break;
  255. }
  256. }
  257. static void show_one_commit(struct commit *commit, int no_name)
  258. {
  259. struct strbuf pretty = STRBUF_INIT;
  260. const char *pretty_str = "(unavailable)";
  261. struct commit_name *name = commit_to_name(commit);
  262. if (commit->object.parsed) {
  263. pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty);
  264. pretty_str = pretty.buf;
  265. }
  266. skip_prefix(pretty_str, "[PATCH] ", &pretty_str);
  267. if (!no_name) {
  268. if (name && name->head_name) {
  269. printf("[%s", name->head_name);
  270. if (name->generation) {
  271. if (name->generation == 1)
  272. printf("^");
  273. else
  274. printf("~%d", name->generation);
  275. }
  276. printf("] ");
  277. }
  278. else
  279. printf("[%s] ",
  280. find_unique_abbrev(&commit->object.oid,
  281. DEFAULT_ABBREV));
  282. }
  283. puts(pretty_str);
  284. strbuf_release(&pretty);
  285. }
  286. static char *ref_name[MAX_REVS + 1];
  287. static int ref_name_cnt;
  288. static const char *find_digit_prefix(const char *s, int *v)
  289. {
  290. const char *p;
  291. int ver;
  292. char ch;
  293. for (p = s, ver = 0;
  294. '0' <= (ch = *p) && ch <= '9';
  295. p++)
  296. ver = ver * 10 + ch - '0';
  297. *v = ver;
  298. return p;
  299. }
  300. static int version_cmp(const char *a, const char *b)
  301. {
  302. while (1) {
  303. int va, vb;
  304. a = find_digit_prefix(a, &va);
  305. b = find_digit_prefix(b, &vb);
  306. if (va != vb)
  307. return va - vb;
  308. while (1) {
  309. int ca = *a;
  310. int cb = *b;
  311. if ('0' <= ca && ca <= '9')
  312. ca = 0;
  313. if ('0' <= cb && cb <= '9')
  314. cb = 0;
  315. if (ca != cb)
  316. return ca - cb;
  317. if (!ca)
  318. break;
  319. a++;
  320. b++;
  321. }
  322. if (!*a && !*b)
  323. return 0;
  324. }
  325. }
  326. static int compare_ref_name(const void *a_, const void *b_)
  327. {
  328. const char * const*a = a_, * const*b = b_;
  329. return version_cmp(*a, *b);
  330. }
  331. static void sort_ref_range(int bottom, int top)
  332. {
  333. QSORT(ref_name + bottom, top - bottom, compare_ref_name);
  334. }
  335. static int append_ref(const char *refname, const struct object_id *oid,
  336. int allow_dups)
  337. {
  338. struct commit *commit = lookup_commit_reference_gently(the_repository,
  339. oid, 1);
  340. int i;
  341. if (!commit)
  342. return 0;
  343. if (!allow_dups) {
  344. /* Avoid adding the same thing twice */
  345. for (i = 0; i < ref_name_cnt; i++)
  346. if (!strcmp(refname, ref_name[i]))
  347. return 0;
  348. }
  349. if (MAX_REVS <= ref_name_cnt) {
  350. warning(Q_("ignoring %s; cannot handle more than %d ref",
  351. "ignoring %s; cannot handle more than %d refs",
  352. MAX_REVS), refname, MAX_REVS);
  353. return 0;
  354. }
  355. ref_name[ref_name_cnt++] = xstrdup(refname);
  356. ref_name[ref_name_cnt] = NULL;
  357. return 0;
  358. }
  359. static int append_head_ref(const char *refname, const struct object_id *oid,
  360. int flag, void *cb_data)
  361. {
  362. struct object_id tmp;
  363. int ofs = 11;
  364. if (!starts_with(refname, "refs/heads/"))
  365. return 0;
  366. /* If both heads/foo and tags/foo exists, get_sha1 would
  367. * get confused.
  368. */
  369. if (get_oid(refname + ofs, &tmp) || !oideq(&tmp, oid))
  370. ofs = 5;
  371. return append_ref(refname + ofs, oid, 0);
  372. }
  373. static int append_remote_ref(const char *refname, const struct object_id *oid,
  374. int flag, void *cb_data)
  375. {
  376. struct object_id tmp;
  377. int ofs = 13;
  378. if (!starts_with(refname, "refs/remotes/"))
  379. return 0;
  380. /* If both heads/foo and tags/foo exists, get_sha1 would
  381. * get confused.
  382. */
  383. if (get_oid(refname + ofs, &tmp) || !oideq(&tmp, oid))
  384. ofs = 5;
  385. return append_ref(refname + ofs, oid, 0);
  386. }
  387. static int append_tag_ref(const char *refname, const struct object_id *oid,
  388. int flag, void *cb_data)
  389. {
  390. if (!starts_with(refname, "refs/tags/"))
  391. return 0;
  392. return append_ref(refname + 5, oid, 0);
  393. }
  394. static const char *match_ref_pattern = NULL;
  395. static int match_ref_slash = 0;
  396. static int append_matching_ref(const char *refname, const struct object_id *oid,
  397. int flag, void *cb_data)
  398. {
  399. /* we want to allow pattern hold/<asterisk> to show all
  400. * branches under refs/heads/hold/, and v0.99.9? to show
  401. * refs/tags/v0.99.9a and friends.
  402. */
  403. const char *tail;
  404. int slash = count_slashes(refname);
  405. for (tail = refname; *tail && match_ref_slash < slash; )
  406. if (*tail++ == '/')
  407. slash--;
  408. if (!*tail)
  409. return 0;
  410. if (wildmatch(match_ref_pattern, tail, 0))
  411. return 0;
  412. if (starts_with(refname, "refs/heads/"))
  413. return append_head_ref(refname, oid, flag, cb_data);
  414. if (starts_with(refname, "refs/tags/"))
  415. return append_tag_ref(refname, oid, flag, cb_data);
  416. return append_ref(refname, oid, 0);
  417. }
  418. static void snarf_refs(int head, int remotes)
  419. {
  420. if (head) {
  421. int orig_cnt = ref_name_cnt;
  422. for_each_ref(append_head_ref, NULL);
  423. sort_ref_range(orig_cnt, ref_name_cnt);
  424. }
  425. if (remotes) {
  426. int orig_cnt = ref_name_cnt;
  427. for_each_ref(append_remote_ref, NULL);
  428. sort_ref_range(orig_cnt, ref_name_cnt);
  429. }
  430. }
  431. static int rev_is_head(const char *head, const char *name,
  432. unsigned char *head_sha1, unsigned char *sha1)
  433. {
  434. if (!head || (head_sha1 && sha1 && !hasheq(head_sha1, sha1)))
  435. return 0;
  436. skip_prefix(head, "refs/heads/", &head);
  437. if (!skip_prefix(name, "refs/heads/", &name))
  438. skip_prefix(name, "heads/", &name);
  439. return !strcmp(head, name);
  440. }
  441. static int show_merge_base(struct commit_list *seen, int num_rev)
  442. {
  443. int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
  444. int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
  445. int exit_status = 1;
  446. while (seen) {
  447. struct commit *commit = pop_commit(&seen);
  448. int flags = commit->object.flags & all_mask;
  449. if (!(flags & UNINTERESTING) &&
  450. ((flags & all_revs) == all_revs)) {
  451. puts(oid_to_hex(&commit->object.oid));
  452. exit_status = 0;
  453. commit->object.flags |= UNINTERESTING;
  454. }
  455. }
  456. return exit_status;
  457. }
  458. static int show_independent(struct commit **rev,
  459. int num_rev,
  460. unsigned int *rev_mask)
  461. {
  462. int i;
  463. for (i = 0; i < num_rev; i++) {
  464. struct commit *commit = rev[i];
  465. unsigned int flag = rev_mask[i];
  466. if (commit->object.flags == flag)
  467. puts(oid_to_hex(&commit->object.oid));
  468. commit->object.flags |= UNINTERESTING;
  469. }
  470. return 0;
  471. }
  472. static void append_one_rev(const char *av)
  473. {
  474. struct object_id revkey;
  475. if (!get_oid(av, &revkey)) {
  476. append_ref(av, &revkey, 0);
  477. return;
  478. }
  479. if (strpbrk(av, "*?[")) {
  480. /* glob style match */
  481. int saved_matches = ref_name_cnt;
  482. match_ref_pattern = av;
  483. match_ref_slash = count_slashes(av);
  484. for_each_ref(append_matching_ref, NULL);
  485. if (saved_matches == ref_name_cnt &&
  486. ref_name_cnt < MAX_REVS)
  487. error(_("no matching refs with %s"), av);
  488. sort_ref_range(saved_matches, ref_name_cnt);
  489. return;
  490. }
  491. die("bad sha1 reference %s", av);
  492. }
  493. static int git_show_branch_config(const char *var, const char *value, void *cb)
  494. {
  495. if (!strcmp(var, "showbranch.default")) {
  496. if (!value)
  497. return config_error_nonbool(var);
  498. /*
  499. * default_arg is now passed to parse_options(), so we need to
  500. * mimic the real argv a bit better.
  501. */
  502. if (!default_args.nr)
  503. strvec_push(&default_args, "show-branch");
  504. strvec_push(&default_args, value);
  505. return 0;
  506. }
  507. if (!strcmp(var, "color.showbranch")) {
  508. showbranch_use_color = git_config_colorbool(var, value);
  509. return 0;
  510. }
  511. return git_color_default_config(var, value, cb);
  512. }
  513. static int omit_in_dense(struct commit *commit, struct commit **rev, int n)
  514. {
  515. /* If the commit is tip of the named branches, do not
  516. * omit it.
  517. * Otherwise, if it is a merge that is reachable from only one
  518. * tip, it is not that interesting.
  519. */
  520. int i, flag, count;
  521. for (i = 0; i < n; i++)
  522. if (rev[i] == commit)
  523. return 0;
  524. flag = commit->object.flags;
  525. for (i = count = 0; i < n; i++) {
  526. if (flag & (1u << (i + REV_SHIFT)))
  527. count++;
  528. }
  529. if (count == 1)
  530. return 1;
  531. return 0;
  532. }
  533. static int reflog = 0;
  534. static int parse_reflog_param(const struct option *opt, const char *arg,
  535. int unset)
  536. {
  537. char *ep;
  538. const char **base = (const char **)opt->value;
  539. BUG_ON_OPT_NEG(unset);
  540. if (!arg)
  541. arg = "";
  542. reflog = strtoul(arg, &ep, 10);
  543. if (*ep == ',')
  544. *base = ep + 1;
  545. else if (*ep)
  546. return error("unrecognized reflog param '%s'", arg);
  547. else
  548. *base = NULL;
  549. if (reflog <= 0)
  550. reflog = DEFAULT_REFLOG;
  551. return 0;
  552. }
  553. int cmd_show_branch(int ac, const char **av, const char *prefix)
  554. {
  555. struct commit *rev[MAX_REVS], *commit;
  556. char *reflog_msg[MAX_REVS];
  557. struct commit_list *list = NULL, *seen = NULL;
  558. unsigned int rev_mask[MAX_REVS];
  559. int num_rev, i, extra = 0;
  560. int all_heads = 0, all_remotes = 0;
  561. int all_mask, all_revs;
  562. enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER;
  563. char *head;
  564. struct object_id head_oid;
  565. int merge_base = 0;
  566. int independent = 0;
  567. int no_name = 0;
  568. int sha1_name = 0;
  569. int shown_merge_point = 0;
  570. int with_current_branch = 0;
  571. int head_at = -1;
  572. int topics = 0;
  573. int dense = 1;
  574. const char *reflog_base = NULL;
  575. struct option builtin_show_branch_options[] = {
  576. OPT_BOOL('a', "all", &all_heads,
  577. N_("show remote-tracking and local branches")),
  578. OPT_BOOL('r', "remotes", &all_remotes,
  579. N_("show remote-tracking branches")),
  580. OPT__COLOR(&showbranch_use_color,
  581. N_("color '*!+-' corresponding to the branch")),
  582. { OPTION_INTEGER, 0, "more", &extra, N_("n"),
  583. N_("show <n> more commits after the common ancestor"),
  584. PARSE_OPT_OPTARG, NULL, (intptr_t)1 },
  585. OPT_SET_INT(0, "list", &extra, N_("synonym to more=-1"), -1),
  586. OPT_BOOL(0, "no-name", &no_name, N_("suppress naming strings")),
  587. OPT_BOOL(0, "current", &with_current_branch,
  588. N_("include the current branch")),
  589. OPT_BOOL(0, "sha1-name", &sha1_name,
  590. N_("name commits with their object names")),
  591. OPT_BOOL(0, "merge-base", &merge_base,
  592. N_("show possible merge bases")),
  593. OPT_BOOL(0, "independent", &independent,
  594. N_("show refs unreachable from any other ref")),
  595. OPT_SET_INT(0, "topo-order", &sort_order,
  596. N_("show commits in topological order"),
  597. REV_SORT_IN_GRAPH_ORDER),
  598. OPT_BOOL(0, "topics", &topics,
  599. N_("show only commits not on the first branch")),
  600. OPT_SET_INT(0, "sparse", &dense,
  601. N_("show merges reachable from only one tip"), 0),
  602. OPT_SET_INT(0, "date-order", &sort_order,
  603. N_("topologically sort, maintaining date order "
  604. "where possible"),
  605. REV_SORT_BY_COMMIT_DATE),
  606. OPT_CALLBACK_F('g', "reflog", &reflog_base, N_("<n>[,<base>]"),
  607. N_("show <n> most recent ref-log entries starting at "
  608. "base"),
  609. PARSE_OPT_OPTARG | PARSE_OPT_NONEG,
  610. parse_reflog_param),
  611. OPT_END()
  612. };
  613. init_commit_name_slab(&name_slab);
  614. git_config(git_show_branch_config, NULL);
  615. /* If nothing is specified, try the default first */
  616. if (ac == 1 && default_args.nr) {
  617. ac = default_args.nr;
  618. av = default_args.v;
  619. }
  620. ac = parse_options(ac, av, prefix, builtin_show_branch_options,
  621. show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION);
  622. if (all_heads)
  623. all_remotes = 1;
  624. if (extra || reflog) {
  625. /* "listing" mode is incompatible with
  626. * independent nor merge-base modes.
  627. */
  628. if (independent || merge_base)
  629. usage_with_options(show_branch_usage,
  630. builtin_show_branch_options);
  631. if (reflog && ((0 < extra) || all_heads || all_remotes))
  632. /*
  633. * Asking for --more in reflog mode does not
  634. * make sense. --list is Ok.
  635. *
  636. * Also --all and --remotes do not make sense either.
  637. */
  638. die(_("--reflog is incompatible with --all, --remotes, "
  639. "--independent or --merge-base"));
  640. }
  641. /* If nothing is specified, show all branches by default */
  642. if (ac <= topics && all_heads + all_remotes == 0)
  643. all_heads = 1;
  644. if (reflog) {
  645. struct object_id oid;
  646. char *ref;
  647. int base = 0;
  648. unsigned int flags = 0;
  649. if (ac == 0) {
  650. static const char *fake_av[2];
  651. fake_av[0] = resolve_refdup("HEAD",
  652. RESOLVE_REF_READING, &oid,
  653. NULL);
  654. fake_av[1] = NULL;
  655. av = fake_av;
  656. ac = 1;
  657. if (!*av)
  658. die(_("no branches given, and HEAD is not valid"));
  659. }
  660. if (ac != 1)
  661. die(_("--reflog option needs one branch name"));
  662. if (MAX_REVS < reflog)
  663. die(Q_("only %d entry can be shown at one time.",
  664. "only %d entries can be shown at one time.",
  665. MAX_REVS), MAX_REVS);
  666. if (!dwim_ref(*av, strlen(*av), &oid, &ref, 0))
  667. die(_("no such ref %s"), *av);
  668. /* Has the base been specified? */
  669. if (reflog_base) {
  670. char *ep;
  671. base = strtoul(reflog_base, &ep, 10);
  672. if (*ep) {
  673. /* Ah, that is a date spec... */
  674. timestamp_t at;
  675. at = approxidate(reflog_base);
  676. read_ref_at(get_main_ref_store(the_repository),
  677. ref, flags, at, -1, &oid, NULL,
  678. NULL, NULL, &base);
  679. }
  680. }
  681. for (i = 0; i < reflog; i++) {
  682. char *logmsg;
  683. char *nth_desc;
  684. const char *msg;
  685. timestamp_t timestamp;
  686. int tz;
  687. if (read_ref_at(get_main_ref_store(the_repository),
  688. ref, flags, 0, base + i, &oid, &logmsg,
  689. &timestamp, &tz, NULL)) {
  690. reflog = i;
  691. break;
  692. }
  693. msg = strchr(logmsg, '\t');
  694. if (!msg)
  695. msg = "(none)";
  696. else
  697. msg++;
  698. reflog_msg[i] = xstrfmt("(%s) %s",
  699. show_date(timestamp, tz,
  700. DATE_MODE(RELATIVE)),
  701. msg);
  702. free(logmsg);
  703. nth_desc = xstrfmt("%s@{%d}", *av, base+i);
  704. append_ref(nth_desc, &oid, 1);
  705. free(nth_desc);
  706. }
  707. free(ref);
  708. }
  709. else {
  710. while (0 < ac) {
  711. append_one_rev(*av);
  712. ac--; av++;
  713. }
  714. if (all_heads + all_remotes)
  715. snarf_refs(all_heads, all_remotes);
  716. }
  717. head = resolve_refdup("HEAD", RESOLVE_REF_READING,
  718. &head_oid, NULL);
  719. if (with_current_branch && head) {
  720. int has_head = 0;
  721. for (i = 0; !has_head && i < ref_name_cnt; i++) {
  722. /* We are only interested in adding the branch
  723. * HEAD points at.
  724. */
  725. if (rev_is_head(head,
  726. ref_name[i],
  727. head_oid.hash, NULL))
  728. has_head++;
  729. }
  730. if (!has_head) {
  731. const char *name = head;
  732. skip_prefix(name, "refs/heads/", &name);
  733. append_one_rev(name);
  734. }
  735. }
  736. if (!ref_name_cnt) {
  737. fprintf(stderr, "No revs to be shown.\n");
  738. exit(0);
  739. }
  740. for (num_rev = 0; ref_name[num_rev]; num_rev++) {
  741. struct object_id revkey;
  742. unsigned int flag = 1u << (num_rev + REV_SHIFT);
  743. if (MAX_REVS <= num_rev)
  744. die(Q_("cannot handle more than %d rev.",
  745. "cannot handle more than %d revs.",
  746. MAX_REVS), MAX_REVS);
  747. if (get_oid(ref_name[num_rev], &revkey))
  748. die(_("'%s' is not a valid ref."), ref_name[num_rev]);
  749. commit = lookup_commit_reference(the_repository, &revkey);
  750. if (!commit)
  751. die(_("cannot find commit %s (%s)"),
  752. ref_name[num_rev], oid_to_hex(&revkey));
  753. parse_commit(commit);
  754. mark_seen(commit, &seen);
  755. /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
  756. * and so on. REV_SHIFT bits from bit 0 are used for
  757. * internal bookkeeping.
  758. */
  759. commit->object.flags |= flag;
  760. if (commit->object.flags == flag)
  761. commit_list_insert_by_date(commit, &list);
  762. rev[num_rev] = commit;
  763. }
  764. for (i = 0; i < num_rev; i++)
  765. rev_mask[i] = rev[i]->object.flags;
  766. if (0 <= extra)
  767. join_revs(&list, &seen, num_rev, extra);
  768. commit_list_sort_by_date(&seen);
  769. if (merge_base)
  770. return show_merge_base(seen, num_rev);
  771. if (independent)
  772. return show_independent(rev, num_rev, rev_mask);
  773. /* Show list; --more=-1 means list-only */
  774. if (1 < num_rev || extra < 0) {
  775. for (i = 0; i < num_rev; i++) {
  776. int j;
  777. int is_head = rev_is_head(head,
  778. ref_name[i],
  779. head_oid.hash,
  780. rev[i]->object.oid.hash);
  781. if (extra < 0)
  782. printf("%c [%s] ",
  783. is_head ? '*' : ' ', ref_name[i]);
  784. else {
  785. for (j = 0; j < i; j++)
  786. putchar(' ');
  787. printf("%s%c%s [%s] ",
  788. get_color_code(i),
  789. is_head ? '*' : '!',
  790. get_color_reset_code(), ref_name[i]);
  791. }
  792. if (!reflog) {
  793. /* header lines never need name */
  794. show_one_commit(rev[i], 1);
  795. }
  796. else
  797. puts(reflog_msg[i]);
  798. if (is_head)
  799. head_at = i;
  800. }
  801. if (0 <= extra) {
  802. for (i = 0; i < num_rev; i++)
  803. putchar('-');
  804. putchar('\n');
  805. }
  806. }
  807. if (extra < 0)
  808. exit(0);
  809. /* Sort topologically */
  810. sort_in_topological_order(&seen, sort_order);
  811. /* Give names to commits */
  812. if (!sha1_name && !no_name)
  813. name_commits(seen, rev, ref_name, num_rev);
  814. all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
  815. all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
  816. while (seen) {
  817. struct commit *commit = pop_commit(&seen);
  818. int this_flag = commit->object.flags;
  819. int is_merge_point = ((this_flag & all_revs) == all_revs);
  820. shown_merge_point |= is_merge_point;
  821. if (1 < num_rev) {
  822. int is_merge = !!(commit->parents &&
  823. commit->parents->next);
  824. if (topics &&
  825. !is_merge_point &&
  826. (this_flag & (1u << REV_SHIFT)))
  827. continue;
  828. if (dense && is_merge &&
  829. omit_in_dense(commit, rev, num_rev))
  830. continue;
  831. for (i = 0; i < num_rev; i++) {
  832. int mark;
  833. if (!(this_flag & (1u << (i + REV_SHIFT))))
  834. mark = ' ';
  835. else if (is_merge)
  836. mark = '-';
  837. else if (i == head_at)
  838. mark = '*';
  839. else
  840. mark = '+';
  841. printf("%s%c%s",
  842. get_color_code(i),
  843. mark, get_color_reset_code());
  844. }
  845. putchar(' ');
  846. }
  847. show_one_commit(commit, no_name);
  848. if (shown_merge_point && --extra < 0)
  849. break;
  850. }
  851. return 0;
  852. }