]> git.ipfire.org Git - thirdparty/git.git/blame - show-branch.c
mailinfo and applymbox updates
[thirdparty/git.git] / show-branch.c
CommitLineData
f76412ed
JH
1#include <stdlib.h>
2#include "cache.h"
3#include "commit.h"
4#include "refs.h"
5
6static const char show_branch_usage[] =
7"git-show-branch [--all] [--heads] [--tags] [--more=count] [<refs>...]";
8
9#define UNINTERESTING 01
10
11#define REV_SHIFT 2
12#define MAX_REVS 29 /* should not exceed bits_per_int - REV_SHIFT */
13
14static struct commit *interesting(struct commit_list *list)
15{
16 while (list) {
17 struct commit *commit = list->item;
18 list = list->next;
19 if (commit->object.flags & UNINTERESTING)
20 continue;
21 return commit;
22 }
23 return NULL;
24}
25
26static struct commit *pop_one_commit(struct commit_list **list_p)
27{
28 struct commit *commit;
29 struct commit_list *list;
30 list = *list_p;
31 commit = list->item;
32 *list_p = list->next;
33 free(list);
34 return commit;
35}
36
37struct commit_name {
38 int head_rev; /* which head's ancestor? */
39 int generation; /* how many parents away from head_rev */
40};
41
42/* Name the commit as nth generation ancestor of head_rev;
43 * we count only the first-parent relationship for naming purposes.
44 */
45static void name_commit(struct commit *commit, int head_rev, int nth)
46{
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;
53}
54
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.
59 */
60static void name_parent(struct commit *commit, struct commit *parent)
61{
62 struct commit_name *commit_name = commit->object.util;
63 struct commit_name *parent_name = parent->object.util;
64 if (!commit_name)
65 return;
66 if (!parent_name ||
67 commit_name->generation + 1 < parent_name->generation)
68 name_commit(parent, commit_name->head_rev,
69 commit_name->generation + 1);
70}
71
72static int mark_seen(struct commit *commit, struct commit_list **seen_p)
73{
74 if (!commit->object.flags) {
75 insert_by_date(commit, seen_p);
76 return 1;
77 }
78 return 0;
79}
80
81static void join_revs(struct commit_list **list_p,
82 struct commit_list **seen_p,
83 int num_rev, int extra)
84{
85 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
86 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
87
88 while (*list_p) {
89 struct commit_list *parents;
90 struct commit *commit = pop_one_commit(list_p);
91 int flags = commit->object.flags & all_mask;
92 int nth_parent = 0;
93 int still_interesting = !!interesting(*list_p);
94
95 if (!still_interesting && extra < 0)
96 break;
97
98 mark_seen(commit, seen_p);
99 if ((flags & all_revs) == all_revs)
100 flags |= UNINTERESTING;
101 parents = commit->parents;
102
103 while (parents) {
104 struct commit *p = parents->item;
105 int this_flag = p->object.flags;
106 parents = parents->next;
107 nth_parent++;
108 if (nth_parent == 1)
109 name_parent(commit, p);
110
111 if ((this_flag & flags) == flags)
112 continue;
113 parse_commit(p);
114 if (mark_seen(p, seen_p) && !still_interesting)
115 extra--;
116 p->object.flags |= flags;
117 insert_by_date(p, list_p);
118 }
119 }
120}
121
122static void show_one_commit(struct commit *commit, char **head_name)
123{
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))
129 cp = pretty + 8;
130 else
131 cp = pretty;
132 if (name && head_name) {
133 printf("[%s", head_name[name->head_rev]);
134 if (name->generation)
135 printf("~%d", name->generation);
136 printf("] ");
137 }
138 puts(cp);
139}
140
141static char *ref_name[MAX_REVS + 1];
142static int ref_name_cnt;
143
628894b2
JH
144static int compare_ref_name(const void *a_, const void *b_)
145{
146 const char * const*a = a_, * const*b = b_;
147 return strcmp(*a, *b);
148}
149
150static void sort_ref_range(int bottom, int top)
151{
152 qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
153 compare_ref_name);
154}
155
f76412ed
JH
156static int append_ref(const char *refname, const unsigned char *sha1)
157{
158 struct commit *commit = lookup_commit_reference_gently(sha1, 1);
159 if (!commit)
160 return 0;
161 if (MAX_REVS < ref_name_cnt) {
162 fprintf(stderr, "warning: ignoring %s; "
163 "cannot handle more than %d refs",
164 refname, MAX_REVS);
165 return 0;
166 }
167 ref_name[ref_name_cnt++] = strdup(refname);
168 ref_name[ref_name_cnt] = NULL;
169 return 0;
170}
171
172static int append_head_ref(const char *refname, const unsigned char *sha1)
173{
174 if (strncmp(refname, "refs/heads/", 11))
175 return 0;
628894b2 176 return append_ref(refname + 11, sha1);
f76412ed
JH
177}
178
179static int append_tag_ref(const char *refname, const unsigned char *sha1)
180{
181 if (strncmp(refname, "refs/tags/", 10))
182 return 0;
183 return append_ref(refname + 5, sha1);
184}
185
186static void snarf_refs(int head, int tag)
187{
628894b2
JH
188 if (head) {
189 int orig_cnt = ref_name_cnt;
f76412ed 190 for_each_ref(append_head_ref);
628894b2
JH
191 sort_ref_range(orig_cnt, ref_name_cnt);
192 }
193 if (tag) {
194 int orig_cnt = ref_name_cnt;
f76412ed 195 for_each_ref(append_tag_ref);
628894b2
JH
196 sort_ref_range(orig_cnt, ref_name_cnt);
197 }
f76412ed
JH
198}
199
200static int rev_is_head(char *head_path, int headlen,
201 char *name,
202 unsigned char *head_sha1, unsigned char *sha1)
203{
204 int namelen;
205 if ((!head_path[0]) || memcmp(head_sha1, sha1, 20))
206 return 0;
207 namelen = strlen(name);
208 if ((headlen < namelen) ||
209 memcmp(head_path + headlen - namelen, name, namelen))
210 return 0;
211 if (headlen == namelen ||
212 head_path[headlen - namelen - 1] == '/')
213 return 1;
214 return 0;
215}
216
217static int show_merge_base(struct commit_list *seen, int num_rev)
218{
219 int all_mask = ((1u << (REV_SHIFT + num_rev)) - 1);
220 int all_revs = all_mask & ~((1u << REV_SHIFT) - 1);
221
222 while (seen) {
223 struct commit *commit = pop_one_commit(&seen);
224 int flags = commit->object.flags & all_mask;
225 if (!(flags & UNINTERESTING) &&
226 ((flags & all_revs) == all_revs)) {
227 puts(sha1_to_hex(commit->object.sha1));
228 return 0;
229 }
230 }
231 return 1;
232}
233
234int main(int ac, char **av)
235{
236 struct commit *rev[MAX_REVS], *commit;
237 struct commit_list *list = NULL, *seen = NULL;
238 int num_rev, i, extra = 0;
239 int all_heads = 0, all_tags = 0;
240 char head_path[128];
241 int head_path_len;
242 unsigned char head_sha1[20];
243 int merge_base = 0;
f5e375c9 244 char **label;
f76412ed
JH
245
246 while (1 < ac && av[1][0] == '-') {
247 char *arg = av[1];
248 if (!strcmp(arg, "--all"))
249 all_heads = all_tags = 1;
250 else if (!strcmp(arg, "--heads"))
251 all_heads = 1;
252 else if (!strcmp(arg, "--tags"))
253 all_tags = 1;
254 else if (!strcmp(arg, "--more"))
255 extra = 1;
256 else if (!strncmp(arg, "--more=", 7)) {
257 extra = atoi(arg + 7);
258 if (extra < 0)
259 usage(show_branch_usage);
260 }
261 else if (!strcmp(arg, "--merge-base"))
262 merge_base = 1;
263 else
264 usage(show_branch_usage);
265 ac--; av++;
266 }
267 ac--; av++;
268
269 if (all_heads + all_tags)
270 snarf_refs(all_heads, all_tags);
271
272 while (0 < ac) {
273 unsigned char revkey[20];
274 if (get_sha1(*av, revkey))
275 die("bad sha1 reference %s", *av);
276 append_ref(*av, revkey);
277 ac--; av++;
278 }
279
280 /* If still no revs, then add heads */
281 if (!ref_name_cnt)
282 snarf_refs(1, 0);
283
284 for (num_rev = 0; ref_name[num_rev]; num_rev++) {
285 unsigned char revkey[20];
286
287 if (MAX_REVS <= num_rev)
288 die("cannot handle more than %d revs.", MAX_REVS);
289 if (get_sha1(ref_name[num_rev], revkey))
290 usage(show_branch_usage);
291 commit = lookup_commit_reference(revkey);
292 if (!commit)
293 die("cannot find commit %s (%s)",
294 ref_name[num_rev], revkey);
295 parse_commit(commit);
296 if (!commit->object.util)
297 name_commit(commit, num_rev, 0);
298 mark_seen(commit, &seen);
299
300 /* rev#0 uses bit REV_SHIFT, rev#1 uses bit REV_SHIFT+1,
301 * and so on. REV_SHIFT bits from bit 0 are used for
302 * internal bookkeeping.
303 */
304 commit->object.flags |= 1u << (num_rev + REV_SHIFT);
305 insert_by_date(commit, &list);
306 rev[num_rev] = commit;
307 }
308 join_revs(&list, &seen, num_rev, extra);
309
310 head_path_len = readlink(".git/HEAD", head_path, sizeof(head_path)-1);
311 if ((head_path_len < 0) || get_sha1("HEAD", head_sha1))
312 head_path[0] = 0;
313 else
314 head_path[head_path_len] = 0;
315
316 if (merge_base)
317 return show_merge_base(seen, num_rev);
318
f5e375c9
JH
319 /* Show list */
320 if (1 < num_rev) {
f76412ed
JH
321 for (i = 0; i < num_rev; i++) {
322 int j;
323 int is_head = rev_is_head(head_path,
324 head_path_len,
325 ref_name[i],
326 head_sha1,
327 rev[i]->object.sha1);
328 for (j = 0; j < i; j++)
329 putchar(' ');
330 printf("%c [%s] ", is_head ? '*' : '!', ref_name[i]);
331 show_one_commit(rev[i], NULL);
332 }
f5e375c9
JH
333 for (i = 0; i < num_rev; i++)
334 putchar('-');
335 putchar('\n');
336 }
337
338 label = ref_name;
f76412ed
JH
339 while (seen) {
340 struct commit *commit = pop_one_commit(&seen);
341 int this_flag = commit->object.flags;
f5e375c9
JH
342 static char *obvious[] = { "" };
343
f76412ed
JH
344 if ((this_flag & UNINTERESTING) && (--extra < 0))
345 break;
f5e375c9
JH
346 if (1 < num_rev) {
347 for (i = 0; i < num_rev; i++)
348 putchar((this_flag & (1u << (i + REV_SHIFT)))
349 ? '+' : ' ');
350 putchar(' ');
351 }
352 show_one_commit(commit, label);
353 if (num_rev == 1)
354 label = obvious;
f76412ed
JH
355 }
356 return 0;
357}