1/*
2* rev-parse.c
3*
4* Copyright (C) Linus Torvalds, 2005
5*/
6#include "cache.h"
7#include "commit.h"
8#include "refs.h"
910
#define DO_REVS 1
11#define DO_NOREV 2
12#define DO_FLAGS 4
13#define DO_NONFLAGS 8
14static int filter = ~0;
1516
static char *def = NULL;
1718
#define NORMAL 0
19#define REVERSED 1
20static int show_type = NORMAL;
21static int symbolic = 0;
22static int output_sq = 0;
2324
static int revs_count = 0;
2526
/*
27* Some arguments are relevant "revision" arguments,
28* others are about output format or other details.
29* This sorts it all out.
30*/
31static int is_rev_argument(const char *arg)
32{
33static const char *rev_args[] = {
34"--bisect",
35"--header",
36"--max-age=",
37"--max-count=",
38"--merge-order",
39"--min-age=",
40"--no-merges",
41"--objects",
42"--parents",
43"--pretty",
44"--show-breaks",
45"--topo-order",
46"--unpacked",
47NULL
48};
49const char **p = rev_args;
5051
for (;;) {
52const char *str = *p++;
53int len;
54if (!str)
55return 0;
56len = strlen(str);
57if (!strcmp(arg, str) ||
58(str[len-1] == '=' && !strncmp(arg, str, len)))
59return 1;
60}
61}
6263
/* Output argument as a string, either SQ or normal */
64static void show(const char *arg)
65{
66if (output_sq) {
67int sq = '\'', ch;
6869
putchar(sq);
70while ((ch = *arg++)) {
71if (ch == sq)
72fputs("'\\'", stdout);
73putchar(ch);
74}
75putchar(sq);
76putchar(' ');
77}
78else
79puts(arg);
80}
8182
/* Output a revision, only if filter allows it */
83static void show_rev(int type, const unsigned char *sha1, const char *name)
84{
85if (!(filter & DO_REVS))
86return;
87def = NULL;
88revs_count++;
8990
if (type != show_type)
91putchar('^');
92if (symbolic && name)
93show(name);
94else
95show(sha1_to_hex(sha1));
96}
9798
/* Output a flag, only if filter allows it. */
99static void show_flag(char *arg)
100{
101if (!(filter & DO_FLAGS))
102return;
103if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
104show(arg);
105}
106107
static void show_default(void)
108{
109char *s = def;
110111
if (s) {
112unsigned char sha1[20];
113114
def = NULL;
115if (!get_sha1(s, sha1)) {
116show_rev(NORMAL, sha1, s);
117return;
118}
119}
120}
121122
static int show_reference(const char *refname, const unsigned char *sha1)
123{
124show_rev(NORMAL, sha1, refname);
125return 0;
126}
127128
int main(int argc, char **argv)
129{
130int i, as_is = 0, verify = 0;
131unsigned char sha1[20];
132const char *prefix = setup_git_directory();
133134
for (i = 1; i < argc; i++) {
135char *arg = argv[i];
136char *dotdot;
137138
if (as_is) {
139show(arg);
140continue;
141}
142if (*arg == '-') {
143if (!strcmp(arg, "--")) {
144as_is = 1;
145continue;
146}
147if (!strcmp(arg, "--default")) {
148def = argv[i+1];
149i++;
150continue;
151}
152if (!strcmp(arg, "--revs-only")) {
153filter &= ~DO_NOREV;
154continue;
155}
156if (!strcmp(arg, "--no-revs")) {
157filter &= ~DO_REVS;
158continue;
159}
160if (!strcmp(arg, "--flags")) {
161filter &= ~DO_NONFLAGS;
162continue;
163}
164if (!strcmp(arg, "--no-flags")) {
165filter &= ~DO_FLAGS;
166continue;
167}
168if (!strcmp(arg, "--verify")) {
169filter &= ~(DO_FLAGS|DO_NOREV);
170verify = 1;
171continue;
172}
173if (!strcmp(arg, "--sq")) {
174output_sq = 1;
175continue;
176}
177if (!strcmp(arg, "--not")) {
178show_type ^= REVERSED;
179continue;
180}
181if (!strcmp(arg, "--symbolic")) {
182symbolic = 1;
183continue;
184}
185if (!strcmp(arg, "--all")) {
186for_each_ref(show_reference);
187continue;
188}
189if (!strcmp(arg, "--show-prefix")) {
190if (prefix)
191puts(prefix);
192continue;
193}
194if (!strcmp(arg, "--git-dir")) {
195const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
196static char cwd[PATH_MAX];
197if (gitdir) {
198puts(gitdir);
199continue;
200}
201if (!prefix) {
202puts(".git");
203continue;
204}
205if (!getcwd(cwd, PATH_MAX))
206die("unable to get current working directory");
207printf("%s/.git\n", cwd);
208continue;
209}
210if (verify)
211die("Needed a single revision");
212show_flag(arg);
213continue;
214}
215216
/* Not a flag argument */
217dotdot = strstr(arg, "..");
218if (dotdot) {
219unsigned char end[20];
220char *n = dotdot+2;
221*dotdot = 0;
222if (!get_sha1(arg, sha1)) {
223if (!*n)
224n = "HEAD";
225if (!get_sha1(n, end)) {
226show_rev(NORMAL, end, n);
227show_rev(REVERSED, sha1, arg);
228continue;
229}
230}
231*dotdot = '.';
232}
233if (!get_sha1(arg, sha1)) {
234show_rev(NORMAL, sha1, arg);
235continue;
236}
237if (*arg == '^' && !get_sha1(arg+1, sha1)) {
238show_rev(REVERSED, sha1, arg+1);
239continue;
240}
241if (verify)
242die("Needed a single revision");
243if ((filter & (DO_NONFLAGS|DO_NOREV)) ==
244(DO_NONFLAGS|DO_NOREV))
245show(arg);
246}
247show_default();
248if (verify && revs_count != 1)
249die("Needed a single revision");
250return 0;
251}