Skip to content

Commit 812907d

Browse files
committed
Merge branch 'ps/revision-stdin-with-options'
The set-up code for the get_revision() API now allows feeding options like --all and --not in the --stdin mode. * ps/revision-stdin-with-options: revision: handle pseudo-opts in `--stdin` mode revision: small readability improvement for reading from stdin revision: reorder `read_revisions_from_stdin()`
2 parents 9748a68 + c40f0b7 commit 812907d

File tree

3 files changed

+103
-39
lines changed

3 files changed

+103
-39
lines changed

Documentation/rev-list-options.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,11 @@ ifndef::git-rev-list[]
236236
endif::git-rev-list[]
237237

238238
--stdin::
239-
In addition to the '<commit>' listed on the command
240-
line, read them from the standard input. If a `--` separator is
241-
seen, stop reading commits and start reading paths to limit the
242-
result.
239+
In addition to getting arguments from the command line, read
240+
them for standard input as well. This accepts commits and
241+
pseudo-options like `--all` and `--glob=`. When a `--` separator
242+
is seen, the following input is treated as paths and used to
243+
limit the result.
243244

244245
ifdef::git-rev-list[]
245246
--quiet::

revision.c

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,39 +2198,6 @@ static void read_pathspec_from_stdin(struct strbuf *sb,
21982198
strvec_push(prune, sb->buf);
21992199
}
22002200

2201-
static void read_revisions_from_stdin(struct rev_info *revs,
2202-
struct strvec *prune)
2203-
{
2204-
struct strbuf sb;
2205-
int seen_dashdash = 0;
2206-
int save_warning;
2207-
2208-
save_warning = warn_on_object_refname_ambiguity;
2209-
warn_on_object_refname_ambiguity = 0;
2210-
2211-
strbuf_init(&sb, 1000);
2212-
while (strbuf_getline(&sb, stdin) != EOF) {
2213-
int len = sb.len;
2214-
if (!len)
2215-
break;
2216-
if (sb.buf[0] == '-') {
2217-
if (len == 2 && sb.buf[1] == '-') {
2218-
seen_dashdash = 1;
2219-
break;
2220-
}
2221-
die("options not supported in --stdin mode");
2222-
}
2223-
if (handle_revision_arg(sb.buf, revs, 0,
2224-
REVARG_CANNOT_BE_FILENAME))
2225-
die("bad revision '%s'", sb.buf);
2226-
}
2227-
if (seen_dashdash)
2228-
read_pathspec_from_stdin(&sb, prune);
2229-
2230-
strbuf_release(&sb);
2231-
warn_on_object_refname_ambiguity = save_warning;
2232-
}
2233-
22342201
static void add_grep(struct rev_info *revs, const char *ptn, enum grep_pat_token what)
22352202
{
22362203
append_grep_pattern(&revs->grep_filter, ptn, "command line", 0, what);
@@ -2819,6 +2786,53 @@ static int handle_revision_pseudo_opt(struct rev_info *revs,
28192786
return 1;
28202787
}
28212788

2789+
static void read_revisions_from_stdin(struct rev_info *revs,
2790+
struct strvec *prune,
2791+
int *flags)
2792+
{
2793+
struct strbuf sb;
2794+
int seen_dashdash = 0;
2795+
int seen_end_of_options = 0;
2796+
int save_warning;
2797+
2798+
save_warning = warn_on_object_refname_ambiguity;
2799+
warn_on_object_refname_ambiguity = 0;
2800+
2801+
strbuf_init(&sb, 1000);
2802+
while (strbuf_getline(&sb, stdin) != EOF) {
2803+
if (!sb.len)
2804+
break;
2805+
2806+
if (!strcmp(sb.buf, "--")) {
2807+
seen_dashdash = 1;
2808+
break;
2809+
}
2810+
2811+
if (!seen_end_of_options && sb.buf[0] == '-') {
2812+
const char *argv[] = { sb.buf, NULL };
2813+
2814+
if (!strcmp(sb.buf, "--end-of-options")) {
2815+
seen_end_of_options = 1;
2816+
continue;
2817+
}
2818+
2819+
if (handle_revision_pseudo_opt(revs, argv, flags) > 0)
2820+
continue;
2821+
2822+
die(_("invalid option '%s' in --stdin mode"), sb.buf);
2823+
}
2824+
2825+
if (handle_revision_arg(sb.buf, revs, 0,
2826+
REVARG_CANNOT_BE_FILENAME))
2827+
die("bad revision '%s'", sb.buf);
2828+
}
2829+
if (seen_dashdash)
2830+
read_pathspec_from_stdin(&sb, prune);
2831+
2832+
strbuf_release(&sb);
2833+
warn_on_object_refname_ambiguity = save_warning;
2834+
}
2835+
28222836
static void NORETURN diagnose_missing_default(const char *def)
28232837
{
28242838
int flags;
@@ -2891,7 +2905,7 @@ int setup_revisions(int argc, const char **argv, struct rev_info *revs, struct s
28912905
}
28922906
if (revs->read_from_stdin++)
28932907
die("--stdin given twice?");
2894-
read_revisions_from_stdin(revs, &prune_data);
2908+
read_revisions_from_stdin(revs, &prune_data, &flags);
28952909
continue;
28962910
}
28972911

t/t6017-rev-list-stdin.sh

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ test_expect_success setup '
4848
git add file-$i &&
4949
test_tick &&
5050
git commit -m side-$i || exit
51-
done
51+
done &&
52+
53+
git update-ref refs/heads/-dashed-branch HEAD
5254
)
5355
'
5456

@@ -60,6 +62,12 @@ check side-1 ^side-7 -- file-2
6062
check side-3 ^side-4 -- file-3
6163
check side-3 ^side-2
6264
check side-3 ^side-2 -- file-1
65+
check --all
66+
check --all --not --branches
67+
check --glob=refs/heads
68+
check --glob=refs/heads --
69+
check --glob=refs/heads -- file-1
70+
check --end-of-options -dashed-branch
6371

6472
test_expect_success 'not only --stdin' '
6573
cat >expect <<-EOF &&
@@ -78,4 +86,45 @@ test_expect_success 'not only --stdin' '
7886
test_cmp expect actual
7987
'
8088

89+
test_expect_success 'pseudo-opt with missing value' '
90+
cat >input <<-EOF &&
91+
--glob
92+
refs/heads
93+
EOF
94+
95+
cat >expect <<-EOF &&
96+
fatal: Option ${SQ}--glob${SQ} requires a value
97+
EOF
98+
99+
test_must_fail git rev-list --stdin <input 2>error &&
100+
test_cmp expect error
101+
'
102+
103+
test_expect_success 'pseudo-opt with invalid value' '
104+
cat >input <<-EOF &&
105+
--no-walk=garbage
106+
EOF
107+
108+
cat >expect <<-EOF &&
109+
error: invalid argument to --no-walk
110+
fatal: invalid option ${SQ}--no-walk=garbage${SQ} in --stdin mode
111+
EOF
112+
113+
test_must_fail git rev-list --stdin <input 2>error &&
114+
test_cmp expect error
115+
'
116+
117+
test_expect_success 'unknown option without --end-of-options' '
118+
cat >input <<-EOF &&
119+
-dashed-branch
120+
EOF
121+
122+
cat >expect <<-EOF &&
123+
fatal: invalid option ${SQ}-dashed-branch${SQ} in --stdin mode
124+
EOF
125+
126+
test_must_fail git rev-list --stdin <input 2>error &&
127+
test_cmp expect error
128+
'
129+
81130
test_done

0 commit comments

Comments
 (0)