blob: dd3e3a7dc09541892d4fb86f66f78f5d8038daa0 [file] [log] [blame]
Lars Hjemlic31820c2006-10-23 23:27:45 +02001/*
2 * Builtin "git branch"
3 *
Junio C Hamanod972cce2007-07-11 22:52:45 -07004 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
Lars Hjemlic31820c2006-10-23 23:27:45 +02005 * Based on git-branch.sh by Junio C Hamano.
6 */
7
Elijah Newrenbc5c5ec2023-05-16 06:33:57 +00008#include "builtin.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07009#include "config.h"
Junio C Hamano85023572006-12-19 14:34:12 -080010#include "color.h"
Elijah Newren4e120822023-04-11 00:41:57 -070011#include "editor.h"
Elijah Newren7ee24e12023-03-21 06:25:57 +000012#include "environment.h"
Lars Hjemlic31820c2006-10-23 23:27:45 +020013#include "refs.h"
14#include "commit.h"
Elijah Newrenf394e092023-03-21 06:25:54 +000015#include "gettext.h"
Elijah Newrendabab1d2023-04-11 00:41:49 -070016#include "object-name.h"
Johannes Schindelin6f084a52007-07-10 18:50:44 +010017#include "remote.h"
Pierre Habouzita8dfd5e2007-10-07 18:26:21 +020018#include "parse-options.h"
Daniel Barkalowe496c002008-02-07 11:40:08 -050019#include "branch.h"
Elijah Newrenc3399322023-05-16 06:33:59 +000020#include "path.h"
Nguyễn Thái Ngọc Duyebe31ef2012-04-13 17:54:38 +070021#include "string-list.h"
22#include "column.h"
Nguyễn Thái Ngọc Duy1452bd62012-08-26 01:17:12 +070023#include "utf8.h"
Karthik Nayak1511b222015-09-23 23:41:11 +053024#include "ref-filter.h"
Kazuki Yamaguchif2922442016-03-29 18:38:39 +090025#include "worktree.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020026#include "help.h"
Rubén Justobec9bb42024-01-11 13:40:34 +010027#include "advice.h"
Derrick Stolee64043552018-07-20 16:33:04 +000028#include "commit-reach.h"
Lars Hjemlic31820c2006-10-23 23:27:45 +020029
Pierre Habouzita8dfd5e2007-10-07 18:26:21 +020030static const char * const builtin_branch_usage[] = {
Aaron Lipman21bf9332020-09-15 22:08:40 -040031 N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"),
Glen Choo961b1302022-01-28 16:04:45 -080032 N_("git branch [<options>] [-f] [--recurse-submodules] <branch-name> [<start-point>]"),
33 N_("git branch [<options>] [-l] [<pattern>...]"),
Alex Henrie9c9b4f22015-01-13 00:44:47 -070034 N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
35 N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
Sahil Dua52d59cc2017-06-18 23:19:16 +020036 N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"),
Karthik Nayakaa3bc552015-09-23 23:41:13 +053037 N_("git branch [<options>] [-r | -a] [--points-at]"),
Karthik Nayak3d9e4ce2017-01-10 14:19:53 +053038 N_("git branch [<options>] [-r | -a] [--format]"),
Pierre Habouzita8dfd5e2007-10-07 18:26:21 +020039 NULL
40};
Lars Hjemlic31820c2006-10-23 23:27:45 +020041
Lars Hjemlic31820c2006-10-23 23:27:45 +020042static const char *head;
brian m. carlson0c77cd22017-02-21 23:47:26 +000043static struct object_id head_oid;
Glen Choo961b1302022-01-28 16:04:45 -080044static int recurse_submodules = 0;
45static int submodule_propagate_branches = 0;
Lars Hjemlic31820c2006-10-23 23:27:45 +020046
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +010047static int branch_use_color = -1;
Andy Parkinsa1158ca2006-12-12 06:41:52 +000048static char branch_colors[][COLOR_MAXLEN] = {
Arjen Laarhovendc6ebd42009-02-13 22:53:40 +010049 GIT_COLOR_RESET,
Karthik Nayak949af062017-01-10 14:19:52 +053050 GIT_COLOR_NORMAL, /* PLAIN */
51 GIT_COLOR_RED, /* REMOTE */
52 GIT_COLOR_NORMAL, /* LOCAL */
53 GIT_COLOR_GREEN, /* CURRENT */
54 GIT_COLOR_BLUE, /* UPSTREAM */
Nickolai Belakovskiab313812019-04-28 22:19:43 -070055 GIT_COLOR_CYAN, /* WORKTREE */
Andy Parkinsa1158ca2006-12-12 06:41:52 +000056};
57enum color_branch {
Arjen Laarhoven74bb2df2009-02-13 22:53:41 +010058 BRANCH_COLOR_RESET = 0,
59 BRANCH_COLOR_PLAIN = 1,
60 BRANCH_COLOR_REMOTE = 2,
61 BRANCH_COLOR_LOCAL = 3,
Felipe Contrerasdbda21f2013-04-14 21:37:49 -050062 BRANCH_COLOR_CURRENT = 4,
Nickolai Belakovskiab313812019-04-28 22:19:43 -070063 BRANCH_COLOR_UPSTREAM = 5,
64 BRANCH_COLOR_WORKTREE = 6
Andy Parkinsa1158ca2006-12-12 06:41:52 +000065};
66
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020067static const char *color_branch_slots[] = {
68 [BRANCH_COLOR_RESET] = "reset",
69 [BRANCH_COLOR_PLAIN] = "plain",
70 [BRANCH_COLOR_REMOTE] = "remote",
71 [BRANCH_COLOR_LOCAL] = "local",
72 [BRANCH_COLOR_CURRENT] = "current",
73 [BRANCH_COLOR_UPSTREAM] = "upstream",
Nickolai Belakovskiab313812019-04-28 22:19:43 -070074 [BRANCH_COLOR_WORKTREE] = "worktree",
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +020075};
76
Nguyễn Thái Ngọc Duyebe31ef2012-04-13 17:54:38 +070077static struct string_list output = STRING_LIST_INIT_DUP;
78static unsigned int colopts;
79
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 15:55:24 +020080define_list_config_array(color_branch_slots);
Andy Parkinsa1158ca2006-12-12 06:41:52 +000081
Glen Chooa4e7e312023-06-28 19:26:22 +000082static int git_branch_config(const char *var, const char *value,
83 const struct config_context *ctx, void *cb)
Andy Parkinsa1158ca2006-12-12 06:41:52 +000084{
René Scharfee3f1da92014-10-04 20:54:50 +020085 const char *slot_name;
Samuel Maftoul560ae1c2018-08-16 11:35:08 +020086
87 if (!strcmp(var, "branch.sort")) {
88 if (!value)
89 return config_error_nonbool(var);
Junio C Hamano98e7ab62021-10-20 12:23:53 -070090 string_list_append(cb, value);
Samuel Maftoul560ae1c2018-08-16 11:35:08 +020091 return 0;
92 }
René Scharfee3f1da92014-10-04 20:54:50 +020093
Christian Couder59556542013-11-30 21:55:40 +010094 if (starts_with(var, "column."))
Nguyễn Thái Ngọc Duyebe31ef2012-04-13 17:54:38 +070095 return git_column_config(var, value, "branch", &colopts);
Andy Parkinsa1158ca2006-12-12 06:41:52 +000096 if (!strcmp(var, "color.branch")) {
Jeff Kinge269eb72011-08-17 22:03:48 -070097 branch_use_color = git_config_colorbool(var, value);
Andy Parkinsa1158ca2006-12-12 06:41:52 +000098 return 0;
99 }
René Scharfee3f1da92014-10-04 20:54:50 +0200100 if (skip_prefix(var, "color.branch.", &slot_name)) {
Nguyễn Thái Ngọc Duya73b3682018-05-26 15:55:21 +0200101 int slot = LOOKUP_CONFIG(color_branch_slots, slot_name);
Jeff King8b8e8622009-12-12 07:25:24 -0500102 if (slot < 0)
103 return 0;
Junio C Hamano5768c982008-02-11 10:45:50 -0800104 if (!value)
105 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 15:33:09 -0400106 return color_parse(value, branch_colors[slot]);
Andy Parkinsa1158ca2006-12-12 06:41:52 +0000107 }
Glen Choo961b1302022-01-28 16:04:45 -0800108 if (!strcmp(var, "submodule.recurse")) {
109 recurse_submodules = git_config_bool(var, value);
110 return 0;
111 }
112 if (!strcasecmp(var, "submodule.propagateBranches")) {
113 submodule_propagate_branches = git_config_bool(var, value);
114 return 0;
115 }
116
Glen Choo97eeeea2023-06-28 19:26:20 +0000117 if (git_color_config(var, value, cb) < 0)
118 return -1;
119
Glen Chooa4e7e312023-06-28 19:26:22 +0000120 return git_default_config(var, value, ctx, cb);
Andy Parkinsa1158ca2006-12-12 06:41:52 +0000121}
122
Pierre Habouzit52fae7d2007-06-07 22:45:00 +0200123static const char *branch_get_color(enum color_branch ix)
Andy Parkinsa1158ca2006-12-12 06:41:52 +0000124{
Jeff Kingdaa0c3d2011-08-17 22:04:23 -0700125 if (want_color(branch_use_color))
Andy Parkinsa1158ca2006-12-12 06:41:52 +0000126 return branch_colors[ix];
127 return "";
128}
129
Junio C Hamano99c419c2009-12-29 22:43:04 -0800130static int branch_merged(int kind, const char *name,
131 struct commit *rev, struct commit *head_rev)
132{
133 /*
134 * This checks whether the merge bases of branch and HEAD (or
135 * the other branch this branch builds upon) contains the
136 * branch, which means that the branch has already been merged
137 * safely to HEAD (or the other branch).
138 */
139 struct commit *reference_rev = NULL;
140 const char *reference_name = NULL;
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700141 void *reference_name_to_free = NULL;
Junio C Hamano99c419c2009-12-29 22:43:04 -0800142 int merged;
143
Karthik Nayak1511b222015-09-23 23:41:11 +0530144 if (kind == FILTER_REFS_BRANCHES) {
Junio C Hamano99c419c2009-12-29 22:43:04 -0800145 struct branch *branch = branch_get(name);
Jeff King3a429d02015-05-21 00:45:32 -0400146 const char *upstream = branch_get_upstream(branch, NULL);
brian m. carlson0c77cd22017-02-21 23:47:26 +0000147 struct object_id oid;
Junio C Hamano99c419c2009-12-29 22:43:04 -0800148
Jeff Kinga9f9f8c2015-05-21 00:45:28 -0400149 if (upstream &&
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700150 (reference_name = reference_name_to_free =
Jeff Kinga9f9f8c2015-05-21 00:45:28 -0400151 resolve_refdup(upstream, RESOLVE_REF_READING,
brian m. carlson0f2dc722017-10-15 22:06:55 +0000152 &oid, NULL)) != NULL)
Stefan Beller2122f672018-06-28 18:21:58 -0700153 reference_rev = lookup_commit_reference(the_repository,
154 &oid);
Junio C Hamano99c419c2009-12-29 22:43:04 -0800155 }
156 if (!reference_rev)
157 reference_rev = head_rev;
158
Ævar Arnfjörð Bjarmasoncb338c22023-03-28 15:58:47 +0200159 merged = reference_rev ? repo_in_merge_bases(the_repository, rev,
160 reference_rev) : 0;
Johannes Schindelin24876eb2024-02-28 09:44:09 +0000161 if (merged < 0)
162 exit(128);
Junio C Hamano99c419c2009-12-29 22:43:04 -0800163
164 /*
165 * After the safety valve is fully redefined to "check with
166 * upstream, if any, otherwise with HEAD", we should just
Ævar Arnfjörð Bjarmasonc7c33f52023-03-28 15:58:57 +0200167 * return the result of the repo_in_merge_bases() above without
Junio C Hamano99c419c2009-12-29 22:43:04 -0800168 * any of the following code, but during the transition period,
169 * a gentle reminder is in order.
170 */
Johannes Schindelin24876eb2024-02-28 09:44:09 +0000171 if (head_rev != reference_rev) {
172 int expect = head_rev ? repo_in_merge_bases(the_repository, rev, head_rev) : 0;
173 if (expect < 0)
174 exit(128);
175 if (expect == merged)
176 ; /* okay */
177 else if (merged)
Ævar Arnfjörð Bjarmason49df4b02011-02-22 23:41:34 +0000178 warning(_("deleting branch '%s' that has been merged to\n"
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100179 " '%s', but not yet merged to HEAD"),
Junio C Hamano99c419c2009-12-29 22:43:04 -0800180 name, reference_name);
181 else
Ævar Arnfjörð Bjarmason49df4b02011-02-22 23:41:34 +0000182 warning(_("not deleting branch '%s' that is not yet merged to\n"
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100183 " '%s', even though it is merged to HEAD"),
Junio C Hamano99c419c2009-12-29 22:43:04 -0800184 name, reference_name);
185 }
Nguyễn Thái Ngọc Duy96ec7b12011-12-13 21:17:48 +0700186 free(reference_name_to_free);
Junio C Hamano99c419c2009-12-29 22:43:04 -0800187 return merged;
188}
189
René Scharfef5d0e162012-10-18 14:02:51 +0200190static int check_branch_commit(const char *branchname, const char *refname,
brian m. carlson0c77cd22017-02-21 23:47:26 +0000191 const struct object_id *oid, struct commit *head_rev,
René Scharfef5d0e162012-10-18 14:02:51 +0200192 int kinds, int force)
193{
Stefan Beller2122f672018-06-28 18:21:58 -0700194 struct commit *rev = lookup_commit_reference(the_repository, oid);
René Scharfe597a9772021-08-27 20:35:35 +0200195 if (!force && !rev) {
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100196 error(_("couldn't look up commit object for '%s'"), refname);
René Scharfef5d0e162012-10-18 14:02:51 +0200197 return -1;
198 }
199 if (!force && !branch_merged(kinds, branchname, rev, head_rev)) {
Rubén Justobec9bb42024-01-11 13:40:34 +0100200 error(_("the branch '%s' is not fully merged"), branchname);
201 advise_if_enabled(ADVICE_FORCE_DELETE_BRANCH,
202 _("If you are sure you want to delete it, "
203 "run 'git branch -D %s'"), branchname);
René Scharfef5d0e162012-10-18 14:02:51 +0200204 return -1;
205 }
206 return 0;
207}
208
René Scharfe22ed7922012-10-18 14:04:08 +0200209static void delete_branch_config(const char *branchname)
210{
211 struct strbuf buf = STRBUF_INIT;
212 strbuf_addf(&buf, "branch.%s", branchname);
213 if (git_config_rename_section(buf.buf, NULL) < 0)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100214 warning(_("update of config-file failed"));
René Scharfe22ed7922012-10-18 14:04:08 +0200215 strbuf_release(&buf);
216}
217
Jeff Kingd65ddf12012-03-26 19:51:06 -0400218static int delete_branches(int argc, const char **argv, int force, int kinds,
219 int quiet)
Lars Hjemlic31820c2006-10-23 23:27:45 +0200220{
René Scharfef5d0e162012-10-18 14:02:51 +0200221 struct commit *head_rev = NULL;
brian m. carlson0c77cd22017-02-21 23:47:26 +0000222 struct object_id oid;
Quy Tonthatb8e9a002006-12-19 09:42:16 +1100223 char *name = NULL;
Ævar Arnfjörð Bjarmasonc1798372012-04-30 15:33:12 +0000224 const char *fmt;
Lars Hjemlic31820c2006-10-23 23:27:45 +0200225 int i;
Quy Tonthatb8e9a002006-12-19 09:42:16 +1100226 int ret = 0;
Ævar Arnfjörð Bjarmasonc1798372012-04-30 15:33:12 +0000227 int remote_branch = 0;
Junio C Hamano8415d5c2009-02-13 23:08:05 -0800228 struct strbuf bname = STRBUF_INIT;
Jeff King6b145e02017-03-02 03:23:10 -0500229 unsigned allowed_interpret;
Phil Hord81989072021-01-20 19:23:32 -0800230 struct string_list refs_to_delete = STRING_LIST_INIT_DUP;
231 struct string_list_item *item;
232 int branch_name_pos;
Clement Mabileau4c643fb2023-04-05 11:43:20 +0000233 const char *fmt_remotes = "refs/remotes/%s";
Lars Hjemlic31820c2006-10-23 23:27:45 +0200234
Junio C Hamanof3d985c2006-12-17 23:58:16 -0800235 switch (kinds) {
Karthik Nayak1511b222015-09-23 23:41:11 +0530236 case FILTER_REFS_REMOTES:
Clement Mabileau4c643fb2023-04-05 11:43:20 +0000237 fmt = fmt_remotes;
Ævar Arnfjörð Bjarmasonc1798372012-04-30 15:33:12 +0000238 /* For subsequent UI messages */
239 remote_branch = 1;
Jeff King6b145e02017-03-02 03:23:10 -0500240 allowed_interpret = INTERPRET_BRANCH_REMOTE;
Ævar Arnfjörð Bjarmasonc1798372012-04-30 15:33:12 +0000241
Junio C Hamanof3d985c2006-12-17 23:58:16 -0800242 force = 1;
243 break;
Karthik Nayak1511b222015-09-23 23:41:11 +0530244 case FILTER_REFS_BRANCHES:
Junio C Hamanof3d985c2006-12-17 23:58:16 -0800245 fmt = "refs/heads/%s";
Jeff King6b145e02017-03-02 03:23:10 -0500246 allowed_interpret = INTERPRET_BRANCH_LOCAL;
Junio C Hamanof3d985c2006-12-17 23:58:16 -0800247 break;
248 default:
Ævar Arnfjörð Bjarmason49df4b02011-02-22 23:41:34 +0000249 die(_("cannot use -a with -d"));
Junio C Hamanof3d985c2006-12-17 23:58:16 -0800250 }
Phil Hord81989072021-01-20 19:23:32 -0800251 branch_name_pos = strcspn(fmt, "%");
Lars Hjemlic31820c2006-10-23 23:27:45 +0200252
Jeff Kingeb20e632022-11-02 01:27:49 -0400253 if (!force)
Stefan Beller2122f672018-06-28 18:21:58 -0700254 head_rev = lookup_commit_reference(the_repository, &head_oid);
Anders Kaseorgc8dd4912021-12-01 14:15:43 -0800255
Stefan Bellera9155c52017-10-03 15:17:40 -0700256 for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
Michael Haggerty8bb04552016-04-25 10:42:19 +0200257 char *target = NULL;
René Scharfe0fe700e2012-10-18 14:07:11 +0200258 int flags = 0;
259
Jeff King6b145e02017-03-02 03:23:10 -0500260 strbuf_branchname(&bname, argv[i], allowed_interpret);
Kazuki Yamaguchif2922442016-03-29 18:38:39 +0900261 free(name);
262 name = mkpathdup(fmt, bname.buf);
263
264 if (kinds == FILTER_REFS_BRANCHES) {
Derrick Stoleeb489b9d2022-06-14 19:27:32 +0000265 const char *path;
266 if ((path = branch_checked_out(name))) {
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100267 error(_("cannot delete branch '%s' "
Rubén Justo92edf612023-08-07 22:42:36 +0200268 "used by worktree at '%s'"),
Derrick Stoleeb489b9d2022-06-14 19:27:32 +0000269 bname.buf, path);
Kazuki Yamaguchif2922442016-03-29 18:38:39 +0900270 ret = 1;
271 continue;
272 }
Quy Tonthatb8e9a002006-12-19 09:42:16 +1100273 }
Lars Hjemlic31820c2006-10-23 23:27:45 +0200274
Michael Haggerty8bb04552016-04-25 10:42:19 +0200275 target = resolve_refdup(name,
276 RESOLVE_REF_READING
277 | RESOLVE_REF_NO_RECURSE
278 | RESOLVE_REF_ALLOW_BAD_NAME,
brian m. carlson0f2dc722017-10-15 22:06:55 +0000279 &oid, &flags);
Ronnie Sahlberg18f29fc2014-09-11 10:34:36 -0700280 if (!target) {
Clement Mabileau4c643fb2023-04-05 11:43:20 +0000281 if (remote_branch) {
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100282 error(_("remote-tracking branch '%s' not found"), bname.buf);
Clement Mabileau4c643fb2023-04-05 11:43:20 +0000283 } else {
284 char *virtual_name = mkpathdup(fmt_remotes, bname.buf);
285 char *virtual_target = resolve_refdup(virtual_name,
286 RESOLVE_REF_READING
287 | RESOLVE_REF_NO_RECURSE
288 | RESOLVE_REF_ALLOW_BAD_NAME,
289 &oid, &flags);
290 FREE_AND_NULL(virtual_name);
291
292 if (virtual_target)
293 error(_("branch '%s' not found.\n"
294 "Did you forget --remote?"),
295 bname.buf);
296 else
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100297 error(_("branch '%s' not found"), bname.buf);
Clement Mabileau4c643fb2023-04-05 11:43:20 +0000298 FREE_AND_NULL(virtual_target);
299 }
Quy Tonthatb8e9a002006-12-19 09:42:16 +1100300 ret = 1;
301 continue;
302 }
Lars Hjemlic31820c2006-10-23 23:27:45 +0200303
Ronnie Sahlbergd0f810f2014-09-03 11:45:43 -0700304 if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
brian m. carlson0c77cd22017-02-21 23:47:26 +0000305 check_branch_commit(bname.buf, name, &oid, head_rev, kinds,
René Scharfef5d0e162012-10-18 14:02:51 +0200306 force)) {
Quy Tonthatb8e9a002006-12-19 09:42:16 +1100307 ret = 1;
Michael Haggerty8bb04552016-04-25 10:42:19 +0200308 goto next;
Lars Hjemlic31820c2006-10-23 23:27:45 +0200309 }
310
Phil Hord81989072021-01-20 19:23:32 -0800311 item = string_list_append(&refs_to_delete, name);
312 item->util = xstrdup((flags & REF_ISBROKEN) ? "broken"
313 : (flags & REF_ISSYMREF) ? target
Ævar Arnfjörð Bjarmasond850b7a2023-03-28 15:58:46 +0200314 : repo_find_unique_abbrev(the_repository, &oid, DEFAULT_ABBREV));
Michael Haggerty8bb04552016-04-25 10:42:19 +0200315
316 next:
317 free(target);
Lars Hjemlic31820c2006-10-23 23:27:45 +0200318 }
Lars Hjemlic31820c2006-10-23 23:27:45 +0200319
Phil Hord81989072021-01-20 19:23:32 -0800320 if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF))
321 ret = 1;
322
323 for_each_string_list_item(item, &refs_to_delete) {
324 char *describe_ref = item->util;
325 char *name = item->string;
326 if (!ref_exists(name)) {
327 char *refname = name + branch_name_pos;
328 if (!quiet)
329 printf(remote_branch
330 ? _("Deleted remote-tracking branch %s (was %s).\n")
331 : _("Deleted branch %s (was %s).\n"),
332 name + branch_name_pos, describe_ref);
333
334 delete_branch_config(refname);
335 }
336 free(describe_ref);
337 }
338 string_list_clear(&refs_to_delete, 0);
339
Jim Meyering8e0f7002008-01-31 18:26:32 +0100340 free(name);
Stefan Bellera9155c52017-10-03 15:17:40 -0700341 strbuf_release(&bname);
Quy Tonthatb8e9a002006-12-19 09:42:16 +1100342
Stefan Bellera9155c52017-10-03 15:17:40 -0700343 return ret;
Lars Hjemlic31820c2006-10-23 23:27:45 +0200344}
Lars Hjemlic31820c2006-10-23 23:27:45 +0200345
Karthik Nayak1511b222015-09-23 23:41:11 +0530346static int calc_maxwidth(struct ref_array *refs, int remote_bonus)
Lars Hjemlib6f637d2008-07-26 12:27:25 +0200347{
Karthik Nayak1051e402015-09-23 23:41:06 +0530348 int i, max = 0;
Karthik Nayak1511b222015-09-23 23:41:11 +0530349 for (i = 0; i < refs->nr; i++) {
350 struct ref_array_item *it = refs->items[i];
Karthik Nayakaedcb7d2015-09-23 23:41:12 +0530351 const char *desc = it->refname;
Karthik Nayak1051e402015-09-23 23:41:06 +0530352 int w;
353
Karthik Nayakaedcb7d2015-09-23 23:41:12 +0530354 skip_prefix(it->refname, "refs/heads/", &desc);
355 skip_prefix(it->refname, "refs/remotes/", &desc);
Karthik Nayak949af062017-01-10 14:19:52 +0530356 if (it->kind == FILTER_REFS_DETACHED_HEAD) {
357 char *head_desc = get_head_description();
358 w = utf8_strwidth(head_desc);
359 free(head_desc);
360 } else
361 w = utf8_strwidth(desc);
Karthik Nayakaedcb7d2015-09-23 23:41:12 +0530362
Karthik Nayak1511b222015-09-23 23:41:11 +0530363 if (it->kind == FILTER_REFS_REMOTES)
Karthik Nayak1051e402015-09-23 23:41:06 +0530364 w += remote_bonus;
365 if (w > max)
366 max = w;
Lars Hjemlib6f637d2008-07-26 12:27:25 +0200367 }
Karthik Nayak1051e402015-09-23 23:41:06 +0530368 return max;
Lars Hjemlib6f637d2008-07-26 12:27:25 +0200369}
370
Karthik Nayak949af062017-01-10 14:19:52 +0530371static const char *quote_literal_for_format(const char *s)
372{
373 static struct strbuf buf = STRBUF_INIT;
374
375 strbuf_reset(&buf);
René Scharfe44ccb332023-06-17 22:41:44 +0200376 while (strbuf_expand_step(&buf, &s))
377 strbuf_addstr(&buf, "%%");
Karthik Nayak949af062017-01-10 14:19:52 +0530378 return buf.buf;
379}
380
381static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix)
382{
383 struct strbuf fmt = STRBUF_INIT;
384 struct strbuf local = STRBUF_INIT;
385 struct strbuf remote = STRBUF_INIT;
386
Nickolai Belakovskiab313812019-04-28 22:19:43 -0700387 strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)%%(if)%%(worktreepath)%%(then)+ %s%%(else) %s%%(end)%%(end)",
388 branch_get_color(BRANCH_COLOR_CURRENT),
389 branch_get_color(BRANCH_COLOR_WORKTREE),
390 branch_get_color(BRANCH_COLOR_LOCAL));
Jeff King7ca260a2017-07-09 06:00:45 -0400391 strbuf_addf(&remote, " %s",
392 branch_get_color(BRANCH_COLOR_REMOTE));
Karthik Nayak949af062017-01-10 14:19:52 +0530393
394 if (filter->verbose) {
Junio C Hamanoac5bbc02017-03-08 14:13:09 -0800395 struct strbuf obname = STRBUF_INIT;
396
397 if (filter->abbrev < 0)
398 strbuf_addf(&obname, "%%(objectname:short)");
399 else if (!filter->abbrev)
400 strbuf_addf(&obname, "%%(objectname)");
401 else
402 strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev);
403
Karthik Nayak949af062017-01-10 14:19:52 +0530404 strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
René Scharfe72d4a9a2017-10-01 16:44:20 +0200405 strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET));
Junio C Hamanoac5bbc02017-03-08 14:13:09 -0800406 strbuf_addf(&local, " %s ", obname.buf);
Karthik Nayak949af062017-01-10 14:19:52 +0530407
408 if (filter->verbose > 1)
Nickolai Belakovski6e938142019-04-28 22:19:44 -0700409 {
410 strbuf_addf(&local, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)",
411 branch_get_color(BRANCH_COLOR_WORKTREE), branch_get_color(BRANCH_COLOR_RESET));
Karthik Nayak949af062017-01-10 14:19:52 +0530412 strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
413 "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
414 branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
Nickolai Belakovski6e938142019-04-28 22:19:44 -0700415 }
Karthik Nayak949af062017-01-10 14:19:52 +0530416 else
417 strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
418
Jeff King7ca260a2017-07-09 06:00:45 -0400419 strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
Junio C Hamanoac5bbc02017-03-08 14:13:09 -0800420 "%%(if)%%(symref)%%(then) -> %%(symref:short)"
421 "%%(else) %s %%(contents:subject)%%(end)",
Jeff King7ca260a2017-07-09 06:00:45 -0400422 maxwidth, quote_literal_for_format(remote_prefix),
Junio C Hamanoac5bbc02017-03-08 14:13:09 -0800423 branch_get_color(BRANCH_COLOR_RESET), obname.buf);
424 strbuf_release(&obname);
Karthik Nayak949af062017-01-10 14:19:52 +0530425 } else {
426 strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
427 branch_get_color(BRANCH_COLOR_RESET));
Jeff King7ca260a2017-07-09 06:00:45 -0400428 strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
429 quote_literal_for_format(remote_prefix),
Karthik Nayak949af062017-01-10 14:19:52 +0530430 branch_get_color(BRANCH_COLOR_RESET));
431 }
432
433 strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf);
434
435 strbuf_release(&local);
436 strbuf_release(&remote);
437 return strbuf_detach(&fmt, NULL);
438}
439
Ævar Arnfjörð Bjarmasond72d4f92021-10-20 20:27:21 +0200440static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting,
441 struct ref_format *format, struct string_list *output)
Lars Hjemlic31820c2006-10-23 23:27:45 +0200442{
443 int i;
Karthik Nayak1511b222015-09-23 23:41:11 +0530444 struct ref_array array;
Karthik Nayak1051e402015-09-23 23:41:06 +0530445 int maxwidth = 0;
446 const char *remote_prefix = "";
Karthik Nayak3d9e4ce2017-01-10 14:19:53 +0530447 char *to_free = NULL;
Lars Hjemlic31820c2006-10-23 23:27:45 +0200448
Karthik Nayak35257aa2015-07-07 21:36:12 +0530449 /*
Karthik Nayak1051e402015-09-23 23:41:06 +0530450 * If we are listing more than just remote branches,
451 * then remote branches will have a "remotes/" prefix.
452 * We need to account for this in the width.
Karthik Nayak35257aa2015-07-07 21:36:12 +0530453 */
Karthik Nayak1511b222015-09-23 23:41:11 +0530454 if (filter->kind != FILTER_REFS_REMOTES)
Karthik Nayak1051e402015-09-23 23:41:06 +0530455 remote_prefix = "remotes/";
Carlos Martín Nieto6c41e972012-02-27 16:11:53 +0100456
Karthik Nayak1511b222015-09-23 23:41:11 +0530457 memset(&array, 0, sizeof(array));
Stefan Beller81c3ce32014-08-10 23:33:26 +0200458
Jeff King17633342021-09-24 14:48:05 -0400459 filter_refs(&array, filter, filter->kind);
Jeff King8376a702014-09-18 06:49:43 -0400460
Karthik Nayak1511b222015-09-23 23:41:11 +0530461 if (filter->verbose)
462 maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
Jeff King8376a702014-09-18 06:49:43 -0400463
Jeff King4a68e362017-07-13 11:01:18 -0400464 if (!format->format)
465 format->format = to_free = build_format(filter, maxwidth, remote_prefix);
Jeff King11b087a2017-07-13 11:09:32 -0400466 format->use_color = branch_use_color;
Jeff King2eda0102017-07-13 10:56:10 -0400467
468 if (verify_ref_format(format))
469 die(_("unable to parse format string"));
Karthik Nayak949af062017-01-10 14:19:52 +0530470
Derrick Stolee49abcd22023-03-20 11:26:54 +0000471 filter_ahead_behind(the_repository, format, &array);
Karthik Nayakaedcb7d2015-09-23 23:41:12 +0530472 ref_array_sort(sorting, &array);
Jeff King8376a702014-09-18 06:49:43 -0400473
Victoria Dyee7574b02023-11-14 19:53:52 +0000474 if (column_active(colopts)) {
475 struct strbuf out = STRBUF_INIT, err = STRBUF_INIT;
476
477 assert(!filter->verbose && "--column and --verbose are incompatible");
478
479 for (i = 0; i < array.nr; i++) {
480 strbuf_reset(&err);
481 strbuf_reset(&out);
482 if (format_ref_array_item(array.items[i], format, &out, &err))
483 die("%s", err.buf);
484
485 /* format to a string_list to let print_columns() do its job */
Ævar Arnfjörð Bjarmasond72d4f92021-10-20 20:27:21 +0200486 string_list_append(output, out.buf);
Karthik Nayak949af062017-01-10 14:19:52 +0530487 }
Victoria Dyee7574b02023-11-14 19:53:52 +0000488
489 strbuf_release(&err);
490 strbuf_release(&out);
491 } else {
492 print_formatted_ref_array(&array, format);
Karthik Nayak949af062017-01-10 14:19:52 +0530493 }
Lars Hjemlic31820c2006-10-23 23:27:45 +0200494
Karthik Nayak1511b222015-09-23 23:41:11 +0530495 ref_array_clear(&array);
Karthik Nayak3d9e4ce2017-01-10 14:19:53 +0530496 free(to_free);
Lars Hjemlic31820c2006-10-23 23:27:45 +0200497}
498
Daniels Umanovskis0ecb1fc2018-10-25 21:04:21 +0200499static void print_current_branch_name(void)
500{
501 int flags;
502 const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags);
503 const char *shortname;
504 if (!refname)
505 die(_("could not resolve HEAD"));
506 else if (!(flags & REF_ISSYMREF))
507 return;
508 else if (skip_prefix(refname, "refs/heads/", &shortname))
509 puts(shortname);
510 else
511 die(_("HEAD (%s) points outside of refs/heads/"), refname);
512}
513
Rubén Justod7f4ca62023-03-27 00:33:09 +0200514static void reject_rebase_or_bisect_branch(struct worktree **worktrees,
515 const char *target)
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700516{
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700517 int i;
518
519 for (i = 0; worktrees[i]; i++) {
520 struct worktree *wt = worktrees[i];
521
522 if (!wt->is_detached)
523 continue;
524
525 if (is_worktree_being_rebased(wt, target))
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100526 die(_("branch %s is being rebased at %s"),
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700527 target, wt->path);
528
529 if (is_worktree_being_bisected(wt, target))
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100530 die(_("branch %s is being bisected at %s"),
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700531 target, wt->path);
532 }
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700533}
534
Rubén Justo2e8af492023-03-27 00:33:02 +0200535/*
536 * Update all per-worktree HEADs pointing at the old ref to point the new ref.
537 * This will be used when renaming a branch. Returns 0 if successful, non-zero
538 * otherwise.
539 */
Rubén Justod7f4ca62023-03-27 00:33:09 +0200540static int replace_each_worktree_head_symref(struct worktree **worktrees,
541 const char *oldref, const char *newref,
Rubén Justo2e8af492023-03-27 00:33:02 +0200542 const char *logmsg)
543{
544 int ret = 0;
Rubén Justo2e8af492023-03-27 00:33:02 +0200545 int i;
546
547 for (i = 0; worktrees[i]; i++) {
548 struct ref_store *refs;
549
550 if (worktrees[i]->is_detached)
551 continue;
552 if (!worktrees[i]->head_ref)
553 continue;
554 if (strcmp(oldref, worktrees[i]->head_ref))
555 continue;
556
557 refs = get_worktree_ref_store(worktrees[i]);
558 if (refs_create_symref(refs, "HEAD", newref, logmsg))
559 ret = error(_("HEAD of working tree %s is not updated"),
560 worktrees[i]->path);
561 }
562
Rubén Justo2e8af492023-03-27 00:33:02 +0200563 return ret;
564}
565
Rubén Justo7a6ccdf2023-03-27 00:33:17 +0200566#define IS_HEAD 1
Rubén Justoa675ad12023-03-27 00:33:27 +0200567#define IS_ORPHAN 2
Rubén Justo7a6ccdf2023-03-27 00:33:17 +0200568
Sahil Dua52d59cc2017-06-18 23:19:16 +0200569static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
Lars Hjemlic976d412006-11-28 15:47:40 +0100570{
Miklos Vajnada3f78a2008-11-17 21:48:37 +0100571 struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
Miklos Vajnada3f78a2008-11-17 21:48:37 +0100572 struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
Kaartic Sivaraam255073c2017-12-01 11:29:33 +0530573 const char *interpreted_oldname = NULL;
574 const char *interpreted_newname = NULL;
Rubén Justo7a6ccdf2023-03-27 00:33:17 +0200575 int recovery = 0, oldref_usage = 0;
Rubén Justod7f4ca62023-03-27 00:33:09 +0200576 struct worktree **worktrees = get_worktrees();
Lars Hjemlic976d412006-11-28 15:47:40 +0100577
Junio C Hamanocbdffe42009-03-21 13:27:31 -0700578 if (strbuf_check_branch_ref(&oldref, oldname)) {
579 /*
580 * Bad name --- this could be an attempt to rename a
581 * ref that we used to allow to be created by accident.
582 */
Nguyễn Thái Ngọc Duyc6893322011-11-13 17:22:14 +0700583 if (ref_exists(oldref.buf))
Junio C Hamanocbdffe42009-03-21 13:27:31 -0700584 recovery = 1;
Kristoffer Haugsbakk8fbd9032024-03-05 21:29:43 +0100585 else {
586 int code = die_message(_("invalid branch name: '%s'"), oldname);
587 advise_if_enabled(ADVICE_REF_SYNTAX,
588 _("See `man git check-ref-format`"));
589 exit(code);
590 }
Junio C Hamanocbdffe42009-03-21 13:27:31 -0700591 }
Lars Hjemlic976d412006-11-28 15:47:40 +0100592
Rubén Justo7a6ccdf2023-03-27 00:33:17 +0200593 for (int i = 0; worktrees[i]; i++) {
594 struct worktree *wt = worktrees[i];
595
596 if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) {
597 oldref_usage |= IS_HEAD;
Rubén Justoa675ad12023-03-27 00:33:27 +0200598 if (is_null_oid(&wt->head_oid))
599 oldref_usage |= IS_ORPHAN;
Rubén Justo7a6ccdf2023-03-27 00:33:17 +0200600 break;
601 }
602 }
603
604 if ((copy || !(oldref_usage & IS_HEAD)) && !ref_exists(oldref.buf)) {
605 if (oldref_usage & IS_HEAD)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100606 die(_("no commit on branch '%s' yet"), oldname);
Rubén Justobcfc82b2022-10-08 02:39:43 +0200607 else
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100608 die(_("no branch named '%s'"), oldname);
Rubén Justobcfc82b2022-10-08 02:39:43 +0200609 }
610
Jonathan Nieder3f594812011-11-25 20:30:02 -0600611 /*
612 * A command like "git branch -M currentbranch currentbranch" cannot
613 * cause the worktree to become inconsistent with HEAD, so allow it.
614 */
Junio C Hamanobc1c9c02017-10-13 13:45:40 +0900615 if (!strcmp(oldname, newname))
616 validate_branchname(newname, &newref);
617 else
618 validate_new_branchname(newname, &newref, force);
Lars Hjemlic976d412006-11-28 15:47:40 +0100619
Rubén Justod7f4ca62023-03-27 00:33:09 +0200620 reject_rebase_or_bisect_branch(worktrees, oldref.buf);
Nguyễn Thái Ngọc Duy14ace5b2016-04-22 20:01:36 +0700621
Kaartic Sivaraam255073c2017-12-01 11:29:33 +0530622 if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
623 !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
Johannes Schindelin033abf92018-05-02 11:38:39 +0200624 BUG("expected prefix missing for refs");
Kaartic Sivaraam255073c2017-12-01 11:29:33 +0530625 }
626
Sahil Dua52d59cc2017-06-18 23:19:16 +0200627 if (copy)
628 strbuf_addf(&logmsg, "Branch: copied %s to %s",
629 oldref.buf, newref.buf);
630 else
631 strbuf_addf(&logmsg, "Branch: renamed %s to %s",
632 oldref.buf, newref.buf);
Lars Hjemli678d0f42006-11-30 03:16:56 +0100633
Rubén Justoa675ad12023-03-27 00:33:27 +0200634 if (!copy && !(oldref_usage & IS_ORPHAN) &&
Johannes Schindelincfaff3a2020-12-11 11:36:55 +0000635 rename_ref(oldref.buf, newref.buf, logmsg.buf))
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100636 die(_("branch rename failed"));
Sahil Dua52d59cc2017-06-18 23:19:16 +0200637 if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100638 die(_("branch copy failed"));
Lars Hjemlic976d412006-11-28 15:47:40 +0100639
Sahil Dua52d59cc2017-06-18 23:19:16 +0200640 if (recovery) {
641 if (copy)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100642 warning(_("created a copy of a misnamed branch '%s'"),
Kaartic Sivaraam255073c2017-12-01 11:29:33 +0530643 interpreted_oldname);
Sahil Dua52d59cc2017-06-18 23:19:16 +0200644 else
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100645 warning(_("renamed a misnamed branch '%s' away"),
Kaartic Sivaraam255073c2017-12-01 11:29:33 +0530646 interpreted_oldname);
Sahil Dua52d59cc2017-06-18 23:19:16 +0200647 }
Junio C Hamanocbdffe42009-03-21 13:27:31 -0700648
Rubén Justo3521c632023-03-27 00:33:40 +0200649 if (!copy && (oldref_usage & IS_HEAD) &&
Rubén Justod7f4ca62023-03-27 00:33:09 +0200650 replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf,
651 logmsg.buf))
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100652 die(_("branch renamed to %s, but HEAD is not updated"), newname);
Lars Hjemli19eba152007-04-06 14:13:00 +0200653
Kyle Meyer39ee4c62017-02-20 20:10:35 -0500654 strbuf_release(&logmsg);
655
Kaartic Sivaraam255073c2017-12-01 11:29:33 +0530656 strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
Kaartic Sivaraam255073c2017-12-01 11:29:33 +0530657 strbuf_addf(&newsection, "branch.%s", interpreted_newname);
Sahil Dua52d59cc2017-06-18 23:19:16 +0200658 if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100659 die(_("branch is renamed, but update of config-file failed"));
Rubén Justocfbd1732022-11-17 02:36:52 +0100660 if (copy && strcmp(interpreted_oldname, interpreted_newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100661 die(_("branch is copied, but update of config-file failed"));
Rubén Justocfbd1732022-11-17 02:36:52 +0100662 strbuf_release(&oldref);
663 strbuf_release(&newref);
Miklos Vajnada3f78a2008-11-17 21:48:37 +0100664 strbuf_release(&oldsection);
665 strbuf_release(&newsection);
Rubén Justod7f4ca62023-03-27 00:33:09 +0200666 free_worktrees(worktrees);
Lars Hjemlic976d412006-11-28 15:47:40 +0100667}
668
Jeff Kingc10388c2017-04-20 17:08:41 -0400669static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
Junio C Hamanob7200e82011-09-20 15:10:08 -0700670
671static int edit_branch_description(const char *branch_name)
672{
Junio C Hamanoe288b3d2022-09-30 11:06:22 -0700673 int exists;
Junio C Hamanob7200e82011-09-20 15:10:08 -0700674 struct strbuf buf = STRBUF_INIT;
675 struct strbuf name = STRBUF_INIT;
676
Junio C Hamanoe288b3d2022-09-30 11:06:22 -0700677 exists = !read_branch_desc(&buf, branch_name);
Junio C Hamanob7200e82011-09-20 15:10:08 -0700678 if (!buf.len || buf.buf[buf.len-1] != '\n')
679 strbuf_addch(&buf, '\n');
Jeff King3a35d962024-03-12 05:17:29 -0400680 strbuf_commented_addf(&buf, comment_line_str,
Vasco Almeidab18237f2016-06-17 21:54:15 +0000681 _("Please edit the description for the branch\n"
682 " %s\n"
Jeff Kingf99e1d92024-03-12 05:17:34 -0400683 "Lines starting with '%s' will be stripped.\n"),
684 branch_name, comment_line_str);
Jeff Kingc10388c2017-04-20 17:08:41 -0400685 write_file_buf(edit_description(), buf.buf, buf.len);
Junio C Hamanob7200e82011-09-20 15:10:08 -0700686 strbuf_reset(&buf);
Jeff Kingc10388c2017-04-20 17:08:41 -0400687 if (launch_editor(edit_description(), &buf, NULL)) {
Junio C Hamanob7200e82011-09-20 15:10:08 -0700688 strbuf_release(&buf);
689 return -1;
690 }
Jeff King2982b652024-03-12 05:17:27 -0400691 strbuf_stripspace(&buf, comment_line_str);
Junio C Hamanob7200e82011-09-20 15:10:08 -0700692
693 strbuf_addf(&name, "branch.%s.description", branch_name);
Junio C Hamanoe288b3d2022-09-30 11:06:22 -0700694 if (buf.len || exists)
695 git_config_set(name.buf, buf.len ? buf.buf : NULL);
Junio C Hamanob7200e82011-09-20 15:10:08 -0700696 strbuf_release(&name);
697 strbuf_release(&buf);
698
Patrick Steinhardtbd25f892016-02-22 12:23:25 +0100699 return 0;
Junio C Hamanob7200e82011-09-20 15:10:08 -0700700}
701
Lars Hjemlic31820c2006-10-23 23:27:45 +0200702int cmd_branch(int argc, const char **argv, const char *prefix)
703{
Glen Choo6e0a2ca2022-01-28 16:04:44 -0800704 /* possible actions */
705 int delete = 0, rename = 0, copy = 0, list = 0,
706 unset_upstream = 0, show_current = 0, edit_description = 0;
Carlos Martín Nieto6183d822012-08-20 15:47:38 +0200707 const char *new_upstream = NULL;
Glen Choo6e0a2ca2022-01-28 16:04:44 -0800708 int noncreate_actions = 0;
709 /* possible options */
Glen Choo961b1302022-01-28 16:04:45 -0800710 int reflog = 0, quiet = 0, icase = 0, force = 0,
711 recurse_submodules_explicit = 0;
Jay Soffian9ed36cf2008-02-19 11:24:37 -0500712 enum branch_track track;
Jeff Kingb9f7daa2023-07-10 17:12:07 -0400713 struct ref_filter filter = REF_FILTER_INIT;
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700714 static struct ref_sorting *sorting;
715 struct string_list sorting_options = STRING_LIST_INIT_DUP;
Jeff King4a68e362017-07-13 11:01:18 -0400716 struct ref_format format = REF_FORMAT_INIT;
Pierre Habouzita8dfd5e2007-10-07 18:26:21 +0200717
718 struct option options[] = {
Nguyễn Thái Ngọc Duy24a85212012-08-20 19:31:55 +0700719 OPT_GROUP(N_("Generic options")),
Karthik Nayak1511b222015-09-23 23:41:11 +0530720 OPT__VERBOSE(&filter.verbose,
Nguyễn Thái Ngọc Duy24a85212012-08-20 19:31:55 +0700721 N_("show hash and subject, give twice for upstream branch")),
722 OPT__QUIET(&quiet, N_("suppress informational messages")),
René Scharfe6327f0e2022-01-20 13:35:54 +0100723 OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)",
Josh Steadmond3115662021-12-20 19:30:23 -0800724 N_("set branch tracking configuration"),
Josh Steadmon15f00282022-01-18 12:49:46 -0800725 PARSE_OPT_OPTARG,
Josh Steadmond3115662021-12-20 19:30:23 -0800726 parse_opt_tracking_mode),
Nguyễn Thái Ngọc Duy3e4a67b2018-05-20 17:42:58 +0200727 OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"),
728 BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN),
Vasco Almeidaab868852016-04-08 20:02:45 +0000729 OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
Dimitriy Ryazantcev11de8dd2019-12-08 11:26:47 +0200730 OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("unset the upstream info")),
Nguyễn Thái Ngọc Duy24a85212012-08-20 19:31:55 +0700731 OPT__COLOR(&branch_use_color, N_("use colored output")),
Junio C Hamanoe12cb982023-07-18 11:27:49 -0700732 OPT_SET_INT_F('r', "remotes", &filter.kind, N_("act on remote-tracking branches"),
733 FILTER_REFS_REMOTES,
734 PARSE_OPT_NONEG),
Karthik Nayak1511b222015-09-23 23:41:11 +0530735 OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")),
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +0000736 OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")),
Karthik Nayak1511b222015-09-23 23:41:11 +0530737 OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")),
Ævar Arnfjörð Bjarmasonac3f5a32017-03-24 18:40:57 +0000738 OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")),
Karthik Nayak1511b222015-09-23 23:41:11 +0530739 OPT__ABBREV(&filter.abbrev),
Pierre Habouzita8dfd5e2007-10-07 18:26:21 +0200740
Nguyễn Thái Ngọc Duy24a85212012-08-20 19:31:55 +0700741 OPT_GROUP(N_("Specific git-branch actions:")),
Junio C Hamanoe12cb982023-07-18 11:27:49 -0700742 OPT_SET_INT_F('a', "all", &filter.kind, N_("list both remote-tracking and local branches"),
743 FILTER_REFS_REMOTES | FILTER_REFS_BRANCHES,
744 PARSE_OPT_NONEG),
Nguyễn Thái Ngọc Duy24a85212012-08-20 19:31:55 +0700745 OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1),
746 OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
747 OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
748 OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
Victoria Dye9d4fcfe2023-11-14 19:53:50 +0000749 OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty,
Øystein Walleaabfdc92023-04-07 19:53:16 +0200750 N_("do not output a newline after empty formatted refs")),
Sahil Dua52d59cc2017-06-18 23:19:16 +0200751 OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
752 OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
Jeff Kinga15d5982018-06-22 05:24:59 -0400753 OPT_BOOL('l', "list", &list, N_("list branch names")),
Daniels Umanovskis0ecb1fc2018-10-25 21:04:21 +0200754 OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")),
Jeff King055930b2018-06-22 05:24:14 -0400755 OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")),
Stefan Bellerd5d09d42013-08-03 13:51:19 +0200756 OPT_BOOL(0, "edit-description", &edit_description,
757 N_("edit the description for the branch")),
Nguyễn Thái Ngọc Duyc01b56a2018-02-09 18:01:47 +0700758 OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
Karthik Nayak1511b222015-09-23 23:41:11 +0530759 OPT_MERGED(&filter, N_("print only branches that are merged")),
760 OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
Nguyễn Thái Ngọc Duy24a85212012-08-20 19:31:55 +0700761 OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700762 OPT_REF_SORT(&sorting_options),
Denton Liu203c8532020-04-28 04:36:28 -0400763 OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"),
764 N_("print only branches of the object"), parse_opt_object_name),
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +0700765 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
Glen Choo961b1302022-01-28 16:04:45 -0800766 OPT_BOOL(0, "recurse-submodules", &recurse_submodules_explicit, N_("recurse through submodules")),
Jeff King4a68e362017-07-13 11:01:18 -0400767 OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
Pierre Habouzita8dfd5e2007-10-07 18:26:21 +0200768 OPT_END(),
769 };
Lars Hjemlic31820c2006-10-23 23:27:45 +0200770
Karthik Nayak56b43602017-01-10 14:19:51 +0530771 setup_ref_filter_porcelain_msg();
772
Karthik Nayak1511b222015-09-23 23:41:11 +0530773 filter.kind = FILTER_REFS_BRANCHES;
774 filter.abbrev = -1;
775
Nguyễn Thái Ngọc Duy1dacfbc2010-10-22 01:42:58 -0500776 if (argc == 2 && !strcmp(argv[1], "-h"))
777 usage_with_options(builtin_branch_usage, options);
778
Victoria Dye56d26ad2023-11-14 19:53:49 +0000779 /*
780 * Try to set sort keys from config. If config does not set any,
781 * fall back on default (refname) sorting.
782 */
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700783 git_config(git_branch_config, &sorting_options);
Victoria Dye56d26ad2023-11-14 19:53:49 +0000784 if (!sorting_options.nr)
785 string_list_append(&sorting_options, "refname");
Matthias Kestenholz6b2f2d92008-02-18 08:26:03 +0100786
Jay Soffian9ed36cf2008-02-19 11:24:37 -0500787 track = git_branch_track;
Lars Hjemlic976d412006-11-28 15:47:40 +0100788
brian m. carlson0f2dc722017-10-15 22:06:55 +0000789 head = resolve_refdup("HEAD", 0, &head_oid, NULL);
Lars Hjemlic31820c2006-10-23 23:27:45 +0200790 if (!head)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100791 die(_("failed to resolve HEAD as a valid ref"));
René Scharfee3f1da92014-10-04 20:54:50 +0200792 if (!strcmp(head, "HEAD"))
Karthik Nayak1511b222015-09-23 23:41:11 +0530793 filter.detached = 1;
René Scharfee3f1da92014-10-04 20:54:50 +0200794 else if (!skip_prefix(head, "refs/heads/", &head))
795 die(_("HEAD not found below refs/heads!"));
Nguyễn Thái Ngọc Duyebe31ef2012-04-13 17:54:38 +0700796
Stephen Boyd37782922009-05-23 11:53:12 -0700797 argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
798 0);
Michael J Grubercddd1272011-08-28 16:54:31 +0200799
Daniels Umanovskis0ecb1fc2018-10-25 21:04:21 +0200800 if (!delete && !rename && !copy && !edit_description && !new_upstream &&
801 !show_current && !unset_upstream && argc == 0)
Michael J Grubercddd1272011-08-28 16:54:31 +0200802 list = 1;
803
Aaron Lipman21bf9332020-09-15 22:08:40 -0400804 if (filter.with_commit || filter.no_commit ||
805 filter.reachable_from || filter.unreachable_from || filter.points_at.nr)
Jeff Kingd0403502013-01-31 01:46:11 -0500806 list = 1;
807
Glen Choo6e0a2ca2022-01-28 16:04:44 -0800808 noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream +
809 !!show_current + !!list + !!edit_description +
810 !!unset_upstream;
811 if (noncreate_actions > 1)
Junio C Hamano049716b2008-07-08 17:55:47 -0700812 usage_with_options(builtin_branch_usage, options);
Lars Hjemlic31820c2006-10-23 23:27:45 +0200813
Glen Choo961b1302022-01-28 16:04:45 -0800814 if (recurse_submodules_explicit) {
815 if (!submodule_propagate_branches)
816 die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled"));
817 if (noncreate_actions)
818 die(_("--recurse-submodules can only be used to create branches"));
819 }
820
821 recurse_submodules =
822 (recurse_submodules || recurse_submodules_explicit) &&
823 submodule_propagate_branches;
824
Karthik Nayak1511b222015-09-23 23:41:11 +0530825 if (filter.abbrev == -1)
826 filter.abbrev = DEFAULT_ABBREV;
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +0700827 filter.ignore_case = icase;
828
Nguyễn Thái Ngọc Duyebe31ef2012-04-13 17:54:38 +0700829 finalize_colopts(&colopts, -1);
Karthik Nayak1511b222015-09-23 23:41:11 +0530830 if (filter.verbose) {
Nguyễn Thái Ngọc Duyebe31ef2012-04-13 17:54:38 +0700831 if (explicitly_enable_column(colopts))
Jean-Noël Avila12909b62022-01-05 20:02:16 +0000832 die(_("options '%s' and '%s' cannot be used together"), "--column", "--verbose");
Nguyễn Thái Ngọc Duyebe31ef2012-04-13 17:54:38 +0700833 colopts = 0;
834 }
Namhyung Kimb792c062011-07-01 15:06:08 +0900835
Michael J Gruber356e91f2014-12-08 17:28:45 +0100836 if (force) {
837 delete *= 2;
838 rename *= 2;
Sahil Dua52d59cc2017-06-18 23:19:16 +0200839 copy *= 2;
Michael J Gruber356e91f2014-12-08 17:28:45 +0100840 }
841
Martin Ågrend74b5412017-11-19 16:03:49 +0100842 if (list)
Martin Ågren0ae19de2017-11-19 16:03:50 +0100843 setup_auto_pager("branch", 1);
Martin Ågrend74b5412017-11-19 16:03:49 +0100844
Rubén Justo2935a972023-06-17 08:41:22 +0200845 UNLEAK(sorting_options);
846
Nguyễn Thái Ngọc Duy640d0402013-01-28 08:18:14 +0700847 if (delete) {
848 if (!argc)
849 die(_("branch name required"));
Karthik Nayak1511b222015-09-23 23:41:11 +0530850 return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
Daniels Umanovskis0ecb1fc2018-10-25 21:04:21 +0200851 } else if (show_current) {
852 print_current_branch_name();
853 return 0;
Nguyễn Thái Ngọc Duy640d0402013-01-28 08:18:14 +0700854 } else if (list) {
Ævar Arnfjörð Bjarmasonffdd02a2021-01-06 11:01:35 +0100855 /* git branch --list also shows HEAD when it is detached */
Karthik Nayak1511b222015-09-23 23:41:11 +0530856 if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
857 filter.kind |= FILTER_REFS_DETACHED_HEAD;
858 filter.name_patterns = argv;
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +0700859 /*
860 * If no sorting parameter is given then we default to sorting
861 * by 'refname'. This would give us an alphabetically sorted
862 * array with the 'HEAD' ref at the beginning followed by
Robert P. J. Day30aa96c2018-06-07 07:53:36 -0400863 * local branches 'refs/heads/...' and finally remote-tracking
Nguyễn Thái Ngọc Duy3bb16a82016-12-04 09:52:25 +0700864 * branches 'refs/remotes/...'.
865 */
Junio C Hamano98e7ab62021-10-20 12:23:53 -0700866 sorting = ref_sorting_options(&sorting_options);
Ævar Arnfjörð Bjarmason7c269a72021-01-07 10:51:51 +0100867 ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase);
Ævar Arnfjörð Bjarmason2708ce62021-01-07 10:51:52 +0100868 ref_sorting_set_sort_flags_all(
869 sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1);
Ævar Arnfjörð Bjarmasond72d4f92021-10-20 20:27:21 +0200870 print_ref_list(&filter, sorting, &format, &output);
Nguyễn Thái Ngọc Duyebe31ef2012-04-13 17:54:38 +0700871 print_columns(&output, colopts, NULL);
872 string_list_clear(&output, 0);
Ævar Arnfjörð Bjarmasond72d4f92021-10-20 20:27:21 +0200873 ref_sorting_release(sorting);
Jeff Kingb571fb92023-07-10 17:12:13 -0400874 ref_filter_clear(&filter);
Karthik Nayakca417992015-09-24 23:39:08 +0530875 return 0;
Tao Qingyun2e3c8942018-10-16 22:19:20 +0800876 } else if (edit_description) {
Junio C Hamanob7200e82011-09-20 15:10:08 -0700877 const char *branch_name;
Junio C Hamanoc2d17ba2012-02-05 17:13:36 -0800878 struct strbuf branch_ref = STRBUF_INIT;
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200879 struct strbuf buf = STRBUF_INIT;
880 int ret = 1; /* assume failure */
Junio C Hamanoc2d17ba2012-02-05 17:13:36 -0800881
Nguyễn Thái Ngọc Duy75135b22013-01-28 08:18:13 +0700882 if (!argc) {
Karthik Nayak1511b222015-09-23 23:41:11 +0530883 if (filter.detached)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100884 die(_("cannot give description to detached HEAD"));
Junio C Hamanob7200e82011-09-20 15:10:08 -0700885 branch_name = head;
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200886 } else if (argc == 1) {
887 strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
888 branch_name = buf.buf;
889 } else {
Nguyễn Thái Ngọc Duy43722c42013-01-28 08:18:15 +0700890 die(_("cannot edit description of more than one branch"));
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200891 }
Junio C Hamanoc2d17ba2012-02-05 17:13:36 -0800892
893 strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200894 if (!ref_exists(branch_ref.buf))
Rubén Justo7a6ccdf2023-03-27 00:33:17 +0200895 error((!argc || branch_checked_out(branch_ref.buf))
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100896 ? _("no commit on branch '%s' yet")
897 : _("no branch named '%s'"),
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200898 branch_name);
899 else if (!edit_branch_description(branch_name))
900 ret = 0; /* happy */
Junio C Hamanoc2d17ba2012-02-05 17:13:36 -0800901
Junio C Hamanoc2d17ba2012-02-05 17:13:36 -0800902 strbuf_release(&branch_ref);
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200903 strbuf_release(&buf);
Junio C Hamanoc2d17ba2012-02-05 17:13:36 -0800904
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200905 return ret;
Rubén Justo77e72672022-10-26 01:01:29 +0200906 } else if (copy || rename) {
Sahil Dua52d59cc2017-06-18 23:19:16 +0200907 if (!argc)
908 die(_("branch name required"));
Rubén Justo77e72672022-10-26 01:01:29 +0200909 else if ((argc == 1) && filter.detached)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100910 die(copy? _("cannot copy the current branch while not on any")
911 : _("cannot rename the current branch while not on any"));
Sahil Dua52d59cc2017-06-18 23:19:16 +0200912 else if (argc == 1)
Rubén Justo77e72672022-10-26 01:01:29 +0200913 copy_or_rename_branch(head, argv[0], copy, copy + rename > 1);
Sahil Dua52d59cc2017-06-18 23:19:16 +0200914 else if (argc == 2)
Rubén Justo77e72672022-10-26 01:01:29 +0200915 copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1);
Sahil Dua52d59cc2017-06-18 23:19:16 +0200916 else
Rubén Justo77e72672022-10-26 01:01:29 +0200917 die(copy? _("too many branches for a copy operation")
918 : _("too many arguments for a rename operation"));
Carlos Martín Nieto6183d822012-08-20 15:47:38 +0200919 } else if (new_upstream) {
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200920 struct branch *branch;
921 struct strbuf buf = STRBUF_INIT;
Carlos Martín Nieto6183d822012-08-20 15:47:38 +0200922
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200923 if (!argc)
924 branch = branch_get(NULL);
925 else if (argc == 1) {
926 strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
927 branch = branch_get(buf.buf);
928 } else
Kaartic Sivaraamf7776232017-08-21 19:06:08 +0530929 die(_("too many arguments to set new upstream"));
Nguyễn Thái Ngọc Duy8efb8892013-02-23 19:22:27 +0700930
931 if (!branch) {
932 if (!argc || !strcmp(argv[0], "HEAD"))
933 die(_("could not set upstream of HEAD to %s when "
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100934 "it does not point to any branch"),
Nguyễn Thái Ngọc Duy8efb8892013-02-23 19:22:27 +0700935 new_upstream);
936 die(_("no such branch '%s'"), argv[0]);
937 }
938
Rubén Justobcfc82b2022-10-08 02:39:43 +0200939 if (!ref_exists(branch->refname)) {
Rubén Justo7a6ccdf2023-03-27 00:33:17 +0200940 if (!argc || branch_checked_out(branch->refname))
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100941 die(_("no commit on branch '%s' yet"), branch->name);
Carlos Martín Nieto6183d822012-08-20 15:47:38 +0200942 die(_("branch '%s' does not exist"), branch->name);
Rubén Justobcfc82b2022-10-08 02:39:43 +0200943 }
Carlos Martín Nieto6183d822012-08-20 15:47:38 +0200944
Glen Chooe89f1512022-01-28 16:04:41 -0800945 dwim_and_setup_tracking(the_repository, branch->name,
946 new_upstream, BRANCH_TRACK_OVERRIDE,
947 quiet);
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200948 strbuf_release(&buf);
Carlos Martín Nietob84869e2012-08-30 19:23:12 +0200949 } else if (unset_upstream) {
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200950 struct branch *branch;
Carlos Martín Nietob84869e2012-08-30 19:23:12 +0200951 struct strbuf buf = STRBUF_INIT;
952
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200953 if (!argc)
954 branch = branch_get(NULL);
955 else if (argc == 1) {
956 strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL);
957 branch = branch_get(buf.buf);
958 } else
Kaartic Sivaraamf7776232017-08-21 19:06:08 +0530959 die(_("too many arguments to unset upstream"));
Nguyễn Thái Ngọc Duy8efb8892013-02-23 19:22:27 +0700960
961 if (!branch) {
962 if (!argc || !strcmp(argv[0], "HEAD"))
963 die(_("could not unset upstream of HEAD when "
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100964 "it does not point to any branch"));
Nguyễn Thái Ngọc Duy8efb8892013-02-23 19:22:27 +0700965 die(_("no such branch '%s'"), argv[0]);
966 }
967
Felipe Contreras54d07f22013-10-31 03:25:38 -0600968 if (!branch_has_merge_config(branch))
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100969 die(_("branch '%s' has no upstream information"), branch->name);
Carlos Martín Nietob84869e2012-08-30 19:23:12 +0200970
Rubén Justo0dc4e5c2022-10-11 01:24:58 +0200971 strbuf_reset(&buf);
Carlos Martín Nietob84869e2012-08-30 19:23:12 +0200972 strbuf_addf(&buf, "branch.%s.remote", branch->name);
Derrick Stolee504ee122020-11-25 22:12:49 +0000973 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
Carlos Martín Nietob84869e2012-08-30 19:23:12 +0200974 strbuf_reset(&buf);
975 strbuf_addf(&buf, "branch.%s.merge", branch->name);
Derrick Stolee504ee122020-11-25 22:12:49 +0000976 git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE);
Carlos Martín Nietob84869e2012-08-30 19:23:12 +0200977 strbuf_release(&buf);
Glen Choo6e0a2ca2022-01-28 16:04:44 -0800978 } else if (!noncreate_actions && argc > 0 && argc <= 2) {
Glen Choo961b1302022-01-28 16:04:45 -0800979 const char *branch_name = argv[0];
980 const char *start_name = argc == 2 ? argv[1] : head;
981
Karthik Nayak1511b222015-09-23 23:41:11 +0530982 if (filter.kind != FILTER_REFS_BRANCHES)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100983 die(_("the -a, and -r, options to 'git branch' do not take a branch name.\n"
Philip Oakley1fde99c2019-05-29 00:16:05 +0100984 "Did you mean to use: -a|-r --list <pattern>?"));
Carlos Martín Nietob347d062012-08-30 19:23:13 +0200985
986 if (track == BRANCH_TRACK_OVERRIDE)
Isoken June Ibizugbe12b99922023-10-23 17:06:56 +0100987 die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead"));
Carlos Martín Nietob347d062012-08-30 19:23:13 +0200988
Glen Choo961b1302022-01-28 16:04:45 -0800989 if (recurse_submodules) {
990 create_branches_recursively(the_repository, branch_name,
991 start_name, NULL, force,
992 reflog, quiet, track, 0);
993 return 0;
994 }
995 create_branch(the_repository, branch_name, start_name, force, 0,
996 reflog, quiet, track, 0);
Matthieu Moy6e8f9932009-12-30 15:45:31 +0100997 } else
Pierre Habouzita8dfd5e2007-10-07 18:26:21 +0200998 usage_with_options(builtin_branch_usage, options);
Lars Hjemlic31820c2006-10-23 23:27:45 +0200999
1000 return 0;
1001}