Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 1 | #include "cache.h" |
| 2 | #include "commit.h" |
| 3 | #include "refs.h" |
Peter Eriksen | 51ce34b | 2006-05-23 14:15:35 +0200 | [diff] [blame] | 4 | #include "builtin.h" |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 5 | #include "color.h" |
René Scharfe | c949b00 | 2015-10-31 20:06:45 +0100 | [diff] [blame] | 6 | #include "argv-array.h" |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 7 | #include "parse-options.h" |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 8 | |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 9 | static const char* show_branch_usage[] = { |
Junio C Hamano | bb831db | 2015-02-11 13:44:19 -0800 | [diff] [blame] | 10 | N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n" |
Ralf Thielow | d6589d1 | 2015-01-20 20:30:28 +0100 | [diff] [blame] | 11 | " [--current] [--color[=<when>] | --no-color] [--sparse]\n" |
| 12 | " [--more=<n> | --list | --independent | --merge-base]\n" |
| 13 | " [--no-name | --sha1-name] [--topics] [(<rev> | <glob>)...]"), |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 14 | N_("git show-branch (-g | --reflog)[=<n>[,<base>]] [--list] [<ref>]"), |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 15 | NULL |
| 16 | }; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 17 | |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 18 | static int showbranch_use_color = -1; |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 19 | |
René Scharfe | c949b00 | 2015-10-31 20:06:45 +0100 | [diff] [blame] | 20 | static struct argv_array default_args = ARGV_ARRAY_INIT; |
Junio C Hamano | c2bdd6a | 2006-01-09 13:29:23 -0800 | [diff] [blame] | 21 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 22 | #define UNINTERESTING 01 |
| 23 | |
| 24 | #define REV_SHIFT 2 |
Linus Torvalds | 885a86a | 2006-06-14 16:45:13 -0700 | [diff] [blame] | 25 | #define MAX_REVS (FLAG_BITS - REV_SHIFT) /* should not exceed bits_per_int - REV_SHIFT */ |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 26 | |
Junio C Hamano | 7e3fe90 | 2006-12-14 15:58:56 -0800 | [diff] [blame] | 27 | #define DEFAULT_REFLOG 4 |
| 28 | |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 29 | static const char *get_color_code(int idx) |
| 30 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 31 | if (want_color(showbranch_use_color)) |
Dan McGee | 7cd52b5 | 2011-04-05 00:40:23 -0500 | [diff] [blame] | 32 | return column_colors_ansi[idx % column_colors_ansi_max]; |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 33 | return ""; |
| 34 | } |
| 35 | |
| 36 | static const char *get_color_reset_code(void) |
| 37 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 38 | if (want_color(showbranch_use_color)) |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 39 | return GIT_COLOR_RESET; |
| 40 | return ""; |
| 41 | } |
| 42 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 43 | static 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 Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 55 | struct commit_name { |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 56 | const char *head_name; /* which head's ancestor? */ |
| 57 | int generation; /* how many parents away from head_name */ |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 60 | /* Name the commit as nth generation ancestor of head_name; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 61 | * we count only the first-parent relationship for naming purposes. |
| 62 | */ |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 63 | static void name_commit(struct commit *commit, const char *head_name, int nth) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 64 | { |
| 65 | struct commit_name *name; |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 66 | if (!commit->util) |
| 67 | commit->util = xmalloc(sizeof(struct commit_name)); |
| 68 | name = commit->util; |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 69 | name->head_name = head_name; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 70 | name->generation = nth; |
| 71 | } |
| 72 | |
| 73 | /* Parent is the first parent of the commit. We may name it |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 74 | * as (n+1)th generation ancestor of the same head_name as |
Junio C Hamano | bcaf60b | 2005-12-08 14:10:02 -0800 | [diff] [blame] | 75 | * commit is nth generation ancestor of, if that generation |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 76 | * number is better than the name it already has. |
| 77 | */ |
| 78 | static void name_parent(struct commit *commit, struct commit *parent) |
| 79 | { |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 80 | struct commit_name *commit_name = commit->util; |
| 81 | struct commit_name *parent_name = parent->util; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 82 | if (!commit_name) |
| 83 | return; |
| 84 | if (!parent_name || |
| 85 | commit_name->generation + 1 < parent_name->generation) |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 86 | name_commit(parent, commit_name->head_name, |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 87 | commit_name->generation + 1); |
| 88 | } |
| 89 | |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 90 | static int name_first_parent_chain(struct commit *c) |
| 91 | { |
| 92 | int i = 0; |
| 93 | while (c) { |
| 94 | struct commit *p; |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 95 | if (!c->util) |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 96 | break; |
| 97 | if (!c->parents) |
| 98 | break; |
| 99 | p = c->parents->item; |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 100 | if (!p->util) { |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 101 | name_parent(c, p); |
| 102 | i++; |
| 103 | } |
Alexandre Julliard | f8263c5 | 2006-07-23 19:51:04 +0200 | [diff] [blame] | 104 | else |
| 105 | break; |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 106 | c = p; |
| 107 | } |
| 108 | return i; |
| 109 | } |
| 110 | |
| 111 | static 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 Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 123 | if (c->util) |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 124 | 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 Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 149 | if (!c->util) |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 150 | continue; |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 151 | n = c->util; |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 152 | parents = c->parents; |
| 153 | nth = 0; |
| 154 | while (parents) { |
| 155 | struct commit *p = parents->item; |
Jeff King | aaa07e3 | 2013-04-05 17:15:50 -0400 | [diff] [blame] | 156 | struct strbuf newname = STRBUF_INIT; |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 157 | parents = parents->next; |
| 158 | nth++; |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 159 | if (p->util) |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 160 | continue; |
Junio C Hamano | fbaf834 | 2005-09-24 23:33:02 -0700 | [diff] [blame] | 161 | switch (n->generation) { |
| 162 | case 0: |
Jeff King | aaa07e3 | 2013-04-05 17:15:50 -0400 | [diff] [blame] | 163 | strbuf_addstr(&newname, n->head_name); |
Junio C Hamano | fbaf834 | 2005-09-24 23:33:02 -0700 | [diff] [blame] | 164 | break; |
| 165 | case 1: |
Jeff King | aaa07e3 | 2013-04-05 17:15:50 -0400 | [diff] [blame] | 166 | strbuf_addf(&newname, "%s^", n->head_name); |
Junio C Hamano | fbaf834 | 2005-09-24 23:33:02 -0700 | [diff] [blame] | 167 | break; |
| 168 | default: |
Jeff King | aaa07e3 | 2013-04-05 17:15:50 -0400 | [diff] [blame] | 169 | strbuf_addf(&newname, "%s~%d", |
| 170 | n->head_name, n->generation); |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 171 | break; |
Junio C Hamano | fbaf834 | 2005-09-24 23:33:02 -0700 | [diff] [blame] | 172 | } |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 173 | if (nth == 1) |
Jeff King | aaa07e3 | 2013-04-05 17:15:50 -0400 | [diff] [blame] | 174 | strbuf_addch(&newname, '^'); |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 175 | else |
Jeff King | aaa07e3 | 2013-04-05 17:15:50 -0400 | [diff] [blame] | 176 | strbuf_addf(&newname, "^%d", nth); |
| 177 | name_commit(p, strbuf_detach(&newname, NULL), 0); |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 178 | i++; |
| 179 | name_first_parent_chain(p); |
| 180 | } |
| 181 | } |
| 182 | } while (i); |
| 183 | } |
| 184 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 185 | static int mark_seen(struct commit *commit, struct commit_list **seen_p) |
| 186 | { |
| 187 | if (!commit->object.flags) { |
Junio C Hamano | 26a8ad2 | 2006-07-16 00:00:09 -0700 | [diff] [blame] | 188 | commit_list_insert(commit, seen_p); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 189 | return 1; |
| 190 | } |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | static 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 Hamano | 9ce7028 | 2005-11-09 23:36:15 -0800 | [diff] [blame] | 203 | int still_interesting = !!interesting(*list_p); |
René Scharfe | e510ab8 | 2015-10-24 18:21:31 +0200 | [diff] [blame] | 204 | struct commit *commit = pop_commit(list_p); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 205 | int flags = commit->object.flags & all_mask; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 206 | |
Junio C Hamano | 9ce7028 | 2005-11-09 23:36:15 -0800 | [diff] [blame] | 207 | if (!still_interesting && extra <= 0) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 208 | 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 Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 219 | if ((this_flag & flags) == flags) |
| 220 | continue; |
Jeff King | 0064053 | 2013-10-24 04:53:01 -0400 | [diff] [blame] | 221 | parse_commit(p); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 222 | if (mark_seen(p, seen_p) && !still_interesting) |
| 223 | extra--; |
| 224 | p->object.flags |= flags; |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 225 | commit_list_insert_by_date(p, list_p); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
Junio C Hamano | 6b209d4 | 2005-11-10 15:47:58 -0800 | [diff] [blame] | 228 | |
| 229 | /* |
| 230 | * Postprocess to complete well-poisoning. |
| 231 | * |
| 232 | * At this point we have all the commits we have seen in |
Junio C Hamano | 26a8ad2 | 2006-07-16 00:00:09 -0700 | [diff] [blame] | 233 | * seen_p list. Mark anything that can be reached from |
| 234 | * uninteresting commits not interesting. |
Junio C Hamano | 6b209d4 | 2005-11-10 15:47:58 -0800 | [diff] [blame] | 235 | */ |
| 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 Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 266 | } |
| 267 | |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 268 | static void show_one_commit(struct commit *commit, int no_name) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 269 | { |
Brandon Casey | f285a2d | 2008-10-09 14:12:12 -0500 | [diff] [blame] | 270 | struct strbuf pretty = STRBUF_INIT; |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 271 | const char *pretty_str = "(unavailable)"; |
Linus Torvalds | d3ff6f5 | 2006-06-17 18:26:18 -0700 | [diff] [blame] | 272 | struct commit_name *name = commit->util; |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 273 | |
| 274 | if (commit->object.parsed) { |
Jeff King | 8b8a537 | 2011-05-26 18:27:24 -0400 | [diff] [blame] | 275 | pp_commit_easy(CMIT_FMT_ONELINE, commit, &pretty); |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 276 | pretty_str = pretty.buf; |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 277 | } |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 278 | if (starts_with(pretty_str, "[PATCH] ")) |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 279 | pretty_str += 8; |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 280 | |
| 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. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 294 | find_unique_abbrev(commit->object.oid.hash, |
Tay Ray Chuan | bd7440f | 2010-05-24 16:50:44 +0800 | [diff] [blame] | 295 | DEFAULT_ABBREV)); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 296 | } |
Junio C Hamano | 80583c0 | 2007-06-11 00:34:54 -0700 | [diff] [blame] | 297 | puts(pretty_str); |
Pierre Habouzit | 674d172 | 2007-09-10 12:35:06 +0200 | [diff] [blame] | 298 | strbuf_release(&pretty); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | static char *ref_name[MAX_REVS + 1]; |
| 302 | static int ref_name_cnt; |
| 303 | |
Junio C Hamano | bb5ebed | 2005-12-23 12:47:18 -0800 | [diff] [blame] | 304 | static 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 | |
| 319 | static 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 Hamano | 628894b | 2005-08-24 23:26:20 -0700 | [diff] [blame] | 348 | static int compare_ref_name(const void *a_, const void *b_) |
| 349 | { |
| 350 | const char * const*a = a_, * const*b = b_; |
Junio C Hamano | bb5ebed | 2005-12-23 12:47:18 -0800 | [diff] [blame] | 351 | return version_cmp(*a, *b); |
Junio C Hamano | 628894b | 2005-08-24 23:26:20 -0700 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | static void sort_ref_range(int bottom, int top) |
| 355 | { |
René Scharfe | 7e65c75 | 2016-10-01 18:19:48 +0200 | [diff] [blame] | 356 | QSORT(ref_name + bottom, top - bottom, compare_ref_name); |
Junio C Hamano | 628894b | 2005-08-24 23:26:20 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 359 | static int append_ref(const char *refname, const struct object_id *oid, |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 360 | int allow_dups) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 361 | { |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 362 | struct commit *commit = lookup_commit_reference_gently(oid->hash, 1); |
Junio C Hamano | bb5ebed | 2005-12-23 12:47:18 -0800 | [diff] [blame] | 363 | int i; |
| 364 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 365 | if (!commit) |
| 366 | return 0; |
Junio C Hamano | bb5ebed | 2005-12-23 12:47:18 -0800 | [diff] [blame] | 367 | |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 368 | 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 Hamano | 79778e4 | 2005-10-23 01:18:42 -0700 | [diff] [blame] | 374 | if (MAX_REVS <= ref_name_cnt) { |
Vasco Almeida | 205d134 | 2016-09-15 14:59:07 +0000 | [diff] [blame] | 375 | 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 Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 378 | return 0; |
| 379 | } |
Shawn Pearce | 9befac4 | 2006-09-02 00:16:31 -0400 | [diff] [blame] | 380 | ref_name[ref_name_cnt++] = xstrdup(refname); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 381 | ref_name[ref_name_cnt] = NULL; |
| 382 | return 0; |
| 383 | } |
| 384 | |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 385 | static int append_head_ref(const char *refname, const struct object_id *oid, |
| 386 | int flag, void *cb_data) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 387 | { |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 388 | struct object_id tmp; |
Junio C Hamano | 9242150 | 2005-11-21 00:43:12 -0800 | [diff] [blame] | 389 | int ofs = 11; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 390 | if (!starts_with(refname, "refs/heads/")) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 391 | return 0; |
Junio C Hamano | 9242150 | 2005-11-21 00:43:12 -0800 | [diff] [blame] | 392 | /* If both heads/foo and tags/foo exists, get_sha1 would |
| 393 | * get confused. |
| 394 | */ |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 395 | if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid)) |
Junio C Hamano | 9242150 | 2005-11-21 00:43:12 -0800 | [diff] [blame] | 396 | ofs = 5; |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 397 | return append_ref(refname + ofs, oid, 0); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 400 | static int append_remote_ref(const char *refname, const struct object_id *oid, |
| 401 | int flag, void *cb_data) |
Brian Gernhardt | f49006b | 2006-12-22 08:58:39 -0500 | [diff] [blame] | 402 | { |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 403 | struct object_id tmp; |
Brian Gernhardt | f49006b | 2006-12-22 08:58:39 -0500 | [diff] [blame] | 404 | int ofs = 13; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 405 | if (!starts_with(refname, "refs/remotes/")) |
Brian Gernhardt | f49006b | 2006-12-22 08:58:39 -0500 | [diff] [blame] | 406 | return 0; |
| 407 | /* If both heads/foo and tags/foo exists, get_sha1 would |
| 408 | * get confused. |
| 409 | */ |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 410 | if (get_sha1(refname + ofs, tmp.hash) || oidcmp(&tmp, oid)) |
Brian Gernhardt | f49006b | 2006-12-22 08:58:39 -0500 | [diff] [blame] | 411 | ofs = 5; |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 412 | return append_ref(refname + ofs, oid, 0); |
Brian Gernhardt | f49006b | 2006-12-22 08:58:39 -0500 | [diff] [blame] | 413 | } |
| 414 | |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 415 | static int append_tag_ref(const char *refname, const struct object_id *oid, |
| 416 | int flag, void *cb_data) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 417 | { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 418 | if (!starts_with(refname, "refs/tags/")) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 419 | return 0; |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 420 | return append_ref(refname + 5, oid, 0); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 423 | static const char *match_ref_pattern = NULL; |
| 424 | static int match_ref_slash = 0; |
| 425 | static 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 Haggerty | a00595f | 2015-05-25 18:38:45 +0000 | [diff] [blame] | 434 | static int append_matching_ref(const char *refname, const struct object_id *oid, |
| 435 | int flag, void *cb_data) |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 436 | { |
| 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 Duy | eb07894 | 2014-02-15 09:01:46 +0700 | [diff] [blame] | 448 | if (wildmatch(match_ref_pattern, tail, 0, NULL)) |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 449 | return 0; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 450 | if (starts_with(refname, "refs/heads/")) |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 451 | return append_head_ref(refname, oid, flag, cb_data); |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 452 | if (starts_with(refname, "refs/tags/")) |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 453 | return append_tag_ref(refname, oid, flag, cb_data); |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 454 | return append_ref(refname, oid, 0); |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 455 | } |
| 456 | |
Brian Gernhardt | f49006b | 2006-12-22 08:58:39 -0500 | [diff] [blame] | 457 | static void snarf_refs(int head, int remotes) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 458 | { |
Junio C Hamano | 628894b | 2005-08-24 23:26:20 -0700 | [diff] [blame] | 459 | if (head) { |
| 460 | int orig_cnt = ref_name_cnt; |
Michael Haggerty | 2b2a5be | 2015-05-25 18:38:28 +0000 | [diff] [blame] | 461 | |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 462 | for_each_ref(append_head_ref, NULL); |
Junio C Hamano | 628894b | 2005-08-24 23:26:20 -0700 | [diff] [blame] | 463 | sort_ref_range(orig_cnt, ref_name_cnt); |
| 464 | } |
Brian Gernhardt | f49006b | 2006-12-22 08:58:39 -0500 | [diff] [blame] | 465 | if (remotes) { |
Junio C Hamano | 628894b | 2005-08-24 23:26:20 -0700 | [diff] [blame] | 466 | int orig_cnt = ref_name_cnt; |
Michael Haggerty | 2b2a5be | 2015-05-25 18:38:28 +0000 | [diff] [blame] | 467 | |
Michael Haggerty | 2e253a4 | 2015-05-25 18:38:46 +0000 | [diff] [blame] | 468 | for_each_ref(append_remote_ref, NULL); |
Junio C Hamano | 628894b | 2005-08-24 23:26:20 -0700 | [diff] [blame] | 469 | sort_ref_range(orig_cnt, ref_name_cnt); |
| 470 | } |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 471 | } |
| 472 | |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 473 | static int rev_is_head(char *head, int headlen, char *name, |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 474 | unsigned char *head_sha1, unsigned char *sha1) |
| 475 | { |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 476 | if ((!head[0]) || |
David Rientjes | a89fccd | 2006-08-17 11:54:57 -0700 | [diff] [blame] | 477 | (head_sha1 && sha1 && hashcmp(head_sha1, sha1))) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 478 | return 0; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 479 | if (starts_with(head, "refs/heads/")) |
Johannes Schindelin | afdcec7 | 2006-09-22 00:07:01 +0200 | [diff] [blame] | 480 | head += 11; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 481 | if (starts_with(name, "refs/heads/")) |
Johannes Schindelin | afdcec7 | 2006-09-22 00:07:01 +0200 | [diff] [blame] | 482 | name += 11; |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 483 | else if (starts_with(name, "heads/")) |
Johannes Schindelin | afdcec7 | 2006-09-22 00:07:01 +0200 | [diff] [blame] | 484 | name += 6; |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 485 | return !strcmp(head, name); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static 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 Hamano | 2f0f8b7 | 2005-09-08 12:15:52 -0700 | [diff] [blame] | 492 | int exit_status = 1; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 493 | |
| 494 | while (seen) { |
René Scharfe | e510ab8 | 2015-10-24 18:21:31 +0200 | [diff] [blame] | 495 | struct commit *commit = pop_commit(&seen); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 496 | int flags = commit->object.flags & all_mask; |
| 497 | if (!(flags & UNINTERESTING) && |
| 498 | ((flags & all_revs) == all_revs)) { |
brian m. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 499 | puts(oid_to_hex(&commit->object.oid)); |
Junio C Hamano | 2f0f8b7 | 2005-09-08 12:15:52 -0700 | [diff] [blame] | 500 | exit_status = 0; |
| 501 | commit->object.flags |= UNINTERESTING; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 502 | } |
| 503 | } |
Junio C Hamano | 2f0f8b7 | 2005-09-08 12:15:52 -0700 | [diff] [blame] | 504 | return exit_status; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 507 | static 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. carlson | f2fd076 | 2015-11-10 02:22:28 +0000 | [diff] [blame] | 519 | puts(oid_to_hex(&commit->object.oid)); |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 520 | commit->object.flags |= UNINTERESTING; |
| 521 | } |
| 522 | return 0; |
| 523 | } |
| 524 | |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 525 | static void append_one_rev(const char *av) |
| 526 | { |
Michael Haggerty | 7a456c1 | 2015-05-25 18:38:47 +0000 | [diff] [blame] | 527 | struct object_id revkey; |
| 528 | if (!get_sha1(av, revkey.hash)) { |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 529 | append_ref(av, &revkey, 0); |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 530 | return; |
| 531 | } |
Junio C Hamano | 87758f9 | 2006-01-11 00:20:25 -0800 | [diff] [blame] | 532 | if (strchr(av, '*') || strchr(av, '?') || strchr(av, '[')) { |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 533 | /* glob style match */ |
| 534 | int saved_matches = ref_name_cnt; |
Michael Haggerty | 2b2a5be | 2015-05-25 18:38:28 +0000 | [diff] [blame] | 535 | |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 536 | match_ref_pattern = av; |
| 537 | match_ref_slash = count_slash(av); |
Michael Haggerty | a00595f | 2015-05-25 18:38:45 +0000 | [diff] [blame] | 538 | for_each_ref(append_matching_ref, NULL); |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 539 | if (saved_matches == ref_name_cnt && |
| 540 | ref_name_cnt < MAX_REVS) |
Vasco Almeida | 8a78d46 | 2016-09-15 14:59:06 +0000 | [diff] [blame] | 541 | error(_("no matching refs with %s"), av); |
René Scharfe | 7e65c75 | 2016-10-01 18:19:48 +0200 | [diff] [blame] | 542 | sort_ref_range(saved_matches, ref_name_cnt); |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 543 | return; |
| 544 | } |
| 545 | die("bad sha1 reference %s", av); |
| 546 | } |
| 547 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 548 | static int git_show_branch_config(const char *var, const char *value, void *cb) |
Junio C Hamano | c2bdd6a | 2006-01-09 13:29:23 -0800 | [diff] [blame] | 549 | { |
| 550 | if (!strcmp(var, "showbranch.default")) { |
Junio C Hamano | 4f9c412 | 2008-02-11 10:51:03 -0800 | [diff] [blame] | 551 | if (!value) |
| 552 | return config_error_nonbool(var); |
Junio C Hamano | 3af1cae | 2009-06-08 23:26:44 -0700 | [diff] [blame] | 553 | /* |
| 554 | * default_arg is now passed to parse_options(), so we need to |
Junio C Hamano | 9517e6b | 2010-02-03 21:23:18 -0800 | [diff] [blame] | 555 | * mimic the real argv a bit better. |
Junio C Hamano | 3af1cae | 2009-06-08 23:26:44 -0700 | [diff] [blame] | 556 | */ |
René Scharfe | c949b00 | 2015-10-31 20:06:45 +0100 | [diff] [blame] | 557 | if (!default_args.argc) |
| 558 | argv_array_push(&default_args, "show-branch"); |
| 559 | argv_array_push(&default_args, value); |
Junio C Hamano | c2bdd6a | 2006-01-09 13:29:23 -0800 | [diff] [blame] | 560 | return 0; |
| 561 | } |
| 562 | |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 563 | if (!strcmp(var, "color.showbranch")) { |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 564 | showbranch_use_color = git_config_colorbool(var, value); |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | return git_color_default_config(var, value, cb); |
Junio C Hamano | c2bdd6a | 2006-01-09 13:29:23 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Junio C Hamano | c24fe42 | 2006-05-01 17:12:26 -0700 | [diff] [blame] | 571 | static 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 Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 592 | static int reflog = 0; |
| 593 | |
| 594 | static int parse_reflog_param(const struct option *opt, const char *arg, |
| 595 | int unset) |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 596 | { |
| 597 | char *ep; |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 598 | const char **base = (const char **)opt->value; |
| 599 | if (!arg) |
| 600 | arg = ""; |
| 601 | reflog = strtoul(arg, &ep, 10); |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 602 | if (*ep == ',') |
| 603 | *base = ep + 1; |
| 604 | else if (*ep) |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 605 | return error("unrecognized reflog param '%s'", arg); |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 606 | else |
| 607 | *base = NULL; |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 608 | if (reflog <= 0) |
| 609 | reflog = DEFAULT_REFLOG; |
| 610 | return 0; |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 611 | } |
| 612 | |
Linus Torvalds | a633fca | 2006-07-28 22:44:25 -0700 | [diff] [blame] | 613 | int cmd_show_branch(int ac, const char **av, const char *prefix) |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 614 | { |
| 615 | struct commit *rev[MAX_REVS], *commit; |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 616 | char *reflog_msg[MAX_REVS]; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 617 | struct commit_list *list = NULL, *seen = NULL; |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 618 | unsigned int rev_mask[MAX_REVS]; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 619 | int num_rev, i, extra = 0; |
Brian Gernhardt | f49006b | 2006-12-22 08:58:39 -0500 | [diff] [blame] | 620 | int all_heads = 0, all_remotes = 0; |
Junio C Hamano | 6b209d4 | 2005-11-10 15:47:58 -0800 | [diff] [blame] | 621 | int all_mask, all_revs; |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 622 | enum rev_sort_order sort_order = REV_SORT_IN_GRAPH_ORDER; |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 623 | char head[128]; |
| 624 | const char *head_p; |
| 625 | int head_len; |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 626 | struct object_id head_oid; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 627 | int merge_base = 0; |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 628 | int independent = 0; |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 629 | int no_name = 0; |
| 630 | int sha1_name = 0; |
Junio C Hamano | 6b209d4 | 2005-11-10 15:47:58 -0800 | [diff] [blame] | 631 | int shown_merge_point = 0; |
Junio C Hamano | 1aa68d6 | 2006-01-11 00:16:42 -0800 | [diff] [blame] | 632 | int with_current_branch = 0; |
Junio C Hamano | ebedc31 | 2006-01-11 14:02:38 -0800 | [diff] [blame] | 633 | int head_at = -1; |
Junio C Hamano | d320a54 | 2006-03-02 17:14:00 -0800 | [diff] [blame] | 634 | int topics = 0; |
Junio C Hamano | c24fe42 | 2006-05-01 17:12:26 -0700 | [diff] [blame] | 635 | int dense = 1; |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 636 | const char *reflog_base = NULL; |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 637 | struct option builtin_show_branch_options[] = { |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 638 | 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 Lodato | 73e9da0 | 2010-02-16 23:55:58 -0500 | [diff] [blame] | 642 | OPT__COLOR(&showbranch_use_color, |
Nguyễn Thái Ngọc Duy | d780bef | 2012-08-20 19:32:44 +0700 | [diff] [blame] | 643 | 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 Boyd | e169b97 | 2009-06-07 16:39:15 -0700 | [diff] [blame] | 646 | PARSE_OPT_OPTARG, NULL, (intptr_t)1 }, |
Nguyễn Thái Ngọc Duy | d780bef | 2012-08-20 19:32:44 +0700 | [diff] [blame] | 647 | OPT_SET_INT(0, "list", &extra, N_("synonym to more=-1"), -1), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 648 | 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 Duy | d780bef | 2012-08-20 19:32:44 +0700 | [diff] [blame] | 656 | N_("show refs unreachable from any other ref")), |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 657 | OPT_SET_INT(0, "topo-order", &sort_order, |
| 658 | N_("show commits in topological order"), |
| 659 | REV_SORT_IN_GRAPH_ORDER), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 660 | OPT_BOOL(0, "topics", &topics, |
| 661 | N_("show only commits not on the first branch")), |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 662 | OPT_SET_INT(0, "sparse", &dense, |
Nguyễn Thái Ngọc Duy | d780bef | 2012-08-20 19:32:44 +0700 | [diff] [blame] | 663 | N_("show merges reachable from only one tip"), 0), |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 664 | OPT_SET_INT(0, "date-order", &sort_order, |
Thomas Rast | 465cf8c | 2013-07-18 14:26:56 +0200 | [diff] [blame] | 665 | N_("topologically sort, maintaining date order " |
Junio C Hamano | 4ca8ae7 | 2013-07-22 11:23:30 -0700 | [diff] [blame] | 666 | "where possible"), |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 667 | REV_SORT_BY_COMMIT_DATE), |
Nguyễn Thái Ngọc Duy | d780bef | 2012-08-20 19:32:44 +0700 | [diff] [blame] | 668 | { OPTION_CALLBACK, 'g', "reflog", &reflog_base, N_("<n>[,<base>]"), |
| 669 | N_("show <n> most recent ref-log entries starting at " |
| 670 | "base"), |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 671 | PARSE_OPT_OPTARG | PARSE_OPT_LITERAL_ARGHELP, |
| 672 | parse_reflog_param }, |
| 673 | OPT_END() |
| 674 | }; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 675 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 676 | git_config(git_show_branch_config, NULL); |
Junio C Hamano | 076b232 | 2005-08-30 16:59:37 -0700 | [diff] [blame] | 677 | |
Junio C Hamano | c2bdd6a | 2006-01-09 13:29:23 -0800 | [diff] [blame] | 678 | /* If nothing is specified, try the default first */ |
René Scharfe | c949b00 | 2015-10-31 20:06:45 +0100 | [diff] [blame] | 679 | if (ac == 1 && default_args.argc) { |
| 680 | ac = default_args.argc; |
| 681 | av = default_args.argv; |
Junio C Hamano | c2bdd6a | 2006-01-09 13:29:23 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 684 | ac = parse_options(ac, av, prefix, builtin_show_branch_options, |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 685 | show_branch_usage, PARSE_OPT_STOP_AT_NON_OPTION); |
| 686 | if (all_heads) |
| 687 | all_remotes = 1; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 688 | |
Junio C Hamano | df373ea | 2007-01-25 22:14:45 -0800 | [diff] [blame] | 689 | if (extra || reflog) { |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 690 | /* "listing" mode is incompatible with |
| 691 | * independent nor merge-base modes. |
| 692 | */ |
| 693 | if (independent || merge_base) |
Stephen Boyd | 5734365 | 2009-05-21 00:33:18 -0700 | [diff] [blame] | 694 | usage_with_options(show_branch_usage, |
| 695 | builtin_show_branch_options); |
Junio C Hamano | df373ea | 2007-01-25 22:14:45 -0800 | [diff] [blame] | 696 | if (reflog && ((0 < extra) || all_heads || all_remotes)) |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 697 | /* |
| 698 | * Asking for --more in reflog mode does not |
Junio C Hamano | b15af07 | 2007-01-19 22:51:49 -0800 | [diff] [blame] | 699 | * make sense. --list is Ok. |
| 700 | * |
| 701 | * Also --all and --remotes do not make sense either. |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 702 | */ |
Vasco Almeida | 8a78d46 | 2016-09-15 14:59:06 +0000 | [diff] [blame] | 703 | die(_("--reflog is incompatible with --all, --remotes, " |
| 704 | "--independent or --merge-base")); |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 705 | } |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 706 | |
Junio C Hamano | bb5ebed | 2005-12-23 12:47:18 -0800 | [diff] [blame] | 707 | /* If nothing is specified, show all branches by default */ |
Mike Hommey | 539d09c | 2015-03-31 07:12:23 +0900 | [diff] [blame] | 708 | if (ac <= topics && all_heads + all_remotes == 0) |
Junio C Hamano | bb5ebed | 2005-12-23 12:47:18 -0800 | [diff] [blame] | 709 | all_heads = 1; |
| 710 | |
Junio C Hamano | 7e3fe90 | 2006-12-14 15:58:56 -0800 | [diff] [blame] | 711 | if (reflog) { |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 712 | struct object_id oid; |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 713 | char *ref; |
| 714 | int base = 0; |
David Aguilar | c41a87d | 2014-09-18 20:45:37 -0700 | [diff] [blame] | 715 | unsigned int flags = 0; |
Junio C Hamano | df373ea | 2007-01-25 22:14:45 -0800 | [diff] [blame] | 716 | |
| 717 | if (ac == 0) { |
| 718 | static const char *fake_av[2]; |
Junio C Hamano | 632ac9f | 2007-02-03 23:31:47 -0800 | [diff] [blame] | 719 | |
Ronnie Sahlberg | 7695d11 | 2014-07-15 12:59:36 -0700 | [diff] [blame] | 720 | fake_av[0] = resolve_refdup("HEAD", |
| 721 | RESOLVE_REF_READING, |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 722 | oid.hash, NULL); |
Junio C Hamano | df373ea | 2007-01-25 22:14:45 -0800 | [diff] [blame] | 723 | fake_av[1] = NULL; |
| 724 | av = fake_av; |
| 725 | ac = 1; |
Jeff King | 7cd17e8 | 2015-09-24 17:02:54 -0400 | [diff] [blame] | 726 | if (!*av) |
Vasco Almeida | 8a78d46 | 2016-09-15 14:59:06 +0000 | [diff] [blame] | 727 | die(_("no branches given, and HEAD is not valid")); |
Junio C Hamano | df373ea | 2007-01-25 22:14:45 -0800 | [diff] [blame] | 728 | } |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 729 | if (ac != 1) |
Vasco Almeida | 8a78d46 | 2016-09-15 14:59:06 +0000 | [diff] [blame] | 730 | die(_("--reflog option needs one branch name")); |
Junio C Hamano | df373ea | 2007-01-25 22:14:45 -0800 | [diff] [blame] | 731 | |
Junio C Hamano | b15af07 | 2007-01-19 22:51:49 -0800 | [diff] [blame] | 732 | if (MAX_REVS < reflog) |
Vasco Almeida | 205d134 | 2016-09-15 14:59:07 +0000 | [diff] [blame] | 733 | 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 Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 736 | if (!dwim_ref(*av, strlen(*av), oid.hash, &ref)) |
Vasco Almeida | 8a78d46 | 2016-09-15 14:59:06 +0000 | [diff] [blame] | 737 | die(_("no such ref %s"), *av); |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 738 | |
| 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 Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 747 | read_ref_at(ref, flags, at, -1, oid.hash, NULL, |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 748 | NULL, NULL, &base); |
| 749 | } |
| 750 | } |
| 751 | |
Junio C Hamano | 7e3fe90 | 2006-12-14 15:58:56 -0800 | [diff] [blame] | 752 | for (i = 0; i < reflog; i++) { |
Jeff King | 2831018 | 2014-06-19 17:24:33 -0400 | [diff] [blame] | 753 | char *logmsg; |
Jeff King | 78f23bd | 2015-08-19 14:12:48 -0400 | [diff] [blame] | 754 | char *nth_desc; |
Shawn O. Pearce | 3a55602 | 2007-03-06 20:44:17 -0500 | [diff] [blame] | 755 | const char *msg; |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 756 | unsigned long timestamp; |
| 757 | int tz; |
| 758 | |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 759 | if (read_ref_at(ref, flags, 0, base+i, oid.hash, &logmsg, |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 760 | ×tamp, &tz, NULL)) { |
| 761 | reflog = i; |
| 762 | break; |
| 763 | } |
| 764 | msg = strchr(logmsg, '\t'); |
| 765 | if (!msg) |
| 766 | msg = "(none)"; |
| 767 | else |
| 768 | msg++; |
Jeff King | 2831018 | 2014-06-19 17:24:33 -0400 | [diff] [blame] | 769 | reflog_msg[i] = xstrfmt("(%s) %s", |
Jeff King | a5481a6 | 2015-06-25 12:55:02 -0400 | [diff] [blame] | 770 | show_date(timestamp, tz, |
| 771 | DATE_MODE(RELATIVE)), |
Jeff King | 2831018 | 2014-06-19 17:24:33 -0400 | [diff] [blame] | 772 | msg); |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 773 | free(logmsg); |
Jeff King | 78f23bd | 2015-08-19 14:12:48 -0400 | [diff] [blame] | 774 | |
| 775 | nth_desc = xstrfmt("%s@{%d}", *av, base+i); |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 776 | append_ref(nth_desc, &oid, 1); |
Jeff King | 78f23bd | 2015-08-19 14:12:48 -0400 | [diff] [blame] | 777 | free(nth_desc); |
Junio C Hamano | 7e3fe90 | 2006-12-14 15:58:56 -0800 | [diff] [blame] | 778 | } |
Jeff King | 28b3563 | 2014-07-24 00:41:11 -0400 | [diff] [blame] | 779 | free(ref); |
Junio C Hamano | 7e3fe90 | 2006-12-14 15:58:56 -0800 | [diff] [blame] | 780 | } |
| 781 | else { |
| 782 | while (0 < ac) { |
| 783 | append_one_rev(*av); |
| 784 | ac--; av++; |
| 785 | } |
Mike Hommey | 539d09c | 2015-03-31 07:12:23 +0900 | [diff] [blame] | 786 | if (all_heads + all_remotes) |
| 787 | snarf_refs(all_heads, all_remotes); |
Junio C Hamano | bb5ebed | 2005-12-23 12:47:18 -0800 | [diff] [blame] | 788 | } |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 789 | |
Ronnie Sahlberg | 7695d11 | 2014-07-15 12:59:36 -0700 | [diff] [blame] | 790 | head_p = resolve_ref_unsafe("HEAD", RESOLVE_REF_READING, |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 791 | head_oid.hash, NULL); |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 792 | if (head_p) { |
| 793 | head_len = strlen(head_p); |
| 794 | memcpy(head, head_p, head_len + 1); |
Junio C Hamano | 1aa68d6 | 2006-01-11 00:16:42 -0800 | [diff] [blame] | 795 | } |
| 796 | else { |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 797 | head_len = 0; |
| 798 | head[0] = 0; |
Junio C Hamano | 1aa68d6 | 2006-01-11 00:16:42 -0800 | [diff] [blame] | 799 | } |
| 800 | |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 801 | if (with_current_branch && head_p) { |
Junio C Hamano | 1aa68d6 | 2006-01-11 00:16:42 -0800 | [diff] [blame] | 802 | 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 Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 807 | if (rev_is_head(head, |
| 808 | head_len, |
Junio C Hamano | 1aa68d6 | 2006-01-11 00:16:42 -0800 | [diff] [blame] | 809 | ref_name[i], |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 810 | head_oid.hash, NULL)) |
Junio C Hamano | 1aa68d6 | 2006-01-11 00:16:42 -0800 | [diff] [blame] | 811 | has_head++; |
| 812 | } |
| 813 | if (!has_head) { |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 814 | int offset = starts_with(head, "refs/heads/") ? 11 : 0; |
Junio C Hamano | f8fcb57 | 2008-05-26 15:09:51 -0700 | [diff] [blame] | 815 | append_one_rev(head + offset); |
Junio C Hamano | 1aa68d6 | 2006-01-11 00:16:42 -0800 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
Junio C Hamano | 287f860 | 2005-12-04 15:58:50 -0800 | [diff] [blame] | 819 | if (!ref_name_cnt) { |
| 820 | fprintf(stderr, "No revs to be shown.\n"); |
| 821 | exit(0); |
| 822 | } |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 823 | |
| 824 | for (num_rev = 0; ref_name[num_rev]; num_rev++) { |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 825 | struct object_id revkey; |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 826 | unsigned int flag = 1u << (num_rev + REV_SHIFT); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 827 | |
| 828 | if (MAX_REVS <= num_rev) |
Vasco Almeida | 205d134 | 2016-09-15 14:59:07 +0000 | [diff] [blame] | 829 | die(Q_("cannot handle more than %d rev.", |
| 830 | "cannot handle more than %d revs.", |
| 831 | MAX_REVS), MAX_REVS); |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 832 | if (get_sha1(ref_name[num_rev], revkey.hash)) |
Vasco Almeida | 8a78d46 | 2016-09-15 14:59:06 +0000 | [diff] [blame] | 833 | die(_("'%s' is not a valid ref."), ref_name[num_rev]); |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 834 | commit = lookup_commit_reference(revkey.hash); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 835 | if (!commit) |
Vasco Almeida | 8a78d46 | 2016-09-15 14:59:06 +0000 | [diff] [blame] | 836 | die(_("cannot find commit %s (%s)"), |
Michael Haggerty | 96062b5 | 2015-05-25 18:38:49 +0000 | [diff] [blame] | 837 | ref_name[num_rev], oid_to_hex(&revkey)); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 838 | parse_commit(commit); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 839 | 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 Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 845 | commit->object.flags |= flag; |
| 846 | if (commit->object.flags == flag) |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 847 | commit_list_insert_by_date(commit, &list); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 848 | rev[num_rev] = commit; |
| 849 | } |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 850 | 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 Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 855 | |
Thiago Farina | 47e44ed | 2010-11-26 23:58:14 -0200 | [diff] [blame] | 856 | commit_list_sort_by_date(&seen); |
Junio C Hamano | 26a8ad2 | 2006-07-16 00:00:09 -0700 | [diff] [blame] | 857 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 858 | if (merge_base) |
| 859 | return show_merge_base(seen, num_rev); |
| 860 | |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 861 | if (independent) |
| 862 | return show_independent(rev, num_rev, ref_name, rev_mask); |
| 863 | |
| 864 | /* Show list; --more=-1 means list-only */ |
Junio C Hamano | c9d023b | 2005-09-10 18:24:46 -0700 | [diff] [blame] | 865 | if (1 < num_rev || extra < 0) { |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 866 | for (i = 0; i < num_rev; i++) { |
| 867 | int j; |
Linus Torvalds | ed378ec | 2006-09-11 20:17:35 -0700 | [diff] [blame] | 868 | int is_head = rev_is_head(head, |
| 869 | head_len, |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 870 | ref_name[i], |
Michael Haggerty | d1516bf | 2015-05-25 18:38:48 +0000 | [diff] [blame] | 871 | head_oid.hash, |
brian m. carlson | ed1c997 | 2015-11-10 02:22:29 +0000 | [diff] [blame] | 872 | rev[i]->object.oid.hash); |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 873 | 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 Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 879 | printf("%s%c%s [%s] ", |
Dan McGee | 7cd52b5 | 2011-04-05 00:40:23 -0500 | [diff] [blame] | 880 | get_color_code(i), |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 881 | is_head ? '*' : '!', |
| 882 | get_color_reset_code(), ref_name[i]); |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 883 | } |
Junio C Hamano | 76a44c5 | 2007-01-19 01:20:23 -0800 | [diff] [blame] | 884 | |
| 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 Hamano | ebedc31 | 2006-01-11 14:02:38 -0800 | [diff] [blame] | 892 | if (is_head) |
| 893 | head_at = i; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 894 | } |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 895 | if (0 <= extra) { |
| 896 | for (i = 0; i < num_rev; i++) |
| 897 | putchar('-'); |
| 898 | putchar('\n'); |
| 899 | } |
Junio C Hamano | f5e375c | 2005-08-22 23:16:46 -0700 | [diff] [blame] | 900 | } |
Junio C Hamano | 1f8af48 | 2005-09-09 15:40:45 -0700 | [diff] [blame] | 901 | if (extra < 0) |
| 902 | exit(0); |
Junio C Hamano | f5e375c | 2005-08-22 23:16:46 -0700 | [diff] [blame] | 903 | |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 904 | /* Sort topologically */ |
Junio C Hamano | 08f704f | 2013-06-06 16:07:14 -0700 | [diff] [blame] | 905 | sort_in_topological_order(&seen, sort_order); |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 906 | |
| 907 | /* Give names to commits */ |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 908 | if (!sha1_name && !no_name) |
| 909 | name_commits(seen, rev, ref_name, num_rev); |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 910 | |
| 911 | all_mask = ((1u << (REV_SHIFT + num_rev)) - 1); |
| 912 | all_revs = all_mask & ~((1u << REV_SHIFT) - 1); |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 913 | |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 914 | while (seen) { |
René Scharfe | e510ab8 | 2015-10-24 18:21:31 +0200 | [diff] [blame] | 915 | struct commit *commit = pop_commit(&seen); |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 916 | int this_flag = commit->object.flags; |
Junio C Hamano | f794c23 | 2006-03-03 14:34:40 -0800 | [diff] [blame] | 917 | int is_merge_point = ((this_flag & all_revs) == all_revs); |
Junio C Hamano | f5e375c | 2005-08-22 23:16:46 -0700 | [diff] [blame] | 918 | |
Junio C Hamano | f794c23 | 2006-03-03 14:34:40 -0800 | [diff] [blame] | 919 | shown_merge_point |= is_merge_point; |
Junio C Hamano | 8e5dd22 | 2005-08-29 17:19:47 -0700 | [diff] [blame] | 920 | |
Junio C Hamano | f5e375c | 2005-08-22 23:16:46 -0700 | [diff] [blame] | 921 | if (1 < num_rev) { |
Junio C Hamano | c24fe42 | 2006-05-01 17:12:26 -0700 | [diff] [blame] | 922 | int is_merge = !!(commit->parents && |
| 923 | commit->parents->next); |
Junio C Hamano | f794c23 | 2006-03-03 14:34:40 -0800 | [diff] [blame] | 924 | if (topics && |
| 925 | !is_merge_point && |
| 926 | (this_flag & (1u << REV_SHIFT))) |
| 927 | continue; |
Junio C Hamano | c24fe42 | 2006-05-01 17:12:26 -0700 | [diff] [blame] | 928 | if (dense && is_merge && |
| 929 | omit_in_dense(commit, rev, num_rev)) |
| 930 | continue; |
Junio C Hamano | ebedc31 | 2006-01-11 14:02:38 -0800 | [diff] [blame] | 931 | 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 Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 941 | printf("%s%c%s", |
Dan McGee | 7cd52b5 | 2011-04-05 00:40:23 -0500 | [diff] [blame] | 942 | get_color_code(i), |
Markus Heidelberg | ab07ba2 | 2009-04-22 23:41:25 +0200 | [diff] [blame] | 943 | mark, get_color_reset_code()); |
Junio C Hamano | ebedc31 | 2006-01-11 14:02:38 -0800 | [diff] [blame] | 944 | } |
Junio C Hamano | f5e375c | 2005-08-22 23:16:46 -0700 | [diff] [blame] | 945 | putchar(' '); |
| 946 | } |
Junio C Hamano | 013f276 | 2005-10-11 15:22:48 -0700 | [diff] [blame] | 947 | show_one_commit(commit, no_name); |
Junio C Hamano | 6b209d4 | 2005-11-10 15:47:58 -0800 | [diff] [blame] | 948 | |
| 949 | if (shown_merge_point && --extra < 0) |
| 950 | break; |
Junio C Hamano | f76412e | 2005-08-21 02:51:10 -0700 | [diff] [blame] | 951 | } |
| 952 | return 0; |
| 953 | } |