7 static const char name_rev_usage
[] =
8 "git-name-rev [--tags | --refs=<pattern>] ( --all | --stdin | committish [committish...] )\n";
10 typedef struct rev_name
{
16 static long cutoff
= LONG_MAX
;
18 static void name_rev(struct commit
*commit
,
19 const char *tip_name
, int merge_traversals
, int generation
,
22 struct rev_name
*name
= (struct rev_name
*)commit
->util
;
23 struct commit_list
*parents
;
24 int parent_number
= 1;
26 if (!commit
->object
.parsed
)
29 if (commit
->date
< cutoff
)
33 char *new_name
= xmalloc(strlen(tip_name
)+3);
34 strcpy(new_name
, tip_name
);
35 strcat(new_name
, "^0");
39 die("generation: %d, but deref?", generation
);
43 name
= xmalloc(sizeof(rev_name
));
46 } else if (name
->merge_traversals
> merge_traversals
||
47 (name
->merge_traversals
== merge_traversals
&&
48 name
->generation
> generation
)) {
50 name
->tip_name
= tip_name
;
51 name
->merge_traversals
= merge_traversals
;
52 name
->generation
= generation
;
56 for (parents
= commit
->parents
;
58 parents
= parents
->next
, parent_number
++) {
59 if (parent_number
> 1) {
60 int len
= strlen(tip_name
);
61 char *new_name
= xmalloc(len
+ 8);
63 if (len
> 2 && !strcmp(tip_name
+ len
- 2, "^0"))
66 sprintf(new_name
, "%.*s~%d^%d", len
, tip_name
,
67 generation
, parent_number
);
69 sprintf(new_name
, "%.*s^%d", len
, tip_name
,
72 name_rev(parents
->item
, new_name
,
73 merge_traversals
+ 1 , 0, 0);
75 name_rev(parents
->item
, tip_name
, merge_traversals
,
81 struct name_ref_data
{
83 const char *ref_filter
;
86 static int name_ref(const char *path
, const unsigned char *sha1
, int flags
, void *cb_data
)
88 struct object
*o
= parse_object(sha1
);
89 struct name_ref_data
*data
= cb_data
;
92 if (data
->tags_only
&& prefixcmp(path
, "refs/tags/"))
95 if (data
->ref_filter
&& fnmatch(data
->ref_filter
, path
, 0))
98 while (o
&& o
->type
== OBJ_TAG
) {
99 struct tag
*t
= (struct tag
*) o
;
101 break; /* broken repository */
102 o
= parse_object(t
->tagged
->sha1
);
105 if (o
&& o
->type
== OBJ_COMMIT
) {
106 struct commit
*commit
= (struct commit
*)o
;
108 if (!prefixcmp(path
, "refs/heads/"))
110 else if (!prefixcmp(path
, "refs/"))
113 name_rev(commit
, xstrdup(path
), 0, 0, deref
);
118 /* returns a static buffer */
119 static const char* get_rev_name(struct object
*o
)
121 static char buffer
[1024];
125 if (o
->type
!= OBJ_COMMIT
)
127 c
= (struct commit
*) o
;
135 int len
= strlen(n
->tip_name
);
136 if (len
> 2 && !strcmp(n
->tip_name
+ len
- 2, "^0"))
138 snprintf(buffer
, sizeof(buffer
), "%.*s~%d", len
, n
->tip_name
,
145 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
147 struct object_array revs
= { 0, 0, NULL
};
148 int as_is
= 0, all
= 0, transform_stdin
= 0;
149 struct name_ref_data data
= { 0, NULL
};
151 git_config(git_default_config
);
154 usage(name_rev_usage
);
156 for (--argc
, ++argv
; argc
; --argc
, ++argv
) {
157 unsigned char sha1
[20];
159 struct commit
*commit
;
161 if (!as_is
&& (*argv
)[0] == '-') {
162 if (!strcmp(*argv
, "--")) {
165 } else if (!strcmp(*argv
, "--tags")) {
168 } else if (!prefixcmp(*argv
, "--refs=")) {
169 data
.ref_filter
= *argv
+ 7;
171 } else if (!strcmp(*argv
, "--all")) {
173 die("Specify either a list, or --all, not both!");
177 } else if (!strcmp(*argv
, "--stdin")) {
179 die("Specify either a list, or --stdin, not both!");
184 usage(name_rev_usage
);
187 if (get_sha1(*argv
, sha1
)) {
188 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
193 o
= deref_tag(parse_object(sha1
), *argv
, 0);
194 if (!o
|| o
->type
!= OBJ_COMMIT
) {
195 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
200 commit
= (struct commit
*)o
;
202 if (cutoff
> commit
->date
)
203 cutoff
= commit
->date
;
205 add_object_array((struct object
*)commit
, *argv
, &revs
);
208 for_each_ref(name_ref
, &data
);
210 if (transform_stdin
) {
214 while (!feof(stdin
)) {
216 p
= fgets(buffer
, sizeof(buffer
), stdin
);
220 for (p_start
= p
; *p
; p
++) {
221 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
224 else if (++forty
== 40 &&
226 unsigned char sha1
[40];
227 const char *name
= "undefined";
233 if (!get_sha1(p
- 39, sha1
)) {
237 name
= get_rev_name(o
);
241 if (!strcmp(name
, "undefined"))
244 fwrite(p_start
, p
- p_start
+ 1, 1,
246 printf(" (%s)", name
);
253 fwrite(p_start
, p
- p_start
, 1, stdout
);
258 max
= get_max_object_index();
259 for (i
= 0; i
< max
; i
++) {
260 struct object
* obj
= get_indexed_object(i
);
263 printf("%s %s\n", sha1_to_hex(obj
->sha1
), get_rev_name(obj
));
267 for (i
= 0; i
< revs
.nr
; i
++)
269 revs
.objects
[i
].name
,
270 get_rev_name(revs
.objects
[i
].item
));