revision: introduce --exclude=<glob> to tame wildcards

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 <gitster@pobox.com>

Junio C Hamano committed Aug 30, 2013 at 16:37 UTC e7b432c521b0177e86eaecd2bd16906b9fc53e10
2 files changed +51 -2
revision.c
+48 -2
@@ -1180,11 +1180,28 @@ struct all_refs_cb {
1180 const char *name_for_errormsg;
1181 };
1182
1183 +static int ref_excluded(struct rev_info *revs, const char *path)
1184 +{
1185 + struct string_list_item *item;
1186 +
1187 + if (!revs->ref_excludes)
1188 + return 0;
1189 + for_each_string_list_item(item, revs->ref_excludes) {
1190 + if (!fnmatch(item->string, path, 0))
1191 + return 1;
1192 + }
1193 + return 0;
1194 +}
1195 +
1196 static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
1197 {
1198 struct all_refs_cb *cb = cb_data;
1186 - struct object *object = get_reference(cb->all_revs, path, sha1,
1187 - cb->all_flags);
1199 + struct object *object;
1200 +
1201 + if (ref_excluded(cb->all_revs, path))
1202 + return 0;
1203 +
1204 + object = get_reference(cb->all_revs, path, sha1, cb->all_flags);
1205 add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
1206 add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
1207 return 0;
@@ -1197,6 +1214,24 @@ static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
1214 cb->all_flags = flags;
1215 }
1216
1217 +static void clear_ref_exclusion(struct rev_info *revs)
1218 +{
1219 + if (revs->ref_excludes) {
1220 + string_list_clear(revs->ref_excludes, 0);
1221 + free(revs->ref_excludes);
1222 + }
1223 + revs->ref_excludes = NULL;
1224 +}
1225 +
1226 +static void add_ref_exclusion(struct rev_info *revs, const char *exclude)
1227 +{
1228 + if (!revs->ref_excludes) {
1229 + revs->ref_excludes = xcalloc(1, sizeof(*revs->ref_excludes));
1230 + revs->ref_excludes->strdup_strings = 1;
1231 + }
1232 + string_list_append(revs->ref_excludes, exclude);
1233 +}
1234 +
1235 static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
1236 int (*for_each)(const char *, each_ref_fn, void *))
1237 {
@@ -1953,33 +1988,44 @@ static int handle_revision_pseudo_opt(const char *submodule,
1988 if (!strcmp(arg, "--all")) {
1989 handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1990 handle_refs(submodule, revs, *flags, head_ref_submodule);
1991 + clear_ref_exclusion(revs);
1992 } else if (!strcmp(arg, "--branches")) {
1993 handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1994 + clear_ref_exclusion(revs);
1995 } else if (!strcmp(arg, "--bisect")) {
1996 handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1997 handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
1998 revs->bisect = 1;
1999 } else if (!strcmp(arg, "--tags")) {
2000 handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2001 + clear_ref_exclusion(revs);
2002 } else if (!strcmp(arg, "--remotes")) {
2003 handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2004 + clear_ref_exclusion(revs);
2005 } else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
2006 struct all_refs_cb cb;
2007 init_all_refs_cb(&cb, revs, *flags);
2008 for_each_glob_ref(handle_one_ref, optarg, &cb);
2009 + clear_ref_exclusion(revs);
2010 + return argcount;
2011 + } else if ((argcount = parse_long_opt("exclude", argv, &optarg))) {
2012 + add_ref_exclusion(revs, optarg);
2013 return argcount;
2014 } else if (!prefixcmp(arg, "--branches=")) {
2015 struct all_refs_cb cb;
2016 init_all_refs_cb(&cb, revs, *flags);
2017 for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2018 + clear_ref_exclusion(revs);
2019 } else if (!prefixcmp(arg, "--tags=")) {
2020 struct all_refs_cb cb;
2021 init_all_refs_cb(&cb, revs, *flags);
2022 for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2023 + clear_ref_exclusion(revs);
2024 } else if (!prefixcmp(arg, "--remotes=")) {
2025 struct all_refs_cb cb;
2026 init_all_refs_cb(&cb, revs, *flags);
2027 for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2028 + clear_ref_exclusion(revs);
2029 } else if (!strcmp(arg, "--reflog")) {
2030 handle_reflog(revs, *flags);
2031 } else if (!strcmp(arg, "--not")) {
revision.h
+3
@@ -59,6 +59,9 @@ struct rev_info {
59 /* The end-points specified by the end user */
60 struct rev_cmdline_info cmdline;
61
62 + /* excluding from --branches, --refs, etc. expansion */
63 + struct string_list *ref_excludes;
64 +
65 /* Basic information */
66 const char *prefix;
67 const char *def;