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 /* How many generations are maximally preferred over _one_ merge traversal? */
21 #define MERGE_TRAVERSAL_WEIGHT 65535
23 static void name_rev(struct commit *commit,
24 const char *tip_name, int generation, int distance,
27 struct rev_name *name = (struct rev_name *)commit->util;
28 struct commit_list *parents;
29 int parent_number = 1;
31 if (!commit->object.parsed)
34 if (commit->date < cutoff)
38 char *new_name = xmalloc(strlen(tip_name)+3);
39 strcpy(new_name, tip_name);
40 strcat(new_name, "^0");
44 die("generation: %d, but deref?", generation);
48 name = xmalloc(sizeof(rev_name));
51 } else if (name->distance > distance) {
53 name->tip_name = tip_name;
54 name->generation = generation;
55 name->distance = distance;
59 for (parents = commit->parents;
61 parents = parents->next, parent_number++) {
62 if (parent_number > 1) {
63 int len = strlen(tip_name);
64 char *new_name = xmalloc(len +
65 1 + decimal_length(generation) + /* ~<n> */
69 if (len > 2 && !strcmp(tip_name + len - 2, "^0"))
72 sprintf(new_name, "%.*s~%d^%d", len, tip_name,
73 generation, parent_number);
75 sprintf(new_name, "%.*s^%d", len, tip_name,
78 name_rev(parents->item, new_name, 0,
79 distance + MERGE_TRAVERSAL_WEIGHT, 0);
81 name_rev(parents->item, tip_name, generation + 1,
87 struct name_ref_data {
90 const char *ref_filter;
93 static int name_ref(const char *path, const unsigned char *sha1, int flags, void *cb_data)
95 struct object *o = parse_object(sha1);
96 struct name_ref_data *data = cb_data;
99 if (data->tags_only && prefixcmp(path, "refs/tags/"))
102 if (data->ref_filter && fnmatch(data->ref_filter, path, 0))
105 while (o && o->type == OBJ_TAG) {
106 struct tag *t = (struct tag *) o;
108 break; /* broken repository */
109 o = parse_object(t->tagged->sha1);
112 if (o && o->type == OBJ_COMMIT) {
113 struct commit *commit = (struct commit *)o;
115 if (!prefixcmp(path, "refs/heads/"))
117 else if (data->tags_only
119 && !prefixcmp(path, "refs/tags/"))
121 else if (!prefixcmp(path, "refs/"))
124 name_rev(commit, xstrdup(path), 0, 0, deref);
129 /* returns a static buffer */
130 static const char* get_rev_name(struct object *o)
132 static char buffer[1024];
136 if (o->type != OBJ_COMMIT)
138 c = (struct commit *) o;
146 int len = strlen(n->tip_name);
147 if (len > 2 && !strcmp(n->tip_name + len - 2, "^0"))
149 snprintf(buffer, sizeof(buffer), "%.*s~%d", len, n->tip_name,
156 int cmd_name_rev(int argc, const char **argv, const char *prefix)
158 struct object_array revs = { 0, 0, NULL };
159 int as_is = 0, all = 0, transform_stdin = 0;
160 struct name_ref_data data = { 0, 0, NULL };
162 git_config(git_default_config);
165 usage(name_rev_usage);
167 for (--argc, ++argv; argc; --argc, ++argv) {
168 unsigned char sha1[20];
170 struct commit *commit;
172 if (!as_is && (*argv)[0] == '-') {
173 if (!strcmp(*argv, "--")) {
176 } else if (!strcmp(*argv, "--name-only")) {
179 } else if (!strcmp(*argv, "--tags")) {
182 } else if (!prefixcmp(*argv, "--refs=")) {
183 data.ref_filter = *argv + 7;
185 } else if (!strcmp(*argv, "--all")) {
187 die("Specify either a list, or --all, not both!");
191 } else if (!strcmp(*argv, "--stdin")) {
193 die("Specify either a list, or --stdin, not both!");
198 usage(name_rev_usage);
201 if (get_sha1(*argv, sha1)) {
202 fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
207 o = deref_tag(parse_object(sha1), *argv, 0);
208 if (!o || o->type != OBJ_COMMIT) {
209 fprintf(stderr, "Could not get commit for %s. Skipping.\n",
214 commit = (struct commit *)o;
216 if (cutoff > commit->date)
217 cutoff = commit->date;
219 add_object_array((struct object *)commit, *argv, &revs);
223 cutoff = cutoff - CUTOFF_DATE_SLOP;
224 for_each_ref(name_ref, &data);
226 if (transform_stdin) {
230 while (!feof(stdin)) {
232 p = fgets(buffer, sizeof(buffer), stdin);
236 for (p_start = p; *p; p++) {
237 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
240 else if (++forty == 40 &&
242 unsigned char sha1[40];
243 const char *name = "undefined";
249 if (!get_sha1(p - 39, sha1)) {
253 name = get_rev_name(o);
257 if (!strcmp(name, "undefined"))
260 fwrite(p_start, p - p_start + 1, 1,
262 printf(" (%s)", name);
269 fwrite(p_start, p - p_start, 1, stdout);
274 max = get_max_object_index();
275 for (i = 0; i < max; i++) {
276 struct object * obj = get_indexed_object(i);
280 printf("%s ", sha1_to_hex(obj->sha1));
281 printf("%s\n", get_rev_name(obj));
285 for (i = 0; i < revs.nr; i++) {
287 printf("%s ", revs.objects[i].name);
288 printf("%s\n", get_rev_name(revs.objects[i].item));