Teach read-tree 2-way merge to ignore intermediate symlinks
[alt-git.git] / builtin-name-rev.c
blob2d94eaaa6a90f2efe4401873930533b12b85522f
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "refs.h"
7 #define CUTOFF_DATE_SLOP 86400 /* one day */
9 static const char name_rev_usage[] =
10 "git-name-rev [--tags | --refs=<pattern>] ( --all | --stdin | committish [committish...] )\n";
12 typedef struct rev_name {
13 const char *tip_name;
14 int merge_traversals;
15 int generation;
16 } rev_name;
18 static long cutoff = LONG_MAX;
20 static void name_rev(struct commit *commit,
21 const char *tip_name, int merge_traversals, int generation,
22 int deref)
24 struct rev_name *name = (struct rev_name *)commit->util;
25 struct commit_list *parents;
26 int parent_number = 1;
28 if (!commit->object.parsed)
29 parse_commit(commit);
31 if (commit->date < cutoff)
32 return;
34 if (deref) {
35 char *new_name = xmalloc(strlen(tip_name)+3);
36 strcpy(new_name, tip_name);
37 strcat(new_name, "^0");
38 tip_name = new_name;
40 if (generation)
41 die("generation: %d, but deref?", generation);
44 if (name == NULL) {
45 name = xmalloc(sizeof(rev_name));
46 commit->util = name;
47 goto copy_data;
48 } else if (name->merge_traversals > merge_traversals ||
49 (name->merge_traversals == merge_traversals &&
50 name->generation > generation)) {
51 copy_data:
52 name->tip_name = tip_name;
53 name->merge_traversals = merge_traversals;
54 name->generation = generation;
55 } else
56 return;
58 for (parents = commit->parents;
59 parents;
60 parents = parents->next, parent_number++) {
61 if (parent_number > 1) {
62 int len = strlen(tip_name);
63 char *new_name = xmalloc(len +
64 1 + decimal_length(generation) + /* ~<n> */
65 1 + 2 + /* ^NN */
66 1);
68 if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
69 len -= 2;
70 if (generation > 0)
71 sprintf(new_name, "%.*s~%d^%d", len, tip_name,
72 generation, parent_number);
73 else
74 sprintf(new_name, "%.*s^%d", len, tip_name,
75 parent_number);
77 name_rev(parents->item, new_name,
78 merge_traversals + 1 , 0, 0);
79 } else {
80 name_rev(parents->item, tip_name, merge_traversals,
81 generation + 1, 0);
86 struct name_ref_data {
87 int tags_only;
88 const char *ref_filter;
91 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
93 struct object *o = parse_object(sha1);
94 struct name_ref_data *data = cb_data;
95 int deref = 0;
97 if (data->tags_only && prefixcmp(path, "refs/tags/"))
98 return 0;
100 if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
101 return 0;
103 while (o && o->type == OBJ_TAG) {
104 struct tag *t = (struct tag *) o;
105 if (!t->tagged)
106 break; /* broken repository */
107 o = parse_object(t->tagged->sha1);
108 deref = 1;
110 if (o && o->type == OBJ_COMMIT) {
111 struct commit *commit = (struct commit *)o;
113 if (!prefixcmp(path, "refs/heads/"))
114 path = path + 11;
115 else if (!prefixcmp(path, "refs/"))
116 path = path + 5;
118 name_rev(commit, xstrdup(path), 0, 0, deref);
120 return 0;
123 /* returns a static buffer */
124 static const char* get_rev_name(struct object *o)
126 static char buffer[1024];
127 struct rev_name *n;
128 struct commit *c;
130 if (o->type != OBJ_COMMIT)
131 return "undefined";
132 c = (struct commit *) o;
133 n = c->util;
134 if (!n)
135 return "undefined";
137 if (!n->generation)
138 return n->tip_name;
139 else {
140 int len = strlen(n->tip_name);
141 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
142 len -= 2;
143 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
144 n->generation);
146 return buffer;
150 int cmd_name_rev(int argc, const char **argv, const char *prefix)
152 struct object_array revs = { 0, 0, NULL };
153 int as_is = 0, all = 0, transform_stdin = 0;
154 struct name_ref_data data = { 0, NULL };
156 git_config(git_default_config);
158 if (argc < 2)
159 usage(name_rev_usage);
161 for (--argc, ++argv; argc; --argc, ++argv) {
162 unsigned char sha1[20];
163 struct object *o;
164 struct commit *commit;
166 if (!as_is && (*argv)[0] == '-') {
167 if (!strcmp(*argv, "--")) {
168 as_is = 1;
169 continue;
170 } else if (!strcmp(*argv, "--tags")) {
171 data.tags_only = 1;
172 continue;
173 } else if (!prefixcmp(*argv, "--refs=")) {
174 data.ref_filter = *argv + 7;
175 continue;
176 } else if (!strcmp(*argv, "--all")) {
177 if (argc > 1)
178 die("Specify either a list, or --all, not both!");
179 all = 1;
180 cutoff = 0;
181 continue;
182 } else if (!strcmp(*argv, "--stdin")) {
183 if (argc > 1)
184 die("Specify either a list, or --stdin, not both!");
185 transform_stdin = 1;
186 cutoff = 0;
187 continue;
189 usage(name_rev_usage);
192 if (get_sha1(*argv, sha1)) {
193 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
194 *argv);
195 continue;
198 o = deref_tag(parse_object(sha1), *argv, 0);
199 if (!o || o->type != OBJ_COMMIT) {
200 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
201 *argv);
202 continue;
205 commit = (struct commit *)o;
207 if (cutoff > commit->date)
208 cutoff = commit->date;
210 add_object_array((struct object *)commit, *argv, &revs);
213 if (cutoff)
214 cutoff = cutoff - CUTOFF_DATE_SLOP;
215 for_each_ref(name_ref, &data);
217 if (transform_stdin) {
218 char buffer[2048];
219 char *p, *p_start;
221 while (!feof(stdin)) {
222 int forty = 0;
223 p = fgets(buffer, sizeof(buffer), stdin);
224 if (!p)
225 break;
227 for (p_start = p; *p; p++) {
228 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
229 if (!ishex(*p))
230 forty = 0;
231 else if (++forty == 40 &&
232 !ishex(*(p+1))) {
233 unsigned char sha1[40];
234 const char *name = "undefined";
235 char c = *(p+1);
237 forty = 0;
239 *(p+1) = 0;
240 if (!get_sha1(p - 39, sha1)) {
241 struct object *o =
242 lookup_object(sha1);
243 if (o)
244 name = get_rev_name(o);
246 *(p+1) = c;
248 if (!strcmp(name, "undefined"))
249 continue;
251 fwrite(p_start, p - p_start + 1, 1,
252 stdout);
253 printf(" (%s)", name);
254 p_start = p + 1;
258 /* flush */
259 if (p_start != p)
260 fwrite(p_start, p - p_start, 1, stdout);
262 } else if (all) {
263 int i, max;
265 max = get_max_object_index();
266 for (i = 0; i < max; i++) {
267 struct object * obj = get_indexed_object(i);
268 if (!obj)
269 continue;
270 printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
272 } else {
273 int i;
274 for (i = 0; i < revs.nr; i++)
275 printf("%s %s\n",
276 revs.objects[i].name,
277 get_rev_name(revs.objects[i].item));
280 return 0;