Make "git gc" pack all refs by default
[git/git.git] / builtin-name-rev.c
1 #include "builtin.h"
2 #include "cache.h"
3 #include "commit.h"
4 #include "tag.h"
5 #include "refs.h"
6
7 static const char name_rev_usage[] =
8 "git-name-rev [--tags | --refs=<pattern>] ( --all | --stdin | committish [committish...] )\n";
9
10 typedef struct rev_name {
11 const char *tip_name;
12 int merge_traversals;
13 int generation;
14 } rev_name;
15
16 static long cutoff = LONG_MAX;
17
18 static void name_rev(struct commit *commit,
19 const char *tip_name, int merge_traversals, int generation,
20 int deref)
21 {
22 struct rev_name *name = (struct rev_name *)commit->util;
23 struct commit_list *parents;
24 int parent_number = 1;
25
26 if (!commit->object.parsed)
27 parse_commit(commit);
28
29 if (commit->date < cutoff)
30 return;
31
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;
37
38 if (generation)
39 die("generation: %d, but deref?", generation);
40 }
41
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;
55
56 for (parents = commit->parents;
57 parents;
58 parents = parents->next, parent_number++) {
59 if (parent_number > 1) {
60 int len = strlen(tip_name);
61 char *new_name = xmalloc(len +
62 1 + decimal_length(generation) + /* ~<n> */
63 1 + 2 + /* ^NN */
64 1);
65
66 if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
67 len -= 2;
68 if (generation > 0)
69 sprintf(new_name, "%.*s~%d^%d", len, tip_name,
70 generation, parent_number);
71 else
72 sprintf(new_name, "%.*s^%d", len, tip_name,
73 parent_number);
74
75 name_rev(parents->item, new_name,
76 merge_traversals + 1 , 0, 0);
77 } else {
78 name_rev(parents->item, tip_name, merge_traversals,
79 generation + 1, 0);
80 }
81 }
82 }
83
84 struct name_ref_data {
85 int tags_only;
86 int name_only;
87 const char *ref_filter;
88 };
89
90 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
91 {
92 struct object *o = parse_object(sha1);
93 struct name_ref_data *data = cb_data;
94 int deref = 0;
95
96 if (data->tags_only && prefixcmp(path, "refs/tags/"))
97 return 0;
98
99 if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
100 return 0;
101
102 while (o && o->type == OBJ_TAG) {
103 struct tag *t = (struct tag *) o;
104 if (!t->tagged)
105 break; /* broken repository */
106 o = parse_object(t->tagged->sha1);
107 deref = 1;
108 }
109 if (o && o->type == OBJ_COMMIT) {
110 struct commit *commit = (struct commit *)o;
111
112 if (!prefixcmp(path, "refs/heads/"))
113 path = path + 11;
114 else if (data->tags_only
115 && data->name_only
116 && !prefixcmp(path, "refs/tags/"))
117 path = path + 10;
118 else if (!prefixcmp(path, "refs/"))
119 path = path + 5;
120
121 name_rev(commit, xstrdup(path), 0, 0, deref);
122 }
123 return 0;
124 }
125
126 /* returns a static buffer */
127 static const char* get_rev_name(struct object *o)
128 {
129 static char buffer[1024];
130 struct rev_name *n;
131 struct commit *c;
132
133 if (o->type != OBJ_COMMIT)
134 return "undefined";
135 c = (struct commit *) o;
136 n = c->util;
137 if (!n)
138 return "undefined";
139
140 if (!n->generation)
141 return n->tip_name;
142 else {
143 int len = strlen(n->tip_name);
144 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
145 len -= 2;
146 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
147 n->generation);
148
149 return buffer;
150 }
151 }
152
153 int cmd_name_rev(int argc, const char **argv, const char *prefix)
154 {
155 struct object_array revs = { 0, 0, NULL };
156 int as_is = 0, all = 0, transform_stdin = 0;
157 struct name_ref_data data = { 0, 0, NULL };
158
159 git_config(git_default_config);
160
161 if (argc < 2)
162 usage(name_rev_usage);
163
164 for (--argc, ++argv; argc; --argc, ++argv) {
165 unsigned char sha1[20];
166 struct object *o;
167 struct commit *commit;
168
169 if (!as_is && (*argv)[0] == '-') {
170 if (!strcmp(*argv, "--")) {
171 as_is = 1;
172 continue;
173 } else if (!strcmp(*argv, "--name-only")) {
174 data.name_only = 1;
175 continue;
176 } else if (!strcmp(*argv, "--tags")) {
177 data.tags_only = 1;
178 continue;
179 } else if (!prefixcmp(*argv, "--refs=")) {
180 data.ref_filter = *argv + 7;
181 continue;
182 } else if (!strcmp(*argv, "--all")) {
183 if (argc > 1)
184 die("Specify either a list, or --all, not both!");
185 all = 1;
186 cutoff = 0;
187 continue;
188 } else if (!strcmp(*argv, "--stdin")) {
189 if (argc > 1)
190 die("Specify either a list, or --stdin, not both!");
191 transform_stdin = 1;
192 cutoff = 0;
193 continue;
194 }
195 usage(name_rev_usage);
196 }
197
198 if (get_sha1(*argv, sha1)) {
199 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
200 *argv);
201 continue;
202 }
203
204 o = deref_tag(parse_object(sha1), *argv, 0);
205 if (!o || o->type != OBJ_COMMIT) {
206 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
207 *argv);
208 continue;
209 }
210
211 commit = (struct commit *)o;
212
213 if (cutoff > commit->date)
214 cutoff = commit->date;
215
216 add_object_array((struct object *)commit, *argv, &revs);
217 }
218
219 for_each_ref(name_ref, &data);
220
221 if (transform_stdin) {
222 char buffer[2048];
223 char *p, *p_start;
224
225 while (!feof(stdin)) {
226 int forty = 0;
227 p = fgets(buffer, sizeof(buffer), stdin);
228 if (!p)
229 break;
230
231 for (p_start = p; *p; p++) {
232 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
233 if (!ishex(*p))
234 forty = 0;
235 else if (++forty == 40 &&
236 !ishex(*(p+1))) {
237 unsigned char sha1[40];
238 const char *name = "undefined";
239 char c = *(p+1);
240
241 forty = 0;
242
243 *(p+1) = 0;
244 if (!get_sha1(p - 39, sha1)) {
245 struct object *o =
246 lookup_object(sha1);
247 if (o)
248 name = get_rev_name(o);
249 }
250 *(p+1) = c;
251
252 if (!strcmp(name, "undefined"))
253 continue;
254
255 fwrite(p_start, p - p_start + 1, 1,
256 stdout);
257 printf(" (%s)", name);
258 p_start = p + 1;
259 }
260 }
261
262 /* flush */
263 if (p_start != p)
264 fwrite(p_start, p - p_start, 1, stdout);
265 }
266 } else if (all) {
267 int i, max;
268
269 max = get_max_object_index();
270 for (i = 0; i < max; i++) {
271 struct object * obj = get_indexed_object(i);
272 if (!obj)
273 continue;
274 if (!data.name_only)
275 printf("%s ", sha1_to_hex(obj->sha1));
276 printf("%s\n", get_rev_name(obj));
277 }
278 } else {
279 int i;
280 for (i = 0; i < revs.nr; i++) {
281 if (!data.name_only)
282 printf("%s ", revs.objects[i].name);
283 printf("%s\n", get_rev_name(revs.objects[i].item));
284 }
285 }
286
287 return 0;
288 }
289