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
;
136 char newname
[1000], *en
;
137 parents
= parents
->next
;
142 switch (n
->generation
) {
144 en
+= sprintf(en
, "%s", n
->head_name
);
147 en
+= sprintf(en
, "%s^", n
->head_name
);
150 en
+= sprintf(en
, "%s~%d",
151 n
->head_name
, n
->generation
);
155 en
+= sprintf(en
, "^");
157 en
+= sprintf(en
, "^%d", nth
);
158 name_commit(p
, strdup(newname
), 0);
160 name_first_parent_chain(p
);
166 static int mark_seen(struct commit
*commit
, struct commit_list
**seen_p
)
168 if (!commit
->object
.flags
) {
169 insert_by_date(commit
, seen_p
);
175 static void join_revs(struct commit_list
**list_p
,
176 struct commit_list
**seen_p
,
177 int num_rev
, int extra
)
179 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
180 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
183 struct commit_list
*parents
;
184 int still_interesting
= !!interesting(*list_p
);
185 struct commit
*commit
= pop_one_commit(list_p
);
186 int flags
= commit
->object
.flags
& all_mask
;
188 if (!still_interesting
&& extra
<= 0)
191 mark_seen(commit
, seen_p
);
192 if ((flags
& all_revs
) == all_revs
)
193 flags
|= UNINTERESTING
;
194 parents
= commit
->parents
;
197 struct commit
*p
= parents
->item
;
198 int this_flag
= p
->object
.flags
;
199 parents
= parents
->next
;
200 if ((this_flag
& flags
) == flags
)
202 if (!p
->object
.parsed
)
204 if (mark_seen(p
, seen_p
) && !still_interesting
)
206 p
->object
.flags
|= flags
;
207 insert_by_date(p
, list_p
);
212 * Postprocess to complete well-poisoning.
214 * At this point we have all the commits we have seen in
215 * seen_p list (which happens to be sorted chronologically but
216 * it does not really matter). Mark anything that can be
217 * reached from uninteresting commits not interesting.
221 struct commit_list
*s
;
222 for (s
= *seen_p
; s
; s
= s
->next
) {
223 struct commit
*c
= s
->item
;
224 struct commit_list
*parents
;
226 if (((c
->object
.flags
& all_revs
) != all_revs
) &&
227 !(c
->object
.flags
& UNINTERESTING
))
230 /* The current commit is either a merge base or
231 * already uninteresting one. Mark its parents
232 * as uninteresting commits _only_ if they are
233 * already parsed. No reason to find new ones
236 parents
= c
->parents
;
238 struct commit
*p
= parents
->item
;
239 parents
= parents
->next
;
240 if (!(p
->object
.flags
& UNINTERESTING
)) {
241 p
->object
.flags
|= UNINTERESTING
;
251 static void show_one_commit(struct commit
*commit
, int no_name
)
253 char pretty
[256], *cp
;
254 struct commit_name
*name
= commit
->object
.util
;
255 if (commit
->object
.parsed
)
256 pretty_print_commit(CMIT_FMT_ONELINE
, commit
->buffer
, ~0,
257 pretty
, sizeof(pretty
));
259 strcpy(pretty
, "(unavailable)");
260 if (!strncmp(pretty
, "[PATCH] ", 8))
266 if (name
&& name
->head_name
) {
267 printf("[%s", name
->head_name
);
268 if (name
->generation
) {
269 if (name
->generation
== 1)
272 printf("~%d", name
->generation
);
278 find_unique_abbrev(commit
->object
.sha1
, 7));
283 static char *ref_name
[MAX_REVS
+ 1];
284 static int ref_name_cnt
;
286 static int compare_ref_name(const void *a_
, const void *b_
)
288 const char * const*a
= a_
, * const*b
= b_
;
289 return strcmp(*a
, *b
);
292 static void sort_ref_range(int bottom
, int top
)
294 qsort(ref_name
+ bottom
, top
- bottom
, sizeof(ref_name
[0]),
298 static int append_ref(const char *refname
, const unsigned char *sha1
)
300 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
303 if (MAX_REVS
<= ref_name_cnt
) {
304 fprintf(stderr
, "warning: ignoring %s; "
305 "cannot handle more than %d refs",
309 ref_name
[ref_name_cnt
++] = strdup(refname
);
310 ref_name
[ref_name_cnt
] = NULL
;
314 static int append_head_ref(const char *refname
, const unsigned char *sha1
)
316 if (strncmp(refname
, "refs/heads/", 11))
318 return append_ref(refname
+ 11, sha1
);
321 static int append_tag_ref(const char *refname
, const unsigned char *sha1
)
323 if (strncmp(refname
, "refs/tags/", 10))
325 return append_ref(refname
+ 5, sha1
);
328 static void snarf_refs(int head
, int tag
)
331 int orig_cnt
= ref_name_cnt
;
332 for_each_ref(append_head_ref
);
333 sort_ref_range(orig_cnt
, ref_name_cnt
);
336 int orig_cnt
= ref_name_cnt
;
337 for_each_ref(append_tag_ref
);
338 sort_ref_range(orig_cnt
, ref_name_cnt
);
342 static int rev_is_head(char *head_path
, int headlen
,
344 unsigned char *head_sha1
, unsigned char *sha1
)
347 if ((!head_path
[0]) || memcmp(head_sha1
, sha1
, 20))
349 namelen
= strlen(name
);
350 if ((headlen
< namelen
) ||
351 memcmp(head_path
+ headlen
- namelen
, name
, namelen
))
353 if (headlen
== namelen
||
354 head_path
[headlen
- namelen
- 1] == '/')
359 static int show_merge_base(struct commit_list
*seen
, int num_rev
)
361 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
362 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
366 struct commit
*commit
= pop_one_commit(&seen
);
367 int flags
= commit
->object
.flags
& all_mask
;
368 if (!(flags
& UNINTERESTING
) &&
369 ((flags
& all_revs
) == all_revs
)) {
370 puts(sha1_to_hex(commit
->object
.sha1
));
372 commit
->object
.flags
|= UNINTERESTING
;
378 static int show_independent(struct commit
**rev
,
381 unsigned int *rev_mask
)
385 for (i
= 0; i
< num_rev
; i
++) {
386 struct commit
*commit
= rev
[i
];
387 unsigned int flag
= rev_mask
[i
];
389 if (commit
->object
.flags
== flag
)
390 puts(sha1_to_hex(commit
->object
.sha1
));
391 commit
->object
.flags
|= UNINTERESTING
;
396 int main(int ac
, char **av
)
398 struct commit
*rev
[MAX_REVS
], *commit
;
399 struct commit_list
*list
= NULL
, *seen
= NULL
;
400 unsigned int rev_mask
[MAX_REVS
];
401 int num_rev
, i
, extra
= 0;
402 int all_heads
= 0, all_tags
= 0;
403 int all_mask
, all_revs
;
405 const char *head_path_p
;
407 unsigned char head_sha1
[20];
412 int shown_merge_point
= 0;
415 setup_git_directory();
417 while (1 < ac
&& av
[1][0] == '-') {
419 if (!strcmp(arg
, "--all"))
420 all_heads
= all_tags
= 1;
421 else if (!strcmp(arg
, "--heads"))
423 else if (!strcmp(arg
, "--tags"))
425 else if (!strcmp(arg
, "--more"))
427 else if (!strcmp(arg
, "--list"))
429 else if (!strcmp(arg
, "--no-name"))
431 else if (!strcmp(arg
, "--sha1-name"))
433 else if (!strncmp(arg
, "--more=", 7))
434 extra
= atoi(arg
+ 7);
435 else if (!strcmp(arg
, "--merge-base"))
437 else if (!strcmp(arg
, "--independent"))
439 else if (!strcmp(arg
, "--topo-order"))
442 usage(show_branch_usage
);
447 /* Only one of these is allowed */
448 if (1 < independent
+ merge_base
+ (extra
!= 0))
449 usage(show_branch_usage
);
451 if (all_heads
+ all_tags
)
452 snarf_refs(all_heads
, all_tags
);
455 unsigned char revkey
[20];
456 if (get_sha1(*av
, revkey
))
457 die("bad sha1 reference %s", *av
);
458 append_ref(*av
, revkey
);
462 /* If still no revs, then add heads */
466 for (num_rev
= 0; ref_name
[num_rev
]; num_rev
++) {
467 unsigned char revkey
[20];
468 unsigned int flag
= 1u << (num_rev
+ REV_SHIFT
);
470 if (MAX_REVS
<= num_rev
)
471 die("cannot handle more than %d revs.", MAX_REVS
);
472 if (get_sha1(ref_name
[num_rev
], revkey
))
473 usage(show_branch_usage
);
474 commit
= lookup_commit_reference(revkey
);
476 die("cannot find commit %s (%s)",
477 ref_name
[num_rev
], revkey
);
478 parse_commit(commit
);
479 mark_seen(commit
, &seen
);
481 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
482 * and so on. REV_SHIFT bits from bit 0 are used for
483 * internal bookkeeping.
485 commit
->object
.flags
|= flag
;
486 if (commit
->object
.flags
== flag
)
487 insert_by_date(commit
, &list
);
488 rev
[num_rev
] = commit
;
490 for (i
= 0; i
< num_rev
; i
++)
491 rev_mask
[i
] = rev
[i
]->object
.flags
;
494 join_revs(&list
, &seen
, num_rev
, extra
);
496 head_path_p
= resolve_ref(git_path("HEAD"), head_sha1
, 1);
498 head_path_len
= strlen(head_path_p
);
499 memcpy(head_path
, head_path_p
, head_path_len
+ 1);
507 return show_merge_base(seen
, num_rev
);
510 return show_independent(rev
, num_rev
, ref_name
, rev_mask
);
512 /* Show list; --more=-1 means list-only */
513 if (1 < num_rev
|| extra
< 0) {
514 for (i
= 0; i
< num_rev
; i
++) {
516 int is_head
= rev_is_head(head_path
,
520 rev
[i
]->object
.sha1
);
523 is_head
? '*' : ' ', ref_name
[i
]);
525 for (j
= 0; j
< i
; j
++)
528 is_head
? '*' : '!', ref_name
[i
]);
530 /* header lines never need name */
531 show_one_commit(rev
[i
], 1);
534 for (i
= 0; i
< num_rev
; i
++)
542 /* Sort topologically */
544 sort_in_topological_order(&seen
);
546 /* Give names to commits */
547 if (!sha1_name
&& !no_name
)
548 name_commits(seen
, rev
, ref_name
, num_rev
);
550 all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
551 all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
554 struct commit
*commit
= pop_one_commit(&seen
);
555 int this_flag
= commit
->object
.flags
;
557 shown_merge_point
|= ((this_flag
& all_revs
) == all_revs
);
560 for (i
= 0; i
< num_rev
; i
++)
561 putchar((this_flag
& (1u << (i
+ REV_SHIFT
)))
565 show_one_commit(commit
, no_name
);
567 if (shown_merge_point
&& --extra
< 0)