6 static const char show_branch_usage
[] =
7 "git-show-branch [--all] [--heads] [--tags] [--more=count] [<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 int head_rev
; /* which head's ancestor? */
39 int generation
; /* how many parents away from head_rev */
42 /* Name the commit as nth generation ancestor of head_rev;
43 * we count only the first-parent relationship for naming purposes.
45 static void name_commit(struct commit
*commit
, int head_rev
, 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_rev
= head_rev
;
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_rev 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_rev
,
69 commit_name
->generation
+ 1);
72 static int mark_seen(struct commit
*commit
, struct commit_list
**seen_p
)
74 if (!commit
->object
.flags
) {
75 insert_by_date(commit
, seen_p
);
81 static void join_revs(struct commit_list
**list_p
,
82 struct commit_list
**seen_p
,
83 int num_rev
, int extra
)
85 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
86 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
89 struct commit_list
*parents
;
90 struct commit
*commit
= pop_one_commit(list_p
);
91 int flags
= commit
->object
.flags
& all_mask
;
93 int still_interesting
= !!interesting(*list_p
);
95 if (!still_interesting
&& extra
< 0)
98 mark_seen(commit
, seen_p
);
99 if ((flags
& all_revs
) == all_revs
)
100 flags
|= UNINTERESTING
;
101 parents
= commit
->parents
;
104 struct commit
*p
= parents
->item
;
105 int this_flag
= p
->object
.flags
;
106 parents
= parents
->next
;
109 name_parent(commit
, p
);
111 if ((this_flag
& flags
) == flags
)
114 if (mark_seen(p
, seen_p
) && !still_interesting
)
116 p
->object
.flags
|= flags
;
117 insert_by_date(p
, list_p
);
122 static void show_one_commit(struct commit
*commit
, char **head_name
)
124 char pretty
[128], *cp
;
125 struct commit_name
*name
= commit
->object
.util
;
126 pretty_print_commit(CMIT_FMT_ONELINE
, commit
->buffer
, ~0,
127 pretty
, sizeof(pretty
));
128 if (!strncmp(pretty
, "[PATCH] ", 8))
132 if (name
&& head_name
) {
133 printf("[%s", head_name
[name
->head_rev
]);
134 if (name
->generation
)
135 printf("~%d", name
->generation
);
141 static char *ref_name
[MAX_REVS
+ 1];
142 static int ref_name_cnt
;
144 static int append_ref(const char *refname
, const unsigned char *sha1
)
146 struct commit
*commit
= lookup_commit_reference_gently(sha1
, 1);
149 if (MAX_REVS
< ref_name_cnt
) {
150 fprintf(stderr
, "warning: ignoring %s; "
151 "cannot handle more than %d refs",
155 ref_name
[ref_name_cnt
++] = strdup(refname
);
156 ref_name
[ref_name_cnt
] = NULL
;
160 static int append_head_ref(const char *refname
, const unsigned char *sha1
)
162 if (strncmp(refname
, "refs/heads/", 11))
164 return append_ref(refname
+ 5, sha1
);
167 static int append_tag_ref(const char *refname
, const unsigned char *sha1
)
169 if (strncmp(refname
, "refs/tags/", 10))
171 return append_ref(refname
+ 5, sha1
);
174 static void snarf_refs(int head
, int tag
)
177 for_each_ref(append_head_ref
);
179 for_each_ref(append_tag_ref
);
182 static int rev_is_head(char *head_path
, int headlen
,
184 unsigned char *head_sha1
, unsigned char *sha1
)
187 if ((!head_path
[0]) || memcmp(head_sha1
, sha1
, 20))
189 namelen
= strlen(name
);
190 if ((headlen
< namelen
) ||
191 memcmp(head_path
+ headlen
- namelen
, name
, namelen
))
193 if (headlen
== namelen
||
194 head_path
[headlen
- namelen
- 1] == '/')
199 static int show_merge_base(struct commit_list
*seen
, int num_rev
)
201 int all_mask
= ((1u << (REV_SHIFT
+ num_rev
)) - 1);
202 int all_revs
= all_mask
& ~((1u << REV_SHIFT
) - 1);
205 struct commit
*commit
= pop_one_commit(&seen
);
206 int flags
= commit
->object
.flags
& all_mask
;
207 if (!(flags
& UNINTERESTING
) &&
208 ((flags
& all_revs
) == all_revs
)) {
209 puts(sha1_to_hex(commit
->object
.sha1
));
216 int main(int ac
, char **av
)
218 struct commit
*rev
[MAX_REVS
], *commit
;
219 struct commit_list
*list
= NULL
, *seen
= NULL
;
220 int num_rev
, i
, extra
= 0;
221 int all_heads
= 0, all_tags
= 0;
224 unsigned char head_sha1
[20];
228 while (1 < ac
&& av
[1][0] == '-') {
230 if (!strcmp(arg
, "--all"))
231 all_heads
= all_tags
= 1;
232 else if (!strcmp(arg
, "--heads"))
234 else if (!strcmp(arg
, "--tags"))
236 else if (!strcmp(arg
, "--more"))
238 else if (!strncmp(arg
, "--more=", 7)) {
239 extra
= atoi(arg
+ 7);
241 usage(show_branch_usage
);
243 else if (!strcmp(arg
, "--merge-base"))
246 usage(show_branch_usage
);
251 if (all_heads
+ all_tags
)
252 snarf_refs(all_heads
, all_tags
);
255 unsigned char revkey
[20];
256 if (get_sha1(*av
, revkey
))
257 die("bad sha1 reference %s", *av
);
258 append_ref(*av
, revkey
);
262 /* If still no revs, then add heads */
266 for (num_rev
= 0; ref_name
[num_rev
]; num_rev
++) {
267 unsigned char revkey
[20];
269 if (MAX_REVS
<= num_rev
)
270 die("cannot handle more than %d revs.", MAX_REVS
);
271 if (get_sha1(ref_name
[num_rev
], revkey
))
272 usage(show_branch_usage
);
273 commit
= lookup_commit_reference(revkey
);
275 die("cannot find commit %s (%s)",
276 ref_name
[num_rev
], revkey
);
277 parse_commit(commit
);
278 if (!commit
->object
.util
)
279 name_commit(commit
, num_rev
, 0);
280 mark_seen(commit
, &seen
);
282 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
283 * and so on. REV_SHIFT bits from bit 0 are used for
284 * internal bookkeeping.
286 commit
->object
.flags
|= 1u << (num_rev
+ REV_SHIFT
);
287 insert_by_date(commit
, &list
);
288 rev
[num_rev
] = commit
;
290 join_revs(&list
, &seen
, num_rev
, extra
);
292 head_path_len
= readlink(".git/HEAD", head_path
, sizeof(head_path
)-1);
293 if ((head_path_len
< 0) || get_sha1("HEAD", head_sha1
))
296 head_path
[head_path_len
] = 0;
299 return show_merge_base(seen
, num_rev
);
303 for (i
= 0; i
< num_rev
; i
++) {
305 int is_head
= rev_is_head(head_path
,
309 rev
[i
]->object
.sha1
);
310 for (j
= 0; j
< i
; j
++)
312 printf("%c [%s] ", is_head
? '*' : '!', ref_name
[i
]);
313 show_one_commit(rev
[i
], NULL
);
315 for (i
= 0; i
< num_rev
; i
++)
322 struct commit
*commit
= pop_one_commit(&seen
);
323 int this_flag
= commit
->object
.flags
;
324 static char *obvious
[] = { "" };
326 if ((this_flag
& UNINTERESTING
) && (--extra
< 0))
329 for (i
= 0; i
< num_rev
; i
++)
330 putchar((this_flag
& (1u << (i
+ REV_SHIFT
)))
334 show_one_commit(commit
, label
);