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
{
89 const char *ref_filter
;
92 static int name_ref(const char *path
, const unsigned char *sha1
, int flags
, void *cb_data
)
94 struct object
*o
= parse_object(sha1
);
95 struct name_ref_data
*data
= cb_data
;
98 if (data
->tags_only
&& prefixcmp(path
, "refs/tags/"))
101 if (data
->ref_filter
&& fnmatch(data
->ref_filter
, path
, 0))
104 while (o
&& o
->type
== OBJ_TAG
) {
105 struct tag
*t
= (struct tag
*) o
;
107 break; /* broken repository */
108 o
= parse_object(t
->tagged
->sha1
);
111 if (o
&& o
->type
== OBJ_COMMIT
) {
112 struct commit
*commit
= (struct commit
*)o
;
114 if (!prefixcmp(path
, "refs/heads/"))
116 else if (data
->tags_only
118 && !prefixcmp(path
, "refs/tags/"))
120 else if (!prefixcmp(path
, "refs/"))
123 name_rev(commit
, xstrdup(path
), 0, 0, deref
);
128 /* returns a static buffer */
129 static const char* get_rev_name(struct object
*o
)
131 static char buffer
[1024];
135 if (o
->type
!= OBJ_COMMIT
)
137 c
= (struct commit
*) o
;
145 int len
= strlen(n
->tip_name
);
146 if (len
> 2 && !strcmp(n
->tip_name
+ len
- 2, "^0"))
148 snprintf(buffer
, sizeof(buffer
), "%.*s~%d", len
, n
->tip_name
,
155 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
157 struct object_array revs
= { 0, 0, NULL
};
158 int as_is
= 0, all
= 0, transform_stdin
= 0;
159 struct name_ref_data data
= { 0, 0, NULL
};
161 git_config(git_default_config
);
164 usage(name_rev_usage
);
166 for (--argc
, ++argv
; argc
; --argc
, ++argv
) {
167 unsigned char sha1
[20];
169 struct commit
*commit
;
171 if (!as_is
&& (*argv
)[0] == '-') {
172 if (!strcmp(*argv
, "--")) {
175 } else if (!strcmp(*argv
, "--name-only")) {
178 } else if (!strcmp(*argv
, "--tags")) {
181 } else if (!prefixcmp(*argv
, "--refs=")) {
182 data
.ref_filter
= *argv
+ 7;
184 } else if (!strcmp(*argv
, "--all")) {
186 die("Specify either a list, or --all, not both!");
190 } else if (!strcmp(*argv
, "--stdin")) {
192 die("Specify either a list, or --stdin, not both!");
197 usage(name_rev_usage
);
200 if (get_sha1(*argv
, sha1
)) {
201 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
206 o
= deref_tag(parse_object(sha1
), *argv
, 0);
207 if (!o
|| o
->type
!= OBJ_COMMIT
) {
208 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
213 commit
= (struct commit
*)o
;
215 if (cutoff
> commit
->date
)
216 cutoff
= commit
->date
;
218 add_object_array((struct object
*)commit
, *argv
, &revs
);
222 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
223 for_each_ref(name_ref
, &data
);
225 if (transform_stdin
) {
229 while (!feof(stdin
)) {
231 p
= fgets(buffer
, sizeof(buffer
), stdin
);
235 for (p_start
= p
; *p
; p
++) {
236 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
239 else if (++forty
== 40 &&
241 unsigned char sha1
[40];
242 const char *name
= "undefined";
248 if (!get_sha1(p
- 39, sha1
)) {
252 name
= get_rev_name(o
);
256 if (!strcmp(name
, "undefined"))
259 fwrite(p_start
, p
- p_start
+ 1, 1,
261 printf(" (%s)", name
);
268 fwrite(p_start
, p
- p_start
, 1, stdout
);
273 max
= get_max_object_index();
274 for (i
= 0; i
< max
; i
++) {
275 struct object
* obj
= get_indexed_object(i
);
279 printf("%s ", sha1_to_hex(obj
->sha1
));
280 printf("%s\n", get_rev_name(obj
));
284 for (i
= 0; i
< revs
.nr
; i
++) {
286 printf("%s ", revs
.objects
[i
].name
);
287 printf("%s\n", get_rev_name(revs
.objects
[i
].item
));