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 {
18 static long cutoff = LONG_MAX;
20 static void name_rev(struct commit *commit,
21 const char *tip_name, int merge_traversals, int generation,
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)
31 if (commit->date < cutoff)
35 char *new_name = xmalloc(strlen(tip_name)+3);
36 strcpy(new_name, tip_name);
37 strcat(new_name, "^0");
41 die("generation: %d, but deref?", generation);
45 name = xmalloc(sizeof(rev_name));
48 } else if (name->merge_traversals > merge_traversals ||
49 (name->merge_traversals == merge_traversals &&
50 name->generation > generation)) {
52 name->tip_name = tip_name;
53 name->merge_traversals = merge_traversals;
54 name->generation = generation;
58 for (parents = commit->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> */
68 if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
71 sprintf(new_name, "%.*s~%d^%d", len, tip_name,
72 generation, parent_number);
74 sprintf(new_name, "%.*s^%d", len, tip_name,
77 name_rev(parents->item, new_name,
78 merge_traversals + 1 , 0, 0);
80 name_rev(parents->item, tip_name, merge_traversals,
86 struct name_ref_data {
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;
97 if (data->tags_only && prefixcmp(path, "refs/tags/"))
100 if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
103 while (o && o->type == OBJ_TAG) {
104 struct tag *t = (struct tag *) o;
106 break; /* broken repository */
107 o = parse_object(t->tagged->sha1);
110 if (o && o->type == OBJ_COMMIT) {
111 struct commit *commit = (struct commit *)o;
113 if (!prefixcmp(path, "refs/heads/"))
115 else if (!prefixcmp(path, "refs/"))
118 name_rev(commit, xstrdup(path), 0, 0, deref);
123 /* returns a static buffer */
124 static const char* get_rev_name(struct object *o)
126 static char buffer[1024];
130 if (o->type != OBJ_COMMIT)
132 c = (struct commit *) o;
140 int len = strlen(n->tip_name);
141 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
143 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
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);
159 usage(name_rev_usage);
161 for (--argc, ++argv; argc; --argc, ++argv) {
162 unsigned char sha1[20];
164 struct commit *commit;
166 if (!as_is && (*argv)[0] == '-') {
167 if (!strcmp(*argv, "--")) {
170 } else if (!strcmp(*argv, "--tags")) {
173 } else if (!prefixcmp(*argv, "--refs=")) {
174 data.ref_filter = *argv + 7;
176 } else if (!strcmp(*argv, "--all")) {
178 die("Specify either a list, or --all, not both!");
182 } else if (!strcmp(*argv, "--stdin")) {
184 die("Specify either a list, or --stdin, not both!");
189 usage(name_rev_usage);
192 if (get_sha1(*argv, sha1)) {
193 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
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",
205 commit = (struct commit *)o;
207 if (cutoff > commit->date)
208 cutoff = commit->date;
210 add_object_array((struct object *)commit, *argv, &revs);
214 cutoff = cutoff - CUTOFF_DATE_SLOP;
215 for_each_ref(name_ref, &data);
217 if (transform_stdin) {
221 while (!feof(stdin)) {
223 p = fgets(buffer, sizeof(buffer), stdin);
227 for (p_start = p; *p; p++) {
228 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
231 else if (++forty == 40 &&
233 unsigned char sha1[40];
234 const char *name = "undefined";
240 if (!get_sha1(p - 39, sha1)) {
244 name = get_rev_name(o);
248 if (!strcmp(name, "undefined"))
251 fwrite(p_start, p - p_start + 1, 1,
253 printf(" (%s)", name);
260 fwrite(p_start, p - p_start, 1, stdout);
265 max = get_max_object_index();
266 for (i = 0; i < max; i++) {
267 struct object * obj = get_indexed_object(i);
270 printf("%s %s\n", sha1_to_hex(obj->sha1), get_rev_name(obj));
274 for (i = 0; i < revs.nr; i++)
276 revs.objects[i].name,
277 get_rev_name(revs.objects[i].item));