Skip to content

Commit b79c985

Browse files
committed
Merge branch 'lh/branch-merged'
* lh/branch-merged: Add tests for `branch --[no-]merged` git-branch.txt: compare --contains, --merged and --no-merged git-branch: add support for --merged and --no-merged
2 parents dc484f2 + f9fd521 commit b79c985

File tree

3 files changed

+80
-3
lines changed

3 files changed

+80
-3
lines changed

Documentation/git-branch.txt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ git-branch - List, create, or delete branches
88
SYNOPSIS
99
--------
1010
[verse]
11-
'git-branch' [--color | --no-color] [-r | -a]
11+
'git-branch' [--color | --no-color] [-r | -a] [--merged | --no-merged]
1212
[-v [--abbrev=<length> | --no-abbrev]]
1313
[--contains <commit>]
1414
'git-branch' [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
@@ -24,6 +24,8 @@ and option `-a` shows both.
2424
With `--contains <commit>`, shows only the branches that
2525
contains the named commit (in other words, the branches whose
2626
tip commits are descendant of the named commit).
27+
With `--merged`, only branches merged into HEAD will be listed, and
28+
with `--no-merged` only branches not merged into HEAD will be listed.
2729

2830
In its second form, a new branch named <branchname> will be created.
2931
It will start out with a head equal to the one given as <start-point>.
@@ -118,6 +120,15 @@ OPTIONS
118120
--no-track::
119121
Ignore the branch.autosetupmerge configuration variable.
120122

123+
--contains <commit>::
124+
Only list branches which contain the specified commit.
125+
126+
--merged::
127+
Only list branches which are fully contained by HEAD.
128+
129+
--no-merged::
130+
Do not list branches which are fully contained by HEAD.
131+
121132
<branchname>::
122133
The name of the branch to create or delete.
123134
The new branch name must pass all checks defined by
@@ -175,6 +186,18 @@ If you are creating a branch that you want to immediately checkout, it's
175186
easier to use the git checkout command with its `-b` option to create
176187
a branch and check it out with a single command.
177188

189+
The options `--contains`, `--merged` and `--no-merged` serves three related
190+
but different purposes:
191+
192+
- `--contains <commit>` is used to find all branches which will need
193+
special attention if <commit> were to be rebased or amended, since those
194+
branches contain the specified <commit>.
195+
196+
- `--merged` is used to find all branches which can be safely deleted,
197+
since those branches are fully contained by HEAD.
198+
199+
- `--no-merged` is used to find branches which are candidates for merging
200+
into HEAD, since those branches are not fully contained by HEAD.
178201

179202
Author
180203
------

builtin-branch.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "branch.h"
1616

1717
static const char * const builtin_branch_usage[] = {
18-
"git-branch [options] [-r | -a]",
18+
"git-branch [options] [-r | -a] [--merged | --no-merged]",
1919
"git-branch [options] [-l] [-f] <branchname> [<start-point>]",
2020
"git-branch [options] [-r] (-d | -D) <branchname>",
2121
"git-branch [options] (-m | -M) [<oldbranch>] <newbranch>",
@@ -46,6 +46,8 @@ enum color_branch {
4646
COLOR_BRANCH_CURRENT = 4,
4747
};
4848

49+
static int mergefilter = -1;
50+
4951
static int parse_branch_color_slot(const char *var, int ofs)
5052
{
5153
if (!strcasecmp(var+ofs, "plain"))
@@ -210,6 +212,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
210212
struct ref_item *newitem;
211213
int kind = REF_UNKNOWN_TYPE;
212214
int len;
215+
static struct commit_list branch;
213216

214217
/* Detect kind */
215218
if (!prefixcmp(refname, "refs/heads/")) {
@@ -231,6 +234,16 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
231234
if ((kind & ref_list->kinds) == 0)
232235
return 0;
233236

237+
if (mergefilter > -1) {
238+
branch.item = lookup_commit_reference_gently(sha1, 1);
239+
if (!branch.item)
240+
die("Unable to lookup tip of branch %s", refname);
241+
if (mergefilter == 0 && has_commit(head_sha1, &branch))
242+
return 0;
243+
if (mergefilter == 1 && !has_commit(head_sha1, &branch))
244+
return 0;
245+
}
246+
234247
/* Resize buffer */
235248
if (ref_list->index >= ref_list->alloc) {
236249
ref_list->alloc = alloc_nr(ref_list->alloc);
@@ -444,6 +457,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
444457
OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
445458
OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"),
446459
OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"),
460+
OPT_SET_INT(0, "merged", &mergefilter, "list only merged branches", 1),
447461
OPT_END(),
448462
};
449463

t/t3201-branch-contains.sh

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/sh
22

3-
test_description='branch --contains <commit>'
3+
test_description='branch --contains <commit>, --merged, and --no-merged'
44

55
. ./test-lib.sh
66

@@ -55,4 +55,44 @@ test_expect_success 'branch --contains=side' '
5555
5656
'
5757

58+
test_expect_success 'side: branch --merged' '
59+
60+
git branch --merged >actual &&
61+
{
62+
echo " master" &&
63+
echo "* side"
64+
} >expect &&
65+
test_cmp expect actual
66+
67+
'
68+
69+
test_expect_success 'side: branch --no-merged' '
70+
71+
git branch --no-merged >actual &&
72+
>expect &&
73+
test_cmp expect actual
74+
75+
'
76+
77+
test_expect_success 'master: branch --merged' '
78+
79+
git checkout master &&
80+
git branch --merged >actual &&
81+
{
82+
echo "* master"
83+
} >expect &&
84+
test_cmp expect actual
85+
86+
'
87+
88+
test_expect_success 'master: branch --no-merged' '
89+
90+
git branch --no-merged >actual &&
91+
{
92+
echo " side"
93+
} >expect &&
94+
test_cmp expect actual
95+
96+
'
97+
5898
test_done

0 commit comments

Comments
 (0)