* [PATCH] git-branch: add support for --merged and --unmerged
@ 2008-04-17 9:37 Lars Hjemli
2008-04-17 11:34 ` [PATCH] bash: support branch's new '--merged' and '--unmerged' options SZEDER Gábor
2008-04-17 19:13 ` [PATCH] git-branch: add support for --merged and --unmerged Junio C Hamano
0 siblings, 2 replies; 8+ messages in thread
From: Lars Hjemli @ 2008-04-17 9:37 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH] bash: support branch's new '--merged' and '--unmerged' options
2008-04-17 9:37 [PATCH] git-branch: add support for --merged and --unmerged Lars Hjemli
@ 2008-04-17 11:34 ` SZEDER Gábor
2008-04-17 18:07 ` [PATCH] git-branch: add support for --merged and --no-merged Lars Hjemli
2008-04-17 19:13 ` [PATCH] git-branch: add support for --merged and --unmerged Junio C Hamano
1 sibling, 1 reply; 8+ messages in thread
From: SZEDER Gábor @ 2008-04-17 11:34 UTC (permalink / raw)
To: Lars Hjemli; +Cc: git, SZEDER Gábor
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH] git-branch: add support for --merged and --no-merged
2008-04-17 11:34 ` [PATCH] bash: support branch's new '--merged' and '--unmerged' options SZEDER Gábor
@ 2008-04-17 18:07 ` Lars Hjemli
0 siblings, 0 replies; 8+ messages in thread
From: Lars Hjemli @ 2008-04-17 18:07 UTC (permalink / raw)
To: SZEDER G�bor, Junio C Hamano; +Cc: git
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH] git-branch: add support for --merged and --unmerged
2008-04-17 9:37 [PATCH] git-branch: add support for --merged and --unmerged Lars Hjemli
2008-04-17 11:34 ` [PATCH] bash: support branch's new '--merged' and '--unmerged' options SZEDER Gábor
@ 2008-04-17 19:13 ` Junio C Hamano
2008-04-17 20:24 ` [PATCH v3] git-branch: add support for --merged and --no-merged Lars Hjemli
1 sibling, 1 reply; 8+ messages in thread
From: Junio C Hamano @ 2008-04-17 19:13 UTC (permalink / raw)
To: Lars Hjemli; +Cc: git
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.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3] git-branch: add support for --merged and --no-merged
2008-04-17 19:13 ` [PATCH] git-branch: add support for --merged and --unmerged Junio C Hamano
@ 2008-04-17 20:24 ` Lars Hjemli
2008-04-17 21:07 ` Junio C Hamano
2008-04-17 22:59 ` [PATCH v3] git-branch: add support for " Lars Hjemli
0 siblings, 2 replies; 8+ messages in thread
From: Lars Hjemli @ 2008-04-17 20:24 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3] git-branch: add support for --merged and --no-merged
2008-04-17 20:24 ` [PATCH v3] git-branch: add support for --merged and --no-merged Lars Hjemli
@ 2008-04-17 21:07 ` Junio C Hamano
2008-04-17 22:27 ` [PATCH] git-branch.txt: compare --contains, " Lars Hjemli
2008-04-17 22:59 ` [PATCH v3] git-branch: add support for " Lars Hjemli
1 sibling, 1 reply; 8+ messages in thread
From: Junio C Hamano @ 2008-04-17 21:07 UTC (permalink / raw)
To: Lars Hjemli; +Cc: git
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.
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH] git-branch.txt: compare --contains, --merged and --no-merged
2008-04-17 21:07 ` Junio C Hamano
@ 2008-04-17 22:27 ` Lars Hjemli
0 siblings, 0 replies; 8+ messages in thread
From: Lars Hjemli @ 2008-04-17 22:27 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH v3] git-branch: add support for --merged and --no-merged
2008-04-17 20:24 ` [PATCH v3] git-branch: add support for --merged and --no-merged Lars Hjemli
2008-04-17 21:07 ` Junio C Hamano
@ 2008-04-17 22:59 ` Lars Hjemli
1 sibling, 0 replies; 8+ messages in thread
From: Lars Hjemli @ 2008-04-17 22:59 UTC (permalink / raw)
To: Junio C Hamano; +Cc: git
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))
^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2008-04-17 23:00 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-04-17 9:37 [PATCH] git-branch: add support for --merged and --unmerged Lars Hjemli
2008-04-17 11:34 ` [PATCH] bash: support branch's new '--merged' and '--unmerged' options SZEDER Gábor
2008-04-17 18:07 ` [PATCH] git-branch: add support for --merged and --no-merged Lars Hjemli
2008-04-17 19:13 ` [PATCH] git-branch: add support for --merged and --unmerged Junio C Hamano
2008-04-17 20:24 ` [PATCH v3] git-branch: add support for --merged and --no-merged Lars Hjemli
2008-04-17 21:07 ` Junio C Hamano
2008-04-17 22:27 ` [PATCH] git-branch.txt: compare --contains, " Lars Hjemli
2008-04-17 22:59 ` [PATCH v3] git-branch: add support for " Lars Hjemli
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).