6 #include "parse-options.h"
8 #define CUTOFF_DATE_SLOP 86400 /* one day */
10 typedef struct rev_name
{
16 static long cutoff
= LONG_MAX
;
18 /* How many generations are maximally preferred over _one_ merge traversal? */
19 #define MERGE_TRAVERSAL_WEIGHT 65535
21 static void name_rev(struct commit
*commit
,
22 const char *tip_name
, int generation
, int distance
,
25 struct rev_name
*name
= (struct rev_name
*)commit
->util
;
26 struct commit_list
*parents
;
27 int parent_number
= 1;
29 if (!commit
->object
.parsed
)
32 if (commit
->date
< cutoff
)
36 char *new_name
= xmalloc(strlen(tip_name
)+3);
37 strcpy(new_name
, tip_name
);
38 strcat(new_name
, "^0");
42 die("generation: %d, but deref?", generation
);
46 name
= xmalloc(sizeof(rev_name
));
49 } else if (name
->distance
> distance
) {
51 name
->tip_name
= tip_name
;
52 name
->generation
= generation
;
53 name
->distance
= distance
;
57 for (parents
= commit
->parents
;
59 parents
= parents
->next
, parent_number
++) {
60 if (parent_number
> 1) {
61 int len
= strlen(tip_name
);
62 char *new_name
= xmalloc(len
+
63 1 + decimal_length(generation
) + /* ~<n> */
67 if (len
> 2 && !strcmp(tip_name
+ len
- 2, "^0"))
70 sprintf(new_name
, "%.*s~%d^%d", len
, tip_name
,
71 generation
, parent_number
);
73 sprintf(new_name
, "%.*s^%d", len
, tip_name
,
76 name_rev(parents
->item
, new_name
, 0,
77 distance
+ MERGE_TRAVERSAL_WEIGHT
, 0);
79 name_rev(parents
->item
, tip_name
, generation
+ 1,
85 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 (data
->tags_only
117 && !prefixcmp(path
, "refs/tags/"))
119 else if (!prefixcmp(path
, "refs/"))
122 name_rev(commit
, xstrdup(path
), 0, 0, deref
);
127 /* returns a static buffer */
128 static const char* get_rev_name(struct object
*o
)
130 static char buffer
[1024];
134 if (o
->type
!= OBJ_COMMIT
)
136 c
= (struct commit
*) o
;
144 int len
= strlen(n
->tip_name
);
145 if (len
> 2 && !strcmp(n
->tip_name
+ len
- 2, "^0"))
147 snprintf(buffer
, sizeof(buffer
), "%.*s~%d", len
, n
->tip_name
,
154 static char const * const name_rev_usage
[] = {
155 "git-name-rev [options] ( --all | --stdin | <commit>... )",
159 int cmd_name_rev(int argc
, const char **argv
, const char *prefix
)
161 struct object_array revs
= { 0, 0, NULL
};
162 int all
= 0, transform_stdin
= 0;
163 struct name_ref_data data
= { 0, 0, NULL
};
164 struct option opts
[] = {
165 OPT_BOOLEAN(0, "name-only", &data
.name_only
, "print only names (no SHA-1)"),
166 OPT_BOOLEAN(0, "tags", &data
.tags_only
, "only use tags to name the commits"),
167 OPT_STRING(0, "refs", &data
.ref_filter
, "pattern",
168 "only use refs matching <pattern>"),
170 OPT_BOOLEAN(0, "all", &all
, "list all commits reachable from all refs"),
171 OPT_BOOLEAN(0, "stdin", &transform_stdin
, "read from stdin"),
175 git_config(git_default_config
);
176 argc
= parse_options(argc
, argv
, opts
, name_rev_usage
, 0);
177 if (!!all
+ !!transform_stdin
+ !!argc
> 1) {
178 error("Specify either a list, or --all, not both!");
179 usage_with_options(name_rev_usage
, opts
);
181 if (all
|| transform_stdin
)
184 for (; argc
; argc
--, argv
++) {
185 unsigned char sha1
[20];
187 struct commit
*commit
;
189 if (get_sha1(*argv
, sha1
)) {
190 fprintf(stderr
, "Could not get sha1 for %s. Skipping.\n",
195 o
= deref_tag(parse_object(sha1
), *argv
, 0);
196 if (!o
|| o
->type
!= OBJ_COMMIT
) {
197 fprintf(stderr
, "Could not get commit for %s. Skipping.\n",
202 commit
= (struct commit
*)o
;
203 if (cutoff
> commit
->date
)
204 cutoff
= commit
->date
;
205 add_object_array((struct object
*)commit
, *argv
, &revs
);
209 cutoff
= cutoff
- CUTOFF_DATE_SLOP
;
210 for_each_ref(name_ref
, &data
);
212 if (transform_stdin
) {
216 while (!feof(stdin
)) {
218 p
= fgets(buffer
, sizeof(buffer
), stdin
);
222 for (p_start
= p
; *p
; p
++) {
223 #define ishex(x) (isdigit((x)) || ((x) >= 'a' && (x) <= 'f'))
226 else if (++forty
== 40 &&
228 unsigned char sha1
[40];
229 const char *name
= "undefined";
235 if (!get_sha1(p
- 39, sha1
)) {
239 name
= get_rev_name(o
);
243 if (!strcmp(name
, "undefined"))
246 fwrite(p_start
, p
- p_start
+ 1, 1,
248 printf(" (%s)", name
);
255 fwrite(p_start
, p
- p_start
, 1, stdout
);
260 max
= get_max_object_index();
261 for (i
= 0; i
< max
; i
++) {
262 struct object
* obj
= get_indexed_object(i
);
266 printf("%s ", sha1_to_hex(obj
->sha1
));
267 printf("%s\n", get_rev_name(obj
));
271 for (i
= 0; i
< revs
.nr
; i
++) {
273 printf("%s ", revs
.objects
[i
].name
);
274 printf("%s\n", get_rev_name(revs
.objects
[i
].item
));