show-branch: --list and --independent
The --list option is what 'git branch' without parameter should have been; it shows the one-line commit message for each branch name. The --independent option is used to filter out commits that can be reachable from other commits, to make detection of fast forward condition in multi-head merge easier. Signed-off-by: Junio C Hamano <junkio@cox.net>
Junio C Hamano committed
Sep 9, 2005 at 15:40 UTC
1f8af483df5ea7070c421489eff40ca8e508b778
2 files changed
+73
-17
Documentation/git-show-branch.txt
+14
-2
@@ -8,7 +8,7 @@ git-show-branch - Show branches and their commits.
8
9
SYNOPSIS
10
--------
11
-'git show-branch [--all] [--heads] [--tags] [--more=<n>] [--merge-base] <reference>...'
11
+'git show-branch [--all] [--heads] [--tags] [--more=<n> | --list | --independent | --merge-base] <reference>...'
12
13
DESCRIPTION
14
-----------
@@ -29,13 +29,25 @@ OPTIONS
29
Usually the command stops output upon showing the commit
30
that is the common ancestor of all the branches. This
31
flag tells the command to go <n> more common commits
32
- beyond that.
32
+ beyond that. When <n> is negative, display only the
33
+ <reference>s given, without showing the commit ancestry
34
+ tree.
35
+
36
+--list::
37
+ Synomym to `--more=-1`
38
39
--merge-base::
40
Instead of showing the commit list, just act like the
41
'git-merge-base -a' command, except that it can accept
42
more than two heads.
43
44
+--independent::
45
+ Among the <reference>s given, display only the ones that
46
+ cannot be reached from any other <reference>.
47
+
48
+Note that --more, --list, --independent and --merge-base options
49
+are mutually exclusive.
50
+
51
52
OUTPUT
53
------
show-branch.c
+59
-15
@@ -4,7 +4,7 @@
4
#include "refs.h"
5
6
static const char show_branch_usage[] =
7
-"git-show-branch [--all] [--heads] [--tags] [--more=count] [--merge-base] [<refs>...]";
7
+"git-show-branch [--all] [--heads] [--tags] [--more=count | --list | --independent | --merge-base ] [<refs>...]";
8
9
#define UNINTERESTING 01
10
@@ -306,10 +306,29 @@ static int show_merge_base(struct commit_list *seen, int num_rev)
306
return exit_status;
307
}
308
309
+static int show_independent(struct commit **rev,
310
+ int num_rev,
311
+ char **ref_name,
312
+ unsigned int *rev_mask)
313
+{
314
+ int i;
315
+
316
+ for (i = 0; i < num_rev; i++) {
317
+ struct commit *commit = rev[i];
318
+ unsigned int flag = rev_mask[i];
319
+
320
+ if (commit->object.flags == flag)
321
+ puts(sha1_to_hex(commit->object.sha1));
322
+ commit->object.flags |= UNINTERESTING;
323
+ }
324
+ return 0;
325
+}
326
+
327
int main(int ac, char **av)
328
{
329
struct commit *rev[MAX_REVS], *commit;
330
struct commit_list *list = NULL, *seen = NULL;
331
+ unsigned int rev_mask[MAX_REVS];
332
int num_rev, i, extra = 0;
333
int all_heads = 0, all_tags = 0;
334
int all_mask, all_revs, shown_merge_point;
@@ -317,6 +336,7 @@ int main(int ac, char **av)
336
int head_path_len;
337
unsigned char head_sha1[20];
338
int merge_base = 0;
339
+ int independent = 0;
340
char **label;
341
342
setup_git_directory();
@@ -331,19 +351,24 @@ int main(int ac, char **av)
351
all_tags = 1;
352
else if (!strcmp(arg, "--more"))
353
extra = 1;
334
- else if (!strncmp(arg, "--more=", 7)) {
354
+ else if (!strcmp(arg, "--list"))
355
+ extra = -1;
356
+ else if (!strncmp(arg, "--more=", 7))
357
extra = atoi(arg + 7);
336
- if (extra < 0)
337
- usage(show_branch_usage);
338
- }
358
else if (!strcmp(arg, "--merge-base"))
359
merge_base = 1;
360
+ else if (!strcmp(arg, "--independent"))
361
+ independent = 1;
362
else
363
usage(show_branch_usage);
364
ac--; av++;
365
}
366
ac--; av++;
367
368
+ /* Only one of these is allowed */
369
+ if (1 < independent + merge_base + (extra != 0))
370
+ usage(show_branch_usage);
371
+
372
if (all_heads + all_tags)
373
snarf_refs(all_heads, all_tags);
374
@@ -361,6 +386,7 @@ int main(int ac, char **av)
386
387
for (num_rev = 0; ref_name[num_rev]; num_rev++) {
388
unsigned char revkey[20];
389
+ unsigned int flag = 1u << (num_rev + REV_SHIFT);
390
391
if (MAX_REVS <= num_rev)
392
die("cannot handle more than %d revs.", MAX_REVS);
@@ -377,11 +403,16 @@ int main(int ac, char **av)
403
* and so on. REV_SHIFT bits from bit 0 are used for
404
* internal bookkeeping.
405
*/
380
- commit->object.flags |= 1u << (num_rev + REV_SHIFT);
381
- insert_by_date(commit, &list);
406
+ commit->object.flags |= flag;
407
+ if (commit->object.flags == flag)
408
+ insert_by_date(commit, &list);
409
rev[num_rev] = commit;
410
}
384
- join_revs(&list, &seen, num_rev, extra);
411
+ for (i = 0; i < num_rev; i++)
412
+ rev_mask[i] = rev[i]->object.flags;
413
+
414
+ if (0 <= extra)
415
+ join_revs(&list, &seen, num_rev, extra);
416
417
head_path_len = readlink(".git/HEAD", head_path, sizeof(head_path)-1);
418
if ((head_path_len < 0) || get_sha1("HEAD", head_sha1))
@@ -392,7 +423,10 @@ int main(int ac, char **av)
423
if (merge_base)
424
return show_merge_base(seen, num_rev);
425
395
- /* Show list */
426
+ if (independent)
427
+ return show_independent(rev, num_rev, ref_name, rev_mask);
428
+
429
+ /* Show list; --more=-1 means list-only */
430
if (1 < num_rev) {
431
for (i = 0; i < num_rev; i++) {
432
int j;
@@ -401,15 +435,25 @@ int main(int ac, char **av)
435
ref_name[i],
436
head_sha1,
437
rev[i]->object.sha1);
404
- for (j = 0; j < i; j++)
405
- putchar(' ');
406
- printf("%c [%s] ", is_head ? '*' : '!', ref_name[i]);
438
+ if (extra < 0)
439
+ printf("%c [%s] ",
440
+ is_head ? '*' : ' ', ref_name[i]);
441
+ else {
442
+ for (j = 0; j < i; j++)
443
+ putchar(' ');
444
+ printf("%c [%s] ",
445
+ is_head ? '*' : '!', ref_name[i]);
446
+ }
447
show_one_commit(rev[i]);
448
}
409
- for (i = 0; i < num_rev; i++)
410
- putchar('-');
411
- putchar('\n');
449
+ if (0 <= extra) {
450
+ for (i = 0; i < num_rev; i++)
451
+ putchar('-');
452
+ putchar('\n');
453
+ }
454
}
455
+ if (extra < 0)
456
+ exit(0);
457
458
/* Sort topologically */
459
sort_in_topological_order(&seen);