7
7
#include "commit.h"
8
8
#include "refs.h"
9
9
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
+
10
16
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 ;
20
17
21
18
#define NORMAL 0
22
19
#define REVERSED 1
23
20
static int show_type = NORMAL ;
21
+ static int symbolic = 0 ;
22
+ static int output_sq = 0 ;
23
+
24
+ static int revs_count = 0 ;
24
25
25
26
/*
26
27
* Some arguments are relevant "revision" arguments,
@@ -30,13 +31,19 @@ static int show_type = NORMAL;
30
31
static int is_rev_argument (const char * arg )
31
32
{
32
33
static const char * rev_args [] = {
33
- "--max-count=" ,
34
+ "--bisect" ,
35
+ "--header" ,
34
36
"--max-age=" ,
35
- "--min-age =" ,
37
+ "--max-count =" ,
36
38
"--merge-order" ,
37
- "--topo-order" ,
38
- "--bisect" ,
39
+ "--min-age=" ,
39
40
"--no-merges" ,
41
+ "--objects" ,
42
+ "--parents" ,
43
+ "--pretty" ,
44
+ "--show-breaks" ,
45
+ "--topo-order" ,
46
+ "--unpacked" ,
40
47
NULL
41
48
};
42
49
const char * * p = rev_args ;
@@ -47,11 +54,13 @@ static int is_rev_argument(const char *arg)
47
54
if (!str )
48
55
return 0 ;
49
56
len = strlen (str );
50
- if (!strncmp (arg , str , len ))
57
+ if (!strcmp (arg , str ) ||
58
+ (str [len - 1 ] == '=' && !strncmp (arg , str , len )))
51
59
return 1 ;
52
60
}
53
61
}
54
62
63
+ /* Output argument as a string, either SQ or normal */
55
64
static void show (const char * arg )
56
65
{
57
66
if (output_sq ) {
@@ -70,11 +79,13 @@ static void show(const char *arg)
70
79
puts (arg );
71
80
}
72
81
82
+ /* Output a revision, only if filter allows it */
73
83
static void show_rev (int type , const unsigned char * sha1 , const char * name )
74
84
{
75
- if (no_revs )
85
+ if (!( filter & DO_REVS ) )
76
86
return ;
77
- output_revs ++ ;
87
+ def = NULL ;
88
+ revs_count ++ ;
78
89
79
90
if (type != show_type )
80
91
putchar ('^' );
@@ -84,29 +95,12 @@ static void show_rev(int type, const unsigned char *sha1, const char *name)
84
95
show (sha1_to_hex (sha1 ));
85
96
}
86
97
87
- static void show_rev_arg (char * rev )
98
+ /* Output a flag, only if filter allows it. */
99
+ static void show_flag (char * arg )
88
100
{
89
- if (no_revs )
101
+ if (!( filter & DO_FLAGS ) )
90
102
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 ))
110
104
show (arg );
111
105
}
112
106
@@ -122,7 +116,6 @@ static void show_default(void)
122
116
show_rev (NORMAL , sha1 , s );
123
117
return ;
124
118
}
125
- show_norev (s );
126
119
}
127
120
}
128
121
@@ -134,7 +127,7 @@ static int show_reference(const char *refname, const unsigned char *sha1)
134
127
135
128
int main (int argc , char * * argv )
136
129
{
137
- int i , as_is = 0 ;
130
+ int i , as_is = 0 , verify = 0 ;
138
131
unsigned char sha1 [20 ];
139
132
const char * prefix = setup_git_directory ();
140
133
@@ -143,41 +136,38 @@ int main(int argc, char **argv)
143
136
char * dotdot ;
144
137
145
138
if (as_is ) {
146
- show_norev (arg );
139
+ show (arg );
147
140
continue ;
148
141
}
149
142
if (* arg == '-' ) {
150
143
if (!strcmp (arg , "--" )) {
151
- show_default ();
152
- if (revs_only || flags_only )
153
- break ;
154
144
as_is = 1 ;
145
+ continue ;
155
146
}
156
147
if (!strcmp (arg , "--default" )) {
157
148
def = argv [i + 1 ];
158
149
i ++ ;
159
150
continue ;
160
151
}
161
152
if (!strcmp (arg , "--revs-only" )) {
162
- revs_only = 1 ;
153
+ filter &= ~ DO_NOREV ;
163
154
continue ;
164
155
}
165
156
if (!strcmp (arg , "--no-revs" )) {
166
- no_revs = 1 ;
157
+ filter &= ~ DO_REVS ;
167
158
continue ;
168
159
}
169
160
if (!strcmp (arg , "--flags" )) {
170
- flags_only = 1 ;
161
+ filter &= ~ DO_NONFLAGS ;
171
162
continue ;
172
163
}
173
164
if (!strcmp (arg , "--no-flags" )) {
174
- no_flags = 1 ;
165
+ filter &= ~ DO_FLAGS ;
175
166
continue ;
176
167
}
177
168
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 ;
181
171
continue ;
182
172
}
183
173
if (!strcmp (arg , "--sq" )) {
@@ -197,12 +187,17 @@ int main(int argc, char **argv)
197
187
continue ;
198
188
}
199
189
if (!strcmp (arg , "--show-prefix" )) {
200
- puts (prefix );
190
+ if (prefix )
191
+ puts (prefix );
201
192
continue ;
202
193
}
203
- show_arg (arg );
194
+ if (verify )
195
+ die ("Needed a single revision" );
196
+ show_flag (arg );
204
197
continue ;
205
198
}
199
+
200
+ /* Not a flag argument */
206
201
dotdot = strstr (arg , ".." );
207
202
if (dotdot ) {
208
203
unsigned char end [20 ];
@@ -212,9 +207,6 @@ int main(int argc, char **argv)
212
207
if (!* n )
213
208
n = "HEAD" ;
214
209
if (!get_sha1 (n , end )) {
215
- if (no_revs )
216
- continue ;
217
- def = NULL ;
218
210
show_rev (NORMAL , end , n );
219
211
show_rev (REVERSED , sha1 , arg );
220
212
continue ;
@@ -223,26 +215,21 @@ int main(int argc, char **argv)
223
215
* dotdot = '.' ;
224
216
}
225
217
if (!get_sha1 (arg , sha1 )) {
226
- if (no_revs )
227
- continue ;
228
- def = NULL ;
229
218
show_rev (NORMAL , sha1 , arg );
230
219
continue ;
231
220
}
232
221
if (* arg == '^' && !get_sha1 (arg + 1 , sha1 )) {
233
- if (no_revs )
234
- continue ;
235
- def = NULL ;
236
222
show_rev (REVERSED , sha1 , arg + 1 );
237
223
continue ;
238
224
}
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 );
241
230
}
242
231
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" );
247
234
return 0 ;
248
235
}
0 commit comments