🌐 AI搜索 & 代理 主页
blob: bb4949ad70364abdf7c3bbca357e5ebc42880614 [file] [log] [blame]
Linus Torvalds178cb242005-06-13 17:06:501/*
2 * rev-parse.c
3 *
4 * Copyright (C) Linus Torvalds, 2005
5 */
6#include "cache.h"
Linus Torvaldsa8be83f2005-06-21 03:28:097#include "commit.h"
Linus Torvalds960bba02005-07-03 20:07:528#include "refs.h"
Linus Torvaldsc1babb12005-09-20 21:13:249#include "quote.h"
Linus Torvaldsa8be83f2005-06-21 03:28:0910
Junio C Hamano4866ccf2005-08-24 21:30:0411#define DO_REVS 1
12#define DO_NOREV 2
13#define DO_FLAGS 4
14#define DO_NONFLAGS 8
15static int filter = ~0;
16
Linus Torvalds023d66e2005-06-24 17:12:5517static char *def = NULL;
Linus Torvalds023d66e2005-06-24 17:12:5518
Linus Torvalds042a4ed2005-06-26 18:34:3019#define NORMAL 0
20#define REVERSED 1
21static int show_type = NORMAL;
Junio C Hamano4866ccf2005-08-24 21:30:0422static int symbolic = 0;
23static int output_sq = 0;
24
25static int revs_count = 0;
Linus Torvalds042a4ed2005-06-26 18:34:3026
Linus Torvalds921d8652005-06-13 18:14:2027/*
28 * Some arguments are relevant "revision" arguments,
29 * others are about output format or other details.
30 * This sorts it all out.
31 */
32static int is_rev_argument(const char *arg)
33{
34 static const char *rev_args[] = {
Junio C Hamanoe091eb92005-10-05 21:49:5435 "--all",
Junio C Hamano5ccfb752005-08-09 02:31:3736 "--bisect",
Junio C Hamano5a83f3b2005-10-30 09:08:3537 "--dense",
Junio C Hamano4866ccf2005-08-24 21:30:0438 "--header",
39 "--max-age=",
40 "--max-count=",
41 "--merge-order",
42 "--min-age=",
Junio C Hamano5ccfb752005-08-09 02:31:3743 "--no-merges",
Junio C Hamano4866ccf2005-08-24 21:30:0444 "--objects",
45 "--parents",
46 "--pretty",
47 "--show-breaks",
Junio C Hamano5a83f3b2005-10-30 09:08:3548 "--sparse",
Junio C Hamano4866ccf2005-08-24 21:30:0449 "--topo-order",
50 "--unpacked",
Linus Torvalds921d8652005-06-13 18:14:2051 NULL
52 };
53 const char **p = rev_args;
54
55 for (;;) {
56 const char *str = *p++;
57 int len;
58 if (!str)
59 return 0;
60 len = strlen(str);
Junio C Hamano4866ccf2005-08-24 21:30:0461 if (!strcmp(arg, str) ||
62 (str[len-1] == '=' && !strncmp(arg, str, len)))
Linus Torvalds921d8652005-06-13 18:14:2063 return 1;
64 }
65}
66
Junio C Hamano4866ccf2005-08-24 21:30:0467/* Output argument as a string, either SQ or normal */
Junio C Hamano5bb2c652005-07-23 02:08:3268static void show(const char *arg)
69{
70 if (output_sq) {
71 int sq = '\'', ch;
72
73 putchar(sq);
74 while ((ch = *arg++)) {
75 if (ch == sq)
76 fputs("'\\'", stdout);
77 putchar(ch);
78 }
79 putchar(sq);
80 putchar(' ');
81 }
82 else
83 puts(arg);
84}
85
Junio C Hamano4866ccf2005-08-24 21:30:0486/* Output a revision, only if filter allows it */
Junio C Hamano30b96fc2005-08-16 19:36:4687static void show_rev(int type, const unsigned char *sha1, const char *name)
Linus Torvalds023d66e2005-06-24 17:12:5588{
Junio C Hamano4866ccf2005-08-24 21:30:0489 if (!(filter & DO_REVS))
Linus Torvalds023d66e2005-06-24 17:12:5590 return;
Junio C Hamano4866ccf2005-08-24 21:30:0491 def = NULL;
92 revs_count++;
Junio C Hamano5bb2c652005-07-23 02:08:3293
Junio C Hamano30b96fc2005-08-16 19:36:4694 if (type != show_type)
95 putchar('^');
96 if (symbolic && name)
97 show(name);
98 else
99 show(sha1_to_hex(sha1));
Linus Torvalds023d66e2005-06-24 17:12:55100}
101
Junio C Hamano4866ccf2005-08-24 21:30:04102/* Output a flag, only if filter allows it. */
103static void show_flag(char *arg)
Linus Torvalds023d66e2005-06-24 17:12:55104{
Junio C Hamano4866ccf2005-08-24 21:30:04105 if (!(filter & DO_FLAGS))
Linus Torvalds023d66e2005-06-24 17:12:55106 return;
Junio C Hamano4866ccf2005-08-24 21:30:04107 if (filter & (is_rev_argument(arg) ? DO_REVS : DO_NOREV))
Linus Torvalds0360e992005-08-23 17:47:54108 show(arg);
Linus Torvalds023d66e2005-06-24 17:12:55109}
110
Linus Torvalds023d66e2005-06-24 17:12:55111static void show_default(void)
112{
113 char *s = def;
114
115 if (s) {
116 unsigned char sha1[20];
117
118 def = NULL;
Junio C Hamano9938af62005-08-04 05:15:49119 if (!get_sha1(s, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46120 show_rev(NORMAL, sha1, s);
Linus Torvalds023d66e2005-06-24 17:12:55121 return;
122 }
Linus Torvalds023d66e2005-06-24 17:12:55123 }
124}
125
Linus Torvalds960bba02005-07-03 20:07:52126static int show_reference(const char *refname, const unsigned char *sha1)
127{
Junio C Hamano30b96fc2005-08-16 19:36:46128 show_rev(NORMAL, sha1, refname);
Linus Torvalds960bba02005-07-03 20:07:52129 return 0;
130}
131
Linus Torvaldsc1babb12005-09-20 21:13:24132static void show_datestring(const char *flag, const char *datestr)
133{
Linus Torvaldsc1babb12005-09-20 21:13:24134 static char buffer[100];
Linus Torvaldsc1babb12005-09-20 21:13:24135
136 /* date handling requires both flags and revs */
137 if ((filter & (DO_FLAGS | DO_REVS)) != (DO_FLAGS | DO_REVS))
138 return;
Linus Torvalds3c07b1d2005-11-15 03:29:06139 snprintf(buffer, sizeof(buffer), "%s%lu", flag, approxidate(datestr));
Linus Torvaldsc1babb12005-09-20 21:13:24140 show(buffer);
141}
142
Linus Torvalds7a3dd472005-10-18 07:16:45143static void show_file(const char *arg)
144{
Linus Torvalds7b34c2f2005-10-25 22:24:55145 show_default();
Linus Torvalds7a3dd472005-10-18 07:16:45146 if ((filter & (DO_NONFLAGS|DO_NOREV)) == (DO_NONFLAGS|DO_NOREV))
147 show(arg);
148}
149
Linus Torvalds178cb242005-06-13 17:06:50150int main(int argc, char **argv)
151{
Junio C Hamano4866ccf2005-08-24 21:30:04152 int i, as_is = 0, verify = 0;
Linus Torvalds178cb242005-06-13 17:06:50153 unsigned char sha1[20];
Linus Torvaldsd288a702005-08-17 01:06:34154 const char *prefix = setup_git_directory();
155
Linus Torvalds178cb242005-06-13 17:06:50156 for (i = 1; i < argc; i++) {
157 char *arg = argv[i];
158 char *dotdot;
159
160 if (as_is) {
Linus Torvalds7a3dd472005-10-18 07:16:45161 show_file(arg);
Linus Torvalds178cb242005-06-13 17:06:50162 continue;
163 }
164 if (*arg == '-') {
165 if (!strcmp(arg, "--")) {
Linus Torvalds178cb242005-06-13 17:06:50166 as_is = 1;
Linus Torvaldsa08b6502005-10-21 00:16:30167 /* Pass on the "--" if we show anything but files.. */
168 if (filter & (DO_FLAGS | DO_REVS))
169 show_file(arg);
Junio C Hamano4866ccf2005-08-24 21:30:04170 continue;
Linus Torvalds178cb242005-06-13 17:06:50171 }
172 if (!strcmp(arg, "--default")) {
Linus Torvalds178cb242005-06-13 17:06:50173 def = argv[i+1];
174 i++;
175 continue;
176 }
Linus Torvalds8ebb0182005-06-13 17:21:11177 if (!strcmp(arg, "--revs-only")) {
Junio C Hamano4866ccf2005-08-24 21:30:04178 filter &= ~DO_NOREV;
Linus Torvalds8ebb0182005-06-13 17:21:11179 continue;
180 }
181 if (!strcmp(arg, "--no-revs")) {
Junio C Hamano4866ccf2005-08-24 21:30:04182 filter &= ~DO_REVS;
Linus Torvalds8ebb0182005-06-13 17:21:11183 continue;
184 }
Linus Torvaldsf79b65a2005-07-06 17:08:08185 if (!strcmp(arg, "--flags")) {
Junio C Hamano4866ccf2005-08-24 21:30:04186 filter &= ~DO_NONFLAGS;
Linus Torvaldsf79b65a2005-07-06 17:08:08187 continue;
188 }
189 if (!strcmp(arg, "--no-flags")) {
Junio C Hamano4866ccf2005-08-24 21:30:04190 filter &= ~DO_FLAGS;
Linus Torvaldsf79b65a2005-07-06 17:08:08191 continue;
192 }
Linus Torvalds023d66e2005-06-24 17:12:55193 if (!strcmp(arg, "--verify")) {
Junio C Hamano4866ccf2005-08-24 21:30:04194 filter &= ~(DO_FLAGS|DO_NOREV);
195 verify = 1;
Linus Torvalds023d66e2005-06-24 17:12:55196 continue;
Linus Torvalds921d8652005-06-13 18:14:20197 }
Junio C Hamano5bb2c652005-07-23 02:08:32198 if (!strcmp(arg, "--sq")) {
199 output_sq = 1;
200 continue;
201 }
Linus Torvalds042a4ed2005-06-26 18:34:30202 if (!strcmp(arg, "--not")) {
203 show_type ^= REVERSED;
204 continue;
205 }
Junio C Hamano30b96fc2005-08-16 19:36:46206 if (!strcmp(arg, "--symbolic")) {
207 symbolic = 1;
208 continue;
209 }
Linus Torvalds960bba02005-07-03 20:07:52210 if (!strcmp(arg, "--all")) {
211 for_each_ref(show_reference);
212 continue;
213 }
Linus Torvaldsd288a702005-08-17 01:06:34214 if (!strcmp(arg, "--show-prefix")) {
Junio C Hamano4866ccf2005-08-24 21:30:04215 if (prefix)
216 puts(prefix);
Linus Torvaldsd288a702005-08-17 01:06:34217 continue;
218 }
Linus Torvaldsa8783ee2005-09-18 18:18:30219 if (!strcmp(arg, "--git-dir")) {
220 const char *gitdir = getenv(GIT_DIR_ENVIRONMENT);
221 static char cwd[PATH_MAX];
222 if (gitdir) {
223 puts(gitdir);
224 continue;
225 }
226 if (!prefix) {
227 puts(".git");
228 continue;
229 }
230 if (!getcwd(cwd, PATH_MAX))
231 die("unable to get current working directory");
232 printf("%s/.git\n", cwd);
233 continue;
234 }
Linus Torvaldsc1babb12005-09-20 21:13:24235 if (!strncmp(arg, "--since=", 8)) {
236 show_datestring("--max-age=", arg+8);
237 continue;
238 }
239 if (!strncmp(arg, "--after=", 8)) {
240 show_datestring("--max-age=", arg+8);
241 continue;
242 }
243 if (!strncmp(arg, "--before=", 9)) {
244 show_datestring("--min-age=", arg+9);
245 continue;
246 }
247 if (!strncmp(arg, "--until=", 8)) {
248 show_datestring("--min-age=", arg+8);
249 continue;
250 }
Junio C Hamano4866ccf2005-08-24 21:30:04251 if (verify)
252 die("Needed a single revision");
253 show_flag(arg);
Linus Torvalds178cb242005-06-13 17:06:50254 continue;
255 }
Junio C Hamano4866ccf2005-08-24 21:30:04256
257 /* Not a flag argument */
Linus Torvalds178cb242005-06-13 17:06:50258 dotdot = strstr(arg, "..");
259 if (dotdot) {
260 unsigned char end[20];
261 char *n = dotdot+2;
262 *dotdot = 0;
Junio C Hamano9938af62005-08-04 05:15:49263 if (!get_sha1(arg, sha1)) {
Linus Torvalds178cb242005-06-13 17:06:50264 if (!*n)
265 n = "HEAD";
Junio C Hamano9938af62005-08-04 05:15:49266 if (!get_sha1(n, end)) {
Junio C Hamano30b96fc2005-08-16 19:36:46267 show_rev(NORMAL, end, n);
268 show_rev(REVERSED, sha1, arg);
Linus Torvalds178cb242005-06-13 17:06:50269 continue;
270 }
271 }
272 *dotdot = '.';
273 }
Junio C Hamano9938af62005-08-04 05:15:49274 if (!get_sha1(arg, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46275 show_rev(NORMAL, sha1, arg);
Linus Torvalds800644c2005-06-20 15:29:13276 continue;
277 }
Junio C Hamano9938af62005-08-04 05:15:49278 if (*arg == '^' && !get_sha1(arg+1, sha1)) {
Junio C Hamano30b96fc2005-08-16 19:36:46279 show_rev(REVERSED, sha1, arg+1);
Linus Torvalds800644c2005-06-20 15:29:13280 continue;
281 }
Junio C Hamano4866ccf2005-08-24 21:30:04282 if (verify)
283 die("Needed a single revision");
Linus Torvaldsaf13cdf2005-10-28 19:41:49284 as_is = 1;
Linus Torvalds7a3dd472005-10-18 07:16:45285 show_file(arg);
Linus Torvalds178cb242005-06-13 17:06:50286 }
Linus Torvalds023d66e2005-06-24 17:12:55287 show_default();
Junio C Hamano4866ccf2005-08-24 21:30:04288 if (verify && revs_count != 1)
289 die("Needed a single revision");
Linus Torvalds178cb242005-06-13 17:06:50290 return 0;
291}