# [PATCH] git-branch: add support for --merged and --unmerged

8 messages from 2008-04-17 to 2008-04-17. Participants: Lars Hjemli, SZEDER Gábor, Junio C Hamano.
Thread: https://gitlist.dev/t/13150

## Lars Hjemli, 2008-04-17 09:37

Subject: [PATCH] git-branch: add support for --merged and --unmerged
Message-ID: <12084250231245-git-send-email-hjemli@gmail.com>
URL: https://gitlist.dev/e/12084250231245-git-send-email-hjemli%40gmail.com

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

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---
 Documentation/git-branch.txt |    4 +++-
 builtin-branch.c             |   29 ++++++++++++++++++++++++++++-
 2 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 6f07a17..2693519 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 | --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>.
diff --git a/builtin-branch.c b/builtin-branch.c
index 5bc4526..7c91ff0 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 | --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(),
 	};
 
-- 
1.5.3.1.1.g1e61

```

## SZEDER Gábor, 2008-04-17 11:34

Subject: [PATCH] bash: support branch's new '--merged' and '--unmerged' options
Message-ID: <1208432040-26271-1-git-send-email-szeder@ira.uka.de>
URL: https://gitlist.dev/e/1208432040-26271-1-git-send-email-szeder%40ira.uka.de
In-Reply-To: <12084250231245-git-send-email-hjemli@gmail.com>

```
Signed-off-by: SZEDER Gábor <szeder@ira.uka.de>
---
> 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(-)

diff --git a/contrib/completion/git-completion.bash b/contrib/completion/git-completion.bash
index 4d81963..c44f9f4 100755
--- a/contrib/completion/git-completion.bash
+++ b/contrib/completion/git-completion.bash
@@ -543,7 +543,7 @@ _git_branch ()
 	--*)
 		__gitcomp "
 			--color --no-color --verbose --abbrev= --no-abbrev
-			--track --no-track
+			--track --no-track --merged --unmerged
 			"
 		;;
 	*)
-- 
1.5.5.90.g17e355

```

## Lars Hjemli, 2008-04-17 18:07

Subject: [PATCH] git-branch: add support for --merged and --no-merged
Message-ID: <1208455637-17457-1-git-send-email-hjemli@gmail.com>
URL: https://gitlist.dev/e/1208455637-17457-1-git-send-email-hjemli%40gmail.com
In-Reply-To: <1208432040-26271-1-git-send-email-szeder@ira.uka.de>

```
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 <hjemli@gmail.com>
---

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

SZEDER GÃ¡bor <szeder@ira.uka.de> wrote:
> 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(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 6f07a17..95e9d0d 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=<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>.
diff --git a/builtin-branch.c b/builtin-branch.c
index 5bc4526..2c26d90 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] <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(),
 	};
 
-- 
1.5.5.64.g3e6a

```

## Junio C Hamano, 2008-04-17 19:13

Subject: Re: [PATCH] git-branch: add support for --merged and --unmerged
Message-ID: <7vr6d4nvkm.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vr6d4nvkm.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <12084250231245-git-send-email-hjemli@gmail.com>

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

```

## Lars Hjemli, 2008-04-17 20:24

Subject: [PATCH v3] git-branch: add support for --merged and --no-merged
Message-ID: <1208463890-2870-1-git-send-email-hjemli@gmail.com>
URL: https://gitlist.dev/e/1208463890-2870-1-git-send-email-hjemli%40gmail.com
In-Reply-To: <7vr6d4nvkm.fsf@gitster.siamese.dyndns.org>

```
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 <hjemli@gmail.com>
---

Junio C Hamano wrote:
> 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(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 6f07a17..95e9d0d 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=<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>.
diff --git a/builtin-branch.c b/builtin-branch.c
index 5bc4526..eecbcf2 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] <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(),
 	};
 
-- 
1.5.5.64.gb1a99

```

## Junio C Hamano, 2008-04-17 21:07

Subject: Re: [PATCH v3] git-branch: add support for --merged and --no-merged
Message-ID: <7vzlrsmbqw.fsf@gitster.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vzlrsmbqw.fsf%40gitster.siamese.dyndns.org
In-Reply-To: <1208463890-2870-1-git-send-email-hjemli@gmail.com>

```
Lars Hjemli <hjemli@gmail.com> writes:

> Junio C Hamano wrote:
>> 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.

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

```

## Lars Hjemli, 2008-04-17 22:27

Subject: [PATCH] git-branch.txt: compare --contains, --merged and --no-merged
Message-ID: <1208471228-26232-1-git-send-email-hjemli@gmail.com>
URL: https://gitlist.dev/e/1208471228-26232-1-git-send-email-hjemli%40gmail.com
In-Reply-To: <7vzlrsmbqw.fsf@gitster.siamese.dyndns.org>

```
Signed-off-by: Lars Hjemli <hjemli@gmail.com>
---

Junio C Hamano wrote:
> You did not answer my question as to how --contains and --merged relate to
> each other.

Sorry, I deemed it to be rhetorical.


> 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(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 95e9d0d..7e37497 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -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
 ------
-- 
1.5.5.64.gb1a99

```

## Lars Hjemli, 2008-04-17 22:59

Subject: Re: [PATCH v3] git-branch: add support for --merged and --no-merged
Message-ID: <8c5c35580804171559u87f70b6j59d327ca514da714@mail.gmail.com>
URL: https://gitlist.dev/e/8c5c35580804171559u87f70b6j59d327ca514da714%40mail.gmail.com
In-Reply-To: <1208463890-2870-1-git-send-email-hjemli@gmail.com>

```
On Thu, Apr 17, 2008 at 10:24 PM, Lars Hjemli <hjemli@gmail.com> wrote:
> 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...)

---
diff --git a/builtin-branch.c b/builtin-branch.c
index eecbcf2..19c508a 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -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))

```
