Skip to content

Commit e8b404c

Browse files
hjemligitster
authored andcommitted
git-branch: add support for --merged and --no-merged
These options filter the output from git branch to only include branches whose tip is either merged or not merged into HEAD. The use-case for these options is when working with integration of branches from many remotes: `git branch --no-merged -a` will show a nice list of merge candidates while `git branch --merged -a` will show the progress of your integration work. Also, a plain `git branch --merged` is a quick way to find local branches which you might want to delete. Signed-off-by: Lars Hjemli <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 5909ca9 commit e8b404c

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Documentation/git-branch.txt

Lines changed: 3 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>.

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

0 commit comments

Comments
 (0)