[PATCH] Fixes problem with --merge-order head ^head
[git/mjg.git] / rev-parse.c
blobfd5f2ddca36ea45cd9feaf706a53aa66956db233
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
8 /*
9 * Some arguments are relevant "revision" arguments,
10 * others are about output format or other details.
11 * This sorts it all out.
13 static int is_rev_argument(const char *arg)
15 static const char *rev_args[] = {
16 "--max-count=",
17 "--max-age=",
18 "--min-age=",
19 "--merge-order",
20 NULL
22 const char **p = rev_args;
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;
35 int main(int argc, char **argv)
37 int i, as_is = 0, revs_only = 0, no_revs = 0;
38 char *def = NULL;
39 unsigned char sha1[20];
41 for (i = 1; i < argc; i++) {
42 char *arg = argv[i];
43 char *dotdot;
45 if (as_is) {
46 printf("%s\n", arg);
47 continue;
49 if (*arg == '-') {
50 if (!strcmp(arg, "--")) {
51 if (def) {
52 printf("%s\n", def);
53 def = NULL;
55 if (revs_only)
56 break;
57 as_is = 1;
59 if (!strcmp(arg, "--default")) {
60 if (def)
61 printf("%s\n", def);
62 def = argv[i+1];
63 i++;
64 continue;
66 if (!strcmp(arg, "--revs-only")) {
67 revs_only = 1;
68 continue;
70 if (!strcmp(arg, "--no-revs")) {
71 no_revs = 1;
72 continue;
74 if (revs_only | no_revs) {
75 if (is_rev_argument(arg) != revs_only)
76 continue;
78 printf("%s\n", arg);
79 continue;
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;
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;
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;
112 *dotdot = '.';
114 if (revs_only)
115 continue;
116 def = NULL;
117 printf("%s\n", arg);
119 if (def)
120 printf("%s\n", def);
121 return 0;