Skip to content

Commit 4866ccf

Browse files
author
Junio C Hamano
committed
Rationalize output selection in rev-parse.
Earlier rounds broke 'whatchanged -p'. In attempting to fix this, make two axis of output selection in rev-parse orthogonal: --revs-only tells it not to output things that are not revisions nor flags that rev-list would take. --no-revs tells it not to output things that are revisions or flags that rev-list would take. --flags tells it not to output parameters that do not start with a '-'. --no-flags tells it not to output parameters that starts with a '-'. So for example 'rev-parse --no-revs -p arch/i386' would yield '-p arch/i386', while 'rev-parse --no-revs --flags -p archi/i386' would give just '-p'. Also the meaning of --verify has been made stronger. It now rejects anything but a single valid rev argument. Earlier it passed some flags through without complaining. Signed-off-by: Junio C Hamano <[email protected]>
1 parent ccf1ee3 commit 4866ccf

File tree

1 file changed

+54
-67
lines changed

1 file changed

+54
-67
lines changed

rev-parse.c

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@
77
#include "commit.h"
88
#include "refs.h"
99

10+
#define DO_REVS 1
11+
#define DO_NOREV 2
12+
#define DO_FLAGS 4
13+
#define DO_NONFLAGS 8
14+
static int filter = ~0;
15+
1016
static char *def = NULL;
11-
static int no_revs = 0;
12-
static int single_rev = 0;
13-
static int revs_only = 0;
14-
static int do_rev_argument = 1;
15-
static int output_revs = 0;
16-
static int flags_only = 0;
17-
static int no_flags = 0;
18-
static int output_sq = 0;
19-
static int symbolic = 0;
2017

2118
#define NORMAL 0
2219
#define REVERSED 1
2320
static int show_type = NORMAL;
21+
static int symbolic = 0;
22+
static int output_sq = 0;
23+
24+
static int revs_count = 0;
2425

2526
/*
2627
* Some arguments are relevant "revision" arguments,
@@ -30,13 +31,19 @@ static int show_type = NORMAL;
3031
static int is_rev_argument(const char *arg)
3132
{
3233
static const char *rev_args[] = {
33-
"--max-count=",
34+
"--bisect",
35+
"--header",
3436
"--max-age=",
35-
"--min-age=",
37+
"--max-count=",
3638
"--merge-order",
37-
"--topo-order",
38-
"--bisect",
39+
"--min-age=",
3940
"--no-merges",
41+
"--objects",
42+
"--parents",
43+
"--pretty",
44+
"--show-breaks",
45+
"--topo-order",
46+
"--unpacked",
4047
NULL
4148
};
4249
const char **p = rev_args;
@@ -47,11 +54,13 @@ static int is_rev_argument(const char *arg)
4754
if (!str)
4855
return 0;
4956
len = strlen(str);
50-
if (!strncmp(arg, str, len))
57+
if (!strcmp(arg, str) ||
58+
(str[len-1] == '=' && !strncmp(arg, str, len)))
5159
return 1;
5260
}
5361
}
5462

63+
/* Output argument as a string, either SQ or normal */
5564
static void show(const char *arg)
5665
{
5766
if (output_sq) {
@@ -70,11 +79,13 @@ static void show(const char *arg)
7079
puts(arg);
7180
}
7281

82+
/* Output a revision, only if filter allows it */
7383
static void show_rev(int type, const unsigned char *sha1, const char *name)
7484
{
75-
if (no_revs)
85+
if (!(filter & DO_REVS))
7686
return;
77-
output_revs++;
87+
def = NULL;
88+
revs_count++;
7889

7990
if (type != show_type)
8091
putchar('^');
@@ -84,29 +95,12 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
8495
show(sha1_to_hex(sha1));
8596
}
8697

87-
static void show_rev_arg(char *rev)
98+
/* Output a flag, only if filter allows it. */
99+
static void show_flag(char *arg)
88100
{
89-
if (no_revs)
101+
if (!(filter & DO_FLAGS))
90102
return;
91-
show(rev);
92-
}
93-
94-
static void show_norev(char *norev)
95-
{
96-
if (flags_only)
97-
return;
98-
if (revs_only)
99-
return;
100-
show(norev);
101-
}
102-
103-
static void show_arg(char *arg)
104-
{
105-
if (no_flags)
106-
return;
107-
if (do_rev_argument && is_rev_argument(arg))
108-
show_rev_arg(arg);
109-
else
103+
if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
110104
show(arg);
111105
}
112106

@@ -122,7 +116,6 @@ static void show_default(void)
122116
show_rev(NORMAL, sha1, s);
123117
return;
124118
}
125-
show_norev(s);
126119
}
127120
}
128121

@@ -134,7 +127,7 @@ static int show_reference(const char *refname, const unsigned char *sha1)
134127

135128
int main(int argc, char **argv)
136129
{
137-
int i, as_is = 0;
130+
int i, as_is = 0, verify = 0;
138131
unsigned char sha1[20];
139132
const char *prefix = setup_git_directory();
140133

@@ -143,41 +136,38 @@ int main(int argc, char **argv)
143136
char *dotdot;
144137

145138
if (as_is) {
146-
show_norev(arg);
139+
show(arg);
147140
continue;
148141
}
149142
if (*arg == '-') {
150143
if (!strcmp(arg, "--")) {
151-
show_default();
152-
if (revs_only || flags_only)
153-
break;
154144
as_is = 1;
145+
continue;
155146
}
156147
if (!strcmp(arg, "--default")) {
157148
def = argv[i+1];
158149
i++;
159150
continue;
160151
}
161152
if (!strcmp(arg, "--revs-only")) {
162-
revs_only = 1;
153+
filter &= ~DO_NOREV;
163154
continue;
164155
}
165156
if (!strcmp(arg, "--no-revs")) {
166-
no_revs = 1;
157+
filter &= ~DO_REVS;
167158
continue;
168159
}
169160
if (!strcmp(arg, "--flags")) {
170-
flags_only = 1;
161+
filter &= ~DO_NONFLAGS;
171162
continue;
172163
}
173164
if (!strcmp(arg, "--no-flags")) {
174-
no_flags = 1;
165+
filter &= ~DO_FLAGS;
175166
continue;
176167
}
177168
if (!strcmp(arg, "--verify")) {
178-
revs_only = 1;
179-
do_rev_argument = 0;
180-
single_rev = 1;
169+
filter &= ~(DO_FLAGS|DO_NOREV);
170+
verify = 1;
181171
continue;
182172
}
183173
if (!strcmp(arg, "--sq")) {
@@ -197,12 +187,17 @@ int main(int argc, char **argv)
197187
continue;
198188
}
199189
if (!strcmp(arg, "--show-prefix")) {
200-
puts(prefix);
190+
if (prefix)
191+
puts(prefix);
201192
continue;
202193
}
203-
show_arg(arg);
194+
if (verify)
195+
die("Needed a single revision");
196+
show_flag(arg);
204197
continue;
205198
}
199+
200+
/* Not a flag argument */
206201
dotdot = strstr(arg, "..");
207202
if (dotdot) {
208203
unsigned char end[20];
@@ -212,9 +207,6 @@ int main(int argc, char **argv)
212207
if (!*n)
213208
n = "HEAD";
214209
if (!get_sha1(n, end)) {
215-
if (no_revs)
216-
continue;
217-
def = NULL;
218210
show_rev(NORMAL, end, n);
219211
show_rev(REVERSED, sha1, arg);
220212
continue;
@@ -223,26 +215,21 @@ int main(int argc, char **argv)
223215
*dotdot = '.';
224216
}
225217
if (!get_sha1(arg, sha1)) {
226-
if (no_revs)
227-
continue;
228-
def = NULL;
229218
show_rev(NORMAL, sha1, arg);
230219
continue;
231220
}
232221
if (*arg == '^' && !get_sha1(arg+1, sha1)) {
233-
if (no_revs)
234-
continue;
235-
def = NULL;
236222
show_rev(REVERSED, sha1, arg+1);
237223
continue;
238224
}
239-
show_default();
240-
show_norev(arg);
225+
if (verify)
226+
die("Needed a single revision");
227+
if ((filter & (DO_NONFLAGS|DO_NOREV)) ==
228+
(DO_NONFLAGS|DO_NOREV))
229+
show(arg);
241230
}
242231
show_default();
243-
if (single_rev && output_revs != 1) {
244-
fprintf(stderr, "Needed a single revision\n");
245-
exit(1);
246-
}
232+
if (verify && revs_count != 1)
233+
die("Needed a single revision");
247234
return 0;
248235
}

0 commit comments

Comments
 (0)