Add git-name-rev
git-name-rev tries to find nice symbolic names for commits. It does so by walking the commits from the refs. When the symbolic name is ambiguous, the following heuristic is applied: Try to avoid too many ~'s, and if two ambiguous names have the same count of ~'s, take the one whose last number is smaller. With "--tags", the names are derived only from tags. With "--stdin", the stdin is parsed, and after every sha1 for which a name could be found, the name is appended. (Try "git log | git name-rev --stdin".) Signed-off-by: Johannes Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Junio C Hamano <junkio@cox.net>
Johannes Schindelin committed
Oct 26, 2005 at 15:10 UTC
bd321bcc51e95f644ac5335abe673afcbcaade62
3 files changed
+313
-1
Documentation/git-name-rev.txt
new
+66
@@ -0,0 +1,66 @@
1
+git-name-rev(1)
2
+===============
3
+
4
+NAME
5
+----
6
+git-name-rev - Find symbolic names for given revs.
7
+
8
+
9
+SYNOPSIS
10
+--------
11
+'git-name-rev' [--tags] ( --all | --stdin | <commitish>... )
12
+
13
+DESCRIPTION
14
+-----------
15
+Finds symbolic names suitable for human digestion for revisions given in any
16
+format parsable by git-rev-parse.
17
+
18
+
19
+OPTIONS
20
+-------
21
+
22
+--tags::
23
+ Do not use branch names, but only tags to name the commits
24
+
25
+--all::
26
+ List all commits reachable from all refs
27
+
28
+--stdin::
29
+ Read from stdin, append "(<rev_name>)" to all sha1's of name'able
30
+ commits, and pass to stdout
31
+
32
+EXAMPLE
33
+-------
34
+
35
+Given a commit, find out where it is relative to the local refs. Say somebody
36
+wrote you about that phantastic commit 33db5f4d9027a10e477ccf054b2c1ab94f74c85a.
37
+Of course, you look into the commit, but that only tells you what happened, but
38
+not the context.
39
+
40
+Enter git-name-rev:
41
+
42
+------------
43
+% git name-rev 33db5f4d9027a10e477ccf054b2c1ab94f74c85a
44
+------------
45
+
46
+Now you are wiser, because you know that it happened 940 revisions before v0.99.
47
+
48
+Another nice thing you can do is:
49
+
50
+------------
51
+% git log | git name-rev --stdin
52
+------------
53
+
54
+
55
+Author
56
+------
57
+Written by Johannes Schindelin <Johannes.Schindelin@gmx.de>
58
+
59
+Documentation
60
+--------------
61
+Documentation by Johannes Schindelin.
62
+
63
+GIT
64
+---
65
+Part of the gitlink:git[7] suite
66
+
Makefile
+1
-1
@@ -122,7 +122,7 @@ PROGRAMS = \
122
git-unpack-objects$X git-update-index$X git-update-server-info$X \
123
git-upload-pack$X git-verify-pack$X git-write-tree$X \
124
git-update-ref$X git-symbolic-ref$X git-check-ref-format$X \
125
- $(SIMPLE_PROGRAMS)
125
+ git-name-rev$X $(SIMPLE_PROGRAMS)
126
127
# Backward compatibility -- to be removed after 1.0
128
PROGRAMS += git-ssh-pull$X git-ssh-push$X
name-rev.c
new
+246
@@ -0,0 +1,246 @@
1
+#include <stdlib.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] ( --all | --stdin | commitish [commitish...] )\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->object.util;
23
+ struct commit_list *parents;
24
+ int parent_number = 0;
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->object.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 > 0) {
60
+ char *new_name = xmalloc(strlen(tip_name)+8);
61
+
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);
67
+
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);
73
+ }
74
+ }
75
+}
76
+
77
+static int tags_only = 0;
78
+
79
+static int name_ref(const char *path, const unsigned char *sha1)
80
+{
81
+ struct object *o = parse_object(sha1);
82
+ int deref = 0;
83
+
84
+ if (tags_only && strncmp(path, "refs/tags/", 10))
85
+ return 0;
86
+
87
+ while (o && o->type == tag_type) {
88
+ struct tag *t = (struct tag *) o;
89
+ if (!t->tagged)
90
+ break; /* broken repository */
91
+ o = parse_object(t->tagged->sha1);
92
+ deref = 1;
93
+ }
94
+ if (o && o->type == commit_type) {
95
+ struct commit *commit = (struct commit *)o;
96
+ const char *p;
97
+
98
+ while ((p = strchr(path, '/')))
99
+ path = p+1;
100
+
101
+ name_rev(commit, strdup(path), 0, 0, deref);
102
+ }
103
+ return 0;
104
+}
105
+
106
+/* returns a static buffer */
107
+static const char* get_rev_name(struct object *o)
108
+{
109
+ static char buffer[1024];
110
+ struct rev_name *n = (struct rev_name *)o->util;
111
+ if (!n)
112
+ return "undefined";
113
+
114
+ if (!n->generation)
115
+ return n->tip_name;
116
+
117
+ snprintf(buffer, sizeof(buffer), "%s~%d", n->tip_name, n->generation);
118
+
119
+ return buffer;
120
+}
121
+
122
+int main(int argc, char **argv)
123
+{
124
+ struct object_list *revs = NULL;
125
+ struct object_list **walker = &revs;
126
+ int as_is = 0, all = 0, transform_stdin = 0;
127
+
128
+ setup_git_directory();
129
+
130
+ if (argc < 2)
131
+ usage(name_rev_usage);
132
+
133
+ for (--argc, ++argv; argc; --argc, ++argv) {
134
+ unsigned char sha1[20];
135
+ struct object *o;
136
+ struct commit *commit;
137
+
138
+ if (!as_is && (*argv)[0] == '-') {
139
+ if (!strcmp(*argv, "--")) {
140
+ as_is = 1;
141
+ continue;
142
+ } else if (!strcmp(*argv, "--tags")) {
143
+ tags_only = 1;
144
+ continue;
145
+ } else if (!strcmp(*argv, "--all")) {
146
+ if (argc > 1)
147
+ die("Specify either a list, or --all, not both!");
148
+ all = 1;
149
+ cutoff = 0;
150
+ continue;
151
+ } else if (!strcmp(*argv, "--stdin")) {
152
+ if (argc > 1)
153
+ die("Specify either a list, or --stdin, not both!");
154
+ transform_stdin = 1;
155
+ cutoff = 0;
156
+ continue;
157
+ }
158
+ usage(name_rev_usage);
159
+ }
160
+
161
+ if (get_sha1(*argv, sha1)) {
162
+ fprintf(stderr, "Could not get sha1 for %s. Skipping.\n",
163
+ *argv);
164
+ continue;
165
+ }
166
+
167
+ o = deref_tag(parse_object(sha1));
168
+ if (!o || o->type != commit_type) {
169
+ fprintf(stderr, "Could not get commit for %s. Skipping.\n",
170
+ *argv);
171
+ continue;
172
+ }
173
+
174
+ commit = (struct commit *)o;
175
+
176
+ if (cutoff > commit->date)
177
+ cutoff = commit->date;
178
+
179
+ object_list_append((struct object *)commit, walker);
180
+ (*walker)->name = *argv;
181
+ walker = &((*walker)->next);
182
+ }
183
+
184
+ for_each_ref(name_ref);
185
+
186
+ if (transform_stdin) {
187
+ char buffer[2048];
188
+ char *p, *p_start;
189
+
190
+ while (!feof(stdin)) {
191
+ int forty = 0;
192
+ p = fgets(buffer, sizeof(buffer), stdin);
193
+ if (!p)
194
+ break;
195
+
196
+ for (p_start = p; *p; p++) {
197
+#define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
198
+ if (!ishex(*p))
199
+ forty = 0;
200
+ else if (++forty == 40 &&
201
+ !ishex(*(p+1))) {
202
+ unsigned char sha1[40];
203
+ const char *name = "undefined";
204
+ char c = *(p+1);
205
+
206
+ forty = 0;
207
+
208
+ *(p+1) = 0;
209
+ if (!get_sha1(p - 39, sha1)) {
210
+ struct object *o =
211
+ lookup_object(sha1);
212
+ if (o)
213
+ name = get_rev_name(o);
214
+ }
215
+ *(p+1) = c;
216
+
217
+ if (!strcmp(name, "undefined"))
218
+ continue;
219
+
220
+ fwrite(p_start, p - p_start, 1, stdout);
221
+ fputc('(', stdout);
222
+ fputs(name, stdout);
223
+ fputc(')', stdout);
224
+ p_start = p + 1;
225
+ }
226
+ }
227
+
228
+ /* flush */
229
+ if (p_start != p)
230
+ fwrite(p_start, p - p_start, 1, stdout);
231
+ }
232
+ } else if (all) {
233
+ extern struct object **objs;
234
+ extern int nr_objs;
235
+ int i;
236
+
237
+ for (i = 0; i < nr_objs; i++)
238
+ printf("%s %s\n", sha1_to_hex(objs[i]->sha1),
239
+ get_rev_name(objs[i]));
240
+ } else
241
+ for ( ; revs; revs = revs->next)
242
+ printf("%s %s\n", revs->name, get_rev_name(revs->item));
243
+
244
+ return 0;
245
+}
246
+