diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt index 6f07a17a2c..95e9d0d0be 100644 --- a/Documentation/git-branch.txt +++ b/Documentation/git-branch.txt @@ -8,7 +8,7 @@ git-branch - List, create, or delete branches SYNOPSIS -------- [verse] -'git-branch' [--color | --no-color] [-r | -a] +'git-branch' [--color | --no-color] [-r | -a] [--merged | --no-merged] [-v [--abbrev= | --no-abbrev]] [--contains ] 'git-branch' [--track | --no-track] [-l] [-f] [] @@ -24,6 +24,8 @@ and option `-a` shows both. With `--contains `, shows only the branches that contains the named commit (in other words, the branches whose tip commits are descendant of the named commit). +With `--merged`, only branches merged into HEAD will be listed, and +with `--no-merged` only branches not merged into HEAD will be listed. In its second form, a new branch named will be created. It will start out with a head equal to the one given as . diff --git a/builtin-branch.c b/builtin-branch.c index 5bc4526f64..19c508a608 100644 --- a/builtin-branch.c +++ b/builtin-branch.c @@ -15,7 +15,7 @@ #include "branch.h" static const char * const builtin_branch_usage[] = { - "git-branch [options] [-r | -a]", + "git-branch [options] [-r | -a] [--merged | --no-merged]", "git-branch [options] [-l] [-f] []", "git-branch [options] [-r] (-d | -D) ", "git-branch [options] (-m | -M) [] ", @@ -46,6 +46,8 @@ enum color_branch { COLOR_BRANCH_CURRENT = 4, }; +static int mergefilter = -1; + static int parse_branch_color_slot(const char *var, int ofs) { if (!strcasecmp(var+ofs, "plain")) @@ -210,6 +212,7 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags, struct ref_item *newitem; int kind = REF_UNKNOWN_TYPE; int len; + static struct commit_list branch; /* Detect kind */ if (!prefixcmp(refname, "refs/heads/")) { @@ -231,6 +234,16 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags, if ((kind & ref_list->kinds) == 0) return 0; + if (mergefilter > -1) { + branch.item = lookup_commit_reference_gently(sha1, 1); + if (!branch.item) + die("Unable to lookup tip of branch %s", refname); + if (mergefilter == 0 && has_commit(head_sha1, &branch)) + return 0; + if (mergefilter == 1 && !has_commit(head_sha1, &branch)) + return 0; + } + /* Resize buffer */ if (ref_list->index >= ref_list->alloc) { ref_list->alloc = alloc_nr(ref_list->alloc); @@ -444,6 +457,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix) OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2), OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"), OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"), + OPT_SET_INT(0, "merged", &mergefilter, "list only merged branches", 1), OPT_END(), };