branch_merged: fix grammar in warning
[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"
68067ca1
JH
16#include "diff.h"
17#include "revision.h"
a8dfd5ea
PH
18
19static const char * const builtin_branch_usage[] = {
1b1dd23f
SB
20 "git branch [options] [-r | -a] [--merged | --no-merged]",
21 "git branch [options] [-l] [-f] <branchname> [<start-point>]",
22 "git branch [options] [-r] (-d | -D) <branchname>",
23 "git branch [options] (-m | -M) [<oldbranch>] <newbranch>",
a8dfd5ea
PH
24 NULL
25};
c31820c2 26
f3d985c3
JH
27#define REF_LOCAL_BRANCH 0x01
28#define REF_REMOTE_BRANCH 0x02
c31820c2
LH
29
30static const char *head;
31static unsigned char head_sha1[20];
32
6b2f2d98 33static int branch_use_color = -1;
a1158cae 34static char branch_colors[][COLOR_MAXLEN] = {
dc6ebd4c
AL
35 GIT_COLOR_RESET,
36 GIT_COLOR_NORMAL, /* PLAIN */
37 GIT_COLOR_RED, /* REMOTE */
38 GIT_COLOR_NORMAL, /* LOCAL */
39 GIT_COLOR_GREEN, /* CURRENT */
a1158cae
AP
40};
41enum color_branch {
74bb2dfb
AL
42 BRANCH_COLOR_RESET = 0,
43 BRANCH_COLOR_PLAIN = 1,
44 BRANCH_COLOR_REMOTE = 2,
45 BRANCH_COLOR_LOCAL = 3,
4b05548f 46 BRANCH_COLOR_CURRENT = 4
a1158cae
AP
47};
48
049716b3
JH
49static enum merge_filter {
50 NO_FILTER = 0,
51 SHOW_NOT_MERGED,
4b05548f 52 SHOW_MERGED
049716b3
JH
53} merge_filter;
54static unsigned char merge_filter_ref[20];
e8b404c2 55
a1158cae
AP
56static int parse_branch_color_slot(const char *var, int ofs)
57{
58 if (!strcasecmp(var+ofs, "plain"))
74bb2dfb 59 return BRANCH_COLOR_PLAIN;
a1158cae 60 if (!strcasecmp(var+ofs, "reset"))
74bb2dfb 61 return BRANCH_COLOR_RESET;
a1158cae 62 if (!strcasecmp(var+ofs, "remote"))
74bb2dfb 63 return BRANCH_COLOR_REMOTE;
a1158cae 64 if (!strcasecmp(var+ofs, "local"))
74bb2dfb 65 return BRANCH_COLOR_LOCAL;
a1158cae 66 if (!strcasecmp(var+ofs, "current"))
74bb2dfb 67 return BRANCH_COLOR_CURRENT;
8b8e8624 68 return -1;
a1158cae
AP
69}
70
ef90d6d4 71static int git_branch_config(const char *var, const char *value, void *cb)
a1158cae
AP
72{
73 if (!strcmp(var, "color.branch")) {
0f6f5a40 74 branch_use_color = git_config_colorbool(var, value, -1);
a1158cae
AP
75 return 0;
76 }
cc44c765 77 if (!prefixcmp(var, "color.branch.")) {
a1158cae 78 int slot = parse_branch_color_slot(var, 13);
8b8e8624
JK
79 if (slot < 0)
80 return 0;
5768c98a
JH
81 if (!value)
82 return config_error_nonbool(var);
a1158cae
AP
83 color_parse(value, var, branch_colors[slot]);
84 return 0;
85 }
ef90d6d4 86 return git_color_default_config(var, value, cb);
a1158cae
AP
87}
88
52fae7de 89static const char *branch_get_color(enum color_branch ix)
a1158cae 90{
6b2f2d98 91 if (branch_use_color > 0)
a1158cae
AP
92 return branch_colors[ix];
93 return "";
94}
95
99c419c9
JH
96static int branch_merged(int kind, const char *name,
97 struct commit *rev, struct commit *head_rev)
98{
99 /*
100 * This checks whether the merge bases of branch and HEAD (or
101 * the other branch this branch builds upon) contains the
102 * branch, which means that the branch has already been merged
103 * safely to HEAD (or the other branch).
104 */
105 struct commit *reference_rev = NULL;
106 const char *reference_name = NULL;
107 int merged;
108
109 if (kind == REF_LOCAL_BRANCH) {
110 struct branch *branch = branch_get(name);
111 unsigned char sha1[20];
112
113 if (branch &&
114 branch->merge &&
115 branch->merge[0] &&
116 branch->merge[0]->dst &&
117 (reference_name =
118 resolve_ref(branch->merge[0]->dst, sha1, 1, NULL)) != NULL)
119 reference_rev = lookup_commit_reference(sha1);
120 }
121 if (!reference_rev)
122 reference_rev = head_rev;
123
124 merged = in_merge_bases(rev, &reference_rev, 1);
125
126 /*
127 * After the safety valve is fully redefined to "check with
128 * upstream, if any, otherwise with HEAD", we should just
129 * return the result of the in_merge_bases() above without
130 * any of the following code, but during the transition period,
131 * a gentle reminder is in order.
132 */
133 if ((head_rev != reference_rev) &&
134 in_merge_bases(rev, &head_rev, 1) != merged) {
135 if (merged)
136 warning("deleting branch '%s' that has been merged to\n"
62270f6b 137 " '%s', but not yet been merged to HEAD.",
99c419c9
JH
138 name, reference_name);
139 else
140 warning("not deleting branch '%s' that is not yet merged to\n"
141 " '%s', even though it is merged to HEAD.",
142 name, reference_name);
143 }
144 return merged;
145}
146
b8e9a00d 147static int delete_branches(int argc, const char **argv, int force, int kinds)
c31820c2 148{
99c419c9 149 struct commit *rev, *head_rev = NULL;
c31820c2 150 unsigned char sha1[20];
b8e9a00d 151 char *name = NULL;
f3d985c3 152 const char *fmt, *remote;
c31820c2 153 int i;
b8e9a00d 154 int ret = 0;
8415d5c7 155 struct strbuf bname = STRBUF_INIT;
c31820c2 156
f3d985c3
JH
157 switch (kinds) {
158 case REF_REMOTE_BRANCH:
159 fmt = "refs/remotes/%s";
160 remote = "remote ";
161 force = 1;
162 break;
163 case REF_LOCAL_BRANCH:
164 fmt = "refs/heads/%s";
165 remote = "";
166 break;
167 default:
168 die("cannot use -a with -d");
169 }
c31820c2 170
67affd51
JH
171 if (!force) {
172 head_rev = lookup_commit_reference(head_sha1);
173 if (!head_rev)
174 die("Couldn't look up commit object for HEAD");
175 }
8415d5c7 176 for (i = 0; i < argc; i++, strbuf_release(&bname)) {
a552de75 177 strbuf_branchname(&bname, argv[i]);
8415d5c7 178 if (kinds == REF_LOCAL_BRANCH && !strcmp(head, bname.buf)) {
b8e9a00d 179 error("Cannot delete the branch '%s' "
8415d5c7 180 "which you are currently on.", bname.buf);
b8e9a00d
QT
181 ret = 1;
182 continue;
183 }
184
8e0f7003 185 free(name);
c31820c2 186
8415d5c7 187 name = xstrdup(mkpath(fmt, bname.buf));
b8e9a00d
QT
188 if (!resolve_ref(name, sha1, 1, NULL)) {
189 error("%sbranch '%s' not found.",
8415d5c7 190 remote, bname.buf);
b8e9a00d
QT
191 ret = 1;
192 continue;
193 }
c31820c2
LH
194
195 rev = lookup_commit_reference(sha1);
b8e9a00d
QT
196 if (!rev) {
197 error("Couldn't look up commit object for '%s'", name);
198 ret = 1;
199 continue;
200 }
c31820c2 201
99c419c9
JH
202 if (!force && !branch_merged(kinds, bname.buf, rev, head_rev)) {
203 error("The branch '%s' is not fully merged.\n"
8415d5c7
JH
204 "If you are sure you want to delete it, "
205 "run 'git branch -D %s'.", bname.buf, bname.buf);
b8e9a00d
QT
206 ret = 1;
207 continue;
c31820c2
LH
208 }
209
eca35a25 210 if (delete_ref(name, sha1, 0)) {
b8e9a00d 211 error("Error deleting %sbranch '%s'", remote,
8415d5c7 212 bname.buf);
b8e9a00d 213 ret = 1;
f1eccbab 214 } else {
3c59c50d 215 struct strbuf buf = STRBUF_INIT;
23fd723c 216 printf("Deleted %sbranch %s (was %s).\n", remote,
8415d5c7
JH
217 bname.buf,
218 find_unique_abbrev(sha1, DEFAULT_ABBREV));
219 strbuf_addf(&buf, "branch.%s", bname.buf);
3c59c50d 220 if (git_config_rename_section(buf.buf, NULL) < 0)
f1eccbab 221 warning("Update of config-file failed");
3c59c50d 222 strbuf_release(&buf);
f1eccbab 223 }
c31820c2 224 }
c31820c2 225
8e0f7003 226 free(name);
b8e9a00d
QT
227
228 return(ret);
c31820c2 229}
c31820c2 230
bfcc9214
AP
231struct ref_item {
232 char *name;
209d336a
JS
233 char *dest;
234 unsigned int kind, len;
68067ca1 235 struct commit *commit;
bfcc9214
AP
236};
237
238struct ref_list {
68067ca1 239 struct rev_info revs;
7e9ff00b 240 int index, alloc, maxwidth, verbose, abbrev;
bfcc9214 241 struct ref_item *list;
694a5775 242 struct commit_list *with_commit;
bfcc9214
AP
243 int kinds;
244};
245
209d336a
JS
246static char *resolve_symref(const char *src, const char *prefix)
247{
248 unsigned char sha1[20];
249 int flag;
250 const char *dst, *cp;
251
252 dst = resolve_ref(src, sha1, 0, &flag);
253 if (!(dst && (flag & REF_ISSYMREF)))
254 return NULL;
255 if (prefix && (cp = skip_prefix(dst, prefix)))
256 dst = cp;
257 return xstrdup(dst);
258}
259
1603ade8
SM
260struct append_ref_cb {
261 struct ref_list *ref_list;
262 int ret;
263};
264
bfcc9214 265static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
c31820c2 266{
1603ade8
SM
267 struct append_ref_cb *cb = (struct append_ref_cb *)(cb_data);
268 struct ref_list *ref_list = cb->ref_list;
bfcc9214 269 struct ref_item *newitem;
68067ca1 270 struct commit *commit;
209d336a
JS
271 int kind, i;
272 const char *prefix, *orig_refname = refname;
273
274 static struct {
275 int kind;
276 const char *prefix;
277 int pfxlen;
278 } ref_kind[] = {
279 { REF_LOCAL_BRANCH, "refs/heads/", 11 },
280 { REF_REMOTE_BRANCH, "refs/remotes/", 13 },
281 };
bfcc9214
AP
282
283 /* Detect kind */
209d336a
JS
284 for (i = 0; i < ARRAY_SIZE(ref_kind); i++) {
285 prefix = ref_kind[i].prefix;
286 if (strncmp(refname, prefix, ref_kind[i].pfxlen))
287 continue;
288 kind = ref_kind[i].kind;
289 refname += ref_kind[i].pfxlen;
290 break;
291 }
292 if (ARRAY_SIZE(ref_kind) <= i)
0f31d680 293 return 0;
bfcc9214
AP
294
295 /* Don't add types the caller doesn't want */
296 if ((kind & ref_list->kinds) == 0)
297 return 0;
298
191d1ac4
LT
299 commit = NULL;
300 if (ref_list->verbose || ref_list->with_commit || merge_filter != NO_FILTER) {
301 commit = lookup_commit_reference_gently(sha1, 1);
1603ade8
SM
302 if (!commit) {
303 cb->ret = error("branch '%s' does not point at a commit", refname);
0e9716e6 304 return 0;
1603ade8 305 }
68067ca1 306
191d1ac4
LT
307 /* Filter with with_commit if specified */
308 if (!is_descendant_of(commit, ref_list->with_commit))
309 return 0;
694a5775 310
191d1ac4
LT
311 if (merge_filter != NO_FILTER)
312 add_pending_object(&ref_list->revs,
313 (struct object *)commit, refname);
314 }
e8b404c2 315
fcbc0d8e 316 ALLOC_GROW(ref_list->list, ref_list->index + 1, ref_list->alloc);
c31820c2 317
bfcc9214
AP
318 /* Record the new item */
319 newitem = &(ref_list->list[ref_list->index++]);
320 newitem->name = xstrdup(refname);
321 newitem->kind = kind;
68067ca1 322 newitem->commit = commit;
209d336a
JS
323 newitem->len = strlen(refname);
324 newitem->dest = resolve_symref(orig_refname, prefix);
325 /* adjust for "remotes/" */
326 if (newitem->kind == REF_REMOTE_BRANCH &&
327 ref_list->kinds != REF_REMOTE_BRANCH)
328 newitem->len += 8;
329 if (newitem->len > ref_list->maxwidth)
330 ref_list->maxwidth = newitem->len;
c31820c2
LH
331
332 return 0;
333}
334
bfcc9214
AP
335static void free_ref_list(struct ref_list *ref_list)
336{
337 int i;
338
209d336a 339 for (i = 0; i < ref_list->index; i++) {
bfcc9214 340 free(ref_list->list[i].name);
209d336a
JS
341 free(ref_list->list[i].dest);
342 }
bfcc9214
AP
343 free(ref_list->list);
344}
345
c31820c2
LH
346static int ref_cmp(const void *r1, const void *r2)
347{
bfcc9214
AP
348 struct ref_item *c1 = (struct ref_item *)(r1);
349 struct ref_item *c2 = (struct ref_item *)(r2);
350
351 if (c1->kind != c2->kind)
352 return c1->kind - c2->kind;
353 return strcmp(c1->name, c2->name);
c31820c2
LH
354}
355
2d8a7f0b
JK
356static void fill_tracking_info(struct strbuf *stat, const char *branch_name,
357 int show_upstream_ref)
94fcb730
JH
358{
359 int ours, theirs;
360 struct branch *branch = branch_get(branch_name);
361
2d8a7f0b
JK
362 if (!stat_tracking_info(branch, &ours, &theirs)) {
363 if (branch && branch->merge && branch->merge[0]->dst &&
364 show_upstream_ref)
365 strbuf_addf(stat, "[%s] ",
6e7b3309 366 shorten_unambiguous_ref(branch->merge[0]->dst, 0));
94fcb730 367 return;
2d8a7f0b
JK
368 }
369
370 strbuf_addch(stat, '[');
371 if (show_upstream_ref)
372 strbuf_addf(stat, "%s: ",
6e7b3309 373 shorten_unambiguous_ref(branch->merge[0]->dst, 0));
94fcb730 374 if (!ours)
2d8a7f0b 375 strbuf_addf(stat, "behind %d] ", theirs);
94fcb730 376 else if (!theirs)
2d8a7f0b 377 strbuf_addf(stat, "ahead %d] ", ours);
94fcb730 378 else
2d8a7f0b 379 strbuf_addf(stat, "ahead %d, behind %d] ", ours, theirs);
94fcb730
JH
380}
381
7950cda7
LH
382static int matches_merge_filter(struct commit *commit)
383{
384 int is_merged;
385
386 if (merge_filter == NO_FILTER)
387 return 1;
388
389 is_merged = !!(commit->object.flags & UNINTERESTING);
390 return (is_merged == (merge_filter == SHOW_MERGED));
391}
392
af0e4ac0 393static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
209d336a 394 int abbrev, int current, char *prefix)
75e6e213 395{
af0e4ac0
LH
396 char c;
397 int color;
68067ca1 398 struct commit *commit = item->commit;
209d336a 399 struct strbuf out = STRBUF_INIT, name = STRBUF_INIT;
68067ca1 400
7950cda7
LH
401 if (!matches_merge_filter(commit))
402 return;
75e6e213 403
af0e4ac0
LH
404 switch (item->kind) {
405 case REF_LOCAL_BRANCH:
74bb2dfb 406 color = BRANCH_COLOR_LOCAL;
af0e4ac0
LH
407 break;
408 case REF_REMOTE_BRANCH:
74bb2dfb 409 color = BRANCH_COLOR_REMOTE;
af0e4ac0
LH
410 break;
411 default:
74bb2dfb 412 color = BRANCH_COLOR_PLAIN;
af0e4ac0
LH
413 break;
414 }
75e6e213 415
af0e4ac0
LH
416 c = ' ';
417 if (current) {
418 c = '*';
74bb2dfb 419 color = BRANCH_COLOR_CURRENT;
af0e4ac0 420 }
75e6e213 421
209d336a
JS
422 strbuf_addf(&name, "%s%s", prefix, item->name);
423 if (verbose)
424 strbuf_addf(&out, "%c %s%-*s%s", c, branch_get_color(color),
425 maxwidth, name.buf,
81474267 426 branch_get_color(BRANCH_COLOR_RESET));
209d336a
JS
427 else
428 strbuf_addf(&out, "%c %s%s%s", c, branch_get_color(color),
81474267 429 name.buf, branch_get_color(BRANCH_COLOR_RESET));
209d336a
JS
430
431 if (item->dest)
432 strbuf_addf(&out, " -> %s", item->dest);
433 else if (verbose) {
d3f9f9a9 434 struct strbuf subject = STRBUF_INIT, stat = STRBUF_INIT;
80583c0e 435 const char *sub = " **** invalid ref ****";
674d1727 436
68067ca1 437 commit = item->commit;
80583c0e 438 if (commit && !parse_commit(commit)) {
dd2e794a 439 struct pretty_print_context ctx = {0};
674d1727 440 pretty_print_commit(CMIT_FMT_ONELINE, commit,
dd2e794a 441 &subject, &ctx);
674d1727 442 sub = subject.buf;
80583c0e 443 }
94fcb730
JH
444
445 if (item->kind == REF_LOCAL_BRANCH)
2d8a7f0b 446 fill_tracking_info(&stat, item->name, verbose > 1);
94fcb730 447
209d336a
JS
448 strbuf_addf(&out, " %s %s%s",
449 find_unique_abbrev(item->commit->object.sha1, abbrev),
450 stat.buf, sub);
d3f9f9a9 451 strbuf_release(&stat);
674d1727 452 strbuf_release(&subject);
af0e4ac0 453 }
209d336a
JS
454 printf("%s\n", out.buf);
455 strbuf_release(&name);
456 strbuf_release(&out);
75e6e213
LH
457}
458
b6f637d1
LH
459static int calc_maxwidth(struct ref_list *refs)
460{
209d336a 461 int i, w = 0;
b6f637d1
LH
462 for (i = 0; i < refs->index; i++) {
463 if (!matches_merge_filter(refs->list[i].commit))
464 continue;
209d336a
JS
465 if (refs->list[i].len > w)
466 w = refs->list[i].len;
b6f637d1
LH
467 }
468 return w;
469}
470
7e9ff00b
LT
471
472static void show_detached(struct ref_list *ref_list)
473{
474 struct commit *head_commit = lookup_commit_reference_gently(head_sha1, 1);
475
476 if (head_commit && is_descendant_of(head_commit, ref_list->with_commit)) {
477 struct ref_item item;
478 item.name = xstrdup("(no branch)");
479 item.len = strlen(item.name);
480 item.kind = REF_LOCAL_BRANCH;
481 item.dest = NULL;
482 item.commit = head_commit;
483 if (item.len > ref_list->maxwidth)
484 ref_list->maxwidth = item.len;
485 print_ref_item(&item, ref_list->maxwidth, ref_list->verbose, ref_list->abbrev, 1, "");
486 free(item.name);
487 }
488}
489
1603ade8 490static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
c31820c2
LH
491{
492 int i;
1603ade8 493 struct append_ref_cb cb;
bfcc9214 494 struct ref_list ref_list;
c31820c2 495
bfcc9214
AP
496 memset(&ref_list, 0, sizeof(ref_list));
497 ref_list.kinds = kinds;
191d1ac4 498 ref_list.verbose = verbose;
7e9ff00b 499 ref_list.abbrev = abbrev;
694a5775 500 ref_list.with_commit = with_commit;
68067ca1
JH
501 if (merge_filter != NO_FILTER)
502 init_revisions(&ref_list.revs, NULL);
1603ade8
SM
503 cb.ref_list = &ref_list;
504 cb.ret = 0;
505 for_each_rawref(append_ref, &cb);
68067ca1
JH
506 if (merge_filter != NO_FILTER) {
507 struct commit *filter;
508 filter = lookup_commit_reference_gently(merge_filter_ref, 0);
509 filter->object.flags |= UNINTERESTING;
510 add_pending_object(&ref_list.revs,
511 (struct object *) filter, "");
512 ref_list.revs.limited = 1;
513 prepare_revision_walk(&ref_list.revs);
b6f637d1
LH
514 if (verbose)
515 ref_list.maxwidth = calc_maxwidth(&ref_list);
68067ca1 516 }
c31820c2 517
bfcc9214 518 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
c31820c2 519
0016a482 520 detached = (detached && (kinds & REF_LOCAL_BRANCH));
7e9ff00b
LT
521 if (detached)
522 show_detached(&ref_list);
0016a482 523
bfcc9214 524 for (i = 0; i < ref_list.index; i++) {
0016a482
LH
525 int current = !detached &&
526 (ref_list.list[i].kind == REF_LOCAL_BRANCH) &&
af0e4ac0 527 !strcmp(ref_list.list[i].name, head);
209d336a
JS
528 char *prefix = (kinds != REF_REMOTE_BRANCH &&
529 ref_list.list[i].kind == REF_REMOTE_BRANCH)
530 ? "remotes/" : "";
af0e4ac0 531 print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose,
209d336a 532 abbrev, current, prefix);
c31820c2 533 }
bfcc9214
AP
534
535 free_ref_list(&ref_list);
1603ade8 536
0e9716e6
SM
537 if (cb.ret)
538 error("some refs could not be read");
539
1603ade8 540 return cb.ret;
c31820c2
LH
541}
542
c976d415
LH
543static void rename_branch(const char *oldname, const char *newname, int force)
544{
da3f78a2 545 struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
c976d415 546 unsigned char sha1[20];
da3f78a2 547 struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
cbdffe40 548 int recovery = 0;
c976d415 549
c847f537 550 if (!oldname)
3dff5379 551 die("cannot rename the current branch while not on any.");
c847f537 552
cbdffe40
JH
553 if (strbuf_check_branch_ref(&oldref, oldname)) {
554 /*
555 * Bad name --- this could be an attempt to rename a
556 * ref that we used to allow to be created by accident.
557 */
558 if (resolve_ref(oldref.buf, sha1, 1, NULL))
559 recovery = 1;
560 else
561 die("Invalid branch name: '%s'", oldname);
562 }
c976d415 563
a2fab531 564 if (strbuf_check_branch_ref(&newref, newname))
03d3aada 565 die("Invalid branch name: '%s'", newname);
c976d415 566
da3f78a2 567 if (resolve_ref(newref.buf, sha1, 1, NULL) && !force)
03d3aada 568 die("A branch named '%s' already exists.", newref.buf + 11);
c976d415 569
da3f78a2
MV
570 strbuf_addf(&logmsg, "Branch: renamed %s to %s",
571 oldref.buf, newref.buf);
678d0f4c 572
da3f78a2 573 if (rename_ref(oldref.buf, newref.buf, logmsg.buf))
c976d415 574 die("Branch rename failed");
da3f78a2 575 strbuf_release(&logmsg);
c976d415 576
cbdffe40
JH
577 if (recovery)
578 warning("Renamed a misnamed branch '%s' away", oldref.buf + 11);
579
8b5157e4 580 /* no need to pass logmsg here as HEAD didn't really move */
da3f78a2 581 if (!strcmp(oldname, head) && create_symref("HEAD", newref.buf, NULL))
c976d415 582 die("Branch renamed to %s, but HEAD is not updated!", newname);
19eba151 583
da3f78a2
MV
584 strbuf_addf(&oldsection, "branch.%s", oldref.buf + 11);
585 strbuf_release(&oldref);
586 strbuf_addf(&newsection, "branch.%s", newref.buf + 11);
587 strbuf_release(&newref);
588 if (git_config_rename_section(oldsection.buf, newsection.buf) < 0)
19eba151 589 die("Branch is renamed, but update of config-file failed");
da3f78a2
MV
590 strbuf_release(&oldsection);
591 strbuf_release(&newsection);
c976d415
LH
592}
593
049716b3
JH
594static int opt_parse_merge_filter(const struct option *opt, const char *arg, int unset)
595{
596 merge_filter = ((opt->long_name[0] == 'n')
597 ? SHOW_NOT_MERGED
598 : SHOW_MERGED);
599 if (unset)
600 merge_filter = SHOW_NOT_MERGED; /* b/c for --no-merged */
601 if (!arg)
602 arg = "HEAD";
603 if (get_sha1(arg, merge_filter_ref))
604 die("malformed object name %s", arg);
605 return 0;
606}
607
c31820c2
LH
608int cmd_branch(int argc, const char **argv, const char *prefix)
609{
d11d44fa 610 int delete = 0, rename = 0, force_create = 0;
0016a482 611 int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0;
9ed36cfa
JS
612 int reflog = 0;
613 enum branch_track track;
d11d44fa 614 int kinds = REF_LOCAL_BRANCH;
694a5775 615 struct commit_list *with_commit = NULL;
a8dfd5ea
PH
616
617 struct option options[] = {
618 OPT_GROUP("Generic options"),
39271425
RS
619 OPT__VERBOSE(&verbose,
620 "show hash and subject, give twice for upstream branch"),
be427d75 621 OPT_SET_INT('t', "track", &track, "set up tracking mode (see git-pull(1))",
9ed36cfa 622 BRANCH_TRACK_EXPLICIT),
4fc50066
IL
623 OPT_SET_INT( 0, "set-upstream", &track, "change upstream info",
624 BRANCH_TRACK_OVERRIDE),
73e9da01 625 OPT__COLOR(&branch_use_color, "use colored output"),
d11d44fa
PH
626 OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches",
627 REF_REMOTE_BRANCH),
e84fb2ff
JH
628 {
629 OPTION_CALLBACK, 0, "contains", &with_commit, "commit",
630 "print only branches that contain the commit",
631 PARSE_OPT_LASTARG_DEFAULT,
269defdf 632 parse_opt_with_commit, (intptr_t)"HEAD",
e84fb2ff 633 },
694a5775
JH
634 {
635 OPTION_CALLBACK, 0, "with", &with_commit, "commit",
636 "print only branches that contain the commit",
e84fb2ff 637 PARSE_OPT_HIDDEN | PARSE_OPT_LASTARG_DEFAULT,
269defdf 638 parse_opt_with_commit, (intptr_t) "HEAD",
694a5775 639 },
a8dfd5ea
PH
640 OPT__ABBREV(&abbrev),
641
642 OPT_GROUP("Specific git-branch actions:"),
d11d44fa
PH
643 OPT_SET_INT('a', NULL, &kinds, "list both remote-tracking and local branches",
644 REF_REMOTE_BRANCH | REF_LOCAL_BRANCH),
645 OPT_BIT('d', NULL, &delete, "delete fully merged branch", 1),
646 OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2),
647 OPT_BIT('m', NULL, &rename, "move/rename a branch and its reflog", 1),
648 OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2),
649 OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"),
76946b76 650 OPT__FORCE(&force_create, "force creation (when already exists)"),
049716b3
JH
651 {
652 OPTION_CALLBACK, 0, "no-merged", &merge_filter_ref,
653 "commit", "print only not merged branches",
654 PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
655 opt_parse_merge_filter, (intptr_t) "HEAD",
656 },
657 {
658 OPTION_CALLBACK, 0, "merged", &merge_filter_ref,
659 "commit", "print only merged branches",
660 PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NONEG,
661 opt_parse_merge_filter, (intptr_t) "HEAD",
662 },
a8dfd5ea
PH
663 OPT_END(),
664 };
c31820c2 665
1dacfbcf
NTND
666 if (argc == 2 && !strcmp(argv[1], "-h"))
667 usage_with_options(builtin_branch_usage, options);
668
ef90d6d4 669 git_config(git_branch_config, NULL);
6b2f2d98
MK
670
671 if (branch_use_color == -1)
672 branch_use_color = git_use_color_default;
673
9ed36cfa 674 track = git_branch_track;
c976d415 675
078f8380 676 head = resolve_ref("HEAD", head_sha1, 0, NULL);
c31820c2
LH
677 if (!head)
678 die("Failed to resolve HEAD as a valid ref.");
078f8380 679 head = xstrdup(head);
0016a482
LH
680 if (!strcmp(head, "HEAD")) {
681 detached = 1;
a8dfd5ea 682 } else {
cc44c765 683 if (prefixcmp(head, "refs/heads/"))
0016a482
LH
684 die("HEAD not found below refs/heads!");
685 head += 11;
686 }
049716b3
JH
687 hashcpy(merge_filter_ref, head_sha1);
688
37782920
SB
689 argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
690 0);
049716b3
JH
691 if (!!delete + !!rename + !!force_create > 1)
692 usage_with_options(builtin_branch_usage, options);
c31820c2
LH
693
694 if (delete)
d11d44fa 695 return delete_branches(argc, argv, delete > 1, kinds);
a8dfd5ea 696 else if (argc == 0)
1603ade8 697 return print_ref_list(kinds, detached, verbose, abbrev, with_commit);
a8dfd5ea 698 else if (rename && (argc == 1))
d11d44fa 699 rename_branch(head, argv[0], rename > 1);
a8dfd5ea 700 else if (rename && (argc == 2))
d11d44fa 701 rename_branch(argv[0], argv[1], rename > 1);
6e8f993a
MM
702 else if (argc <= 2) {
703 if (kinds != REF_LOCAL_BRANCH)
704 die("-a and -r options to 'git branch' do not make sense with a branch name");
e496c003 705 create_branch(head, argv[0], (argc == 2) ? argv[1] : head,
45994a1e 706 force_create, reflog, track);
6e8f993a 707 } else
a8dfd5ea 708 usage_with_options(builtin_branch_usage, options);
c31820c2
LH
709
710 return 0;
711}