1/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
7
8/*
9 * Some arguments are relevant "revision" arguments,
10 * others are about output format or other details.
11 * This sorts it all out.
12 */
13static int is_rev_argument(const char *arg)
14{
15 static const char *rev_args[] = {
16 "--max-count=",
17 "--max-age=",
18 "--min-age=",
19 "--merge-order",
20 NULL
21 };
22 const char **p = rev_args;
23
24 for (;;) {
25 const char *str = *p++;
26 int len;
27 if (!str)
28 return 0;
29 len = strlen(str);
30 if (!strncmp(arg, str, len))
31 return 1;
32 }
33}
34
35int main(int argc, char **argv)
36{
37 int i, as_is = 0, revs_only = 0, no_revs = 0;
38 char *def = NULL;
39 unsigned char sha1[20];
40
41 for (i = 1; i < argc; i++) {
42 char *arg = argv[i];
43 char *dotdot;
44
45 if (as_is) {
46 printf("%s\n", arg);
47 continue;
48 }
49 if (*arg == '-') {
50 if (!strcmp(arg, "--")) {
51 if (def) {
52 printf("%s\n", def);
53 def = NULL;
54 }
55 if (revs_only)
56 break;
57 as_is = 1;
58 }
59 if (!strcmp(arg, "--default")) {
60 if (def)
61 printf("%s\n", def);
62 def = argv[i+1];
63 i++;
64 continue;
65 }
66 if (!strcmp(arg, "--revs-only")) {
67 revs_only = 1;
68 continue;
69 }
70 if (!strcmp(arg, "--no-revs")) {
71 no_revs = 1;
72 continue;
73 }
74 if (revs_only | no_revs) {
75 if (is_rev_argument(arg) != revs_only)
76 continue;
77 }
78 printf("%s\n", arg);
79 continue;
80 }
81 if (!get_sha1(arg, sha1)) {
82 if (no_revs)
83 continue;
84 def = NULL;
85 printf("%s\n", sha1_to_hex(sha1));
86 continue;
87 }
88 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
89 if (no_revs)
90 continue;
91 def = NULL;
92 printf("^%s\n", sha1_to_hex(sha1));
93 continue;
94 }
95 dotdot = strstr(arg, "..");
96 if (dotdot) {
97 unsigned char end[20];
98 char *n = dotdot+2;
99 *dotdot = 0;
100 if (!get_sha1(arg, sha1)) {
101 if (!*n)
102 n = "HEAD";
103 if (!get_sha1(n, end)) {
104 if (no_revs)
105 continue;
106 def = NULL;
107 printf("%s\n", sha1_to_hex(end));
108 printf("^%s\n", sha1_to_hex(sha1));
109 continue;
110 }
111 }
112 *dotdot = '.';
113 }
114 if (revs_only)
115 continue;
116 def = NULL;
117 printf("%s\n", arg);
118 }
119 if (def)
120 printf("%s\n", def);
121 return 0;
122}