Subject: [PATCH] revision: introduce --exclude=<glob> to tame wildcards

From: Junio C Hamano <[email-masked]>
Date: Fri, 30 Aug 2013 16:55:29 -0700

People often find "git log --branches" etc. that includes _all_
branches is cumbersome to use when they want to grab most but except
some.  The same applies to --tags, --all and --glob.

Teach the revision machinery to remember patterns, and then upon the
next such a globbing option, exclude those that match the pattern.

With this, I can view only my integration branches (e.g. maint,
master, etc.) without topic branches, which are named after two
letters from primary authors' names, slash and topic name.

    git rev-list --no-walk --exclude=??/* --branches |
    git name-rev --refs refs/heads/* --stdin

This one shows things reachable from local and remote branches that
have not been merged to the integration branches.

    git log --remotes --branches --not --exclude=??/* --branches

It may be a bit rough around the edges, in that the pattern to give
the exclude option depends on what globbing option follows.  In
these examples, the pattern "??/*" is used, not "refs/heads/??/*",
because the globbing option that follows the -"-exclude=<pattern>"
is "--branches".  As each use of globbing option resets previously
set "--exclude", this may not be such a bad thing, though.

Signed-off-by: Junio C Hamano <[email-masked]>
---

 Junio C Hamano <[email-masked]> writes:

 > It may be a good idea to step back a bit and think of this topic as
 > a way to enhance the --branches option and its friends with only the
 > inclusive wildcard semantics.  It lets us include those that match
 > the pattern with "--branches=wip/*", but there is no way to say "oh
 > by the way, I do not want those that match this pattern included
 > when you expand this short-hand".  We have --branches=pattern that
 > is inclusive; perhaps it can be prefixed with --branches=!pattern to
 > pre-declare "whatever the next --branches expands to, do not include
 > those that match this pattern", or something, which would make the
 > earlier "wip" example to be:
 >
 > 	--all --not --branches='!wip/*' --branches

 So here is a quick attempt at that approach, which does not look
 too intrusive. 

 revision.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 revision.h |  3 +++
 2 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/revision.c b/revision.c
index 84ccc05..3e82874 100644
--- a/revision.c
+++ b/revision.c
@@ -1180,11 +1180,28 @@ struct all_refs_cb {
 	const char *name_for_errormsg;
 };
 
+static int ref_excluded(struct rev_info *revs, const char *path)
+{
+	struct string_list_item *item;
+
+	if (!revs->ref_excludes)
+		return 0;
+	for_each_string_list_item(item, revs->ref_excludes) {
+		if (!fnmatch(item->string, path, 0))
+			return 1;
+	}
+	return 0;
+}
+
 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
 {
 	struct all_refs_cb *cb = cb_data;
-	struct object *object = get_reference(cb->all_revs, path, sha1,
-					      cb->all_flags);
+	struct object *object;
+
+	if (ref_excluded(cb->all_revs, path))
+	    return 0;
+
+	object = get_reference(cb->all_revs, path, sha1, cb->all_flags);
 	add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
 	add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
 	return 0;
@@ -1197,6 +1214,24 @@ static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
 	cb->all_flags = flags;
 }
 
+static void clear_ref_exclusion(struct rev_info *revs)
+{
+	if (revs->ref_excludes) {
+		string_list_clear(revs->ref_excludes, 0);
+		free(revs->ref_excludes);
+	}
+	revs->ref_excludes = NULL;
+}
+
+static void add_ref_exclusion(struct rev_info *revs, const char *exclude)
+{
+	if (!revs->ref_excludes) {
+		revs->ref_excludes = xcalloc(1, sizeof(*revs->ref_excludes));
+		revs->ref_excludes->strdup_strings = 1;
+	}
+	string_list_append(revs->ref_excludes, exclude);
+}
+
 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
 		int (*for_each)(const char *, each_ref_fn, void *))
 {
@@ -1953,33 +1988,44 @@ static int handle_revision_pseudo_opt(const char *submodule,
 	if (!strcmp(arg, "--all")) {
 		handle_refs(submodule, revs, *flags, for_each_ref_submodule);
 		handle_refs(submodule, revs, *flags, head_ref_submodule);
+		clear_ref_exclusion(revs);
 	} else if (!strcmp(arg, "--branches")) {
 		handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
+		clear_ref_exclusion(revs);
 	} else if (!strcmp(arg, "--bisect")) {
 		handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
 		handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
 		revs->bisect = 1;
 	} else if (!strcmp(arg, "--tags")) {
 		handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
+		clear_ref_exclusion(revs);
 	} else if (!strcmp(arg, "--remotes")) {
 		handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
+		clear_ref_exclusion(revs);
 	} else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
 		struct all_refs_cb cb;
 		init_all_refs_cb(&cb, revs, *flags);
 		for_each_glob_ref(handle_one_ref, optarg, &cb);
+		clear_ref_exclusion(revs);
+		return argcount;
+	} else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
+		add_ref_exclusion(revs, optarg);
 		return argcount;
 	} else if (!prefixcmp(arg, "--branches=")) {
 		struct all_refs_cb cb;
 		init_all_refs_cb(&cb, revs, *flags);
 		for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
+		clear_ref_exclusion(revs);
 	} else if (!prefixcmp(arg, "--tags=")) {
 		struct all_refs_cb cb;
 		init_all_refs_cb(&cb, revs, *flags);
 		for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
+		clear_ref_exclusion(revs);
 	} else if (!prefixcmp(arg, "--remotes=")) {
 		struct all_refs_cb cb;
 		init_all_refs_cb(&cb, revs, *flags);
 		for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
+		clear_ref_exclusion(revs);
 	} else if (!strcmp(arg, "--reflog")) {
 		handle_reflog(revs, *flags);
 	} else if (!strcmp(arg, "--not")) {
diff --git a/revision.h b/revision.h
index 95859ba..b4dc56c 100644
--- a/revision.h
+++ b/revision.h
@@ -59,6 +59,9 @@ struct rev_info {
 	/* The end-points specified by the end user */
 	struct rev_cmdline_info cmdline;
 
+	/* excluding from --branches, --refs, etc. expansion */
+	struct string_list *ref_excludes;
+
 	/* Basic information */
 	const char *prefix;
 	const char *def;
-- 
1.8.4-358-g02cbb92