gitweb: Use "return" instead of "return undef" for some subs
[git/git-p4.git] / builtin-name-rev.c
blob52886b69b068eca899bea574c5d9790395eeb006
1 #include <stdlib.h>
2 #include "builtin.h"
3 #include "cache.h"
4 #include "commit.h"
5 #include "tag.h"
6 #include "refs.h"
8 static const char name_rev_usage[] =
9 "git-name-rev [--tags] ( --all | --stdin | committish [committish...] )\n";
11 typedef struct rev_name {
12 const char *tip_name;
13 int merge_traversals;
14 int generation;
15 } rev_name;
17 static long cutoff = LONG_MAX;
19 static void name_rev(struct commit *commit,
20 const char *tip_name, int merge_traversals, int generation,
21 int deref)
23 struct rev_name *name = (struct rev_name *)commit->util;
24 struct commit_list *parents;
25 int parent_number = 1;
27 if (!commit->object.parsed)
28 parse_commit(commit);
30 if (commit->date < cutoff)
31 return;
33 if (deref) {
34 char *new_name = xmalloc(strlen(tip_name)+3);
35 strcpy(new_name, tip_name);
36 strcat(new_name, "^0");
37 tip_name = new_name;
39 if (generation)
40 die("generation: %d, but deref?", generation);
43 if (name == NULL) {
44 name = xmalloc(sizeof(rev_name));
45 commit->util = name;
46 goto copy_data;
47 } else if (name->merge_traversals > merge_traversals ||
48 (name->merge_traversals == merge_traversals &&
49 name->generation > generation)) {
50 copy_data:
51 name->tip_name = tip_name;
52 name->merge_traversals = merge_traversals;
53 name->generation = generation;
54 } else
55 return;
57 for (parents = commit->parents;
58 parents;
59 parents = parents->next, parent_number++) {
60 if (parent_number > 1) {
61 char *new_name = xmalloc(strlen(tip_name)+8);
63 if (generation > 0)
64 sprintf(new_name, "%s~%d^%d", tip_name,
65 generation, parent_number);
66 else
67 sprintf(new_name, "%s^%d", tip_name, parent_number);
69 name_rev(parents->item, new_name,
70 merge_traversals + 1 , 0, 0);
71 } else {
72 name_rev(parents->item, tip_name, merge_traversals,
73 generation + 1, 0);
78 static int tags_only;
80 static int name_ref(const char *path, const unsigned char *sha1)
82 struct object *o = parse_object(sha1);
83 int deref = 0;
85 if (tags_only && strncmp(path, "refs/tags/", 10))
86 return 0;
88 while (o && o->type == OBJ_TAG) {
89 struct tag *t = (struct tag *) o;
90 if (!t->tagged)
91 break; /* broken repository */
92 o = parse_object(t->tagged->sha1);
93 deref = 1;
95 if (o && o->type == OBJ_COMMIT) {
96 struct commit *commit = (struct commit *)o;
98 if (!strncmp(path, "refs/heads/", 11))
99 path = path + 11;
100 else if (!strncmp(path, "refs/", 5))
101 path = path + 5;
103 name_rev(commit, xstrdup(path), 0, 0, deref);
105 return 0;
108 /* returns a static buffer */
109 static const char* get_rev_name(struct object *o)
111 static char buffer[1024];
112 struct rev_name *n;
113 struct commit *c;
115 if (o->type != OBJ_COMMIT)
116 return "undefined";
117 c = (struct commit *) o;
118 n = c->util;
119 if (!n)
120 return "undefined";
122 if (!n->generation)
123 return n->tip_name;
125 snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
127 return buffer;
130 int cmd_name_rev(int argc, const char **argv, const char *prefix)
132 struct object_array revs = { 0, 0, NULL };
133 int as_is = 0, all = 0, transform_stdin = 0;
135 git_config(git_default_config);
137 if (argc < 2)
138 usage(name_rev_usage);
140 for (--argc, ++argv; argc; --argc, ++argv) {
141 unsigned char sha1[20];
142 struct object *o;
143 struct commit *commit;
145 if (!as_is && (*argv)[0] == '-') {
146 if (!strcmp(*argv, "--")) {
147 as_is = 1;
148 continue;
149 } else if (!strcmp(*argv, "--tags")) {
150 tags_only = 1;
151 continue;
152 } else if (!strcmp(*argv, "--all")) {
153 if (argc > 1)
154 die("Specify either a list, or --all, not both!");
155 all = 1;
156 cutoff = 0;
157 continue;
158 } else if (!strcmp(*argv, "--stdin")) {
159 if (argc > 1)
160 die("Specify either a list, or --stdin, not both!");
161 transform_stdin = 1;
162 cutoff = 0;
163 continue;
165 usage(name_rev_usage);
168 if (get_sha1(*argv, sha1)) {
169 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
170 *argv);
171 continue;
174 o = deref_tag(parse_object(sha1), *argv, 0);
175 if (!o || o->type != OBJ_COMMIT) {
176 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
177 *argv);
178 continue;
181 commit = (struct commit *)o;
183 if (cutoff > commit->date)
184 cutoff = commit->date;
186 add_object_array((struct object *)commit, *argv, &revs);
189 for_each_ref(name_ref);
191 if (transform_stdin) {
192 char buffer[2048];
193 char *p, *p_start;
195 while (!feof(stdin)) {
196 int forty = 0;
197 p = fgets(buffer, sizeof(buffer), stdin);
198 if (!p)
199 break;
201 for (p_start = p; *p; p++) {
202 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
203 if (!ishex(*p))
204 forty = 0;
205 else if (++forty == 40 &&
206 !ishex(*(p+1))) {
207 unsigned char sha1[40];
208 const char *name = "undefined";
209 char c = *(p+1);
211 forty = 0;
213 *(p+1) = 0;
214 if (!get_sha1(p - 39, sha1)) {
215 struct object *o =
216 lookup_object(sha1);
217 if (o)
218 name = get_rev_name(o);
220 *(p+1) = c;
222 if (!strcmp(name, "undefined"))
223 continue;
225 fwrite(p_start, p - p_start + 1, 1,
226 stdout);
227 printf(" (%s)", name);
228 p_start = p + 1;
232 /* flush */
233 if (p_start != p)
234 fwrite(p_start, p - p_start, 1, stdout);
236 } else if (all) {
237 int i, max;
239 max = get_max_object_index();
240 for (i = 0; i < max; i++) {
241 struct object * obj = get_indexed_object(i);
242 if (!obj)
243 continue;
244 printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
246 } else {
247 int i;
248 for (i = 0; i < revs.nr; i++)
249 printf("%s %s\n",
250 revs.objects[i].name,
251 get_rev_name(revs.objects[i].item));
254 return 0;