6 static const char show_branch_usage
[] =
7 "git-show-branch [--all] [--heads] [--tags] [--more=count | --list | --independent | --merge-base ] [<refs>...]";
9 #define UNINTERESTING 01
12 #define MAX_REVS 29 /* should not exceed bits_per_int - REV_SHIFT */
14 static struct commit
*interesting(struct commit_list
*list
)
17 struct commit
*commit
= list
->item
;
19 if (commit
->object
.flags
& UNINTERESTING
)
26 static struct commit
*pop_one_commit(struct commit_list
**list_p
)
28 struct commit
*commit
;
29 struct commit_list
*list
;
38 const char *head_name
; /* which head's ancestor? */
39 int generation
; /* how many parents away from head_name */
42 /* Name the commit as nth generation ancestor of head_name;
43 * we count only the first-parent relationship for naming purposes.
45 static void name_commit(struct commit
*commit
, const char *head_name
, int nth
)
47 struct commit_name
*name
;
48 if (!commit
->object
.util
)
49 commit
->object
.util
= xmalloc(sizeof(struct commit_name
));
50 name
= commit
->object
.util
;
51 name
->head_name
= head_name
;
52 name
->generation
= nth
;
55 /* Parent is the first parent of the commit. We may name it
56 * as (n+1)th generation ancestor of the same head_name as
57 * commit is nth generation ancestore of, if that generation
58 * number is better than the name it already has.
60 static void name_parent(struct commit
*commit
, struct commit
*parent
)
62 struct commit_name
*commit_name
= commit
->object
.util
;
63 struct commit_name
*parent_name
= parent
->object
.util
;
67 commit_name
->generation
+ 1 < parent_name
->generation
)
68 name_commit(parent
, commit_name
->head_name
,
69 commit_name
->generation
+ 1);
72 static int name_first_parent_chain(struct commit
*c
)
82 if (!p
->object
.util
) {
91 static void name_commits(struct commit_list
*list
,
96 struct commit_list
*cl
;
100 /* First give names to the given heads */
101 for (cl
= list
; cl
; cl
= cl
->next
) {
105 for (i
= 0; i
< num_rev
; i
++) {
107 name_commit(c
, ref_name
[i
], 0);
113 /* Then commits on the first parent ancestry chain */
116 for (cl
= list
; cl
; cl
= cl
->next
) {
117 i
+= name_first_parent_chain(cl
->item
);
121 /* Finally, any unnamed commits */
124 for (cl
= list
; cl
; cl
= cl
->next
) {
125 struct commit_list
*parents
;
126 struct commit_name
*n
;
132 parents
= c
->parents
;
135 struct commit
*p
= parents
->item
;
137 parents
= parents
->next
;
141 sprintf(newname
, "%s^%d", n
->head_name
, nth
);
142 name_commit(p
, strdup(newname
), 0);
144 name_first_parent_chain(p
);
150 static int mark_seen(struct commit
*commit
, struct commit_list
**seen_p
)
152 if (!commit
->object
.flags
) {
153 insert_by_date(commit
, seen_p
);
159 static void join_revs(struct commit_list
**list_p
,
160 struct commit_list
**seen_p
,
161 int num_rev
, int extra
)
163 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
164 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
167 struct commit_list
*parents
;
168 struct commit
*commit
= pop_one_commit(list_p
);
169 int flags
= commit
->object
.flags
& all_mask
;
170 int still_interesting
= !!interesting(*list_p
);
172 if (!still_interesting
&& extra
< 0)
175 mark_seen(commit
, seen_p
);
176 if ((flags
& all_revs
) == all_revs
)
177 flags
|= UNINTERESTING
;
178 parents
= commit
->parents
;
181 struct commit
*p
= parents
->item
;
182 int this_flag
= p
->object
.flags
;
183 parents
= parents
->next
;
184 if ((this_flag
& flags
) == flags
)
187 if (mark_seen(p
, seen_p
) && !still_interesting
)
189 p
->object
.flags
|= flags
;
190 insert_by_date(p
, list_p
);
195 static void show_one_commit(struct commit
*commit
)
197 char pretty
[128], *cp
;
198 struct commit_name
*name
= commit
->object
.util
;
199 pretty_print_commit(CMIT_FMT_ONELINE
, commit
->buffer
, ~0,
200 pretty
, sizeof(pretty
));
201 if (!strncmp(pretty
, "[PATCH] ", 8))
205 if (name
&& name
->head_name
) {
206 printf("[%s", name
->head_name
);
207 if (name
->generation
)
208 printf("~%d", name
->generation
);
214 static char *ref_name
[MAX_REVS
+ 1];
215 static int ref_name_cnt
;
217 static int compare_ref_name(const void *a_
, const void *b_
)
219 const char * const*a
= a_
, * const*b
= b_
;
220 return strcmp(*a
, *b
);
223 static void sort_ref_range(int bottom
, int top
)
225 qsort(ref_name
+ bottom
, top
- bottom
, sizeof(ref_name
[0]),
229 static int append_ref(const char *refname
, const unsigned char *sha1
)
231 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
234 if (MAX_REVS
< ref_name_cnt
) {
235 fprintf(stderr
, "warning: ignoring %s; "
236 "cannot handle more than %d refs",
240 ref_name
[ref_name_cnt
++] = strdup(refname
);
241 ref_name
[ref_name_cnt
] = NULL
;
245 static int append_head_ref(const char *refname
, const unsigned char *sha1
)
247 if (strncmp(refname
, "refs/heads/", 11))
249 return append_ref(refname
+ 11, sha1
);
252 static int append_tag_ref(const char *refname
, const unsigned char *sha1
)
254 if (strncmp(refname
, "refs/tags/", 10))
256 return append_ref(refname
+ 5, sha1
);
259 static void snarf_refs(int head
, int tag
)
262 int orig_cnt
= ref_name_cnt
;
263 for_each_ref(append_head_ref
);
264 sort_ref_range(orig_cnt
, ref_name_cnt
);
267 int orig_cnt
= ref_name_cnt
;
268 for_each_ref(append_tag_ref
);
269 sort_ref_range(orig_cnt
, ref_name_cnt
);
273 static int rev_is_head(char *head_path
, int headlen
,
275 unsigned char *head_sha1
, unsigned char *sha1
)
278 if ((!head_path
[0]) || memcmp(head_sha1
, sha1
, 20))
280 namelen
= strlen(name
);
281 if ((headlen
< namelen
) ||
282 memcmp(head_path
+ headlen
- namelen
, name
, namelen
))
284 if (headlen
== namelen
||
285 head_path
[headlen
- namelen
- 1] == '/')
290 static int show_merge_base(struct commit_list
*seen
, int num_rev
)
292 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
293 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
297 struct commit
*commit
= pop_one_commit(&seen
);
298 int flags
= commit
->object
.flags
& all_mask
;
299 if (!(flags
& UNINTERESTING
) &&
300 ((flags
& all_revs
) == all_revs
)) {
301 puts(sha1_to_hex(commit
->object
.sha1
));
303 commit
->object
.flags
|= UNINTERESTING
;
309 static int show_independent(struct commit
**rev
,
312 unsigned int *rev_mask
)
316 for (i
= 0; i
< num_rev
; i
++) {
317 struct commit
*commit
= rev
[i
];
318 unsigned int flag
= rev_mask
[i
];
320 if (commit
->object
.flags
== flag
)
321 puts(sha1_to_hex(commit
->object
.sha1
));
322 commit
->object
.flags
|= UNINTERESTING
;
327 int main(int ac
, char **av
)
329 struct commit
*rev
[MAX_REVS
], *commit
;
330 struct commit_list
*list
= NULL
, *seen
= NULL
;
331 unsigned int rev_mask
[MAX_REVS
];
332 int num_rev
, i
, extra
= 0;
333 int all_heads
= 0, all_tags
= 0;
334 int all_mask
, all_revs
, shown_merge_point
;
337 unsigned char head_sha1
[20];
342 setup_git_directory();
344 while (1 < ac
&& av
[1][0] == '-') {
346 if (!strcmp(arg
, "--all"))
347 all_heads
= all_tags
= 1;
348 else if (!strcmp(arg
, "--heads"))
350 else if (!strcmp(arg
, "--tags"))
352 else if (!strcmp(arg
, "--more"))
354 else if (!strcmp(arg
, "--list"))
356 else if (!strncmp(arg
, "--more=", 7))
357 extra
= atoi(arg
+ 7);
358 else if (!strcmp(arg
, "--merge-base"))
360 else if (!strcmp(arg
, "--independent"))
363 usage(show_branch_usage
);
368 /* Only one of these is allowed */
369 if (1 < independent
+ merge_base
+ (extra
!= 0))
370 usage(show_branch_usage
);
372 if (all_heads
+ all_tags
)
373 snarf_refs(all_heads
, all_tags
);
376 unsigned char revkey
[20];
377 if (get_sha1(*av
, revkey
))
378 die("bad sha1 reference %s", *av
);
379 append_ref(*av
, revkey
);
383 /* If still no revs, then add heads */
387 for (num_rev
= 0; ref_name
[num_rev
]; num_rev
++) {
388 unsigned char revkey
[20];
389 unsigned int flag
= 1u << (num_rev
+ REV_SHIFT
);
391 if (MAX_REVS
<= num_rev
)
392 die("cannot handle more than %d revs.", MAX_REVS
);
393 if (get_sha1(ref_name
[num_rev
], revkey
))
394 usage(show_branch_usage
);
395 commit
= lookup_commit_reference(revkey
);
397 die("cannot find commit %s (%s)",
398 ref_name
[num_rev
], revkey
);
399 parse_commit(commit
);
400 mark_seen(commit
, &seen
);
402 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
403 * and so on. REV_SHIFT bits from bit 0 are used for
404 * internal bookkeeping.
406 commit
->object
.flags
|= flag
;
407 if (commit
->object
.flags
== flag
)
408 insert_by_date(commit
, &list
);
409 rev
[num_rev
] = commit
;
411 for (i
= 0; i
< num_rev
; i
++)
412 rev_mask
[i
] = rev
[i
]->object
.flags
;
415 join_revs(&list
, &seen
, num_rev
, extra
);
417 head_path_len
= readlink(".git/HEAD", head_path
, sizeof(head_path
)-1);
418 if ((head_path_len
< 0) || get_sha1("HEAD", head_sha1
))
421 head_path
[head_path_len
] = 0;
424 return show_merge_base(seen
, num_rev
);
427 return show_independent(rev
, num_rev
, ref_name
, rev_mask
);
429 /* Show list; --more=-1 means list-only */
430 if (1 < num_rev
|| extra
< 0) {
431 for (i
= 0; i
< num_rev
; i
++) {
433 int is_head
= rev_is_head(head_path
,
437 rev
[i
]->object
.sha1
);
440 is_head
? '*' : ' ', ref_name
[i
]);
442 for (j
= 0; j
< i
; j
++)
445 is_head
? '*' : '!', ref_name
[i
]);
447 show_one_commit(rev
[i
]);
450 for (i
= 0; i
< num_rev
; i
++)
458 /* Sort topologically */
459 sort_in_topological_order(&seen
);
461 /* Give names to commits */
462 name_commits(seen
, rev
, ref_name
, num_rev
);
464 all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
465 all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
466 shown_merge_point
= 0;
469 struct commit
*commit
= pop_one_commit(&seen
);
470 int this_flag
= commit
->object
.flags
;
471 int is_merge_point
= (this_flag
& all_revs
) == all_revs
;
472 static char *obvious
[] = { "" };
475 shown_merge_point
= 1;
478 for (i
= 0; i
< num_rev
; i
++)
479 putchar((this_flag
& (1u << (i
+ REV_SHIFT
)))
483 show_one_commit(commit
);
486 if (shown_merge_point
&& is_merge_point
)