Skip to contentSkip to conversation

git-branch: add support for --merged and --unmerged

PATCH In review Lars Hjemli 3 participants 8 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 git-branch: add support for --merged and --unmerged

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 --unmerged -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.

---
 Documentation/git-branch.txt |    4 +++-
 builtin-branch.c             |   29 ++++++++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 2 deletions(-)
Documentation/git-branch.txt +3 -1
index 6f07a17..2693519 100644
@@ -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 | --unmerged]
 	   [-v [--abbrev=<length> | --no-abbrev]]
 	   [--contains <commit>]
 'git-branch' [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
@@ -24,6 +24,8 @@ and option `-a` shows both.
 With `--contains <commit>`, 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 `--unmerged` only branches not merged into HEAD will be listed.
 
 In its second form, a new branch named <branchname> will be created.
 It will start out with a head equal to the one given as <start-point>.
builtin-branch.c +28 -1
index 5bc4526..7c91ff0 100644
@@ -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 | --unmerged]",
 	"git-branch [options] [-l] [-f] <branchname> [<start-point>]",
 	"git-branch [options] [-r] (-d | -D) <branchname>",
 	"git-branch [options] (-m | -M) [<oldbranch>] <newbranch>",
@@ -30,6 +30,8 @@ static const char * const builtin_branch_usage[] = {
 static const char *head;
 static unsigned char head_sha1[20];
 
+static int merged, unmerged;
+
 static int branch_use_color = -1;
 static char branch_colors[][COLOR_MAXLEN] = {
 	"\033[m",	/* reset */
@@ -204,6 +206,22 @@ static int has_commit(const unsigned char *sha1, struct commit_list *with_commit
 	return 0;
 }
 
+static int is_merged(const unsigned char *sha1, const char *refname)
+{
+	static struct commit *head_commit;
+	struct commit *branch;
+
+	if (!head_commit) {
+		head_commit = lookup_commit_reference(head_sha1);
+		if (!head_commit)
+			die("Unable to lookup HEAD");
+	}
+	branch = lookup_commit_reference(sha1);
+	if (!branch)
+		die("Unable to lookup branch %s", refname);
+	return in_merge_bases(branch, &head_commit, 1);
+}
+
 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 {
 	struct ref_list *ref_list = (struct ref_list*)(cb_data);
@@ -231,6 +249,12 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	if ((kind & ref_list->kinds) == 0)
 		return 0;
 
+	if (unmerged == 1 && is_merged(sha1, refname))
+		return 0;
+
+	if (merged == 1 && !is_merged(sha1, refname))
+		return 0;
+
 	/* Resize buffer */
 	if (ref_list->index >= ref_list->alloc) {
 		ref_list->alloc = alloc_nr(ref_list->alloc);
@@ -444,6 +468,9 @@ 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_BIT(0, "unmerged", &unmerged, "list only unmerged branches", 1),
+		OPT_BIT(0, "merged", &merged, "list only merged branches", 1),
+
 		OPT_END(),
 	};
 
Signature · 1 line
1.5.3.1.1.g1e61
#1 lore raw

PATCH 1/1 bash: support branch's new '--merged' and '--unmerged' options

---

Quoted 2 lines

These options filter the output from git branch to only include branches whose tip is either merged or not merged into HEAD.

I really like the idea, thanks!
How about making it easier to type?

 contrib/completion/git-completion.bash |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
contrib/completion/git-completion.bash +1 -1
index 4d81963..c44f9f4 100755
@@ -543,7 +543,7 @@ _git_branch ()
 	--*)
 		__gitcomp "
 			--color --no-color --verbose --abbrev= --no-abbrev
-			--track --no-track
+			--track --no-track --merged --unmerged
 			"
 		;;
 	*)
Signature · 1 line
1.5.5.90.g17e355
#2 lore raw

PATCH 1/1 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.

---

This is an alternative implementation which (ab)uses the option parser to achive the same effect as the former patch. I prefer this approach.

Quoted 1 line · SZEDER Gábor

How about making it easier to type?

Yes, that's nice, thank you. If my latter patch (i.e. this one ;) is accepted, would you mind updating your patch?

 Documentation/git-branch.txt |    4 +++-
 builtin-branch.c             |   27 ++++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 2 deletions(-)
Documentation/git-branch.txt +3 -1
index 6f07a17..95e9d0d 100644
@@ -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=<length> | --no-abbrev]]
 	   [--contains <commit>]
 'git-branch' [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
@@ -24,6 +24,8 @@ and option `-a` shows both.
 With `--contains <commit>`, 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 <branchname> will be created.
 It will start out with a head equal to the one given as <start-point>.
builtin-branch.c +26 -1
index 5bc4526..2c26d90 100644
@@ -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] <branchname> [<start-point>]",
 	"git-branch [options] [-r] (-d | -D) <branchname>",
 	"git-branch [options] (-m | -M) [<oldbranch>] <newbranch>",
@@ -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"))
@@ -204,6 +206,22 @@ static int has_commit(const unsigned char *sha1, struct commit_list *with_commit
 	return 0;
 }
 
+static int is_merged(const unsigned char *sha1, const char *refname)
+{
+	static struct commit *head_commit;
+	struct commit *branch;
+
+	if (!head_commit) {
+		head_commit = lookup_commit_reference(head_sha1);
+		if (!head_commit)
+			die("Unable to lookup HEAD");
+	}
+	branch = lookup_commit_reference(sha1);
+	if (!branch)
+		die("Unable to lookup branch %s", refname);
+	return in_merge_bases(branch, &head_commit, 1);
+}
+
 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
 {
 	struct ref_list *ref_list = (struct ref_list*)(cb_data);
@@ -231,6 +249,12 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	if ((kind & ref_list->kinds) == 0)
 		return 0;
 
+	if (mergefilter == 0 && is_merged(sha1, refname))
+		return 0;
+
+	if (mergefilter == 1 && !is_merged(sha1, refname))
+		return 0;
+
 	/* Resize buffer */
 	if (ref_list->index >= ref_list->alloc) {
 		ref_list->alloc = alloc_nr(ref_list->alloc);
@@ -444,6 +468,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(),
 	};
 
Signature · 1 line
1.5.5.64.g3e6a
#3 lore raw
In reply to Lars Hjemli

How does --merged compare with existing --contains?

If the existing --contains implemenation can be extended to allow negative selection, we do not have to introduce yet another mechanism that is very similar.

#4 lore raw

PATCH v3 1/1 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.

---

Quoted 3 lines · Junio C Hamano

If the existing --contains implemenation can be extended to allow negative selection, we do not have to introduce yet another mechanism that is very similar.

Very true, so here's an updated version which reuses has_commit(). Thanks for the feedback.

 Documentation/git-branch.txt |    4 +++-
 builtin-branch.c             |   16 +++++++++++++++-
 2 files changed, 18 insertions(+), 2 deletions(-)
Documentation/git-branch.txt +3 -1
index 6f07a17..95e9d0d 100644
@@ -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=<length> | --no-abbrev]]
 	   [--contains <commit>]
 'git-branch' [--track | --no-track] [-l] [-f] <branchname> [<start-point>]
@@ -24,6 +24,8 @@ and option `-a` shows both.
 With `--contains <commit>`, 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 <branchname> will be created.
 It will start out with a head equal to the one given as <start-point>.
builtin-branch.c +15 -1
index 5bc4526..eecbcf2 100644
@@ -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] <branchname> [<start-point>]",
 	"git-branch [options] [-r] (-d | -D) <branchname>",
 	"git-branch [options] (-m | -M) [<oldbranch>] <newbranch>",
@@ -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 HEAD 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(),
 	};
 
Signature · 1 line
1.5.5.64.gb1a99
#5 lore raw

Re: [PATCH v3] git-branch: add support for --merged and --no-merged

Quoted 8 lines · Lars Hjemli
Quoted 3 lines · Junio C Hamano

If the existing --contains implemenation can be extended to allow negative selection, we do not have to introduce yet another mechanism that is very similar.

Very true, so here's an updated version which reuses has_commit(). Thanks for the feedback.

builtin-branch.c static int append_ref(const char *refname, const unsigned char *sha1, int flags, · line 245

Quoted 9 lines
builtin-branch.c +9 -0
+	if (mergefilter > -1) {
+		branch.item = lookup_commit_reference_gently(sha1, 1);
+		if (!branch.item)
+			die("Unable to lookup HEAD of branch %s", refname);
+		if (mergefilter == 0 && has_commit(head_sha1, &branch))
+			return 0;
+		if (mergefilter == 1 && !has_commit(head_sha1, &branch))
+			return 0;
+	}

You did not answer my question as to how --contains and --merged relate to each other. I'll answer it myself (please add a documentation updates, as this would be confusing). The output from the former is "branches I need to be careful about, if I were to rewind and rebuild this branch" (that is what I invented the option for). The output from the latter is "branches I can remove with 'git branch -d'".

* --contains (aka with_commit) gets a single commit; only the branches that are descendant of that named commit will be shown (iow, these branches contain the commit), with the expression:

	/* Filter with with_commit if specified */
	if (!has_commit(sha1, ref_list->with_commit))
		return 0;

where "sha1" is the commit at the tip of the branch in question in this round of the loop.

* --merged is the opposite way. Only the branches whose tips are ancestors of the HEAD will be shown (iow, the HEAD contains these branches), with the expression:

if (has_commit(head_sha1, &branch))

where "head_sha1" is the HEAD, and "branch" is the branch in question in this round of the loop. i.e. it asks the same question as --contains but in the opposite way.

#6 lore raw

PATCH 1/1 git-branch.txt: compare --contains, --merged and --no-merged

---

Quoted 2 lines · Junio C Hamano

You did not answer my question as to how --contains and --merged relate to each other.

Sorry, I deemed it to be rhetorical.

Quoted 2 lines

I'll answer it myself (please add a documentation updates, as this would be confusing).

Something like this, maybe?

 Documentation/git-branch.txt |   18 ++++++++++++++++++
 1 files changed, 18 insertions(+), 0 deletions(-)
Documentation/git-branch.txt +18 -0
index 95e9d0d..7e37497 100644
@@ -120,6 +120,15 @@ OPTIONS
 --no-track::
 	Ignore the branch.autosetupmerge configuration variable.
 
+--contains <commit>::
+	Only list branches which contain the specified commit.
+
+--merged::
+	Only list branches which are fully contained by HEAD.
+
+--no-merged::
+	Do not list branches which are fully contained by HEAD.
+
 <branchname>::
 	The name of the branch to create or delete.
 	The new branch name must pass all checks defined by
@@ -177,6 +186,15 @@ If you are creating a branch that you want to immediately checkout, it's
 easier to use the git checkout command with its `-b` option to create
 a branch and check it out with a single command.
 
+The options `--contains`, `--merged` and `--no-merged` serves three related
+but different purposes:
+<1> `--contains <commit>` is used to find all branches which will need
+special attention if <commit> were to be rebased or amended, since those
+branches contain the specified <commit>.
+<2> `--merged` is used to find all branches which can be safely deleted,
+since those branches are fully contained by HEAD.
+<3> `--no-merged` is used to find branches which are candidates for merging
+into HEAD, since those branches are not fully contained by HEAD.
 
 Author
 ------
Signature · 1 line
1.5.5.64.gb1a99
#7 lore raw

Re: [PATCH v3] git-branch: add support for --merged and --no-merged

In reply to Lars Hjemli
Quoted 2 lines · Lars Hjemli

These options filter the output from git branch to only include branches
whose tip is either merged or not merged into HEAD.

Gaah, I just noticed a slight terminology slip. Could you please apply this on top of [PATCH v3]?

(hopefully not mangled by gmail...)

---

builtin-branch.c +1 -1
index eecbcf2..19c508a 100644
@@ -237,7 +237,7 @@ static int append_ref(const char *refname
        if (mergefilter > -1) {
                branch.item = lookup_commit_reference_gently(sha1, 1);
                if (!branch.item)
-                       die("Unable to lookup HEAD of branch %s", refname);
+                       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))