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