Skip to content

Commit e7b432c

Browse files
committed
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 <[email protected]>
1 parent e230c56 commit e7b432c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

revision.c

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,11 +1180,28 @@ struct all_refs_cb {
11801180
const char *name_for_errormsg;
11811181
};
11821182

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+
11831196
static int handle_one_ref(const char *path, const unsigned char *sha1, int flag, void *cb_data)
11841197
{
11851198
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);
11881205
add_rev_cmdline(cb->all_revs, object, path, REV_CMD_REF, cb->all_flags);
11891206
add_pending_sha1(cb->all_revs, path, sha1, cb->all_flags);
11901207
return 0;
@@ -1197,6 +1214,24 @@ static void init_all_refs_cb(struct all_refs_cb *cb, struct rev_info *revs,
11971214
cb->all_flags = flags;
11981215
}
11991216

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+
12001235
static void handle_refs(const char *submodule, struct rev_info *revs, unsigned flags,
12011236
int (*for_each)(const char *, each_ref_fn, void *))
12021237
{
@@ -1953,33 +1988,44 @@ static int handle_revision_pseudo_opt(const char *submodule,
19531988
if (!strcmp(arg, "--all")) {
19541989
handle_refs(submodule, revs, *flags, for_each_ref_submodule);
19551990
handle_refs(submodule, revs, *flags, head_ref_submodule);
1991+
clear_ref_exclusion(revs);
19561992
} else if (!strcmp(arg, "--branches")) {
19571993
handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1994+
clear_ref_exclusion(revs);
19581995
} else if (!strcmp(arg, "--bisect")) {
19591996
handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
19601997
handle_refs(submodule, revs, *flags ^ (UNINTERESTING | BOTTOM), for_each_good_bisect_ref);
19611998
revs->bisect = 1;
19621999
} else if (!strcmp(arg, "--tags")) {
19632000
handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
2001+
clear_ref_exclusion(revs);
19642002
} else if (!strcmp(arg, "--remotes")) {
19652003
handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
2004+
clear_ref_exclusion(revs);
19662005
} else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
19672006
struct all_refs_cb cb;
19682007
init_all_refs_cb(&cb, revs, *flags);
19692008
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);
19702013
return argcount;
19712014
} else if (!prefixcmp(arg, "--branches=")) {
19722015
struct all_refs_cb cb;
19732016
init_all_refs_cb(&cb, revs, *flags);
19742017
for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
2018+
clear_ref_exclusion(revs);
19752019
} else if (!prefixcmp(arg, "--tags=")) {
19762020
struct all_refs_cb cb;
19772021
init_all_refs_cb(&cb, revs, *flags);
19782022
for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
2023+
clear_ref_exclusion(revs);
19792024
} else if (!prefixcmp(arg, "--remotes=")) {
19802025
struct all_refs_cb cb;
19812026
init_all_refs_cb(&cb, revs, *flags);
19822027
for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
2028+
clear_ref_exclusion(revs);
19832029
} else if (!strcmp(arg, "--reflog")) {
19842030
handle_reflog(revs, *flags);
19852031
} else if (!strcmp(arg, "--not")) {

revision.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ struct rev_info {
5959
/* The end-points specified by the end user */
6060
struct rev_cmdline_info cmdline;
6161

62+
/* excluding from --branches, --refs, etc. expansion */
63+
struct string_list *ref_excludes;
64+
6265
/* Basic information */
6366
const char *prefix;
6467
const char *def;

0 commit comments

Comments
 (0)