name-rev: introduce the --refs=<pattern> option
[git/debian.git] / builtin-name-rev.c
blob36f1ba6d384d963c3ef3af1c82aff360f2409995
1 #include "builtin.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 | --refs=<pattern>] ( --all | --stdin | committish [committish...] )\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->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->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 struct name_ref_data {
78 int tags_only;
79 const char *ref_filter;
82 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
84 struct object *o = parse_object(sha1);
85 struct name_ref_data *data = cb_data;
86 int deref = 0;
88 if (data->tags_only && strncmp(path, "refs/tags/", 10))
89 return 0;
91 if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
92 return 0;
94 while (o && o->type == OBJ_TAG) {
95 struct tag *t = (struct tag *) o;
96 if (!t->tagged)
97 break; /* broken repository */
98 o = parse_object(t->tagged->sha1);
99 deref = 1;
101 if (o && o->type == OBJ_COMMIT) {
102 struct commit *commit = (struct commit *)o;
104 if (!strncmp(path, "refs/heads/", 11))
105 path = path + 11;
106 else if (!strncmp(path, "refs/", 5))
107 path = path + 5;
109 name_rev(commit, xstrdup(path), 0, 0, deref);
111 return 0;
114 /* returns a static buffer */
115 static const char* get_rev_name(struct object *o)
117 static char buffer[1024];
118 struct rev_name *n;
119 struct commit *c;
121 if (o->type != OBJ_COMMIT)
122 return "undefined";
123 c = (struct commit *) o;
124 n = c->util;
125 if (!n)
126 return "undefined";
128 if (!n->generation)
129 return n->tip_name;
131 snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
133 return buffer;
136 int cmd_name_rev(int argc, const char **argv, const char *prefix)
138 struct object_array revs = { 0, 0, NULL };
139 int as_is = 0, all = 0, transform_stdin = 0;
140 struct name_ref_data data = { 0, NULL };
142 git_config(git_default_config);
144 if (argc < 2)
145 usage(name_rev_usage);
147 for (--argc, ++argv; argc; --argc, ++argv) {
148 unsigned char sha1[20];
149 struct object *o;
150 struct commit *commit;
152 if (!as_is && (*argv)[0] == '-') {
153 if (!strcmp(*argv, "--")) {
154 as_is = 1;
155 continue;
156 } else if (!strcmp(*argv, "--tags")) {
157 data.tags_only = 1;
158 continue;
159 } else if (!strncmp(*argv, "--refs=", 7)) {
160 data.ref_filter = *argv + 7;
161 continue;
162 } else if (!strcmp(*argv, "--all")) {
163 if (argc > 1)
164 die("Specify either a list, or --all, not both!");
165 all = 1;
166 cutoff = 0;
167 continue;
168 } else if (!strcmp(*argv, "--stdin")) {
169 if (argc > 1)
170 die("Specify either a list, or --stdin, not both!");
171 transform_stdin = 1;
172 cutoff = 0;
173 continue;
175 usage(name_rev_usage);
178 if (get_sha1(*argv, sha1)) {
179 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
180 *argv);
181 continue;
184 o = deref_tag(parse_object(sha1), *argv, 0);
185 if (!o || o->type != OBJ_COMMIT) {
186 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
187 *argv);
188 continue;
191 commit = (struct commit *)o;
193 if (cutoff > commit->date)
194 cutoff = commit->date;
196 add_object_array((struct object *)commit, *argv, &revs);
199 for_each_ref(name_ref, &data);
201 if (transform_stdin) {
202 char buffer[2048];
203 char *p, *p_start;
205 while (!feof(stdin)) {
206 int forty = 0;
207 p = fgets(buffer, sizeof(buffer), stdin);
208 if (!p)
209 break;
211 for (p_start = p; *p; p++) {
212 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
213 if (!ishex(*p))
214 forty = 0;
215 else if (++forty == 40 &&
216 !ishex(*(p+1))) {
217 unsigned char sha1[40];
218 const char *name = "undefined";
219 char c = *(p+1);
221 forty = 0;
223 *(p+1) = 0;
224 if (!get_sha1(p - 39, sha1)) {
225 struct object *o =
226 lookup_object(sha1);
227 if (o)
228 name = get_rev_name(o);
230 *(p+1) = c;
232 if (!strcmp(name, "undefined"))
233 continue;
235 fwrite(p_start, p - p_start + 1, 1,
236 stdout);
237 printf(" (%s)", name);
238 p_start = p + 1;
242 /* flush */
243 if (p_start != p)
244 fwrite(p_start, p - p_start, 1, stdout);
246 } else if (all) {
247 int i, max;
249 max = get_max_object_index();
250 for (i = 0; i < max; i++) {
251 struct object * obj = get_indexed_object(i);
252 if (!obj)
253 continue;
254 printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
256 } else {
257 int i;
258 for (i = 0; i < revs.nr; i++)
259 printf("%s %s\n",
260 revs.objects[i].name,
261 get_rev_name(revs.objects[i].item));
264 return 0;