xdiff: post-process hunks to make them consistent.
[git/mingw/4msysgit/kblees.git] / name-rev.c
blobbad8a5377771e678624d01895ca0603070d6bede
1 #include <stdlib.h>
2 #include "cache.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "refs.h"
7 static const char name_rev_usage[] =
8 "git-name-rev [--tags] ( --all | --stdin | commitish [commitish...] )\n";
10 typedef struct rev_name {
11 const char *tip_name;
12 int merge_traversals;
13 int generation;
14 } rev_name;
16 static long cutoff = LONG_MAX;
18 static void name_rev(struct commit *commit,
19 const char *tip_name, int merge_traversals, int generation,
20 int deref)
22 struct rev_name *name = (struct rev_name *)commit->object.util;
23 struct commit_list *parents;
24 int parent_number = 1;
26 if (!commit->object.parsed)
27 parse_commit(commit);
29 if (commit->date < cutoff)
30 return;
32 if (deref) {
33 char *new_name = xmalloc(strlen(tip_name)+3);
34 strcpy(new_name, tip_name);
35 strcat(new_name, "^0");
36 tip_name = new_name;
38 if (generation)
39 die("generation: %d, but deref?", generation);
42 if (name == NULL) {
43 name = xmalloc(sizeof(rev_name));
44 commit->object.util = name;
45 goto copy_data;
46 } else if (name->merge_traversals > merge_traversals ||
47 (name->merge_traversals == merge_traversals &&
48 name->generation > generation)) {
49 copy_data:
50 name->tip_name = tip_name;
51 name->merge_traversals = merge_traversals;
52 name->generation = generation;
53 } else
54 return;
56 for (parents = commit->parents;
57 parents;
58 parents = parents->next, parent_number++) {
59 if (parent_number > 1) {
60 char *new_name = xmalloc(strlen(tip_name)+8);
62 if (generation > 0)
63 sprintf(new_name, "%s~%d^%d", tip_name,
64 generation, parent_number);
65 else
66 sprintf(new_name, "%s^%d", tip_name, parent_number);
68 name_rev(parents->item, new_name,
69 merge_traversals + 1 , 0, 0);
70 } else {
71 name_rev(parents->item, tip_name, merge_traversals,
72 generation + 1, 0);
77 static int tags_only = 0;
79 static int name_ref(const char *path, const unsigned char *sha1)
81 struct object *o = parse_object(sha1);
82 int deref = 0;
84 if (tags_only && strncmp(path, "refs/tags/", 10))
85 return 0;
87 while (o && o->type == tag_type) {
88 struct tag *t = (struct tag *) o;
89 if (!t->tagged)
90 break; /* broken repository */
91 o = parse_object(t->tagged->sha1);
92 deref = 1;
94 if (o && o->type == commit_type) {
95 struct commit *commit = (struct commit *)o;
97 if (!strncmp(path, "refs/heads/", 11))
98 path = path + 11;
99 else if (!strncmp(path, "refs/", 5))
100 path = path + 5;
102 name_rev(commit, strdup(path), 0, 0, deref);
104 return 0;
107 /* returns a static buffer */
108 static const char* get_rev_name(struct object *o)
110 static char buffer[1024];
111 struct rev_name *n = (struct rev_name *)o->util;
112 if (!n)
113 return "undefined";
115 if (!n->generation)
116 return n->tip_name;
118 snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
120 return buffer;
123 int main(int argc, char **argv)
125 struct object_list *revs = NULL;
126 struct object_list **walker = &revs;
127 int as_is = 0, all = 0, transform_stdin = 0;
129 setup_git_directory();
130 git_config(git_default_config);
132 if (argc < 2)
133 usage(name_rev_usage);
135 for (--argc, ++argv; argc; --argc, ++argv) {
136 unsigned char sha1[20];
137 struct object *o;
138 struct commit *commit;
140 if (!as_is && (*argv)[0] == '-') {
141 if (!strcmp(*argv, "--")) {
142 as_is = 1;
143 continue;
144 } else if (!strcmp(*argv, "--tags")) {
145 tags_only = 1;
146 continue;
147 } else if (!strcmp(*argv, "--all")) {
148 if (argc > 1)
149 die("Specify either a list, or --all, not both!");
150 all = 1;
151 cutoff = 0;
152 continue;
153 } else if (!strcmp(*argv, "--stdin")) {
154 if (argc > 1)
155 die("Specify either a list, or --stdin, not both!");
156 transform_stdin = 1;
157 cutoff = 0;
158 continue;
160 usage(name_rev_usage);
163 if (get_sha1(*argv, sha1)) {
164 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
165 *argv);
166 continue;
169 o = deref_tag(parse_object(sha1), *argv, 0);
170 if (!o || o->type != commit_type) {
171 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
172 *argv);
173 continue;
176 commit = (struct commit *)o;
178 if (cutoff > commit->date)
179 cutoff = commit->date;
181 object_list_append((struct object *)commit, walker);
182 (*walker)->name = *argv;
183 walker = &((*walker)->next);
186 for_each_ref(name_ref);
188 if (transform_stdin) {
189 char buffer[2048];
190 char *p, *p_start;
192 while (!feof(stdin)) {
193 int forty = 0;
194 p = fgets(buffer, sizeof(buffer), stdin);
195 if (!p)
196 break;
198 for (p_start = p; *p; p++) {
199 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
200 if (!ishex(*p))
201 forty = 0;
202 else if (++forty == 40 &&
203 !ishex(*(p+1))) {
204 unsigned char sha1[40];
205 const char *name = "undefined";
206 char c = *(p+1);
208 forty = 0;
210 *(p+1) = 0;
211 if (!get_sha1(p - 39, sha1)) {
212 struct object *o =
213 lookup_object(sha1);
214 if (o)
215 name = get_rev_name(o);
217 *(p+1) = c;
219 if (!strcmp(name, "undefined"))
220 continue;
222 fwrite(p_start, p - p_start + 1, 1,
223 stdout);
224 printf(" (%s)", name);
225 p_start = p + 1;
229 /* flush */
230 if (p_start != p)
231 fwrite(p_start, p - p_start, 1, stdout);
233 } else if (all) {
234 int i;
236 for (i = 0; i < obj_allocs; i++)
237 if (objs[i])
238 printf("%s %s\n", sha1_to_hex(objs[i]->sha1),
239 get_rev_name(objs[i]));
240 } else
241 for ( ; revs; revs = revs->next)
242 printf("%s %s\n", revs->name, get_rev_name(revs->item));
244 return 0;