From: Lars Hjemli <hjemli@gmail.com>
To: "SZEDER G�bor" <szeder@ira.uka.de>, "Junio C Hamano" <gitster@pobox.com>
Cc: git@vger.kernel.org
Subject: [PATCH] git-branch: add support for --merged and --no-merged
Date: Thu, 17 Apr 2008 20:07:17 +0200 [thread overview]
Message-ID: <1208455637-17457-1-git-send-email-hjemli@gmail.com> (raw)
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
next prev parent reply other threads:[~2008-04-17 18:07 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` Lars Hjemli [this message]
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1208455637-17457-1-git-send-email-hjemli@gmail.com \
--to=hjemli@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=szeder@ira.uka.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).