Skip to content

Commit 3e1a363

Browse files
committed
Merge branch 'jn/setup-revisions-glob-and-friends-passthru'
* jn/setup-revisions-glob-and-friends-passthru: revisions: allow --glob and friends in parse_options-enabled commands revisions: split out handle_revision_pseudo_opt function
2 parents 2f3e3f5 + 0fc63ec commit 3e1a363

File tree

2 files changed

+122
-65
lines changed

2 files changed

+122
-65
lines changed

revision.c

Lines changed: 72 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,7 +1180,9 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
11801180
!strcmp(arg, "--tags") || !strcmp(arg, "--remotes") ||
11811181
!strcmp(arg, "--reflog") || !strcmp(arg, "--not") ||
11821182
!strcmp(arg, "--no-walk") || !strcmp(arg, "--do-walk") ||
1183-
!strcmp(arg, "--bisect"))
1183+
!strcmp(arg, "--bisect") || !prefixcmp(arg, "--glob=") ||
1184+
!prefixcmp(arg, "--branches=") || !prefixcmp(arg, "--tags=") ||
1185+
!prefixcmp(arg, "--remotes="))
11841186
{
11851187
unkv[(*unkc)++] = arg;
11861188
return 1;
@@ -1535,6 +1537,69 @@ static void append_prune_data(const char ***prune_data, const char **av)
15351537
*prune_data = prune;
15361538
}
15371539

1540+
static int handle_revision_pseudo_opt(const char *submodule,
1541+
struct rev_info *revs,
1542+
int argc, const char **argv, int *flags)
1543+
{
1544+
const char *arg = argv[0];
1545+
const char *optarg;
1546+
int argcount;
1547+
1548+
/*
1549+
* NOTE!
1550+
*
1551+
* Commands like "git shortlog" will not accept the options below
1552+
* unless parse_revision_opt queues them (as opposed to erroring
1553+
* out).
1554+
*
1555+
* When implementing your new pseudo-option, remember to
1556+
* register it in the list at the top of handle_revision_opt.
1557+
*/
1558+
if (!strcmp(arg, "--all")) {
1559+
handle_refs(submodule, revs, *flags, for_each_ref_submodule);
1560+
handle_refs(submodule, revs, *flags, head_ref_submodule);
1561+
} else if (!strcmp(arg, "--branches")) {
1562+
handle_refs(submodule, revs, *flags, for_each_branch_ref_submodule);
1563+
} else if (!strcmp(arg, "--bisect")) {
1564+
handle_refs(submodule, revs, *flags, for_each_bad_bisect_ref);
1565+
handle_refs(submodule, revs, *flags ^ UNINTERESTING, for_each_good_bisect_ref);
1566+
revs->bisect = 1;
1567+
} else if (!strcmp(arg, "--tags")) {
1568+
handle_refs(submodule, revs, *flags, for_each_tag_ref_submodule);
1569+
} else if (!strcmp(arg, "--remotes")) {
1570+
handle_refs(submodule, revs, *flags, for_each_remote_ref_submodule);
1571+
} else if ((argcount = parse_long_opt("glob", argv, &optarg))) {
1572+
struct all_refs_cb cb;
1573+
init_all_refs_cb(&cb, revs, *flags);
1574+
for_each_glob_ref(handle_one_ref, optarg, &cb);
1575+
return argcount;
1576+
} else if (!prefixcmp(arg, "--branches=")) {
1577+
struct all_refs_cb cb;
1578+
init_all_refs_cb(&cb, revs, *flags);
1579+
for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1580+
} else if (!prefixcmp(arg, "--tags=")) {
1581+
struct all_refs_cb cb;
1582+
init_all_refs_cb(&cb, revs, *flags);
1583+
for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1584+
} else if (!prefixcmp(arg, "--remotes=")) {
1585+
struct all_refs_cb cb;
1586+
init_all_refs_cb(&cb, revs, *flags);
1587+
for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1588+
} else if (!strcmp(arg, "--reflog")) {
1589+
handle_reflog(revs, *flags);
1590+
} else if (!strcmp(arg, "--not")) {
1591+
*flags ^= UNINTERESTING;
1592+
} else if (!strcmp(arg, "--no-walk")) {
1593+
revs->no_walk = 1;
1594+
} else if (!strcmp(arg, "--do-walk")) {
1595+
revs->no_walk = 0;
1596+
} else {
1597+
return 0;
1598+
}
1599+
1600+
return 1;
1601+
}
1602+
15381603
/*
15391604
* Parse revision information, filling in the "rev_info" structure,
15401605
* and removing the used arguments from the argument list.
@@ -1547,8 +1612,6 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
15471612
int i, flags, left, seen_dashdash, read_from_stdin, got_rev_arg = 0;
15481613
const char **prune_data = NULL;
15491614
const char *submodule = NULL;
1550-
const char *optarg;
1551-
int argcount;
15521615

15531616
if (opt)
15541617
submodule = opt->submodule;
@@ -1575,70 +1638,14 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
15751638
if (*arg == '-') {
15761639
int opts;
15771640

1578-
if (!strcmp(arg, "--all")) {
1579-
handle_refs(submodule, revs, flags, for_each_ref_submodule);
1580-
handle_refs(submodule, revs, flags, head_ref_submodule);
1581-
continue;
1582-
}
1583-
if (!strcmp(arg, "--branches")) {
1584-
handle_refs(submodule, revs, flags, for_each_branch_ref_submodule);
1585-
continue;
1586-
}
1587-
if (!strcmp(arg, "--bisect")) {
1588-
handle_refs(submodule, revs, flags, for_each_bad_bisect_ref);
1589-
handle_refs(submodule, revs, flags ^ UNINTERESTING, for_each_good_bisect_ref);
1590-
revs->bisect = 1;
1591-
continue;
1592-
}
1593-
if (!strcmp(arg, "--tags")) {
1594-
handle_refs(submodule, revs, flags, for_each_tag_ref_submodule);
1595-
continue;
1596-
}
1597-
if (!strcmp(arg, "--remotes")) {
1598-
handle_refs(submodule, revs, flags, for_each_remote_ref_submodule);
1599-
continue;
1600-
}
1601-
if ((argcount = parse_long_opt("glob", argv + i, &optarg))) {
1602-
struct all_refs_cb cb;
1603-
i += argcount - 1;
1604-
init_all_refs_cb(&cb, revs, flags);
1605-
for_each_glob_ref(handle_one_ref, optarg, &cb);
1606-
continue;
1607-
}
1608-
if (!prefixcmp(arg, "--branches=")) {
1609-
struct all_refs_cb cb;
1610-
init_all_refs_cb(&cb, revs, flags);
1611-
for_each_glob_ref_in(handle_one_ref, arg + 11, "refs/heads/", &cb);
1612-
continue;
1613-
}
1614-
if (!prefixcmp(arg, "--tags=")) {
1615-
struct all_refs_cb cb;
1616-
init_all_refs_cb(&cb, revs, flags);
1617-
for_each_glob_ref_in(handle_one_ref, arg + 7, "refs/tags/", &cb);
1618-
continue;
1619-
}
1620-
if (!prefixcmp(arg, "--remotes=")) {
1621-
struct all_refs_cb cb;
1622-
init_all_refs_cb(&cb, revs, flags);
1623-
for_each_glob_ref_in(handle_one_ref, arg + 10, "refs/remotes/", &cb);
1624-
continue;
1625-
}
1626-
if (!strcmp(arg, "--reflog")) {
1627-
handle_reflog(revs, flags);
1628-
continue;
1629-
}
1630-
if (!strcmp(arg, "--not")) {
1631-
flags ^= UNINTERESTING;
1632-
continue;
1633-
}
1634-
if (!strcmp(arg, "--no-walk")) {
1635-
revs->no_walk = 1;
1636-
continue;
1637-
}
1638-
if (!strcmp(arg, "--do-walk")) {
1639-
revs->no_walk = 0;
1641+
opts = handle_revision_pseudo_opt(submodule,
1642+
revs, argc - i, argv + i,
1643+
&flags);
1644+
if (opts > 0) {
1645+
i += opts - 1;
16401646
continue;
16411647
}
1648+
16421649
if (!strcmp(arg, "--stdin")) {
16431650
if (revs->disable_stdin) {
16441651
argv[left++] = arg;

t/t6018-rev-list-glob.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ test_expect_success 'rev-parse --glob=heads/subspace' '
6969
7070
'
7171

72+
test_expect_failure 'rev-parse accepts --glob as detached option' '
73+
74+
compare rev-parse "subspace/one subspace/two" "--glob heads/subspace"
75+
76+
'
77+
78+
test_expect_failure 'rev-parse is not confused by option-like glob' '
79+
80+
compare rev-parse "master" "--glob --symbolic master"
81+
82+
'
83+
7284
test_expect_success 'rev-parse --branches=subspace/*' '
7385
7486
compare rev-parse "subspace/one subspace/two" "--branches=subspace/*"
@@ -129,6 +141,12 @@ test_expect_success 'rev-list --glob refs/heads/subspace/*' '
129141
130142
'
131143

144+
test_expect_success 'rev-list not confused by option-like --glob arg' '
145+
146+
compare rev-list "master" "--glob -0 master"
147+
148+
'
149+
132150
test_expect_success 'rev-list --glob=heads/subspace/*' '
133151
134152
compare rev-list "subspace/one subspace/two" "--glob=heads/subspace/*"
@@ -213,4 +231,36 @@ test_expect_success 'rev-list --remotes=foo' '
213231
214232
'
215233

234+
test_expect_success 'shortlog accepts --glob/--tags/--remotes' '
235+
236+
compare shortlog "subspace/one subspace/two" --branches=subspace &&
237+
compare shortlog \
238+
"master subspace-x someref other/three subspace/one subspace/two" \
239+
--branches &&
240+
compare shortlog master "--glob=heads/someref/* master" &&
241+
compare shortlog "subspace/one subspace/two other/three" \
242+
"--glob=heads/subspace/* --glob=heads/other/*" &&
243+
compare shortlog \
244+
"master other/three someref subspace-x subspace/one subspace/two" \
245+
"--glob=heads/*" &&
246+
compare shortlog foo/bar --tags=foo &&
247+
compare shortlog foo/bar --tags &&
248+
compare shortlog foo/baz --remotes=foo
249+
250+
'
251+
252+
test_expect_failure 'shortlog accepts --glob as detached option' '
253+
254+
compare shortlog \
255+
"master other/three someref subspace-x subspace/one subspace/two" \
256+
"--glob heads/*"
257+
258+
'
259+
260+
test_expect_failure 'shortlog --glob is not confused by option-like argument' '
261+
262+
compare shortlog master "--glob -e master"
263+
264+
'
265+
216266
test_done

0 commit comments

Comments
 (0)