[PATCH] local-pull: implement fetch_ref()
[git/jnareb-git.git] / rev-parse.c
blob40707ac6ca7258878b47ed72074b3cf7364d3a56
1 /*
2 * rev-parse.c
4 * Copyright (C) Linus Torvalds, 2005
5 */
6 #include "cache.h"
7 #include "commit.h"
9 static int get_extended_sha1(char *name, unsigned char *sha1);
12 * Some arguments are relevant "revision" arguments,
13 * others are about output format or other details.
14 * This sorts it all out.
16 static int is_rev_argument(const char *arg)
18 static const char *rev_args[] = {
19 "--max-count=",
20 "--max-age=",
21 "--min-age=",
22 "--merge-order",
23 NULL
25 const char **p = rev_args;
27 for (;;) {
28 const char *str = *p++;
29 int len;
30 if (!str)
31 return 0;
32 len = strlen(str);
33 if (!strncmp(arg, str, len))
34 return 1;
38 static int get_parent(char *name, unsigned char *result, int idx)
40 unsigned char sha1[20];
41 int ret = get_extended_sha1(name, sha1);
42 struct commit *commit;
43 struct commit_list *p;
45 if (ret)
46 return ret;
47 commit = lookup_commit_reference(sha1);
48 if (!commit)
49 return -1;
50 if (parse_commit(commit))
51 return -1;
52 p = commit->parents;
53 while (p) {
54 if (!--idx) {
55 memcpy(result, p->item->object.sha1, 20);
56 return 0;
58 p = p->next;
60 return -1;
64 * This is like "get_sha1()", except it allows "sha1 expressions",
65 * notably "xyz^" for "parent of xyz"
67 static int get_extended_sha1(char *name, unsigned char *sha1)
69 int parent;
70 int len = strlen(name);
72 parent = 1;
73 if (len > 2 && name[len-1] >= '1' && name[len-1] <= '9') {
74 parent = name[len-1] - '0';
75 len--;
77 if (len > 1 && name[len-1] == '^') {
78 int ret;
79 name[len-1] = 0;
80 ret = get_parent(name, sha1, parent);
81 name[len-1] = '^';
82 if (!ret)
83 return 0;
85 return get_sha1(name, sha1);
88 int main(int argc, char **argv)
90 int i, as_is = 0, revs_only = 0, no_revs = 0;
91 char *def = NULL;
92 unsigned char sha1[20];
94 for (i = 1; i < argc; i++) {
95 char *arg = argv[i];
96 char *dotdot;
98 if (as_is) {
99 printf("%s\n", arg);
100 continue;
102 if (*arg == '-') {
103 if (!strcmp(arg, "--")) {
104 if (def) {
105 printf("%s\n", def);
106 def = NULL;
108 if (revs_only)
109 break;
110 as_is = 1;
112 if (!strcmp(arg, "--default")) {
113 if (def)
114 printf("%s\n", def);
115 def = argv[i+1];
116 i++;
117 continue;
119 if (!strcmp(arg, "--revs-only")) {
120 revs_only = 1;
121 continue;
123 if (!strcmp(arg, "--no-revs")) {
124 no_revs = 1;
125 continue;
127 if (revs_only | no_revs) {
128 if (is_rev_argument(arg) != revs_only)
129 continue;
131 printf("%s\n", arg);
132 continue;
134 dotdot = strstr(arg, "..");
135 if (dotdot) {
136 unsigned char end[20];
137 char *n = dotdot+2;
138 *dotdot = 0;
139 if (!get_extended_sha1(arg, sha1)) {
140 if (!*n)
141 n = "HEAD";
142 if (!get_extended_sha1(n, end)) {
143 if (no_revs)
144 continue;
145 def = NULL;
146 printf("%s\n", sha1_to_hex(end));
147 printf("^%s\n", sha1_to_hex(sha1));
148 continue;
151 *dotdot = '.';
153 if (!get_extended_sha1(arg, sha1)) {
154 if (no_revs)
155 continue;
156 def = NULL;
157 printf("%s\n", sha1_to_hex(sha1));
158 continue;
160 if (*arg == '^' && !get_extended_sha1(arg+1, sha1)) {
161 if (no_revs)
162 continue;
163 def = NULL;
164 printf("^%s\n", sha1_to_hex(sha1));
165 continue;
167 if (def) {
168 printf("%s\n", def);
169 def = NULL;
171 if (revs_only)
172 continue;
173 printf("%s\n", arg);
175 if (def)
176 printf("%s\n", def);
177 return 0;