builtin-branch.c: remove unused code in append_ref() callback function
[git/git.git] / builtin-branch.c
CommitLineData
c31820c2
LH
1/*
2 * Builtin "git branch"
3 *
d972cce0 4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
c31820c2
LH
5 * Based on git-branch.sh by Junio C Hamano.
6 */
7
8#include "cache.h"
85023577 9#include "color.h"
c31820c2
LH
10#include "refs.h"
11#include "commit.h"
12#include "builtin.h"
6f084a56 13#include "remote.h"
a8dfd5ea 14#include "parse-options.h"
e496c003 15#include "branch.h"
a8dfd5ea
PH
16
17static const char * const builtin_branch_usage[] = {
1b1dd23f
SB
18 "git branch [options] [-r | -a] [--merged | --no-merged]",
19 "git branch [options] [-l] [-f] <branchname> [<start-point>]",
20 "git branch [options] [-r] (-d | -D) <branchname>",
21 "git branch [options] (-m | -M) [<oldbranch>] <newbranch>",
a8dfd5ea
PH
22 NULL
23};
c31820c2 24
f3d985c3
JH
25#define REF_LOCAL_BRANCH 0x01
26#define REF_REMOTE_BRANCH 0x02
c31820c2
LH
27
28static const char *head;
29static unsigned char head_sha1[20];
30
6b2f2d98 31static int branch_use_color = -1;
a1158cae
AP
32static char branch_colors[][COLOR_MAXLEN] = {
33 "\033[m", /* reset */
34 "", /* PLAIN (normal) */
35 "\033[31m", /* REMOTE (red) */
753f96a4
JH
36 "", /* LOCAL (normal) */
37 "\033[32m", /* CURRENT (green) */
a1158cae
AP
38};
39enum color_branch {
40 COLOR_BRANCH_RESET = 0,
41 COLOR_BRANCH_PLAIN = 1,
42 COLOR_BRANCH_REMOTE = 2,
43 COLOR_BRANCH_LOCAL = 3,
44 COLOR_BRANCH_CURRENT = 4,
45};
46
049716b3
JH
47static enum merge_filter {
48 NO_FILTER = 0,
49 SHOW_NOT_MERGED,
50 SHOW_MERGED,
51} merge_filter;
52static unsigned char merge_filter_ref[20];
e8b404c2 53
a1158cae
AP
54static int parse_branch_color_slot(const char *var, int ofs)
55{
56 if (!strcasecmp(var+ofs, "plain"))
57 return COLOR_BRANCH_PLAIN;
58 if (!strcasecmp(var+ofs, "reset"))
59 return COLOR_BRANCH_RESET;
60 if (!strcasecmp(var+ofs, "remote"))
61 return COLOR_BRANCH_REMOTE;
62 if (!strcasecmp(var+ofs, "local"))
63 return COLOR_BRANCH_LOCAL;
64 if (!strcasecmp(var+ofs, "current"))
65 return COLOR_BRANCH_CURRENT;
66 die("bad config variable '%s'", var);
67}
68
ef90d6d4 69static int git_branch_config(const char *var, const char *value, void *cb)
a1158cae
AP
70{
71 if (!strcmp(var, "color.branch")) {
0f6f5a40 72 branch_use_color = git_config_colorbool(var, value, -1);
a1158cae
AP
73 return 0;
74 }
cc44c765 75 if (!prefixcmp(var, "color.branch.")) {
a1158cae 76 int slot = parse_branch_color_slot(var, 13);
5768c98a
JH
77 if (!value)
78 return config_error_nonbool(var);
a1158cae
AP
79 color_parse(value, var, branch_colors[slot]);
80 return 0;
81 }
ef90d6d4 82 return git_color_default_config(var, value, cb);
a1158cae
AP
83}
84
52fae7de 85static const char *branch_get_color(enum color_branch ix)
a1158cae 86{
6b2f2d98 87 if (branch_use_color > 0)
a1158cae
AP
88 return branch_colors[ix];
89 return "";
90}
91
b8e9a00d 92static int delete_branches(int argc, const char **argv, int force, int kinds)
c31820c2 93{
67affd51 94 struct commit *rev, *head_rev = head_rev;
c31820c2 95 unsigned char sha1[20];
b8e9a00d 96 char *name = NULL;
f3d985c3 97 const char *fmt, *remote;
f1eccbab 98 char section[PATH_MAX];
c31820c2 99 int i;
b8e9a00d 100 int ret = 0;
c31820c2 101
f3d985c3
JH
102 switch (kinds) {
103 case REF_REMOTE_BRANCH:
104 fmt = "refs/remotes/%s";
105 remote = "remote ";
106 force = 1;
107 break;
108 case REF_LOCAL_BRANCH:
109 fmt = "refs/heads/%s";
110 remote = "";
111 break;
112 default:
113 die("cannot use -a with -d");
114 }
c31820c2 115
67affd51
JH
116 if (!force) {
117 head_rev = lookup_commit_reference(head_sha1);
118 if (!head_rev)
119 die("Couldn't look up commit object for HEAD");
120 }
c31820c2 121 for (i = 0; i < argc; i++) {
b8e9a00d
QT
122 if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) {
123 error("Cannot delete the branch '%s' "
124 "which you are currently on.", argv[i]);
125 ret = 1;
126 continue;
127 }
128
8e0f7003 129 free(name);
c31820c2 130
f3d985c3 131 name = xstrdup(mkpath(fmt, argv[i]));
b8e9a00d
QT
132 if (!resolve_ref(name, sha1, 1, NULL)) {
133 error("%sbranch '%s' not found.",
134 remote, argv[i]);
135 ret = 1;
136 continue;
137 }
c31820c2
LH
138
139 rev = lookup_commit_reference(sha1);
b8e9a00d
QT
140 if (!rev) {
141 error("Couldn't look up commit object for '%s'", name);
142 ret = 1;
143 continue;
144 }
c31820c2
LH
145
146 /* This checks whether the merge bases of branch and
147 * HEAD contains branch -- which means that the HEAD
148 * contains everything in both.
149 */
150
151 if (!force &&
03840fc3 152 !in_merge_bases(rev, &head_rev, 1)) {
00ae8289 153 error("The branch '%s' is not an ancestor of "
b8e9a00d
QT
154 "your current HEAD.\n"
155 "If you are sure you want to delete it, "
156 "run 'git branch -D %s'.", argv[i], argv[i]);
157 ret = 1;
158 continue;
c31820c2
LH
159 }
160
b8e9a00d
QT
161 if (delete_ref(name, sha1)) {
162 error("Error deleting %sbranch '%s'", remote,
f3d985c3 163 argv[i]);
b8e9a00d 164 ret = 1;
f1eccbab 165 } else {
f3d985c3 166 printf("Deleted %sbranch %s.\n", remote, argv[i]);
f1eccbab
GP
167 snprintf(section, sizeof(section), "branch.%s",
168 argv[i]);
169 if (git_config_rename_section(section, NULL) < 0)
170 warning("Update of config-file failed");
171 }
c31820c2 172 }
c31820c2 173
8e0f7003 174 free(name);
b8e9a00d
QT
175
176 return(ret);
c31820c2 177}
c31820c2 178
bfcc9214
AP
179struct ref_item {
180 char *name;
181 unsigned int kind;
75e6e213 182 unsigned char sha1[20];
bfcc9214
AP
183};
184
185struct ref_list {
75e6e213 186 int index, alloc, maxwidth;
bfcc9214 187 struct ref_item *list;
694a5775 188 struct commit_list *with_commit;
bfcc9214
AP
189 int kinds;
190};
191
694a5775
JH
192static int has_commit(const unsigned char *sha1, struct commit_list *with_commit)
193{
194 struct commit *commit;
195
196 if (!with_commit)
197 return 1;
198 commit = lookup_commit_reference_gently(sha1, 1);
199 if (!commit)
200 return 0;
201 while (with_commit) {
202 struct commit *other;
203
204 other = with_commit->item;
205 with_commit = with_commit->next;
206 if (in_merge_bases(other, &commit, 1))
207 return 1;
208 }
209 return 0;
210}
211
bfcc9214 212static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
c31820c2 213{
bfcc9214
AP
214 struct ref_list *ref_list = (struct ref_list*)(cb_data);
215 struct ref_item *newitem;
0f31d680 216 int kind;
75e6e213 217 int len;
e8b404c2 218 static struct commit_list branch;
bfcc9214
AP
219
220 /* Detect kind */
cc44c765 221 if (!prefixcmp(refname, "refs/heads/")) {
bfcc9214
AP
222 kind = REF_LOCAL_BRANCH;
223 refname += 11;
cc44c765 224 } else if (!prefixcmp(refname, "refs/remotes/")) {
bfcc9214
AP
225 kind = REF_REMOTE_BRANCH;
226 refname += 13;
0f31d680
JH
227 } else
228 return 0;
bfcc9214 229
694a5775
JH
230 /* Filter with with_commit if specified */
231 if (!has_commit(sha1, ref_list->with_commit))
232 return 0;
233
bfcc9214
AP
234 /* Don't add types the caller doesn't want */
235 if ((kind & ref_list->kinds) == 0)
236 return 0;
237
049716b3 238 if (merge_filter != NO_FILTER) {
e8b404c2
LH
239 branch.item = lookup_commit_reference_gently(sha1, 1);
240 if (!branch.item)
241 die("Unable to lookup tip of branch %s", refname);
049716b3
JH
242 if (merge_filter == SHOW_NOT_MERGED &&
243 has_commit(merge_filter_ref, &branch))
e8b404c2 244 return 0;
049716b3
JH
245 if (merge_filter == SHOW_MERGED &&
246 !has_commit(merge_filter_ref, &branch))
e8b404c2
LH
247 return 0;
248 }
249
bfcc9214
AP
250 /* Resize buffer */
251 if (ref_list->index >= ref_list->alloc) {
252 ref_list->alloc = alloc_nr(ref_list->alloc);
253 ref_list->list = xrealloc(ref_list->list,
254 ref_list->alloc * sizeof(struct ref_item));
c31820c2
LH
255 }
256
bfcc9214
AP
257 /* Record the new item */
258 newitem = &(ref_list->list[ref_list->index++]);
259 newitem->name = xstrdup(refname);
260 newitem->kind = kind;
75e6e213
LH
261 hashcpy(newitem->sha1, sha1);
262 len = strlen(newitem->name);
263 if (len > ref_list->maxwidth)
264 ref_list->maxwidth = len;
c31820c2
LH
265
266 return 0;
267}
268
bfcc9214
AP
269static void free_ref_list(struct ref_list *ref_list)
270{
271 int i;
272
273 for (i = 0; i < ref_list->index; i++)
274 free(ref_list->list[i].name);
275 free(ref_list->list);
276}
277
c31820c2
LH
278static int ref_cmp(const void *r1, const void *r2)
279{
bfcc9214
AP
280 struct ref_item *c1 = (struct ref_item *)(r1);
281 struct ref_item *c2 = (struct ref_item *)(r2);
282
283 if (c1->kind != c2->kind)
284 return c1->kind - c2->kind;
285 return strcmp(c1->name, c2->name);
c31820c2
LH
286}
287
94fcb730
JH
288static void fill_tracking_info(char *stat, const char *branch_name)
289{
290 int ours, theirs;
291 struct branch *branch = branch_get(branch_name);
292
926ab840 293 if (!stat_tracking_info(branch, &ours, &theirs) || (!ours && !theirs))
94fcb730 294 return;
94fcb730
JH
295 if (!ours)
296 sprintf(stat, "[behind %d] ", theirs);
297 else if (!theirs)
298 sprintf(stat, "[ahead %d] ", ours);
299 else
300 sprintf(stat, "[ahead %d, behind %d] ", ours, theirs);
301}
302
af0e4ac0
LH
303static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
304 int abbrev, int current)
75e6e213 305{
af0e4ac0
LH
306 char c;
307 int color;
75e6e213 308 struct commit *commit;
75e6e213 309
af0e4ac0
LH
310 switch (item->kind) {
311 case REF_LOCAL_BRANCH:
312 color = COLOR_BRANCH_LOCAL;
313 break;
314 case REF_REMOTE_BRANCH:
315 color = COLOR_BRANCH_REMOTE;
316 break;
317 default:
318 color = COLOR_BRANCH_PLAIN;
319 break;
320 }
75e6e213 321
af0e4ac0
LH
322 c = ' ';
323 if (current) {
324 c = '*';
325 color = COLOR_BRANCH_CURRENT;
326 }
75e6e213 327
af0e4ac0 328 if (verbose) {
674d1727 329 struct strbuf subject;
80583c0e 330 const char *sub = " **** invalid ref ****";
94fcb730 331 char stat[128];
80583c0e 332
674d1727 333 strbuf_init(&subject, 0);
926ab840 334 stat[0] = '\0';
674d1727 335
af0e4ac0 336 commit = lookup_commit(item->sha1);
80583c0e 337 if (commit && !parse_commit(commit)) {
674d1727 338 pretty_print_commit(CMIT_FMT_ONELINE, commit,
4593fb84 339 &subject, 0, NULL, NULL, 0, 0);
674d1727 340 sub = subject.buf;
80583c0e 341 }
94fcb730
JH
342
343 if (item->kind == REF_LOCAL_BRANCH)
344 fill_tracking_info(stat, item->name);
345
346 printf("%c %s%-*s%s %s %s%s\n", c, branch_get_color(color),
af0e4ac0
LH
347 maxwidth, item->name,
348 branch_get_color(COLOR_BRANCH_RESET),
94fcb730
JH
349 find_unique_abbrev(item->sha1, abbrev),
350 stat, sub);
674d1727 351 strbuf_release(&subject);
af0e4ac0
LH
352 } else {
353 printf("%c %s%s%s\n", c, branch_get_color(color), item->name,
354 branch_get_color(COLOR_BRANCH_RESET));
355 }
75e6e213
LH
356}
357
694a5775 358static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
c31820c2
LH
359{
360 int i;
bfcc9214 361 struct ref_list ref_list;
c31820c2 362
bfcc9214
AP
363 memset(&ref_list, 0, sizeof(ref_list));
364 ref_list.kinds = kinds;
694a5775 365 ref_list.with_commit = with_commit;
bfcc9214 366 for_each_ref(append_ref, &ref_list);
c31820c2 367
bfcc9214 368 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
c31820c2 369
0016a482 370 detached = (detached && (kinds & REF_LOCAL_BRANCH));
694a5775 371 if (detached && has_commit(head_sha1, with_commit)) {
0016a482 372 struct ref_item item;
3a55602e 373 item.name = xstrdup("(no branch)");
0016a482
LH
374 item.kind = REF_LOCAL_BRANCH;
375 hashcpy(item.sha1, head_sha1);
376 if (strlen(item.name) > ref_list.maxwidth)
377 ref_list.maxwidth = strlen(item.name);
378 print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1);
3a55602e 379 free(item.name);
0016a482
LH
380 }
381
bfcc9214 382 for (i = 0; i < ref_list.index; i++) {
0016a482
LH
383 int current = !detached &&
384 (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
af0e4ac0
LH
385 !strcmp(ref_list.list[i].name, head);
386 print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
387 abbrev, current);
c31820c2 388 }
bfcc9214
AP
389
390 free_ref_list(&ref_list);
c31820c2
LH
391}
392
c976d415
LH
393static void rename_branch(const char *oldname, const char *newname, int force)
394{
678d0f4c 395 char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
c976d415 396 unsigned char sha1[20];
19eba151 397 char oldsection[PATH_MAX], newsection[PATH_MAX];
c976d415 398
c847f537 399 if (!oldname)
3dff5379 400 die("cannot rename the current branch while not on any.");
c847f537 401
c976d415
LH
402 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
403 die("Old branchname too long");
404
405 if (check_ref_format(oldref))
406 die("Invalid branch name: %s", oldref);
407
408 if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
409 die("New branchname too long");
410
411 if (check_ref_format(newref))
412 die("Invalid branch name: %s", newref);
413
414 if (resolve_ref(newref, sha1, 1, NULL) && !force)
415 die("A branch named '%s' already exists.", newname);
416
678d0f4c
LH
417 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
418 oldref, newref);
419
420 if (rename_ref(oldref, newref, logmsg))
c976d415
LH
421 die("Branch rename failed");
422
8b5157e4
NP
423 /* no need to pass logmsg here as HEAD didn't really move */
424 if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL))
c976d415 425 die("Branch renamed to %s, but HEAD is not updated!", newname);
19eba151
LH
426
427 snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11);
428 snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11);
429 if (git_config_rename_section(oldsection, newsection) < 0)
430 die("Branch is renamed, but update of config-file failed");
c976d415
LH
431}
432
694a5775
JH
433static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset)
434{
435 unsigned char sha1[20];
436 struct commit *commit;
437
438 if (!arg)
439 return -1;
440 if (get_sha1(arg, sha1))
441 die("malformed object name %s", arg);
442 commit = lookup_commit_reference(sha1);
443 if (!commit)
444 die("no such commit %s", arg);
445 commit_list_insert(commit, opt->value);
446 return 0;
447}
448
049716b3
JH
449static int opt_parse_merge_filter(const struct option *opt, const char *arg, int unset)
450{
451 merge_filter = ((opt->long_name[0] == 'n')
452 ? SHOW_NOT_MERGED
453 : SHOW_MERGED);
454 if (unset)
455 merge_filter = SHOW_NOT_MERGED; /* b/c for --no-merged */
456 if (!arg)
457 arg = "HEAD";
458 if (get_sha1(arg, merge_filter_ref))
459 die("malformed object name %s", arg);
460 return 0;
461}
462
c31820c2
LH
463int cmd_branch(int argc, const char **argv, const char *prefix)
464{
d11d44fa 465 int delete = 0, rename = 0, force_create = 0;
0016a482 466 int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
9ed36cfa
JS
467 int reflog = 0;
468 enum branch_track track;
d11d44fa 469 int kinds = REF_LOCAL_BRANCH;
694a5775 470 struct commit_list *with_commit = NULL;
a8dfd5ea
PH
471
472 struct option options[] = {
473 OPT_GROUP("Generic options"),
474 OPT__VERBOSE(&verbose),
9ed36cfa
JS
475 OPT_SET_INT( 0 , "track", &track, "set up tracking mode (see git-pull(1))",
476 BRANCH_TRACK_EXPLICIT),
a8dfd5ea 477 OPT_BOOLEAN( 0 , "color", &branch_use_color, "use colored output"),
d11d44fa
PH
478 OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches",
479 REF_REMOTE_BRANCH),
e84fb2ff
JH
480 {
481 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
482 "print only branches that contain the commit",
483 PARSE_OPT_LASTARG_DEFAULT,
484 opt_parse_with_commit, (intptr_t)"HEAD",
485 },
694a5775
JH
486 {
487 OPTION_CALLBACK, 0, "with", &with_commit, "commit",
488 "print only branches that contain the commit",
e84fb2ff
JH
489 PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT,
490 opt_parse_with_commit, (intptr_t) "HEAD",
694a5775 491 },
a8dfd5ea
PH
492 OPT__ABBREV(&abbrev),
493
494 OPT_GROUP("Specific git-branch actions:"),
d11d44fa
PH
495 OPT_SET_INT('a', NULL, &kinds, "list both remote-tracking and local branches",
496 REF_REMOTE_BRANCH | REF_LOCAL_BRANCH),
497 OPT_BIT('d', NULL, &delete, "delete fully merged branch", 1),
498 OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2),
499 OPT_BIT('m', NULL, &rename, "move/rename a branch and its reflog", 1),
500 OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
501 OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"),
502 OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"),
049716b3
JH
503 {
504 OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
505 "commit", "print only not merged branches",
506 PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
507 opt_parse_merge_filter, (intptr_t) "HEAD",
508 },
509 {
510 OPTION_CALLBACK, 0, "merged", &merge_filter_ref,
511 "commit", "print only merged branches",
512 PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
513 opt_parse_merge_filter, (intptr_t) "HEAD",
514 },
a8dfd5ea
PH
515 OPT_END(),
516 };
c31820c2 517
ef90d6d4 518 git_config(git_branch_config, NULL);
6b2f2d98
MK
519
520 if (branch_use_color == -1)
521 branch_use_color = git_use_color_default;
522
9ed36cfa 523 track = git_branch_track;
c976d415 524
078f8380 525 head = resolve_ref("HEAD", head_sha1, 0, NULL);
c31820c2
LH
526 if (!head)
527 die("Failed to resolve HEAD as a valid ref.");
078f8380 528 head = xstrdup(head);
0016a482
LH
529 if (!strcmp(head, "HEAD")) {
530 detached = 1;
a8dfd5ea 531 } else {
cc44c765 532 if (prefixcmp(head, "refs/heads/"))
0016a482
LH
533 die("HEAD not found below refs/heads!");
534 head += 11;
535 }
049716b3
JH
536 hashcpy(merge_filter_ref, head_sha1);
537
538 argc = parse_options(argc, argv, options, builtin_branch_usage, 0);
539 if (!!delete + !!rename + !!force_create > 1)
540 usage_with_options(builtin_branch_usage, options);
c31820c2
LH
541
542 if (delete)
d11d44fa 543 return delete_branches(argc, argv, delete > 1, kinds);
a8dfd5ea 544 else if (argc == 0)
694a5775 545 print_ref_list(kinds, detached, verbose, abbrev, with_commit);
a8dfd5ea 546 else if (rename && (argc == 1))
d11d44fa 547 rename_branch(head, argv[0], rename > 1);
a8dfd5ea 548 else if (rename && (argc == 2))
d11d44fa 549 rename_branch(argv[0], argv[1], rename > 1);
a8dfd5ea 550 else if (argc <= 2)
e496c003 551 create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
45994a1e 552 force_create, reflog, track);
c31820c2 553 else
a8dfd5ea 554 usage_with_options(builtin_branch_usage, options);
c31820c2
LH
555
556 return 0;
557}