1/*
2 * Builtin "git branch"
3 *
4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-branch.sh by Junio C Hamano.
6 */
7
8#include "cache.h"
9#include "config.h"
10#include "color.h"
11#include "refs.h"
12#include "commit.h"
13#include "builtin.h"
14#include "remote.h"
15#include "parse-options.h"
16#include "branch.h"
17#include "diff.h"
18#include "revision.h"
19#include "string-list.h"
20#include "column.h"
21#include "utf8.h"
22#include "wt-status.h"
23#include "ref-filter.h"
24#include "worktree.h"
25#include "help.h"
26
27static const char * const builtin_branch_usage[] = {
28 N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
29 N_("git branch [<options>] [-l] [-f] <branch-name> [<start-point>]"),
30 N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
31 N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
32 N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"),
33 N_("git branch [<options>] [-r | -a] [--points-at]"),
34 N_("git branch [<options>] [-r | -a] [--format]"),
35 NULL
36};
37
38static const char *head;
39static struct object_id head_oid;
40
41static int branch_use_color = -1;
42static char branch_colors[][COLOR_MAXLEN] = {
43 GIT_COLOR_RESET,
44 GIT_COLOR_NORMAL, /* PLAIN */
45 GIT_COLOR_RED, /* REMOTE */
46 GIT_COLOR_NORMAL, /* LOCAL */
47 GIT_COLOR_GREEN, /* CURRENT */
48 GIT_COLOR_BLUE, /* UPSTREAM */
49};
50enum color_branch {
51 BRANCH_COLOR_RESET = 0,
52 BRANCH_COLOR_PLAIN = 1,
53 BRANCH_COLOR_REMOTE = 2,
54 BRANCH_COLOR_LOCAL = 3,
55 BRANCH_COLOR_CURRENT = 4,
56 BRANCH_COLOR_UPSTREAM = 5
57};
58
59static const char *color_branch_slots[] = {
60 [BRANCH_COLOR_RESET] = "reset",
61 [BRANCH_COLOR_PLAIN] = "plain",
62 [BRANCH_COLOR_REMOTE] = "remote",
63 [BRANCH_COLOR_LOCAL] = "local",
64 [BRANCH_COLOR_CURRENT] = "current",
65 [BRANCH_COLOR_UPSTREAM] = "upstream",
66};
67
68static struct string_list output = STRING_LIST_INIT_DUP;
69static unsigned int colopts;
70
71define_list_config_array(color_branch_slots);
72
73static int git_branch_config(const char *var, const char *value, void *cb)
74{
75 const char *slot_name;
76 struct ref_sorting **sorting_tail = (struct ref_sorting **)cb;
77
78 if (!strcmp(var, "branch.sort")) {
79 if (!value)
80 return config_error_nonbool(var);
81 parse_ref_sorting(sorting_tail, value);
82 return 0;
83 }
84
85 if (starts_with(var, "column."))
86 return git_column_config(var, value, "branch", &colopts);
87 if (!strcmp(var, "color.branch")) {
88 branch_use_color = git_config_colorbool(var, value);
89 return 0;
90 }
91 if (skip_prefix(var, "color.branch.", &slot_name)) {
92 int slot = LOOKUP_CONFIG(color_branch_slots, slot_name);
93 if (slot < 0)
94 return 0;
95 if (!value)
96 return config_error_nonbool(var);
97 return color_parse(value, branch_colors[slot]);
98 }
99 return git_color_default_config(var, value, cb);
100}
101
102static const char *branch_get_color(enum color_branch ix)
103{
104 if (want_color(branch_use_color))
105 return branch_colors[ix];
106 return "";
107}
108
109static int branch_merged(int kind, const char *name,
110 struct commit *rev, struct commit *head_rev)
111{
112 /*
113 * This checks whether the merge bases of branch and HEAD (or
114 * the other branch this branch builds upon) contains the
115 * branch, which means that the branch has already been merged
116 * safely to HEAD (or the other branch).
117 */
118 struct commit *reference_rev = NULL;
119 const char *reference_name = NULL;
120 void *reference_name_to_free = NULL;
121 int merged;
122
123 if (kind == FILTER_REFS_BRANCHES) {
124 struct branch *branch = branch_get(name);
125 const char *upstream = branch_get_upstream(branch, NULL);
126 struct object_id oid;
127
128 if (upstream &&
129 (reference_name = reference_name_to_free =
130 resolve_refdup(upstream, RESOLVE_REF_READING,
131 &oid, NULL)) != NULL)
132 reference_rev = lookup_commit_reference(the_repository,
133 &oid);
134 }
135 if (!reference_rev)
136 reference_rev = head_rev;
137
138 merged = in_merge_bases(rev, reference_rev);
139
140 /*
141 * After the safety valve is fully redefined to "check with
142 * upstream, if any, otherwise with HEAD", we should just
143 * return the result of the in_merge_bases() above without
144 * any of the following code, but during the transition period,
145 * a gentle reminder is in order.
146 */
147 if ((head_rev != reference_rev) &&
148 in_merge_bases(rev, head_rev) != merged) {
149 if (merged)
150 warning(_("deleting branch '%s' that has been merged to\n"
151 " '%s', but not yet merged to HEAD."),
152 name, reference_name);
153 else
154 warning(_("not deleting branch '%s' that is not yet merged to\n"
155 " '%s', even though it is merged to HEAD."),
156 name, reference_name);
157 }
158 free(reference_name_to_free);
159 return merged;
160}
161
162static int check_branch_commit(const char *branchname, const char *refname,
163 const struct object_id *oid, struct commit *head_rev,
164 int kinds, int force)
165{
166 struct commit *rev = lookup_commit_reference(the_repository, oid);
167 if (!rev) {
168 error(_("Couldn't look up commit object for '%s'"), refname);
169 return -1;
170 }
171 if (!force && !branch_merged(kinds, branchname, rev, head_rev)) {
172 error(_("The branch '%s' is not fully merged.\n"
173 "If you are sure you want to delete it, "
174 "run 'git branch -D %s'."), branchname, branchname);
175 return -1;
176 }
177 return 0;
178}
179
180static void delete_branch_config(const char *branchname)
181{
182 struct strbuf buf = STRBUF_INIT;
183 strbuf_addf(&buf, "branch.%s", branchname);
184 if (git_config_rename_section(buf.buf, NULL) < 0)
185 warning(_("Update of config-file failed"));
186 strbuf_release(&buf);
187}
188
189static int delete_branches(int argc, const char **argv, int force, int kinds,
190 int quiet)
191{
192 struct commit *head_rev = NULL;
193 struct object_id oid;
194 char *name = NULL;
195 const char *fmt;
196 int i;
197 int ret = 0;
198 int remote_branch = 0;
199 struct strbuf bname = STRBUF_INIT;
200 unsigned allowed_interpret;
201
202 switch (kinds) {
203 case FILTER_REFS_REMOTES:
204 fmt = "refs/remotes/%s";
205 /* For subsequent UI messages */
206 remote_branch = 1;
207 allowed_interpret = INTERPRET_BRANCH_REMOTE;
208
209 force = 1;
210 break;
211 case FILTER_REFS_BRANCHES:
212 fmt = "refs/heads/%s";
213 allowed_interpret = INTERPRET_BRANCH_LOCAL;
214 break;
215 default:
216 die(_("cannot use -a with -d"));
217 }
218
219 if (!force) {
220 head_rev = lookup_commit_reference(the_repository, &head_oid);
221 if (!head_rev)
222 die(_("Couldn't look up commit object for HEAD"));
223 }
224 for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
225 char *target = NULL;
226 int flags = 0;
227
228 strbuf_branchname(&bname, argv[i], allowed_interpret);
229 free(name);
230 name = mkpathdup(fmt, bname.buf);
231
232 if (kinds == FILTER_REFS_BRANCHES) {
233 const struct worktree *wt =
234 find_shared_symref("HEAD", name);
235 if (wt) {
236 error(_("Cannot delete branch '%s' "
237 "checked out at '%s'"),
238 bname.buf, wt->path);
239 ret = 1;
240 continue;
241 }
242 }
243
244 target = resolve_refdup(name,
245 RESOLVE_REF_READING
246 | RESOLVE_REF_NO_RECURSE
247 | RESOLVE_REF_ALLOW_BAD_NAME,
248 &oid, &flags);
249 if (!target) {
250 error(remote_branch
251 ? _("remote-tracking branch '%s' not found.")
252 : _("branch '%s' not found."), bname.buf);
253 ret = 1;
254 continue;
255 }
256
257 if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
258 check_branch_commit(bname.buf, name, &oid, head_rev, kinds,
259 force)) {
260 ret = 1;
261 goto next;
262 }
263
264 if (delete_ref(NULL, name, is_null_oid(&oid) ? NULL : &oid,
265 REF_NO_DEREF)) {
266 error(remote_branch
267 ? _("Error deleting remote-tracking branch '%s'")
268 : _("Error deleting branch '%s'"),
269 bname.buf);
270 ret = 1;
271 goto next;
272 }
273 if (!quiet) {
274 printf(remote_branch
275 ? _("Deleted remote-tracking branch %s (was %s).\n")
276 : _("Deleted branch %s (was %s).\n"),
277 bname.buf,
278 (flags & REF_ISBROKEN) ? "broken"
279 : (flags & REF_ISSYMREF) ? target
280 : find_unique_abbrev(&oid, DEFAULT_ABBREV));
281 }
282 delete_branch_config(bname.buf);
283
284 next:
285 free(target);
286 }
287
288 free(name);
289 strbuf_release(&bname);
290
291 return ret;
292}
293
294static int calc_maxwidth(struct ref_array *refs, int remote_bonus)
295{
296 int i, max = 0;
297 for (i = 0; i < refs->nr; i++) {
298 struct ref_array_item *it = refs->items[i];
299 const char *desc = it->refname;
300 int w;
301
302 skip_prefix(it->refname, "refs/heads/", &desc);
303 skip_prefix(it->refname, "refs/remotes/", &desc);
304 if (it->kind == FILTER_REFS_DETACHED_HEAD) {
305 char *head_desc = get_head_description();
306 w = utf8_strwidth(head_desc);
307 free(head_desc);
308 } else
309 w = utf8_strwidth(desc);
310
311 if (it->kind == FILTER_REFS_REMOTES)
312 w += remote_bonus;
313 if (w > max)
314 max = w;
315 }
316 return max;
317}
318
319static const char *quote_literal_for_format(const char *s)
320{
321 static struct strbuf buf = STRBUF_INIT;
322
323 strbuf_reset(&buf);
324 while (*s) {
325 const char *ep = strchrnul(s, '%');
326 if (s < ep)
327 strbuf_add(&buf, s, ep - s);
328 if (*ep == '%') {
329 strbuf_addstr(&buf, "%%");
330 s = ep + 1;
331 } else {
332 s = ep;
333 }
334 }
335 return buf.buf;
336}
337
338static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix)
339{
340 struct strbuf fmt = STRBUF_INIT;
341 struct strbuf local = STRBUF_INIT;
342 struct strbuf remote = STRBUF_INIT;
343
344 strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else) %s%%(end)",
345 branch_get_color(BRANCH_COLOR_CURRENT),
346 branch_get_color(BRANCH_COLOR_LOCAL));
347 strbuf_addf(&remote, " %s",
348 branch_get_color(BRANCH_COLOR_REMOTE));
349
350 if (filter->verbose) {
351 struct strbuf obname = STRBUF_INIT;
352
353 if (filter->abbrev < 0)
354 strbuf_addf(&obname, "%%(objectname:short)");
355 else if (!filter->abbrev)
356 strbuf_addf(&obname, "%%(objectname)");
357 else
358 strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev);
359
360 strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
361 strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET));
362 strbuf_addf(&local, " %s ", obname.buf);
363
364 if (filter->verbose > 1)
365 strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
366 "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
367 branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
368 else
369 strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
370
371 strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
372 "%%(if)%%(symref)%%(then) -> %%(symref:short)"
373 "%%(else) %s %%(contents:subject)%%(end)",
374 maxwidth, quote_literal_for_format(remote_prefix),
375 branch_get_color(BRANCH_COLOR_RESET), obname.buf);
376 strbuf_release(&obname);
377 } else {
378 strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
379 branch_get_color(BRANCH_COLOR_RESET));
380 strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
381 quote_literal_for_format(remote_prefix),
382 branch_get_color(BRANCH_COLOR_RESET));
383 }
384
385 strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf);
386
387 strbuf_release(&local);
388 strbuf_release(&remote);
389 return strbuf_detach(&fmt, NULL);
390}
391
392static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, struct ref_format *format)
393{
394 int i;
395 struct ref_array array;
396 int maxwidth = 0;
397 const char *remote_prefix = "";
398 char *to_free = NULL;
399
400 /*
401 * If we are listing more than just remote branches,
402 * then remote branches will have a "remotes/" prefix.
403 * We need to account for this in the width.
404 */
405 if (filter->kind != FILTER_REFS_REMOTES)
406 remote_prefix = "remotes/";
407
408 memset(&array, 0, sizeof(array));
409
410 filter_refs(&array, filter, filter->kind | FILTER_REFS_INCLUDE_BROKEN);
411
412 if (filter->verbose)
413 maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
414
415 if (!format->format)
416 format->format = to_free = build_format(filter, maxwidth, remote_prefix);
417 format->use_color = branch_use_color;
418
419 if (verify_ref_format(format))
420 die(_("unable to parse format string"));
421
422 ref_array_sort(sorting, &array);
423
424 for (i = 0; i < array.nr; i++) {
425 struct strbuf out = STRBUF_INIT;
426 struct strbuf err = STRBUF_INIT;
427 if (format_ref_array_item(array.items[i], format, &out, &err))
428 die("%s", err.buf);
429 if (column_active(colopts)) {
430 assert(!filter->verbose && "--column and --verbose are incompatible");
431 /* format to a string_list to let print_columns() do its job */
432 string_list_append(&output, out.buf);
433 } else {
434 fwrite(out.buf, 1, out.len, stdout);
435 putchar('\n');
436 }
437 strbuf_release(&err);
438 strbuf_release(&out);
439 }
440
441 ref_array_clear(&array);
442 free(to_free);
443}
444
445static void reject_rebase_or_bisect_branch(const char *target)
446{
447 struct worktree **worktrees = get_worktrees(0);
448 int i;
449
450 for (i = 0; worktrees[i]; i++) {
451 struct worktree *wt = worktrees[i];
452
453 if (!wt->is_detached)
454 continue;
455
456 if (is_worktree_being_rebased(wt, target))
457 die(_("Branch %s is being rebased at %s"),
458 target, wt->path);
459
460 if (is_worktree_being_bisected(wt, target))
461 die(_("Branch %s is being bisected at %s"),
462 target, wt->path);
463 }
464
465 free_worktrees(worktrees);
466}
467
468static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
469{
470 struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
471 struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
472 const char *interpreted_oldname = NULL;
473 const char *interpreted_newname = NULL;
474 int recovery = 0;
475
476 if (!oldname) {
477 if (copy)
478 die(_("cannot copy the current branch while not on any."));
479 else
480 die(_("cannot rename the current branch while not on any."));
481 }
482
483 if (strbuf_check_branch_ref(&oldref, oldname)) {
484 /*
485 * Bad name --- this could be an attempt to rename a
486 * ref that we used to allow to be created by accident.
487 */
488 if (ref_exists(oldref.buf))
489 recovery = 1;
490 else
491 die(_("Invalid branch name: '%s'"), oldname);
492 }
493
494 /*
495 * A command like "git branch -M currentbranch currentbranch" cannot
496 * cause the worktree to become inconsistent with HEAD, so allow it.
497 */
498 if (!strcmp(oldname, newname))
499 validate_branchname(newname, &newref);
500 else
501 validate_new_branchname(newname, &newref, force);
502
503 reject_rebase_or_bisect_branch(oldref.buf);
504
505 if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
506 !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
507 BUG("expected prefix missing for refs");
508 }
509
510 if (copy)
511 strbuf_addf(&logmsg, "Branch: copied %s to %s",
512 oldref.buf, newref.buf);
513 else
514 strbuf_addf(&logmsg, "Branch: renamed %s to %s",
515 oldref.buf, newref.buf);
516
517 if (!copy && rename_ref(oldref.buf, newref.buf, logmsg.buf))
518 die(_("Branch rename failed"));
519 if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
520 die(_("Branch copy failed"));
521
522 if (recovery) {
523 if (copy)
524 warning(_("Created a copy of a misnamed branch '%s'"),
525 interpreted_oldname);
526 else
527 warning(_("Renamed a misnamed branch '%s' away"),
528 interpreted_oldname);
529 }
530
531 if (!copy &&
532 replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf))
533 die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
534
535 strbuf_release(&logmsg);
536
537 strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
538 strbuf_release(&oldref);
539 strbuf_addf(&newsection, "branch.%s", interpreted_newname);
540 strbuf_release(&newref);
541 if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
542 die(_("Branch is renamed, but update of config-file failed"));
543 if (copy && strcmp(oldname, newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0)
544 die(_("Branch is copied, but update of config-file failed"));
545 strbuf_release(&oldsection);
546 strbuf_release(&newsection);
547}
548
549static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
550
551static int edit_branch_description(const char *branch_name)
552{
553 struct strbuf buf = STRBUF_INIT;
554 struct strbuf name = STRBUF_INIT;
555
556 read_branch_desc(&buf, branch_name);
557 if (!buf.len || buf.buf[buf.len-1] != '\n')
558 strbuf_addch(&buf, '\n');
559 strbuf_commented_addf(&buf,
560 _("Please edit the description for the branch\n"
561 " %s\n"
562 "Lines starting with '%c' will be stripped.\n"),
563 branch_name, comment_line_char);
564 write_file_buf(edit_description(), buf.buf, buf.len);
565 strbuf_reset(&buf);
566 if (launch_editor(edit_description(), &buf, NULL)) {
567 strbuf_release(&buf);
568 return -1;
569 }
570 strbuf_stripspace(&buf, 1);
571
572 strbuf_addf(&name, "branch.%s.description", branch_name);
573 git_config_set(name.buf, buf.len ? buf.buf : NULL);
574 strbuf_release(&name);
575 strbuf_release(&buf);
576
577 return 0;
578}
579
580int cmd_branch(int argc, const char **argv, const char *prefix)
581{
582 int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
583 int reflog = 0, edit_description = 0;
584 int quiet = 0, unset_upstream = 0;
585 const char *new_upstream = NULL;
586 enum branch_track track;
587 struct ref_filter filter;
588 int icase = 0;
589 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
590 struct ref_format format = REF_FORMAT_INIT;
591
592 struct option options[] = {
593 OPT_GROUP(N_("Generic options")),
594 OPT__VERBOSE(&filter.verbose,
595 N_("show hash and subject, give twice for upstream branch")),
596 OPT__QUIET(&quiet, N_("suppress informational messages")),
597 OPT_SET_INT('t', "track", &track, N_("set up tracking mode (see git-pull(1))"),
598 BRANCH_TRACK_EXPLICIT),
599 OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
600 BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
601 OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
602 OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("Unset the upstream info")),
603 OPT__COLOR(&branch_use_color, N_("use colored output")),
604 OPT_SET_INT('r', "remotes", &filter.kind, N_("act on remote-tracking branches"),
605 FILTER_REFS_REMOTES),
606 OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")),
607 OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")),
608 OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")),
609 OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")),
610 OPT__ABBREV(&filter.abbrev),
611
612 OPT_GROUP(N_("Specific git-branch actions:")),
613 OPT_SET_INT('a', "all", &filter.kind, N_("list both remote-tracking and local branches"),
614 FILTER_REFS_REMOTES | FILTER_REFS_BRANCHES),
615 OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1),
616 OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
617 OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
618 OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
619 OPT_BIT('c', "copy", ©, N_("copy a branch and its reflog"), 1),
620 OPT_BIT('C', NULL, ©, N_("copy a branch, even if target exists"), 2),
621 OPT_BOOL('l', "list", &list, N_("list branch names")),
622 OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
623 OPT_BOOL(0, "edit-description", &edit_description,
624 N_("edit the description for the branch")),
625 OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
626 OPT_MERGED(&filter, N_("print only branches that are merged")),
627 OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
628 OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
629 OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
630 N_("field name to sort on"), &parse_opt_ref_sorting),
631 {
632 OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
633 N_("print only branches of the object"), 0, parse_opt_object_name
634 },
635 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
636 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
637 OPT_END(),
638 };
639
640 setup_ref_filter_porcelain_msg();
641
642 memset(&filter, 0, sizeof(filter));
643 filter.kind = FILTER_REFS_BRANCHES;
644 filter.abbrev = -1;
645
646 if (argc == 2 && !strcmp(argv[1], "-h"))
647 usage_with_options(builtin_branch_usage, options);
648
649 git_config(git_branch_config, sorting_tail);
650
651 track = git_branch_track;
652
653 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
654 if (!head)
655 die(_("Failed to resolve HEAD as a valid ref."));
656 if (!strcmp(head, "HEAD"))
657 filter.detached = 1;
658 else if (!skip_prefix(head, "refs/heads/", &head))
659 die(_("HEAD not found below refs/heads!"));
660
661 argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
662 0);
663
664 if (!delete && !rename && !copy && !edit_description && !new_upstream && !unset_upstream && argc == 0)
665 list = 1;
666
667 if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
668 filter.no_commit)
669 list = 1;
670
671 if (!!delete + !!rename + !!copy + !!new_upstream +
672 list + unset_upstream > 1)
673 usage_with_options(builtin_branch_usage, options);
674
675 if (filter.abbrev == -1)
676 filter.abbrev = DEFAULT_ABBREV;
677 filter.ignore_case = icase;
678
679 finalize_colopts(&colopts, -1);
680 if (filter.verbose) {
681 if (explicitly_enable_column(colopts))
682 die(_("--column and --verbose are incompatible"));
683 colopts = 0;
684 }
685
686 if (force) {
687 delete *= 2;
688 rename *= 2;
689 copy *= 2;
690 }
691
692 if (list)
693 setup_auto_pager("branch", 1);
694
695 if (delete) {
696 if (!argc)
697 die(_("branch name required"));
698 return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
699 } else if (list) {
700 /* git branch --local also shows HEAD when it is detached */
701 if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
702 filter.kind |= FILTER_REFS_DETACHED_HEAD;
703 filter.name_patterns = argv;
704 /*
705 * If no sorting parameter is given then we default to sorting
706 * by 'refname'. This would give us an alphabetically sorted
707 * array with the 'HEAD' ref at the beginning followed by
708 * local branches 'refs/heads/...' and finally remote-tracking
709 * branches 'refs/remotes/...'.
710 */
711 if (!sorting)
712 sorting = ref_default_sorting();
713 sorting->ignore_case = icase;
714 print_ref_list(&filter, sorting, &format);
715 print_columns(&output, colopts, NULL);
716 string_list_clear(&output, 0);
717 return 0;
718 }
719 else if (edit_description) {
720 const char *branch_name;
721 struct strbuf branch_ref = STRBUF_INIT;
722
723 if (!argc) {
724 if (filter.detached)
725 die(_("Cannot give description to detached HEAD"));
726 branch_name = head;
727 } else if (argc == 1)
728 branch_name = argv[0];
729 else
730 die(_("cannot edit description of more than one branch"));
731
732 strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
733 if (!ref_exists(branch_ref.buf)) {
734 strbuf_release(&branch_ref);
735
736 if (!argc)
737 return error(_("No commit on branch '%s' yet."),
738 branch_name);
739 else
740 return error(_("No branch named '%s'."),
741 branch_name);
742 }
743 strbuf_release(&branch_ref);
744
745 if (edit_branch_description(branch_name))
746 return 1;
747 } else if (copy) {
748 if (!argc)
749 die(_("branch name required"));
750 else if (argc == 1)
751 copy_or_rename_branch(head, argv[0], 1, copy > 1);
752 else if (argc == 2)
753 copy_or_rename_branch(argv[0], argv[1], 1, copy > 1);
754 else
755 die(_("too many branches for a copy operation"));
756 } else if (rename) {
757 if (!argc)
758 die(_("branch name required"));
759 else if (argc == 1)
760 copy_or_rename_branch(head, argv[0], 0, rename > 1);
761 else if (argc == 2)
762 copy_or_rename_branch(argv[0], argv[1], 0, rename > 1);
763 else
764 die(_("too many arguments for a rename operation"));
765 } else if (new_upstream) {
766 struct branch *branch = branch_get(argv[0]);
767
768 if (argc > 1)
769 die(_("too many arguments to set new upstream"));
770
771 if (!branch) {
772 if (!argc || !strcmp(argv[0], "HEAD"))
773 die(_("could not set upstream of HEAD to %s when "
774 "it does not point to any branch."),
775 new_upstream);
776 die(_("no such branch '%s'"), argv[0]);
777 }
778
779 if (!ref_exists(branch->refname))
780 die(_("branch '%s' does not exist"), branch->name);
781
782 /*
783 * create_branch takes care of setting up the tracking
784 * info and making sure new_upstream is correct
785 */
786 create_branch(branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
787 } else if (unset_upstream) {
788 struct branch *branch = branch_get(argv[0]);
789 struct strbuf buf = STRBUF_INIT;
790
791 if (argc > 1)
792 die(_("too many arguments to unset upstream"));
793
794 if (!branch) {
795 if (!argc || !strcmp(argv[0], "HEAD"))
796 die(_("could not unset upstream of HEAD when "
797 "it does not point to any branch."));
798 die(_("no such branch '%s'"), argv[0]);
799 }
800
801 if (!branch_has_merge_config(branch))
802 die(_("Branch '%s' has no upstream information"), branch->name);
803
804 strbuf_addf(&buf, "branch.%s.remote", branch->name);
805 git_config_set_multivar(buf.buf, NULL, NULL, 1);
806 strbuf_reset(&buf);
807 strbuf_addf(&buf, "branch.%s.merge", branch->name);
808 git_config_set_multivar(buf.buf, NULL, NULL, 1);
809 strbuf_release(&buf);
810 } else if (argc > 0 && argc <= 2) {
811 struct branch *branch = branch_get(argv[0]);
812
813 if (!branch)
814 die(_("no such branch '%s'"), argv[0]);
815
816 if (filter.kind != FILTER_REFS_BRANCHES)
817 die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
818
819 if (track == BRANCH_TRACK_OVERRIDE)
820 die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
821
822 create_branch(argv[0], (argc == 2) ? argv[1] : head,
823 force, 0, reflog, quiet, track);
824
825 } else
826 usage_with_options(builtin_branch_usage, options);
827
828 return 0;
829}