]> git.ipfire.org Git - thirdparty/git.git/blame - builtin-name-rev.c
Fix doc typos.
[thirdparty/git.git] / builtin-name-rev.c
CommitLineData
d6b64ed0 1#include "builtin.h"
bd321bcc
JS
2#include "cache.h"
3#include "commit.h"
4#include "tag.h"
5#include "refs.h"
edefb1a2 6#include "parse-options.h"
bd321bcc 7
c075aea5
JH
8#define CUTOFF_DATE_SLOP 86400 /* one day */
9
bd321bcc
JS
10typedef struct rev_name {
11 const char *tip_name;
bd321bcc 12 int generation;
ac076c29 13 int distance;
bd321bcc
JS
14} rev_name;
15
16static long cutoff = LONG_MAX;
17
ac076c29
JS
18/* How many generations are maximally preferred over _one_ merge traversal? */
19#define MERGE_TRAVERSAL_WEIGHT 65535
20
bd321bcc 21static void name_rev(struct commit *commit,
ac076c29 22 const char *tip_name, int generation, int distance,
bd321bcc
JS
23 int deref)
24{
d3ff6f55 25 struct rev_name *name = (struct rev_name *)commit->util;
bd321bcc 26 struct commit_list *parents;
f2e6f1c9 27 int parent_number = 1;
bd321bcc
JS
28
29 if (!commit->object.parsed)
30 parse_commit(commit);
31
32 if (commit->date < cutoff)
33 return;
34
35 if (deref) {
36 char *new_name = xmalloc(strlen(tip_name)+3);
37 strcpy(new_name, tip_name);
38 strcat(new_name, "^0");
39 tip_name = new_name;
40
41 if (generation)
42 die("generation: %d, but deref?", generation);
43 }
44
45 if (name == NULL) {
46 name = xmalloc(sizeof(rev_name));
d3ff6f55 47 commit->util = name;
bd321bcc 48 goto copy_data;
ac076c29 49 } else if (name->distance > distance) {
bd321bcc
JS
50copy_data:
51 name->tip_name = tip_name;
bd321bcc 52 name->generation = generation;
ac076c29 53 name->distance = distance;
bd321bcc
JS
54 } else
55 return;
56
57 for (parents = commit->parents;
58 parents;
59 parents = parents->next, parent_number++) {
f2e6f1c9 60 if (parent_number > 1) {
59d3f541 61 int len = strlen(tip_name);
cf606e3d
AW
62 char *new_name = xmalloc(len +
63 1 + decimal_length(generation) + /* ~<n> */
64 1 + 2 + /* ^NN */
65 1);
bd321bcc 66
59d3f541
JS
67 if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
68 len -= 2;
bd321bcc 69 if (generation > 0)
59d3f541 70 sprintf(new_name, "%.*s~%d^%d", len, tip_name,
bd321bcc
JS
71 generation, parent_number);
72 else
59d3f541
JS
73 sprintf(new_name, "%.*s^%d", len, tip_name,
74 parent_number);
bd321bcc 75
ac076c29
JS
76 name_rev(parents->item, new_name, 0,
77 distance + MERGE_TRAVERSAL_WEIGHT, 0);
bd321bcc 78 } else {
ac076c29
JS
79 name_rev(parents->item, tip_name, generation + 1,
80 distance + 1, 0);
bd321bcc
JS
81 }
82 }
83}
84
2afc29aa
JS
85struct name_ref_data {
86 int tags_only;
23615708 87 int name_only;
2afc29aa
JS
88 const char *ref_filter;
89};
90
8da19775 91static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
bd321bcc
JS
92{
93 struct object *o = parse_object(sha1);
2afc29aa 94 struct name_ref_data *data = cb_data;
bd321bcc
JS
95 int deref = 0;
96
cc44c765 97 if (data->tags_only && prefixcmp(path, "refs/tags/"))
2afc29aa
JS
98 return 0;
99
100 if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
bd321bcc
JS
101 return 0;
102
1974632c 103 while (o && o->type == OBJ_TAG) {
bd321bcc
JS
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;
109 }
1974632c 110 if (o && o->type == OBJ_COMMIT) {
bd321bcc 111 struct commit *commit = (struct commit *)o;
bd321bcc 112
cc44c765 113 if (!prefixcmp(path, "refs/heads/"))
2c817df2 114 path = path + 11;
23615708
SP
115 else if (data->tags_only
116 && data->name_only
117 && !prefixcmp(path, "refs/tags/"))
118 path = path + 10;
cc44c765 119 else if (!prefixcmp(path, "refs/"))
2c817df2 120 path = path + 5;
bd321bcc 121
9befac47 122 name_rev(commit, xstrdup(path), 0, 0, deref);
bd321bcc
JS
123 }
124 return 0;
125}
126
127/* returns a static buffer */
a2cf9f44 128static const char *get_rev_name(struct object *o)
bd321bcc
JS
129{
130 static char buffer[1024];
d3ff6f55
LT
131 struct rev_name *n;
132 struct commit *c;
133
1974632c 134 if (o->type != OBJ_COMMIT)
a2cf9f44 135 return NULL;
d3ff6f55
LT
136 c = (struct commit *) o;
137 n = c->util;
bd321bcc 138 if (!n)
a2cf9f44 139 return NULL;
bd321bcc
JS
140
141 if (!n->generation)
142 return n->tip_name;
59d3f541
JS
143 else {
144 int len = strlen(n->tip_name);
145 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
146 len -= 2;
147 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
148 n->generation);
149
150 return buffer;
151 }
bd321bcc 152}
1f1e895f 153
edefb1a2
PH
154static char const * const name_rev_usage[] = {
155 "git-name-rev [options] ( --all | --stdin | <commit>... )",
156 NULL
157};
158
d6b64ed0 159int cmd_name_rev(int argc, const char **argv, const char *prefix)
bd321bcc 160{
1f1e895f 161 struct object_array revs = { 0, 0, NULL };
a2cf9f44 162 int all = 0, transform_stdin = 0, allow_undefined = 1;
23615708 163 struct name_ref_data data = { 0, 0, NULL };
edefb1a2
PH
164 struct option opts[] = {
165 OPT_BOOLEAN(0, "name-only", &data.name_only, "print only names (no SHA-1)"),
166 OPT_BOOLEAN(0, "tags", &data.tags_only, "only use tags to name the commits"),
167 OPT_STRING(0, "refs", &data.ref_filter, "pattern",
168 "only use refs matching <pattern>"),
169 OPT_GROUP(""),
170 OPT_BOOLEAN(0, "all", &all, "list all commits reachable from all refs"),
171 OPT_BOOLEAN(0, "stdin", &transform_stdin, "read from stdin"),
a2cf9f44 172 OPT_BOOLEAN(0, "undefined", &allow_undefined, "allow to print `undefined` names"),
edefb1a2
PH
173 OPT_END(),
174 };
bd321bcc 175
84a9b58c 176 git_config(git_default_config);
edefb1a2
PH
177 argc = parse_options(argc, argv, opts, name_rev_usage, 0);
178 if (!!all + !!transform_stdin + !!argc > 1) {
179 error("Specify either a list, or --all, not both!");
180 usage_with_options(name_rev_usage, opts);
181 }
182 if (all || transform_stdin)
183 cutoff = 0;
bd321bcc 184
edefb1a2 185 for (; argc; argc--, argv++) {
bd321bcc
JS
186 unsigned char sha1[20];
187 struct object *o;
188 struct commit *commit;
189
bd321bcc
JS
190 if (get_sha1(*argv, sha1)) {
191 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
192 *argv);
193 continue;
194 }
195
9534f40b 196 o = deref_tag(parse_object(sha1), *argv, 0);
1974632c 197 if (!o || o->type != OBJ_COMMIT) {
bd321bcc
JS
198 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
199 *argv);
200 continue;
201 }
202
203 commit = (struct commit *)o;
bd321bcc
JS
204 if (cutoff > commit->date)
205 cutoff = commit->date;
1f1e895f 206 add_object_array((struct object *)commit, *argv, &revs);
bd321bcc
JS
207 }
208
c075aea5
JH
209 if (cutoff)
210 cutoff = cutoff - CUTOFF_DATE_SLOP;
2afc29aa 211 for_each_ref(name_ref, &data);
bd321bcc
JS
212
213 if (transform_stdin) {
214 char buffer[2048];
215 char *p, *p_start;
216
217 while (!feof(stdin)) {
218 int forty = 0;
219 p = fgets(buffer, sizeof(buffer), stdin);
220 if (!p)
221 break;
222
223 for (p_start = p; *p; p++) {
224#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
225 if (!ishex(*p))
226 forty = 0;
227 else if (++forty == 40 &&
228 !ishex(*(p+1))) {
229 unsigned char sha1[40];
a2cf9f44 230 const char *name = NULL;
bd321bcc
JS
231 char c = *(p+1);
232
233 forty = 0;
234
235 *(p+1) = 0;
236 if (!get_sha1(p - 39, sha1)) {
237 struct object *o =
238 lookup_object(sha1);
239 if (o)
240 name = get_rev_name(o);
241 }
242 *(p+1) = c;
243
a2cf9f44 244 if (!name)
bd321bcc
JS
245 continue;
246
a2cf9f44 247 fwrite(p_start, p - p_start + 1, 1, stdout);
2d76d0d1 248 printf(" (%s)", name);
bd321bcc
JS
249 p_start = p + 1;
250 }
251 }
252
253 /* flush */
254 if (p_start != p)
255 fwrite(p_start, p - p_start, 1, stdout);
256 }
257 } else if (all) {
fc046a75 258 int i, max;
bd321bcc 259
fc046a75
LT
260 max = get_max_object_index();
261 for (i = 0; i < max; i++) {
262 struct object * obj = get_indexed_object(i);
a2cf9f44 263 const char *name;
fc046a75
LT
264 if (!obj)
265 continue;
23615708
SP
266 if (!data.name_only)
267 printf("%s ", sha1_to_hex(obj->sha1));
a2cf9f44
PH
268 name = get_rev_name(obj);
269 if (name)
270 printf("%s\n", name);
271 else if (allow_undefined)
272 printf("undefined\n");
273 else
274 die("cannot describe '%s'", sha1_to_hex(obj->sha1));
fc046a75 275 }
1f1e895f
LT
276 } else {
277 int i;
23615708 278 for (i = 0; i < revs.nr; i++) {
a2cf9f44 279 const char *name;
23615708
SP
280 if (!data.name_only)
281 printf("%s ", revs.objects[i].name);
a2cf9f44
PH
282 name = get_rev_name(revs.objects[i].item);
283 if (name)
284 printf("%s\n", name);
285 else if (allow_undefined)
286 printf("undefined\n");
287 else
288 die("cannot describe '%s'", sha1_to_hex(revs.objects[i].item->sha1));
23615708 289 }
1f1e895f 290 }
bd321bcc
JS
291
292 return 0;
293}