Skip to content
Newer
Older
100644 725 lines (665 sloc) 15.8 KB
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
1 /*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
a8be83f Make rev-parse understand "extended sha1" syntax
Linus Torvalds authored Jun 20, 2005
7 #include "commit.h"
960bba0 Add "--all" flag to rev-parse that shows all refs
Linus Torvalds authored Jul 3, 2005
8 #include "refs.h"
c1babb1 [PATCH] Teach "git-rev-parse" about date-based cut-offs
Linus Torvalds authored Sep 20, 2005
9 #include "quote.h"
895f10c @chriscool Builtin git-rev-parse.
chriscool authored Jun 3, 2006
10 #include "builtin.h"
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
11 #include "parse-options.h"
a8be83f Make rev-parse understand "extended sha1" syntax
Linus Torvalds authored Jun 21, 2005
12
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
13 #define DO_REVS 1
14 #define DO_NOREV 2
15 #define DO_FLAGS 4
16 #define DO_NONFLAGS 8
17 static int filter = ~0;
18
96f1e58 remove unnecessary initializations
David Rientjes authored Aug 15, 2006
19 static const char *def;
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
20
042a4ed git-rev-parse: add "--not" flag to mark subsequent heads negative
Linus Torvalds authored Jun 26, 2005
21 #define NORMAL 0
22 #define REVERSED 1
23 static int show_type = NORMAL;
a6d97d4 @gitster git-rev-parse --symbolic-full-name
gitster authored Jan 5, 2008
24
25 #define SHOW_SYMBOLIC_ASIS 1
26 #define SHOW_SYMBOLIC_FULL 2
96f1e58 remove unnecessary initializations
David Rientjes authored Aug 15, 2006
27 static int symbolic;
28 static int abbrev;
a45d346 @bertwesarg rev-parse: --abbrev-ref option to shorten ref name
bertwesarg authored Apr 13, 2009
29 static int abbrev_ref;
30 static int abbrev_ref_strict;
96f1e58 remove unnecessary initializations
David Rientjes authored Aug 15, 2006
31 static int output_sq;
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
32
921d865 Teach git-rev-parse about revision-specifying arguments
Linus Torvalds authored Jun 13, 2005
33 /*
34 * Some arguments are relevant "revision" arguments,
35 * others are about output format or other details.
36 * This sorts it all out.
37 */
38 static int is_rev_argument(const char *arg)
39 {
40 static const char *rev_args[] = {
e091eb9 upload-pack: Do not choke on too many heads request.
Junio C Hamano authored Oct 5, 2005
41 "--all",
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
42 "--bisect",
5a83f3b Update git-rev-list options list in rev-parse.
Junio C Hamano authored Oct 30, 2005
43 "--dense",
b09fe97 rev-parse --branches/--tags/--remotes=pattern
Ilari Liusvaara authored Jan 20, 2010
44 "--branches=",
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 13, 2006
45 "--branches",
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
46 "--header",
921d865 Teach git-rev-parse about revision-specifying arguments
Linus Torvalds authored Jun 13, 2005
47 "--max-age=",
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
48 "--max-count=",
49 "--min-age=",
5ccfb75 Update rev-parse flags list.
Junio C Hamano authored Aug 8, 2005
50 "--no-merges",
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
51 "--objects",
c649657 rev-list --objects-edge
Junio C Hamano authored Feb 19, 2006
52 "--objects-edge",
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
53 "--parents",
54 "--pretty",
b09fe97 rev-parse --branches/--tags/--remotes=pattern
Ilari Liusvaara authored Jan 20, 2010
55 "--remotes=",
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
56 "--remotes",
d08bae7 rev-parse --glob
Ilari Liusvaara authored Jan 20, 2010
57 "--glob=",
5a83f3b Update git-rev-list options list in rev-parse.
Junio C Hamano authored Oct 30, 2005
58 "--sparse",
b09fe97 rev-parse --branches/--tags/--remotes=pattern
Ilari Liusvaara authored Jan 20, 2010
59 "--tags=",
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
60 "--tags",
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
61 "--topo-order",
4c8725f topo-order: make --date-order optional.
Junio C Hamano authored Feb 15, 2006
62 "--date-order",
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
63 "--unpacked",
921d865 Teach git-rev-parse about revision-specifying arguments
Linus Torvalds authored Jun 13, 2005
64 NULL
65 };
66 const char **p = rev_args;
67
8233340 rev-list: allow -<n> as shorthand for --max-count=<n>
Eric Wong authored Jan 29, 2006
68 /* accept -<digit>, like traditional "head" */
69 if ((*arg == '-') && isdigit(arg[1]))
70 return 1;
71
921d865 Teach git-rev-parse about revision-specifying arguments
Linus Torvalds authored Jun 13, 2005
72 for (;;) {
73 const char *str = *p++;
74 int len;
75 if (!str)
76 return 0;
77 len = strlen(str);
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
78 if (!strcmp(arg, str) ||
79 (str[len-1] == '=' && !strncmp(arg, str, len)))
921d865 Teach git-rev-parse about revision-specifying arguments
Linus Torvalds authored Jun 13, 2005
80 return 1;
81 }
82 }
83
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
84 /* Output argument as a string, either SQ or normal */
5bb2c65 [PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
Junio C Hamano authored Jul 22, 2005
85 static void show(const char *arg)
86 {
87 if (output_sq) {
88 int sq = '\'', ch;
89
90 putchar(sq);
91 while ((ch = *arg++)) {
92 if (ch == sq)
93 fputs("'\\'", stdout);
94 putchar(ch);
95 }
96 putchar(sq);
97 putchar(' ');
98 }
99 else
100 puts(arg);
101 }
102
e00f379 rev-parse --symbolic-full-name: don't print '^' if SHA1 is not a ref
Johannes Sixt authored May 23, 2008
103 /* Like show(), but with a negation prefix according to type */
104 static void show_with_type(int type, const char *arg)
105 {
106 if (type != show_type)
107 putchar('^');
108 show(arg);
109 }
110
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
111 /* Output a revision, only if filter allows it */
30b96fc Add --symbolic flag to git-rev-parse.
Junio C Hamano authored Aug 16, 2005
112 static void show_rev(int type, const unsigned char *sha1, const char *name)
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
113 {
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
114 if (!(filter & DO_REVS))
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
115 return;
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
116 def = NULL;
5bb2c65 [PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
Junio C Hamano authored Jul 23, 2005
117
a45d346 @bertwesarg rev-parse: --abbrev-ref option to shorten ref name
bertwesarg authored Apr 13, 2009
118 if ((symbolic || abbrev_ref) && name) {
119 if (symbolic == SHOW_SYMBOLIC_FULL || abbrev_ref) {
a6d97d4 @gitster git-rev-parse --symbolic-full-name
gitster authored Jan 5, 2008
120 unsigned char discard[20];
121 char *full;
122
123 switch (dwim_ref(name, strlen(name), discard, &full)) {
124 case 0:
125 /*
126 * Not found -- not a ref. We could
127 * emit "name" here, but symbolic-full
128 * users are interested in finding the
129 * refs spelled in full, and they would
130 * need to filter non-refs if we did so.
131 */
132 break;
133 case 1: /* happy */
a45d346 @bertwesarg rev-parse: --abbrev-ref option to shorten ref name
bertwesarg authored Apr 13, 2009
134 if (abbrev_ref)
135 full = shorten_unambiguous_ref(full,
136 abbrev_ref_strict);
e00f379 rev-parse --symbolic-full-name: don't print '^' if SHA1 is not a ref
Johannes Sixt authored May 23, 2008
137 show_with_type(type, full);
a6d97d4 @gitster git-rev-parse --symbolic-full-name
gitster authored Jan 5, 2008
138 break;
139 default: /* ambiguous */
140 error("refname '%s' is ambiguous", name);
141 break;
142 }
143 } else {
e00f379 rev-parse --symbolic-full-name: don't print '^' if SHA1 is not a ref
Johannes Sixt authored May 23, 2008
144 show_with_type(type, name);
a6d97d4 @gitster git-rev-parse --symbolic-full-name
gitster authored Jan 5, 2008
145 }
146 }
d501250 rev-parse: --abbrev option.
Junio C Hamano authored Jan 25, 2006
147 else if (abbrev)
e00f379 rev-parse --symbolic-full-name: don't print '^' if SHA1 is not a ref
Johannes Sixt authored May 23, 2008
148 show_with_type(type, find_unique_abbrev(sha1, abbrev));
30b96fc Add --symbolic flag to git-rev-parse.
Junio C Hamano authored Aug 16, 2005
149 else
e00f379 rev-parse --symbolic-full-name: don't print '^' if SHA1 is not a ref
Johannes Sixt authored May 23, 2008
150 show_with_type(type, sha1_to_hex(sha1));
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
151 }
152
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
153 /* Output a flag, only if filter allows it. */
16cee38 rev-parse: tighten constness properly.
Junio C Hamano authored Jun 5, 2006
154 static int show_flag(const char *arg)
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
155 {
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
156 if (!(filter & DO_FLAGS))
9523a4c Fix git-rev-parse over-eager errors
Linus Torvalds authored Feb 5, 2006
157 return 0;
158 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV)) {
0360e99 [PATCH] Fix git-rev-parse --default and --flags handling
Linus Torvalds authored Aug 23, 2005
159 show(arg);
9523a4c Fix git-rev-parse over-eager errors
Linus Torvalds authored Feb 5, 2006
160 return 1;
161 }
162 return 0;
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
163 }
164
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
165 static int show_default(void)
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
166 {
16cee38 rev-parse: tighten constness properly.
Junio C Hamano authored Jun 6, 2006
167 const char *s = def;
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
168
169 if (s) {
170 unsigned char sha1[20];
171
172 def = NULL;
9938af6 Update get_sha1() to grok extended format.
Junio C Hamano authored Aug 3, 2005
173 if (!get_sha1(s, sha1)) {
30b96fc Add --symbolic flag to git-rev-parse.
Junio C Hamano authored Aug 16, 2005
174 show_rev(NORMAL, sha1, s);
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
175 return 1;
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
176 }
177 }
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
178 return 0;
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
179 }
180
8da1977 Tell between packed, unpacked and symbolic refs.
Junio C Hamano authored Sep 20, 2006
181 static int show_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
960bba0 Add "--all" flag to rev-parse that shows all refs
Linus Torvalds authored Jul 3, 2005
182 {
30b96fc Add --symbolic flag to git-rev-parse.
Junio C Hamano authored Aug 16, 2005
183 show_rev(NORMAL, sha1, refname);
960bba0 Add "--all" flag to rev-parse that shows all refs
Linus Torvalds authored Jul 3, 2005
184 return 0;
185 }
186
ad3f9a7 @torvalds Add '--bisect' revision machinery argument
torvalds authored Oct 27, 2009
187 static int anti_reference(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
188 {
189 show_rev(REVERSED, sha1, refname);
190 return 0;
191 }
192
c1babb1 [PATCH] Teach "git-rev-parse" about date-based cut-offs
Linus Torvalds authored Sep 20, 2005
193 static void show_datestring(const char *flag, const char *datestr)
194 {
195 static char buffer[100];
196
197 /* date handling requires both flags and revs */
198 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
199 return;
3c07b1d git's rev-parse.c function show_datestring presumes gnu date
Linus Torvalds authored Nov 14, 2005
200 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
c1babb1 [PATCH] Teach "git-rev-parse" about date-based cut-offs
Linus Torvalds authored Sep 20, 2005
201 show(buffer);
202 }
203
9ad0a93 rev-parse lstat() workaround cleanup.
Linus Torvalds authored Feb 5, 2006
204 static int show_file(const char *arg)
7a3dd47 Avoid ambiguity between refname and filename in rev-parse
Linus Torvalds authored Oct 18, 2005
205 {
7b34c2f git-rev-list: make --dense the default (and introduce "--sparse")
Linus Torvalds authored Oct 25, 2005
206 show_default();
9ad0a93 rev-parse lstat() workaround cleanup.
Linus Torvalds authored Feb 6, 2006
207 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV)) {
7a3dd47 Avoid ambiguity between refname and filename in rev-parse
Linus Torvalds authored Oct 18, 2005
208 show(arg);
9ad0a93 rev-parse lstat() workaround cleanup.
Linus Torvalds authored Feb 6, 2006
209 return 1;
210 }
211 return 0;
7a3dd47 Avoid ambiguity between refname and filename in rev-parse
Linus Torvalds authored Oct 18, 2005
212 }
213
b7d936b builtin-rev-parse.c: constness tightening
Junio C Hamano authored Jul 6, 2006
214 static int try_difference(const char *arg)
3dd4e73 Teach rev-parse the ... syntax.
Santi Béjar authored Jul 4, 2006
215 {
216 char *dotdot;
217 unsigned char sha1[20];
218 unsigned char end[20];
219 const char *next;
220 const char *this;
221 int symmetric;
222
223 if (!(dotdot = strstr(arg, "..")))
224 return 0;
225 next = dotdot + 2;
226 this = arg;
227 symmetric = (*next == '.');
228
229 *dotdot = 0;
230 next += symmetric;
231
232 if (!*next)
233 next = "HEAD";
234 if (dotdot == arg)
235 this = "HEAD";
236 if (!get_sha1(this, sha1) && !get_sha1(next, end)) {
237 show_rev(NORMAL, end, next);
238 show_rev(symmetric ? NORMAL : REVERSED, sha1, this);
239 if (symmetric) {
240 struct commit_list *exclude;
241 struct commit *a, *b;
242 a = lookup_commit_reference(sha1);
243 b = lookup_commit_reference(end);
244 exclude = get_merge_bases(a, b, 1);
245 while (exclude) {
246 struct commit_list *n = exclude->next;
247 show_rev(REVERSED,
248 exclude->item->object.sha1,NULL);
249 free(exclude);
250 exclude = n;
251 }
252 }
253 return 1;
254 }
255 *dotdot = '.';
256 return 0;
257 }
258
2122f8b @dotdash rev-parse: Add support for the ^! and ^@ syntax
dotdash authored Jul 26, 2008
259 static int try_parent_shorthands(const char *arg)
260 {
261 char *dotdot;
262 unsigned char sha1[20];
263 struct commit *commit;
264 struct commit_list *parents;
265 int parents_only;
266
267 if ((dotdot = strstr(arg, "^!")))
268 parents_only = 0;
269 else if ((dotdot = strstr(arg, "^@")))
270 parents_only = 1;
271
272 if (!dotdot || dotdot[2])
273 return 0;
274
275 *dotdot = 0;
276 if (get_sha1(arg, sha1))
277 return 0;
278
279 if (!parents_only)
280 show_rev(NORMAL, sha1, arg);
281 commit = lookup_commit_reference(sha1);
282 for (parents = commit->parents; parents; parents = parents->next)
283 show_rev(parents_only ? NORMAL : REVERSED,
284 parents->item->object.sha1, arg);
285
286 return 1;
287 }
288
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
289 static int parseopt_dump(const struct option *o, const char *arg, int unset)
290 {
291 struct strbuf *parsed = o->value;
292 if (unset)
293 strbuf_addf(parsed, " --no-%s", o->long_name);
294 else if (o->short_name)
295 strbuf_addf(parsed, " -%c", o->short_name);
296 else
297 strbuf_addf(parsed, " --%s", o->long_name);
298 if (arg) {
299 strbuf_addch(parsed, ' ');
300 sq_quote_buf(parsed, arg);
301 }
302 return 0;
303 }
304
305 static const char *skipspaces(const char *s)
306 {
307 while (isspace(*s))
308 s++;
309 return s;
310 }
311
312 static int cmd_parseopt(int argc, const char **argv, const char *prefix)
313 {
6e0800e parse-opt: make PARSE_OPT_STOP_AT_NON_OPTION available to git rev-parse
Uwe Kleine-König authored Jun 14, 2009
314 static int keep_dashdash = 0, stop_at_non_option = 0;
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
315 static char const * const parseopt_usage[] = {
1b1dd23 @sbeyer Make usage strings dash-less
sbeyer authored Jul 13, 2008
316 "git rev-parse --parseopt [options] -- [<args>...]",
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
317 NULL
318 };
319 static struct option parseopt_opts[] = {
320 OPT_BOOLEAN(0, "keep-dashdash", &keep_dashdash,
321 "keep the `--` passed as an arg"),
6e0800e parse-opt: make PARSE_OPT_STOP_AT_NON_OPTION available to git rev-parse
Uwe Kleine-König authored Jun 13, 2009
322 OPT_BOOLEAN(0, "stop-at-non-option", &stop_at_non_option,
323 "stop parsing after the "
324 "first non-option argument"),
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
325 OPT_END(),
326 };
327
f285a2d Replace calls to strbuf_init(&foo, 0) with STRBUF_INIT initializer
Brandon Casey authored Oct 9, 2008
328 struct strbuf sb = STRBUF_INIT, parsed = STRBUF_INIT;
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
329 const char **usage = NULL;
330 struct option *opts = NULL;
331 int onb = 0, osz = 0, unb = 0, usz = 0;
332
333 strbuf_addstr(&parsed, "set --");
3778292 @bebarino parse-opts: prepare for OPT_FILENAME
bebarino authored May 23, 2009
334 argc = parse_options(argc, argv, prefix, parseopt_opts, parseopt_usage,
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
335 PARSE_OPT_KEEP_DASHDASH);
336 if (argc < 1 || strcmp(argv[0], "--"))
337 usage_with_options(parseopt_usage, parseopt_opts);
338
339 /* get the usage up to the first line with a -- on it */
340 for (;;) {
341 if (strbuf_getline(&sb, stdin, '\n') == EOF)
342 die("premature end of input");
343 ALLOC_GROW(usage, unb + 1, usz);
344 if (!strcmp("--", sb.buf)) {
345 if (unb < 1)
346 die("no usage string given before the `--' separator");
347 usage[unb] = NULL;
348 break;
349 }
350 usage[unb++] = strbuf_detach(&sb, NULL);
351 }
352
353 /* parse: (<short>|<short>,<long>|<long>)[=?]? SP+ <help> */
354 while (strbuf_getline(&sb, stdin, '\n') != EOF) {
355 const char *s;
356 struct option *o;
357
358 if (!sb.len)
359 continue;
360
361 ALLOC_GROW(opts, onb + 1, osz);
362 memset(opts + onb, 0, sizeof(opts[onb]));
363
364 o = &opts[onb++];
365 s = strchr(sb.buf, ' ');
366 if (!s || *sb.buf == ' ') {
367 o->type = OPTION_GROUP;
e103343 @jaysoffian rev-parse: fix potential bus error with --parseopt option spec handling
jaysoffian authored Feb 25, 2008
368 o->help = xstrdup(skipspaces(sb.buf));
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
369 continue;
370 }
371
372 o->type = OPTION_CALLBACK;
373 o->help = xstrdup(skipspaces(s));
374 o->value = &parsed;
ff962a3 parse-opt: bring PARSE_OPT_HIDDEN and NONEG to git-rev-parse --parseopt
Pierre Habouzit authored Mar 2, 2008
375 o->flags = PARSE_OPT_NOARG;
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
376 o->callback = &parseopt_dump;
ff962a3 parse-opt: bring PARSE_OPT_HIDDEN and NONEG to git-rev-parse --parseopt
Pierre Habouzit authored Mar 2, 2008
377 while (s > sb.buf && strchr("*=?!", s[-1])) {
378 switch (*--s) {
379 case '=':
380 o->flags &= ~PARSE_OPT_NOARG;
381 break;
382 case '?':
383 o->flags &= ~PARSE_OPT_NOARG;
384 o->flags |= PARSE_OPT_OPTARG;
385 break;
386 case '!':
387 o->flags |= PARSE_OPT_NONEG;
388 break;
389 case '*':
390 o->flags |= PARSE_OPT_HIDDEN;
391 break;
392 }
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
393 }
394
395 if (s - sb.buf == 1) /* short option only */
396 o->short_name = *sb.buf;
397 else if (sb.buf[1] != ',') /* long option only */
398 o->long_name = xmemdupz(sb.buf, s - sb.buf);
399 else {
400 o->short_name = *sb.buf;
401 o->long_name = xmemdupz(sb.buf + 2, s - sb.buf - 2);
402 }
403 }
404 strbuf_release(&sb);
405
406 /* put an OPT_END() */
407 ALLOC_GROW(opts, onb + 1, osz);
408 memset(opts + onb, 0, sizeof(opts[onb]));
3778292 @bebarino parse-opts: prepare for OPT_FILENAME
bebarino authored May 23, 2009
409 argc = parse_options(argc, argv, prefix, opts, usage,
6e0800e parse-opt: make PARSE_OPT_STOP_AT_NON_OPTION available to git rev-parse
Uwe Kleine-König authored Jun 13, 2009
410 keep_dashdash ? PARSE_OPT_KEEP_DASHDASH : 0 |
411 stop_at_non_option ? PARSE_OPT_STOP_AT_NON_OPTION : 0);
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
412
413 strbuf_addf(&parsed, " --");
b319ce4 @chriscool Trace and quote with argv: get rid of unneeded count argument.
chriscool authored Dec 3, 2007
414 sq_quote_argv(&parsed, argv, 0);
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
415 puts(parsed.buf);
416 return 0;
417 }
418
5032537 @chriscool rev-parse: add --sq-quote to shell quote arguments
chriscool authored Apr 25, 2009
419 static int cmd_sq_quote(int argc, const char **argv)
420 {
421 struct strbuf buf = STRBUF_INIT;
422
423 if (argc)
424 sq_quote_argv(&buf, argv, 0);
425 printf("%s\n", buf.buf);
426 strbuf_release(&buf);
427
428 return 0;
429 }
430
b1b3596 @chriscool rev-parse: teach "--verify" to be quiet when using "-q" or "--quiet"
chriscool authored Apr 26, 2008
431 static void die_no_single_rev(int quiet)
432 {
433 if (quiet)
434 exit(1);
435 else
436 die("Needed a single revision");
437 }
438
7006b5b @jrn Show usage string for 'git rev-parse -h'
jrn authored Nov 9, 2009
439 static const char builtin_rev_parse_usage[] =
440 "git rev-parse --parseopt [options] -- [<args>...]\n"
441 " or: git rev-parse --sq-quote [<arg>...]\n"
442 " or: git rev-parse [options] [<arg>...]\n"
443 "\n"
444 "Run \"git rev-parse --parseopt -h\" for more information on the first usage.";
445
a633fca Call setup_git_directory() much earlier
Linus Torvalds authored Jul 28, 2006
446 int cmd_rev_parse(int argc, const char **argv, const char *prefix)
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
447 {
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
448 int i, as_is = 0, verify = 0, quiet = 0, revs_count = 0, type = 0;
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
449 unsigned char sha1[20];
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
450 const char *name = NULL;
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
451
21d4783 Add a parseopt mode to git-rev-parse to bring parse-options to shell …
Pierre Habouzit authored Nov 4, 2007
452 if (argc > 1 && !strcmp("--parseopt", argv[1]))
453 return cmd_parseopt(argc - 1, argv + 1, prefix);
454
5032537 @chriscool rev-parse: add --sq-quote to shell quote arguments
chriscool authored Apr 25, 2009
455 if (argc > 1 && !strcmp("--sq-quote", argv[1]))
456 return cmd_sq_quote(argc - 2, argv + 2);
457
7006b5b @jrn Show usage string for 'git rev-parse -h'
jrn authored Nov 9, 2009
458 if (argc > 1 && !strcmp("-h", argv[1]))
459 usage(builtin_rev_parse_usage);
460
5410a02 @gitster git-rev-parse --parseopt
gitster authored Nov 6, 2007
461 prefix = setup_git_directory();
ef90d6d @dscho Provide git_config with a callback-data parameter
dscho authored May 14, 2008
462 git_config(git_default_config, NULL);
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
463 for (i = 1; i < argc; i++) {
16cee38 rev-parse: tighten constness properly.
Junio C Hamano authored Jun 6, 2006
464 const char *arg = argv[i];
fb18a2e Fix error handling for nonexistent names
Linus Torvalds authored Mar 26, 2006
465
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
466 if (as_is) {
fb18a2e Fix error handling for nonexistent names
Linus Torvalds authored Mar 27, 2006
467 if (show_file(arg) && as_is < 2)
e23d0b4 Fix filename verification when in a subdirectory
Linus Torvalds authored Apr 26, 2006
468 verify_filename(prefix, arg);
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
469 continue;
470 }
3af0698 rev-list: allow -n<n> as shorthand for --max-count=<n>
Eric Wong authored Jan 29, 2006
471 if (!strcmp(arg,"-n")) {
472 if (++i >= argc)
473 die("-n requires an argument");
474 if ((filter & DO_FLAGS) && (filter & DO_REVS)) {
475 show(arg);
476 show(argv[i]);
477 }
478 continue;
479 }
1968d77 prefixcmp(): fix-up leftover strncmp().
Junio C Hamano authored Feb 20, 2007
480 if (!prefixcmp(arg, "-n")) {
3af0698 rev-list: allow -n<n> as shorthand for --max-count=<n>
Eric Wong authored Jan 30, 2006
481 if ((filter & DO_FLAGS) && (filter & DO_REVS))
482 show(arg);
483 continue;
484 }
485
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
486 if (*arg == '-') {
487 if (!strcmp(arg, "--")) {
fb18a2e Fix error handling for nonexistent names
Linus Torvalds authored Mar 27, 2006
488 as_is = 2;
a08b650 git-rev-parse: pass on "--" flag when required
Linus Torvalds authored Oct 20, 2005
489 /* Pass on the "--" if we show anything but files.. */
490 if (filter & (DO_FLAGS | DO_REVS))
491 show_file(arg);
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
492 continue;
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
493 }
494 if (!strcmp(arg, "--default")) {
495 def = argv[i+1];
496 i++;
497 continue;
498 }
8ebb018 git-rev-parse: split "revs" and "non-revs"
Linus Torvalds authored Jun 13, 2005
499 if (!strcmp(arg, "--revs-only")) {
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
500 filter &= ~DO_NOREV;
8ebb018 git-rev-parse: split "revs" and "non-revs"
Linus Torvalds authored Jun 13, 2005
501 continue;
502 }
503 if (!strcmp(arg, "--no-revs")) {
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
504 filter &= ~DO_REVS;
8ebb018 git-rev-parse: split "revs" and "non-revs"
Linus Torvalds authored Jun 13, 2005
505 continue;
506 }
f79b65a Add "--flags" and "--no-flags" arguments to git-rev-parse
Linus Torvalds authored Jul 6, 2005
507 if (!strcmp(arg, "--flags")) {
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
508 filter &= ~DO_NONFLAGS;
f79b65a Add "--flags" and "--no-flags" arguments to git-rev-parse
Linus Torvalds authored Jul 6, 2005
509 continue;
510 }
511 if (!strcmp(arg, "--no-flags")) {
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
512 filter &= ~DO_FLAGS;
f79b65a Add "--flags" and "--no-flags" arguments to git-rev-parse
Linus Torvalds authored Jul 6, 2005
513 continue;
514 }
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
515 if (!strcmp(arg, "--verify")) {
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
516 filter &= ~(DO_FLAGS|DO_NOREV);
517 verify = 1;
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
518 continue;
921d865 Teach git-rev-parse about revision-specifying arguments
Linus Torvalds authored Jun 13, 2005
519 }
b1b3596 @chriscool rev-parse: teach "--verify" to be quiet when using "-q" or "--quiet"
chriscool authored Apr 26, 2008
520 if (!strcmp(arg, "--quiet") || !strcmp(arg, "-q")) {
521 quiet = 1;
522 continue;
523 }
62a604b Rename rev-parse --abbrev to --short.
Junio C Hamano authored Jan 26, 2006
524 if (!strcmp(arg, "--short") ||
cc44c76 Mechanical conversion to use prefixcmp()
Junio C Hamano authored Feb 20, 2007
525 !prefixcmp(arg, "--short=")) {
d501250 rev-parse: --abbrev option.
Junio C Hamano authored Jan 25, 2006
526 filter &= ~(DO_FLAGS|DO_NOREV);
527 verify = 1;
528 abbrev = DEFAULT_ABBREV;
44de0da @jonas git-rev-parse: Fix --short= option parsing
jonas authored Feb 18, 2006
529 if (arg[7] == '=')
530 abbrev = strtoul(arg + 8, NULL, 10);
1dc4fb8 rev-parse --abbrev: do not try abbrev shorter than minimum.
Junio C Hamano authored Jan 26, 2006
531 if (abbrev < MINIMUM_ABBREV)
532 abbrev = MINIMUM_ABBREV;
533 else if (40 <= abbrev)
534 abbrev = 40;
d501250 rev-parse: --abbrev option.
Junio C Hamano authored Jan 25, 2006
535 continue;
536 }
5bb2c65 [PATCH] Help scripts that use git-rev-parse to grok args with SP/TAB/LF
Junio C Hamano authored Jul 23, 2005
537 if (!strcmp(arg, "--sq")) {
538 output_sq = 1;
539 continue;
540 }
042a4ed git-rev-parse: add "--not" flag to mark subsequent heads negative
Linus Torvalds authored Jun 26, 2005
541 if (!strcmp(arg, "--not")) {
542 show_type ^= REVERSED;
543 continue;
544 }
30b96fc Add --symbolic flag to git-rev-parse.
Junio C Hamano authored Aug 16, 2005
545 if (!strcmp(arg, "--symbolic")) {
a6d97d4 @gitster git-rev-parse --symbolic-full-name
gitster authored Jan 5, 2008
546 symbolic = SHOW_SYMBOLIC_ASIS;
547 continue;
548 }
549 if (!strcmp(arg, "--symbolic-full-name")) {
550 symbolic = SHOW_SYMBOLIC_FULL;
30b96fc Add --symbolic flag to git-rev-parse.
Junio C Hamano authored Aug 16, 2005
551 continue;
552 }
a45d346 @bertwesarg rev-parse: --abbrev-ref option to shorten ref name
bertwesarg authored Apr 13, 2009
553 if (!prefixcmp(arg, "--abbrev-ref") &&
554 (!arg[12] || arg[12] == '=')) {
555 abbrev_ref = 1;
556 abbrev_ref_strict = warn_ambiguous_refs;
557 if (arg[12] == '=') {
558 if (!strcmp(arg + 13, "strict"))
559 abbrev_ref_strict = 1;
560 else if (!strcmp(arg + 13, "loose"))
561 abbrev_ref_strict = 0;
562 else
563 die("unknown mode for %s", arg);
564 }
565 continue;
566 }
960bba0 Add "--all" flag to rev-parse that shows all refs
Linus Torvalds authored Jul 3, 2005
567 if (!strcmp(arg, "--all")) {
cb5d709 Add callback data to for_each_ref() family.
Junio C Hamano authored Sep 20, 2006
568 for_each_ref(show_reference, NULL);
960bba0 Add "--all" flag to rev-parse that shows all refs
Linus Torvalds authored Jul 3, 2005
569 continue;
570 }
ad3f9a7 @torvalds Add '--bisect' revision machinery argument
torvalds authored Oct 27, 2009
571 if (!strcmp(arg, "--bisect")) {
572 for_each_ref_in("refs/bisect/bad", show_reference, NULL);
573 for_each_ref_in("refs/bisect/good", anti_reference, NULL);
574 continue;
575 }
b09fe97 rev-parse --branches/--tags/--remotes=pattern
Ilari Liusvaara authored Jan 20, 2010
576 if (!prefixcmp(arg, "--branches=")) {
577 for_each_glob_ref_in(show_reference, arg + 11,
578 "refs/heads/", NULL);
579 continue;
580 }
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
581 if (!strcmp(arg, "--branches")) {
cb5d709 Add callback data to for_each_ref() family.
Junio C Hamano authored Sep 21, 2006
582 for_each_branch_ref(show_reference, NULL);
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
583 continue;
584 }
b09fe97 rev-parse --branches/--tags/--remotes=pattern
Ilari Liusvaara authored Jan 20, 2010
585 if (!prefixcmp(arg, "--tags=")) {
586 for_each_glob_ref_in(show_reference, arg + 7,
587 "refs/tags/", NULL);
588 continue;
589 }
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
590 if (!strcmp(arg, "--tags")) {
cb5d709 Add callback data to for_each_ref() family.
Junio C Hamano authored Sep 21, 2006
591 for_each_tag_ref(show_reference, NULL);
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
592 continue;
593 }
d08bae7 rev-parse --glob
Ilari Liusvaara authored Jan 20, 2010
594 if (!prefixcmp(arg, "--glob=")) {
595 for_each_glob_ref(show_reference, arg + 7, NULL);
596 continue;
597 }
b09fe97 rev-parse --branches/--tags/--remotes=pattern
Ilari Liusvaara authored Jan 20, 2010
598 if (!prefixcmp(arg, "--remotes=")) {
599 for_each_glob_ref_in(show_reference, arg + 10,
600 "refs/remotes/", NULL);
601 continue;
602 }
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
603 if (!strcmp(arg, "--remotes")) {
cb5d709 Add callback data to for_each_ref() family.
Junio C Hamano authored Sep 21, 2006
604 for_each_remote_ref(show_reference, NULL);
a62be77 @loops Add "--branches", "--tags" and "--remotes" options to git-rev-parse.
loops authored May 14, 2006
605 continue;
606 }
7cceca5 Add 'git rev-parse --show-toplevel' option.
Steven Drake authored Jan 12, 2010
607 if (!strcmp(arg, "--show-toplevel")) {
608 const char *work_tree = get_git_work_tree();
609 if (work_tree)
610 puts(work_tree);
611 continue;
612 }
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored Aug 16, 2005
613 if (!strcmp(arg, "--show-prefix")) {
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
614 if (prefix)
615 puts(prefix);
d288a70 [PATCH] Make "git diff" work inside relative subdirectories
Linus Torvalds authored Aug 17, 2005
616 continue;
617 }
5f94c73 rev-parse: --show-cdup
Junio C Hamano authored Dec 22, 2005
618 if (!strcmp(arg, "--show-cdup")) {
619 const char *pfx = prefix;
e90fdc3 @dscho Clean up work-tree handling
dscho authored Aug 1, 2007
620 if (!is_inside_work_tree()) {
621 const char *work_tree =
622 get_git_work_tree();
623 if (work_tree)
624 printf("%s\n", work_tree);
625 continue;
626 }
5f94c73 rev-parse: --show-cdup
Junio C Hamano authored Dec 23, 2005
627 while (pfx) {
628 pfx = strchr(pfx, '/');
629 if (pfx) {
630 pfx++;
631 printf("../");
632 }
633 }
634 putchar('\n');
635 continue;
636 }
a8783ee [PATCH] Add "--git-dir" flag to git-rev-parse
Linus Torvalds authored Sep 18, 2005
637 if (!strcmp(arg, "--git-dir")) {
638 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
639 static char cwd[PATH_MAX];
640 if (gitdir) {
641 puts(gitdir);
642 continue;
643 }
644 if (!prefix) {
645 puts(".git");
646 continue;
647 }
648 if (!getcwd(cwd, PATH_MAX))
0721c31 @trast Use die_errno() instead of die() when checking syscalls
trast authored Jun 27, 2009
649 die_errno("unable to get current working directory");
a8783ee [PATCH] Add "--git-dir" flag to git-rev-parse
Linus Torvalds authored Sep 18, 2005
650 printf("%s/.git\n", cwd);
651 continue;
652 }
6d9ba67 @dscho Commands requiring a work tree must not run in GIT_DIR
dscho authored Jan 23, 2007
653 if (!strcmp(arg, "--is-inside-git-dir")) {
654 printf("%s\n", is_inside_git_dir() ? "true"
655 : "false");
656 continue;
657 }
892c41b @matled introduce GIT_WORK_TREE to specify the work tree
matled authored Jun 6, 2007
658 if (!strcmp(arg, "--is-inside-work-tree")) {
659 printf("%s\n", is_inside_work_tree() ? "true"
660 : "false");
661 continue;
662 }
493c774 @matled rev-parse: introduce --is-bare-repository
matled authored Jun 3, 2007
663 if (!strcmp(arg, "--is-bare-repository")) {
664 printf("%s\n", is_bare_repository() ? "true"
665 : "false");
666 continue;
667 }
cc44c76 Mechanical conversion to use prefixcmp()
Junio C Hamano authored Feb 20, 2007
668 if (!prefixcmp(arg, "--since=")) {
c1babb1 [PATCH] Teach "git-rev-parse" about date-based cut-offs
Linus Torvalds authored Sep 20, 2005
669 show_datestring("--max-age=", arg+8);
670 continue;
671 }
cc44c76 Mechanical conversion to use prefixcmp()
Junio C Hamano authored Feb 20, 2007
672 if (!prefixcmp(arg, "--after=")) {
c1babb1 [PATCH] Teach "git-rev-parse" about date-based cut-offs
Linus Torvalds authored Sep 20, 2005
673 show_datestring("--max-age=", arg+8);
674 continue;
675 }
cc44c76 Mechanical conversion to use prefixcmp()
Junio C Hamano authored Feb 20, 2007
676 if (!prefixcmp(arg, "--before=")) {
c1babb1 [PATCH] Teach "git-rev-parse" about date-based cut-offs
Linus Torvalds authored Sep 20, 2005
677 show_datestring("--min-age=", arg+9);
678 continue;
679 }
cc44c76 Mechanical conversion to use prefixcmp()
Junio C Hamano authored Feb 20, 2007
680 if (!prefixcmp(arg, "--until=")) {
c1babb1 [PATCH] Teach "git-rev-parse" about date-based cut-offs
Linus Torvalds authored Sep 20, 2005
681 show_datestring("--min-age=", arg+8);
682 continue;
683 }
9523a4c Fix git-rev-parse over-eager errors
Linus Torvalds authored Feb 5, 2006
684 if (show_flag(arg) && verify)
b1b3596 @chriscool rev-parse: teach "--verify" to be quiet when using "-q" or "--quiet"
chriscool authored Apr 26, 2008
685 die_no_single_rev(quiet);
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
686 continue;
687 }
4866ccf Rationalize output selection in rev-parse.
Junio C Hamano authored Aug 24, 2005
688
689 /* Not a flag argument */
3dd4e73 Teach rev-parse the ... syntax.
Santi Béjar authored Jul 4, 2006
690 if (try_difference(arg))
691 continue;
2122f8b @dotdash rev-parse: Add support for the ^! and ^@ syntax
dotdash authored Jul 26, 2008
692 if (try_parent_shorthands(arg))
693 continue;
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
694 name = arg;
695 type = NORMAL;
696 if (*arg == '^') {
697 name++;
698 type = REVERSED;
800644c git-rev-parse: parse ".." before simple SHA1's
Linus Torvalds authored Jun 20, 2005
699 }
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
700 if (!get_sha1(name, sha1)) {
701 if (verify)
702 revs_count++;
703 else
704 show_rev(type, sha1, name);
800644c git-rev-parse: parse ".." before simple SHA1's
Linus Torvalds authored Jun 20, 2005
705 continue;
706 }
75ecfce @chriscool rev-parse: fix --verify to error out when passed junk after a good rev
chriscool authored Apr 26, 2008
707 if (verify)
708 die_no_single_rev(quiet);
9ad0a93 rev-parse lstat() workaround cleanup.
Linus Torvalds authored Feb 6, 2006
709 as_is = 1;
710 if (!show_file(arg))
711 continue;
e23d0b4 Fix filename verification when in a subdirectory
Linus Torvalds authored Apr 26, 2006
712 verify_filename(prefix, arg);
023d66e git-rev-parse: re-organize and be more careful
Linus Torvalds authored Jun 24, 2005
713 }
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
714 if (verify) {
715 if (revs_count == 1) {
716 show_rev(type, sha1, name);
717 return 0;
718 } else if (revs_count == 0 && show_default())
719 return 0;
b1b3596 @chriscool rev-parse: teach "--verify" to be quiet when using "-q" or "--quiet"
chriscool authored Apr 26, 2008
720 die_no_single_rev(quiet);
dfd1b74 @chriscool rev-parse --verify: do not output anything on error
chriscool authored May 11, 2008
721 } else
722 show_default();
178cb24 Add 'git-rev-parse' helper script
Linus Torvalds authored Jun 13, 2005
723 return 0;
724 }
Something went wrong with that request. Please try again.