Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Builtin "git branch" |
| 3 | * |
Junio C Hamano | d972cce | 2007-07-11 22:52:45 -0700 | [diff] [blame] | 4 | * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com> |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 5 | * Based on git-branch.sh by Junio C Hamano. |
| 6 | */ |
| 7 | |
| 8 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 9 | #include "config.h" |
Junio C Hamano | 8502357 | 2006-12-19 14:34:12 -0800 | [diff] [blame] | 10 | #include "color.h" |
Elijah Newren | 4e12082 | 2023-04-11 00:41:57 -0700 | [diff] [blame] | 11 | #include "editor.h" |
Elijah Newren | 7ee24e1 | 2023-03-21 06:25:57 +0000 | [diff] [blame] | 12 | #include "environment.h" |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 13 | #include "refs.h" |
| 14 | #include "commit.h" |
| 15 | #include "builtin.h" |
Elijah Newren | f394e09 | 2023-03-21 06:25:54 +0000 | [diff] [blame] | 16 | #include "gettext.h" |
Elijah Newren | dabab1d | 2023-04-11 00:41:49 -0700 | [diff] [blame] | 17 | #include "object-name.h" |
Johannes Schindelin | 6f084a5 | 2007-07-10 18:50:44 +0100 | [diff] [blame] | 18 | #include "remote.h" |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 19 | #include "parse-options.h" |
Daniel Barkalow | e496c00 | 2008-02-07 11:40:08 -0500 | [diff] [blame] | 20 | #include "branch.h" |
Junio C Hamano | 68067ca | 2008-07-23 15:13:41 -0700 | [diff] [blame] | 21 | #include "diff.h" |
| 22 | #include "revision.h" |
Nguyễn Thái Ngọc Duy | ebe31ef | 2012-04-13 17:54:38 +0700 | [diff] [blame] | 23 | #include "string-list.h" |
| 24 | #include "column.h" |
Nguyễn Thái Ngọc Duy | 1452bd6 | 2012-08-26 01:17:12 +0700 | [diff] [blame] | 25 | #include "utf8.h" |
Nguyễn Thái Ngọc Duy | c8183cd | 2013-03-13 18:42:53 +0700 | [diff] [blame] | 26 | #include "wt-status.h" |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 27 | #include "ref-filter.h" |
Kazuki Yamaguchi | f292244 | 2016-03-29 18:38:39 +0900 | [diff] [blame] | 28 | #include "worktree.h" |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 29 | #include "help.h" |
Derrick Stolee | 6404355 | 2018-07-20 16:33:04 +0000 | [diff] [blame] | 30 | #include "commit-reach.h" |
Elijah Newren | d5ebb50 | 2023-03-21 06:26:01 +0000 | [diff] [blame] | 31 | #include "wrapper.h" |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 32 | |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 33 | static const char * const builtin_branch_usage[] = { |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 34 | N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"), |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 35 | N_("git branch [<options>] [-f] [--recurse-submodules] <branch-name> [<start-point>]"), |
| 36 | N_("git branch [<options>] [-l] [<pattern>...]"), |
Alex Henrie | 9c9b4f2 | 2015-01-13 00:44:47 -0700 | [diff] [blame] | 37 | N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."), |
| 38 | N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"), |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 39 | N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"), |
Karthik Nayak | aa3bc55 | 2015-09-23 23:41:13 +0530 | [diff] [blame] | 40 | N_("git branch [<options>] [-r | -a] [--points-at]"), |
Karthik Nayak | 3d9e4ce | 2017-01-10 14:19:53 +0530 | [diff] [blame] | 41 | N_("git branch [<options>] [-r | -a] [--format]"), |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 42 | NULL |
| 43 | }; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 44 | |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 45 | static const char *head; |
brian m. carlson | 0c77cd2 | 2017-02-21 23:47:26 +0000 | [diff] [blame] | 46 | static struct object_id head_oid; |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 47 | static int recurse_submodules = 0; |
| 48 | static int submodule_propagate_branches = 0; |
Øystein Walle | aabfdc9 | 2023-04-07 19:53:16 +0200 | [diff] [blame] | 49 | static int omit_empty = 0; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 50 | |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 51 | static int branch_use_color = -1; |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 52 | static char branch_colors[][COLOR_MAXLEN] = { |
Arjen Laarhoven | dc6ebd4 | 2009-02-13 22:53:40 +0100 | [diff] [blame] | 53 | GIT_COLOR_RESET, |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 54 | GIT_COLOR_NORMAL, /* PLAIN */ |
| 55 | GIT_COLOR_RED, /* REMOTE */ |
| 56 | GIT_COLOR_NORMAL, /* LOCAL */ |
| 57 | GIT_COLOR_GREEN, /* CURRENT */ |
| 58 | GIT_COLOR_BLUE, /* UPSTREAM */ |
Nickolai Belakovski | ab31381 | 2019-04-28 22:19:43 -0700 | [diff] [blame] | 59 | GIT_COLOR_CYAN, /* WORKTREE */ |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 60 | }; |
| 61 | enum color_branch { |
Arjen Laarhoven | 74bb2df | 2009-02-13 22:53:41 +0100 | [diff] [blame] | 62 | BRANCH_COLOR_RESET = 0, |
| 63 | BRANCH_COLOR_PLAIN = 1, |
| 64 | BRANCH_COLOR_REMOTE = 2, |
| 65 | BRANCH_COLOR_LOCAL = 3, |
Felipe Contreras | dbda21f | 2013-04-14 21:37:49 -0500 | [diff] [blame] | 66 | BRANCH_COLOR_CURRENT = 4, |
Nickolai Belakovski | ab31381 | 2019-04-28 22:19:43 -0700 | [diff] [blame] | 67 | BRANCH_COLOR_UPSTREAM = 5, |
| 68 | BRANCH_COLOR_WORKTREE = 6 |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 71 | static const char *color_branch_slots[] = { |
| 72 | [BRANCH_COLOR_RESET] = "reset", |
| 73 | [BRANCH_COLOR_PLAIN] = "plain", |
| 74 | [BRANCH_COLOR_REMOTE] = "remote", |
| 75 | [BRANCH_COLOR_LOCAL] = "local", |
| 76 | [BRANCH_COLOR_CURRENT] = "current", |
| 77 | [BRANCH_COLOR_UPSTREAM] = "upstream", |
Nickolai Belakovski | ab31381 | 2019-04-28 22:19:43 -0700 | [diff] [blame] | 78 | [BRANCH_COLOR_WORKTREE] = "worktree", |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 79 | }; |
| 80 | |
Nguyễn Thái Ngọc Duy | ebe31ef | 2012-04-13 17:54:38 +0700 | [diff] [blame] | 81 | static struct string_list output = STRING_LIST_INIT_DUP; |
| 82 | static unsigned int colopts; |
| 83 | |
Nguyễn Thái Ngọc Duy | 3ac68a9 | 2018-05-26 15:55:24 +0200 | [diff] [blame] | 84 | define_list_config_array(color_branch_slots); |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 85 | |
Johannes Schindelin | ef90d6d | 2008-05-14 18:46:53 +0100 | [diff] [blame] | 86 | static int git_branch_config(const char *var, const char *value, void *cb) |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 87 | { |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 88 | const char *slot_name; |
Samuel Maftoul | 560ae1c | 2018-08-16 11:35:08 +0200 | [diff] [blame] | 89 | |
| 90 | if (!strcmp(var, "branch.sort")) { |
| 91 | if (!value) |
| 92 | return config_error_nonbool(var); |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 93 | string_list_append(cb, value); |
Samuel Maftoul | 560ae1c | 2018-08-16 11:35:08 +0200 | [diff] [blame] | 94 | return 0; |
| 95 | } |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 96 | |
Christian Couder | 5955654 | 2013-11-30 21:55:40 +0100 | [diff] [blame] | 97 | if (starts_with(var, "column.")) |
Nguyễn Thái Ngọc Duy | ebe31ef | 2012-04-13 17:54:38 +0700 | [diff] [blame] | 98 | return git_column_config(var, value, "branch", &colopts); |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 99 | if (!strcmp(var, "color.branch")) { |
Jeff King | e269eb7 | 2011-08-17 22:03:48 -0700 | [diff] [blame] | 100 | branch_use_color = git_config_colorbool(var, value); |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 101 | return 0; |
| 102 | } |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 103 | if (skip_prefix(var, "color.branch.", &slot_name)) { |
Nguyễn Thái Ngọc Duy | a73b368 | 2018-05-26 15:55:21 +0200 | [diff] [blame] | 104 | int slot = LOOKUP_CONFIG(color_branch_slots, slot_name); |
Jeff King | 8b8e862 | 2009-12-12 07:25:24 -0500 | [diff] [blame] | 105 | if (slot < 0) |
| 106 | return 0; |
Junio C Hamano | 5768c98 | 2008-02-11 10:45:50 -0800 | [diff] [blame] | 107 | if (!value) |
| 108 | return config_error_nonbool(var); |
Jeff King | f6c5a29 | 2014-10-07 15:33:09 -0400 | [diff] [blame] | 109 | return color_parse(value, branch_colors[slot]); |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 110 | } |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 111 | if (!strcmp(var, "submodule.recurse")) { |
| 112 | recurse_submodules = git_config_bool(var, value); |
| 113 | return 0; |
| 114 | } |
| 115 | if (!strcasecmp(var, "submodule.propagateBranches")) { |
| 116 | submodule_propagate_branches = git_config_bool(var, value); |
| 117 | return 0; |
| 118 | } |
| 119 | |
Jeff King | 33c643b | 2017-10-13 13:24:31 -0400 | [diff] [blame] | 120 | return git_color_default_config(var, value, cb); |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Pierre Habouzit | 52fae7d | 2007-06-07 22:45:00 +0200 | [diff] [blame] | 123 | static const char *branch_get_color(enum color_branch ix) |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 124 | { |
Jeff King | daa0c3d | 2011-08-17 22:04:23 -0700 | [diff] [blame] | 125 | if (want_color(branch_use_color)) |
Andy Parkins | a1158ca | 2006-12-12 06:41:52 +0000 | [diff] [blame] | 126 | return branch_colors[ix]; |
| 127 | return ""; |
| 128 | } |
| 129 | |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 130 | static 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 Duy | 96ec7b1 | 2011-12-13 21:17:48 +0700 | [diff] [blame] | 141 | void *reference_name_to_free = NULL; |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 142 | int merged; |
| 143 | |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 144 | if (kind == FILTER_REFS_BRANCHES) { |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 145 | struct branch *branch = branch_get(name); |
Jeff King | 3a429d0 | 2015-05-21 00:45:32 -0400 | [diff] [blame] | 146 | const char *upstream = branch_get_upstream(branch, NULL); |
brian m. carlson | 0c77cd2 | 2017-02-21 23:47:26 +0000 | [diff] [blame] | 147 | struct object_id oid; |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 148 | |
Jeff King | a9f9f8c | 2015-05-21 00:45:28 -0400 | [diff] [blame] | 149 | if (upstream && |
Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 21:17:48 +0700 | [diff] [blame] | 150 | (reference_name = reference_name_to_free = |
Jeff King | a9f9f8c | 2015-05-21 00:45:28 -0400 | [diff] [blame] | 151 | resolve_refdup(upstream, RESOLVE_REF_READING, |
brian m. carlson | 0f2dc72 | 2017-10-15 22:06:55 +0000 | [diff] [blame] | 152 | &oid, NULL)) != NULL) |
Stefan Beller | 2122f67 | 2018-06-28 18:21:58 -0700 | [diff] [blame] | 153 | reference_rev = lookup_commit_reference(the_repository, |
| 154 | &oid); |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 155 | } |
| 156 | if (!reference_rev) |
| 157 | reference_rev = head_rev; |
| 158 | |
Ævar Arnfjörð Bjarmason | cb338c2 | 2023-03-28 15:58:47 +0200 | [diff] [blame] | 159 | merged = reference_rev ? repo_in_merge_bases(the_repository, rev, |
| 160 | reference_rev) : 0; |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 161 | |
| 162 | /* |
| 163 | * After the safety valve is fully redefined to "check with |
| 164 | * upstream, if any, otherwise with HEAD", we should just |
Ævar Arnfjörð Bjarmason | c7c33f5 | 2023-03-28 15:58:57 +0200 | [diff] [blame] | 165 | * return the result of the repo_in_merge_bases() above without |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 166 | * any of the following code, but during the transition period, |
| 167 | * a gentle reminder is in order. |
| 168 | */ |
| 169 | if ((head_rev != reference_rev) && |
Ævar Arnfjörð Bjarmason | cb338c2 | 2023-03-28 15:58:47 +0200 | [diff] [blame] | 170 | (head_rev ? repo_in_merge_bases(the_repository, rev, head_rev) : 0) != merged) { |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 171 | if (merged) |
Ævar Arnfjörð Bjarmason | 49df4b0 | 2011-02-22 23:41:34 +0000 | [diff] [blame] | 172 | warning(_("deleting branch '%s' that has been merged to\n" |
Junio C Hamano | 6c80cd2 | 2011-04-01 17:55:55 -0700 | [diff] [blame] | 173 | " '%s', but not yet merged to HEAD."), |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 174 | name, reference_name); |
| 175 | else |
Ævar Arnfjörð Bjarmason | 49df4b0 | 2011-02-22 23:41:34 +0000 | [diff] [blame] | 176 | warning(_("not deleting branch '%s' that is not yet merged to\n" |
| 177 | " '%s', even though it is merged to HEAD."), |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 178 | name, reference_name); |
| 179 | } |
Nguyễn Thái Ngọc Duy | 96ec7b1 | 2011-12-13 21:17:48 +0700 | [diff] [blame] | 180 | free(reference_name_to_free); |
Junio C Hamano | 99c419c | 2009-12-29 22:43:04 -0800 | [diff] [blame] | 181 | return merged; |
| 182 | } |
| 183 | |
René Scharfe | f5d0e16 | 2012-10-18 14:02:51 +0200 | [diff] [blame] | 184 | static int check_branch_commit(const char *branchname, const char *refname, |
brian m. carlson | 0c77cd2 | 2017-02-21 23:47:26 +0000 | [diff] [blame] | 185 | const struct object_id *oid, struct commit *head_rev, |
René Scharfe | f5d0e16 | 2012-10-18 14:02:51 +0200 | [diff] [blame] | 186 | int kinds, int force) |
| 187 | { |
Stefan Beller | 2122f67 | 2018-06-28 18:21:58 -0700 | [diff] [blame] | 188 | struct commit *rev = lookup_commit_reference(the_repository, oid); |
René Scharfe | 597a977 | 2021-08-27 20:35:35 +0200 | [diff] [blame] | 189 | if (!force && !rev) { |
René Scharfe | f5d0e16 | 2012-10-18 14:02:51 +0200 | [diff] [blame] | 190 | error(_("Couldn't look up commit object for '%s'"), refname); |
| 191 | return -1; |
| 192 | } |
| 193 | if (!force && !branch_merged(kinds, branchname, rev, head_rev)) { |
| 194 | error(_("The branch '%s' is not fully merged.\n" |
| 195 | "If you are sure you want to delete it, " |
| 196 | "run 'git branch -D %s'."), branchname, branchname); |
| 197 | return -1; |
| 198 | } |
| 199 | return 0; |
| 200 | } |
| 201 | |
René Scharfe | 22ed792 | 2012-10-18 14:04:08 +0200 | [diff] [blame] | 202 | static void delete_branch_config(const char *branchname) |
| 203 | { |
| 204 | struct strbuf buf = STRBUF_INIT; |
| 205 | strbuf_addf(&buf, "branch.%s", branchname); |
| 206 | if (git_config_rename_section(buf.buf, NULL) < 0) |
| 207 | warning(_("Update of config-file failed")); |
| 208 | strbuf_release(&buf); |
| 209 | } |
| 210 | |
Jeff King | d65ddf1 | 2012-03-26 19:51:06 -0400 | [diff] [blame] | 211 | static int delete_branches(int argc, const char **argv, int force, int kinds, |
| 212 | int quiet) |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 213 | { |
René Scharfe | f5d0e16 | 2012-10-18 14:02:51 +0200 | [diff] [blame] | 214 | struct commit *head_rev = NULL; |
brian m. carlson | 0c77cd2 | 2017-02-21 23:47:26 +0000 | [diff] [blame] | 215 | struct object_id oid; |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 216 | char *name = NULL; |
Ævar Arnfjörð Bjarmason | c179837 | 2012-04-30 15:33:12 +0000 | [diff] [blame] | 217 | const char *fmt; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 218 | int i; |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 219 | int ret = 0; |
Ævar Arnfjörð Bjarmason | c179837 | 2012-04-30 15:33:12 +0000 | [diff] [blame] | 220 | int remote_branch = 0; |
Junio C Hamano | 8415d5c | 2009-02-13 23:08:05 -0800 | [diff] [blame] | 221 | struct strbuf bname = STRBUF_INIT; |
Jeff King | 6b145e0 | 2017-03-02 03:23:10 -0500 | [diff] [blame] | 222 | unsigned allowed_interpret; |
Phil Hord | 8198907 | 2021-01-20 19:23:32 -0800 | [diff] [blame] | 223 | struct string_list refs_to_delete = STRING_LIST_INIT_DUP; |
| 224 | struct string_list_item *item; |
| 225 | int branch_name_pos; |
Clement Mabileau | 4c643fb | 2023-04-05 11:43:20 +0000 | [diff] [blame] | 226 | const char *fmt_remotes = "refs/remotes/%s"; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 227 | |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 228 | switch (kinds) { |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 229 | case FILTER_REFS_REMOTES: |
Clement Mabileau | 4c643fb | 2023-04-05 11:43:20 +0000 | [diff] [blame] | 230 | fmt = fmt_remotes; |
Ævar Arnfjörð Bjarmason | c179837 | 2012-04-30 15:33:12 +0000 | [diff] [blame] | 231 | /* For subsequent UI messages */ |
| 232 | remote_branch = 1; |
Jeff King | 6b145e0 | 2017-03-02 03:23:10 -0500 | [diff] [blame] | 233 | allowed_interpret = INTERPRET_BRANCH_REMOTE; |
Ævar Arnfjörð Bjarmason | c179837 | 2012-04-30 15:33:12 +0000 | [diff] [blame] | 234 | |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 235 | force = 1; |
| 236 | break; |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 237 | case FILTER_REFS_BRANCHES: |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 238 | fmt = "refs/heads/%s"; |
Jeff King | 6b145e0 | 2017-03-02 03:23:10 -0500 | [diff] [blame] | 239 | allowed_interpret = INTERPRET_BRANCH_LOCAL; |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 240 | break; |
| 241 | default: |
Ævar Arnfjörð Bjarmason | 49df4b0 | 2011-02-22 23:41:34 +0000 | [diff] [blame] | 242 | die(_("cannot use -a with -d")); |
Junio C Hamano | f3d985c | 2006-12-17 23:58:16 -0800 | [diff] [blame] | 243 | } |
Phil Hord | 8198907 | 2021-01-20 19:23:32 -0800 | [diff] [blame] | 244 | branch_name_pos = strcspn(fmt, "%"); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 245 | |
Jeff King | eb20e63 | 2022-11-02 01:27:49 -0400 | [diff] [blame] | 246 | if (!force) |
Stefan Beller | 2122f67 | 2018-06-28 18:21:58 -0700 | [diff] [blame] | 247 | head_rev = lookup_commit_reference(the_repository, &head_oid); |
Anders Kaseorg | c8dd491 | 2021-12-01 14:15:43 -0800 | [diff] [blame] | 248 | |
Stefan Beller | a9155c5 | 2017-10-03 15:17:40 -0700 | [diff] [blame] | 249 | for (i = 0; i < argc; i++, strbuf_reset(&bname)) { |
Michael Haggerty | 8bb0455 | 2016-04-25 10:42:19 +0200 | [diff] [blame] | 250 | char *target = NULL; |
René Scharfe | 0fe700e | 2012-10-18 14:07:11 +0200 | [diff] [blame] | 251 | int flags = 0; |
| 252 | |
Jeff King | 6b145e0 | 2017-03-02 03:23:10 -0500 | [diff] [blame] | 253 | strbuf_branchname(&bname, argv[i], allowed_interpret); |
Kazuki Yamaguchi | f292244 | 2016-03-29 18:38:39 +0900 | [diff] [blame] | 254 | free(name); |
| 255 | name = mkpathdup(fmt, bname.buf); |
| 256 | |
| 257 | if (kinds == FILTER_REFS_BRANCHES) { |
Derrick Stolee | b489b9d | 2022-06-14 19:27:32 +0000 | [diff] [blame] | 258 | const char *path; |
| 259 | if ((path = branch_checked_out(name))) { |
Kazuki Yamaguchi | f292244 | 2016-03-29 18:38:39 +0900 | [diff] [blame] | 260 | error(_("Cannot delete branch '%s' " |
| 261 | "checked out at '%s'"), |
Derrick Stolee | b489b9d | 2022-06-14 19:27:32 +0000 | [diff] [blame] | 262 | bname.buf, path); |
Kazuki Yamaguchi | f292244 | 2016-03-29 18:38:39 +0900 | [diff] [blame] | 263 | ret = 1; |
| 264 | continue; |
| 265 | } |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 266 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 267 | |
Michael Haggerty | 8bb0455 | 2016-04-25 10:42:19 +0200 | [diff] [blame] | 268 | target = resolve_refdup(name, |
| 269 | RESOLVE_REF_READING |
| 270 | | RESOLVE_REF_NO_RECURSE |
| 271 | | RESOLVE_REF_ALLOW_BAD_NAME, |
brian m. carlson | 0f2dc72 | 2017-10-15 22:06:55 +0000 | [diff] [blame] | 272 | &oid, &flags); |
Ronnie Sahlberg | 18f29fc | 2014-09-11 10:34:36 -0700 | [diff] [blame] | 273 | if (!target) { |
Clement Mabileau | 4c643fb | 2023-04-05 11:43:20 +0000 | [diff] [blame] | 274 | if (remote_branch) { |
| 275 | error(_("remote-tracking branch '%s' not found."), bname.buf); |
| 276 | } else { |
| 277 | char *virtual_name = mkpathdup(fmt_remotes, bname.buf); |
| 278 | char *virtual_target = resolve_refdup(virtual_name, |
| 279 | RESOLVE_REF_READING |
| 280 | | RESOLVE_REF_NO_RECURSE |
| 281 | | RESOLVE_REF_ALLOW_BAD_NAME, |
| 282 | &oid, &flags); |
| 283 | FREE_AND_NULL(virtual_name); |
| 284 | |
| 285 | if (virtual_target) |
| 286 | error(_("branch '%s' not found.\n" |
| 287 | "Did you forget --remote?"), |
| 288 | bname.buf); |
| 289 | else |
| 290 | error(_("branch '%s' not found."), bname.buf); |
| 291 | FREE_AND_NULL(virtual_target); |
| 292 | } |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 293 | ret = 1; |
| 294 | continue; |
| 295 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 296 | |
Ronnie Sahlberg | d0f810f | 2014-09-03 11:45:43 -0700 | [diff] [blame] | 297 | if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) && |
brian m. carlson | 0c77cd2 | 2017-02-21 23:47:26 +0000 | [diff] [blame] | 298 | check_branch_commit(bname.buf, name, &oid, head_rev, kinds, |
René Scharfe | f5d0e16 | 2012-10-18 14:02:51 +0200 | [diff] [blame] | 299 | force)) { |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 300 | ret = 1; |
Michael Haggerty | 8bb0455 | 2016-04-25 10:42:19 +0200 | [diff] [blame] | 301 | goto next; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Phil Hord | 8198907 | 2021-01-20 19:23:32 -0800 | [diff] [blame] | 304 | item = string_list_append(&refs_to_delete, name); |
| 305 | item->util = xstrdup((flags & REF_ISBROKEN) ? "broken" |
| 306 | : (flags & REF_ISSYMREF) ? target |
Ævar Arnfjörð Bjarmason | d850b7a | 2023-03-28 15:58:46 +0200 | [diff] [blame] | 307 | : repo_find_unique_abbrev(the_repository, &oid, DEFAULT_ABBREV)); |
Michael Haggerty | 8bb0455 | 2016-04-25 10:42:19 +0200 | [diff] [blame] | 308 | |
| 309 | next: |
| 310 | free(target); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 311 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 312 | |
Phil Hord | 8198907 | 2021-01-20 19:23:32 -0800 | [diff] [blame] | 313 | if (delete_refs(NULL, &refs_to_delete, REF_NO_DEREF)) |
| 314 | ret = 1; |
| 315 | |
| 316 | for_each_string_list_item(item, &refs_to_delete) { |
| 317 | char *describe_ref = item->util; |
| 318 | char *name = item->string; |
| 319 | if (!ref_exists(name)) { |
| 320 | char *refname = name + branch_name_pos; |
| 321 | if (!quiet) |
| 322 | printf(remote_branch |
| 323 | ? _("Deleted remote-tracking branch %s (was %s).\n") |
| 324 | : _("Deleted branch %s (was %s).\n"), |
| 325 | name + branch_name_pos, describe_ref); |
| 326 | |
| 327 | delete_branch_config(refname); |
| 328 | } |
| 329 | free(describe_ref); |
| 330 | } |
| 331 | string_list_clear(&refs_to_delete, 0); |
| 332 | |
Jim Meyering | 8e0f700 | 2008-01-31 18:26:32 +0100 | [diff] [blame] | 333 | free(name); |
Stefan Beller | a9155c5 | 2017-10-03 15:17:40 -0700 | [diff] [blame] | 334 | strbuf_release(&bname); |
Quy Tonthat | b8e9a00 | 2006-12-19 09:42:16 +1100 | [diff] [blame] | 335 | |
Stefan Beller | a9155c5 | 2017-10-03 15:17:40 -0700 | [diff] [blame] | 336 | return ret; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 337 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 338 | |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 339 | static int calc_maxwidth(struct ref_array *refs, int remote_bonus) |
Lars Hjemli | b6f637d | 2008-07-26 12:27:25 +0200 | [diff] [blame] | 340 | { |
Karthik Nayak | 1051e40 | 2015-09-23 23:41:06 +0530 | [diff] [blame] | 341 | int i, max = 0; |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 342 | for (i = 0; i < refs->nr; i++) { |
| 343 | struct ref_array_item *it = refs->items[i]; |
Karthik Nayak | aedcb7d | 2015-09-23 23:41:12 +0530 | [diff] [blame] | 344 | const char *desc = it->refname; |
Karthik Nayak | 1051e40 | 2015-09-23 23:41:06 +0530 | [diff] [blame] | 345 | int w; |
| 346 | |
Karthik Nayak | aedcb7d | 2015-09-23 23:41:12 +0530 | [diff] [blame] | 347 | skip_prefix(it->refname, "refs/heads/", &desc); |
| 348 | skip_prefix(it->refname, "refs/remotes/", &desc); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 349 | if (it->kind == FILTER_REFS_DETACHED_HEAD) { |
| 350 | char *head_desc = get_head_description(); |
| 351 | w = utf8_strwidth(head_desc); |
| 352 | free(head_desc); |
| 353 | } else |
| 354 | w = utf8_strwidth(desc); |
Karthik Nayak | aedcb7d | 2015-09-23 23:41:12 +0530 | [diff] [blame] | 355 | |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 356 | if (it->kind == FILTER_REFS_REMOTES) |
Karthik Nayak | 1051e40 | 2015-09-23 23:41:06 +0530 | [diff] [blame] | 357 | w += remote_bonus; |
| 358 | if (w > max) |
| 359 | max = w; |
Lars Hjemli | b6f637d | 2008-07-26 12:27:25 +0200 | [diff] [blame] | 360 | } |
Karthik Nayak | 1051e40 | 2015-09-23 23:41:06 +0530 | [diff] [blame] | 361 | return max; |
Lars Hjemli | b6f637d | 2008-07-26 12:27:25 +0200 | [diff] [blame] | 362 | } |
| 363 | |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 364 | static const char *quote_literal_for_format(const char *s) |
| 365 | { |
| 366 | static struct strbuf buf = STRBUF_INIT; |
| 367 | |
| 368 | strbuf_reset(&buf); |
René Scharfe | 44ccb33 | 2023-06-17 22:41:44 +0200 | [diff] [blame^] | 369 | while (strbuf_expand_step(&buf, &s)) |
| 370 | strbuf_addstr(&buf, "%%"); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 371 | return buf.buf; |
| 372 | } |
| 373 | |
| 374 | static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix) |
| 375 | { |
| 376 | struct strbuf fmt = STRBUF_INIT; |
| 377 | struct strbuf local = STRBUF_INIT; |
| 378 | struct strbuf remote = STRBUF_INIT; |
| 379 | |
Nickolai Belakovski | ab31381 | 2019-04-28 22:19:43 -0700 | [diff] [blame] | 380 | strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)%%(if)%%(worktreepath)%%(then)+ %s%%(else) %s%%(end)%%(end)", |
| 381 | branch_get_color(BRANCH_COLOR_CURRENT), |
| 382 | branch_get_color(BRANCH_COLOR_WORKTREE), |
| 383 | branch_get_color(BRANCH_COLOR_LOCAL)); |
Jeff King | 7ca260a | 2017-07-09 06:00:45 -0400 | [diff] [blame] | 384 | strbuf_addf(&remote, " %s", |
| 385 | branch_get_color(BRANCH_COLOR_REMOTE)); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 386 | |
| 387 | if (filter->verbose) { |
Junio C Hamano | ac5bbc0 | 2017-03-08 14:13:09 -0800 | [diff] [blame] | 388 | struct strbuf obname = STRBUF_INIT; |
| 389 | |
| 390 | if (filter->abbrev < 0) |
| 391 | strbuf_addf(&obname, "%%(objectname:short)"); |
| 392 | else if (!filter->abbrev) |
| 393 | strbuf_addf(&obname, "%%(objectname)"); |
| 394 | else |
| 395 | strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev); |
| 396 | |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 397 | strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth); |
René Scharfe | 72d4a9a | 2017-10-01 16:44:20 +0200 | [diff] [blame] | 398 | strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET)); |
Junio C Hamano | ac5bbc0 | 2017-03-08 14:13:09 -0800 | [diff] [blame] | 399 | strbuf_addf(&local, " %s ", obname.buf); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 400 | |
| 401 | if (filter->verbose > 1) |
Nickolai Belakovski | 6e93814 | 2019-04-28 22:19:44 -0700 | [diff] [blame] | 402 | { |
| 403 | strbuf_addf(&local, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)", |
| 404 | branch_get_color(BRANCH_COLOR_WORKTREE), branch_get_color(BRANCH_COLOR_RESET)); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 405 | strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)" |
| 406 | "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)", |
| 407 | branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET)); |
Nickolai Belakovski | 6e93814 | 2019-04-28 22:19:44 -0700 | [diff] [blame] | 408 | } |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 409 | else |
| 410 | strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)"); |
| 411 | |
Jeff King | 7ca260a | 2017-07-09 06:00:45 -0400 | [diff] [blame] | 412 | strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s" |
Junio C Hamano | ac5bbc0 | 2017-03-08 14:13:09 -0800 | [diff] [blame] | 413 | "%%(if)%%(symref)%%(then) -> %%(symref:short)" |
| 414 | "%%(else) %s %%(contents:subject)%%(end)", |
Jeff King | 7ca260a | 2017-07-09 06:00:45 -0400 | [diff] [blame] | 415 | maxwidth, quote_literal_for_format(remote_prefix), |
Junio C Hamano | ac5bbc0 | 2017-03-08 14:13:09 -0800 | [diff] [blame] | 416 | branch_get_color(BRANCH_COLOR_RESET), obname.buf); |
| 417 | strbuf_release(&obname); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 418 | } else { |
| 419 | strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", |
| 420 | branch_get_color(BRANCH_COLOR_RESET)); |
Jeff King | 7ca260a | 2017-07-09 06:00:45 -0400 | [diff] [blame] | 421 | strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", |
| 422 | quote_literal_for_format(remote_prefix), |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 423 | branch_get_color(BRANCH_COLOR_RESET)); |
| 424 | } |
| 425 | |
| 426 | strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf); |
| 427 | |
| 428 | strbuf_release(&local); |
| 429 | strbuf_release(&remote); |
| 430 | return strbuf_detach(&fmt, NULL); |
| 431 | } |
| 432 | |
Ævar Arnfjörð Bjarmason | d72d4f9 | 2021-10-20 20:27:21 +0200 | [diff] [blame] | 433 | static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, |
| 434 | struct ref_format *format, struct string_list *output) |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 435 | { |
| 436 | int i; |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 437 | struct ref_array array; |
ZheNing Hu | 844c3f0 | 2021-04-20 16:52:11 +0000 | [diff] [blame] | 438 | struct strbuf out = STRBUF_INIT; |
| 439 | struct strbuf err = STRBUF_INIT; |
Karthik Nayak | 1051e40 | 2015-09-23 23:41:06 +0530 | [diff] [blame] | 440 | int maxwidth = 0; |
| 441 | const char *remote_prefix = ""; |
Karthik Nayak | 3d9e4ce | 2017-01-10 14:19:53 +0530 | [diff] [blame] | 442 | char *to_free = NULL; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 443 | |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 444 | /* |
Karthik Nayak | 1051e40 | 2015-09-23 23:41:06 +0530 | [diff] [blame] | 445 | * If we are listing more than just remote branches, |
| 446 | * then remote branches will have a "remotes/" prefix. |
| 447 | * We need to account for this in the width. |
Karthik Nayak | 35257aa | 2015-07-07 21:36:12 +0530 | [diff] [blame] | 448 | */ |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 449 | if (filter->kind != FILTER_REFS_REMOTES) |
Karthik Nayak | 1051e40 | 2015-09-23 23:41:06 +0530 | [diff] [blame] | 450 | remote_prefix = "remotes/"; |
Carlos Martín Nieto | 6c41e97 | 2012-02-27 16:11:53 +0100 | [diff] [blame] | 451 | |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 452 | memset(&array, 0, sizeof(array)); |
Stefan Beller | 81c3ce3 | 2014-08-10 23:33:26 +0200 | [diff] [blame] | 453 | |
Jeff King | 1763334 | 2021-09-24 14:48:05 -0400 | [diff] [blame] | 454 | filter_refs(&array, filter, filter->kind); |
Jeff King | 8376a70 | 2014-09-18 06:49:43 -0400 | [diff] [blame] | 455 | |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 456 | if (filter->verbose) |
| 457 | maxwidth = calc_maxwidth(&array, strlen(remote_prefix)); |
Jeff King | 8376a70 | 2014-09-18 06:49:43 -0400 | [diff] [blame] | 458 | |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 459 | if (!format->format) |
| 460 | format->format = to_free = build_format(filter, maxwidth, remote_prefix); |
Jeff King | 11b087a | 2017-07-13 11:09:32 -0400 | [diff] [blame] | 461 | format->use_color = branch_use_color; |
Jeff King | 2eda010 | 2017-07-13 10:56:10 -0400 | [diff] [blame] | 462 | |
| 463 | if (verify_ref_format(format)) |
| 464 | die(_("unable to parse format string")); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 465 | |
Derrick Stolee | 49abcd2 | 2023-03-20 11:26:54 +0000 | [diff] [blame] | 466 | filter_ahead_behind(the_repository, format, &array); |
Karthik Nayak | aedcb7d | 2015-09-23 23:41:12 +0530 | [diff] [blame] | 467 | ref_array_sort(sorting, &array); |
Jeff King | 8376a70 | 2014-09-18 06:49:43 -0400 | [diff] [blame] | 468 | |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 469 | for (i = 0; i < array.nr; i++) { |
ZheNing Hu | 844c3f0 | 2021-04-20 16:52:11 +0000 | [diff] [blame] | 470 | strbuf_reset(&err); |
| 471 | strbuf_reset(&out); |
Olga Telezhnaya | 3019eca | 2018-03-29 12:49:45 +0000 | [diff] [blame] | 472 | if (format_ref_array_item(array.items[i], format, &out, &err)) |
| 473 | die("%s", err.buf); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 474 | if (column_active(colopts)) { |
| 475 | assert(!filter->verbose && "--column and --verbose are incompatible"); |
| 476 | /* format to a string_list to let print_columns() do its job */ |
Ævar Arnfjörð Bjarmason | d72d4f9 | 2021-10-20 20:27:21 +0200 | [diff] [blame] | 477 | string_list_append(output, out.buf); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 478 | } else { |
| 479 | fwrite(out.buf, 1, out.len, stdout); |
Øystein Walle | aabfdc9 | 2023-04-07 19:53:16 +0200 | [diff] [blame] | 480 | if (out.len || !omit_empty) |
| 481 | putchar('\n'); |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 482 | } |
Karthik Nayak | 949af06 | 2017-01-10 14:19:52 +0530 | [diff] [blame] | 483 | } |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 484 | |
ZheNing Hu | 844c3f0 | 2021-04-20 16:52:11 +0000 | [diff] [blame] | 485 | strbuf_release(&err); |
| 486 | strbuf_release(&out); |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 487 | ref_array_clear(&array); |
Karthik Nayak | 3d9e4ce | 2017-01-10 14:19:53 +0530 | [diff] [blame] | 488 | free(to_free); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 489 | } |
| 490 | |
Daniels Umanovskis | 0ecb1fc | 2018-10-25 21:04:21 +0200 | [diff] [blame] | 491 | static void print_current_branch_name(void) |
| 492 | { |
| 493 | int flags; |
| 494 | const char *refname = resolve_ref_unsafe("HEAD", 0, NULL, &flags); |
| 495 | const char *shortname; |
| 496 | if (!refname) |
| 497 | die(_("could not resolve HEAD")); |
| 498 | else if (!(flags & REF_ISSYMREF)) |
| 499 | return; |
| 500 | else if (skip_prefix(refname, "refs/heads/", &shortname)) |
| 501 | puts(shortname); |
| 502 | else |
| 503 | die(_("HEAD (%s) points outside of refs/heads/"), refname); |
| 504 | } |
| 505 | |
Rubén Justo | d7f4ca6 | 2023-03-27 00:33:09 +0200 | [diff] [blame] | 506 | static void reject_rebase_or_bisect_branch(struct worktree **worktrees, |
| 507 | const char *target) |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 508 | { |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 509 | int i; |
| 510 | |
| 511 | for (i = 0; worktrees[i]; i++) { |
| 512 | struct worktree *wt = worktrees[i]; |
| 513 | |
| 514 | if (!wt->is_detached) |
| 515 | continue; |
| 516 | |
| 517 | if (is_worktree_being_rebased(wt, target)) |
| 518 | die(_("Branch %s is being rebased at %s"), |
| 519 | target, wt->path); |
| 520 | |
| 521 | if (is_worktree_being_bisected(wt, target)) |
| 522 | die(_("Branch %s is being bisected at %s"), |
| 523 | target, wt->path); |
| 524 | } |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 525 | } |
| 526 | |
Rubén Justo | 2e8af49 | 2023-03-27 00:33:02 +0200 | [diff] [blame] | 527 | /* |
| 528 | * Update all per-worktree HEADs pointing at the old ref to point the new ref. |
| 529 | * This will be used when renaming a branch. Returns 0 if successful, non-zero |
| 530 | * otherwise. |
| 531 | */ |
Rubén Justo | d7f4ca6 | 2023-03-27 00:33:09 +0200 | [diff] [blame] | 532 | static int replace_each_worktree_head_symref(struct worktree **worktrees, |
| 533 | const char *oldref, const char *newref, |
Rubén Justo | 2e8af49 | 2023-03-27 00:33:02 +0200 | [diff] [blame] | 534 | const char *logmsg) |
| 535 | { |
| 536 | int ret = 0; |
Rubén Justo | 2e8af49 | 2023-03-27 00:33:02 +0200 | [diff] [blame] | 537 | int i; |
| 538 | |
| 539 | for (i = 0; worktrees[i]; i++) { |
| 540 | struct ref_store *refs; |
| 541 | |
| 542 | if (worktrees[i]->is_detached) |
| 543 | continue; |
| 544 | if (!worktrees[i]->head_ref) |
| 545 | continue; |
| 546 | if (strcmp(oldref, worktrees[i]->head_ref)) |
| 547 | continue; |
| 548 | |
| 549 | refs = get_worktree_ref_store(worktrees[i]); |
| 550 | if (refs_create_symref(refs, "HEAD", newref, logmsg)) |
| 551 | ret = error(_("HEAD of working tree %s is not updated"), |
| 552 | worktrees[i]->path); |
| 553 | } |
| 554 | |
Rubén Justo | 2e8af49 | 2023-03-27 00:33:02 +0200 | [diff] [blame] | 555 | return ret; |
| 556 | } |
| 557 | |
Rubén Justo | 7a6ccdf | 2023-03-27 00:33:17 +0200 | [diff] [blame] | 558 | #define IS_HEAD 1 |
Rubén Justo | a675ad1 | 2023-03-27 00:33:27 +0200 | [diff] [blame] | 559 | #define IS_ORPHAN 2 |
Rubén Justo | 7a6ccdf | 2023-03-27 00:33:17 +0200 | [diff] [blame] | 560 | |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 561 | static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force) |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 562 | { |
Miklos Vajna | da3f78a | 2008-11-17 21:48:37 +0100 | [diff] [blame] | 563 | struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; |
Miklos Vajna | da3f78a | 2008-11-17 21:48:37 +0100 | [diff] [blame] | 564 | struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; |
Kaartic Sivaraam | 255073c | 2017-12-01 11:29:33 +0530 | [diff] [blame] | 565 | const char *interpreted_oldname = NULL; |
| 566 | const char *interpreted_newname = NULL; |
Rubén Justo | 7a6ccdf | 2023-03-27 00:33:17 +0200 | [diff] [blame] | 567 | int recovery = 0, oldref_usage = 0; |
Rubén Justo | d7f4ca6 | 2023-03-27 00:33:09 +0200 | [diff] [blame] | 568 | struct worktree **worktrees = get_worktrees(); |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 569 | |
Junio C Hamano | cbdffe4 | 2009-03-21 13:27:31 -0700 | [diff] [blame] | 570 | if (strbuf_check_branch_ref(&oldref, oldname)) { |
| 571 | /* |
| 572 | * Bad name --- this could be an attempt to rename a |
| 573 | * ref that we used to allow to be created by accident. |
| 574 | */ |
Nguyễn Thái Ngọc Duy | c689332 | 2011-11-13 17:22:14 +0700 | [diff] [blame] | 575 | if (ref_exists(oldref.buf)) |
Junio C Hamano | cbdffe4 | 2009-03-21 13:27:31 -0700 | [diff] [blame] | 576 | recovery = 1; |
| 577 | else |
Ævar Arnfjörð Bjarmason | 49df4b0 | 2011-02-22 23:41:34 +0000 | [diff] [blame] | 578 | die(_("Invalid branch name: '%s'"), oldname); |
Junio C Hamano | cbdffe4 | 2009-03-21 13:27:31 -0700 | [diff] [blame] | 579 | } |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 580 | |
Rubén Justo | 7a6ccdf | 2023-03-27 00:33:17 +0200 | [diff] [blame] | 581 | for (int i = 0; worktrees[i]; i++) { |
| 582 | struct worktree *wt = worktrees[i]; |
| 583 | |
| 584 | if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) { |
| 585 | oldref_usage |= IS_HEAD; |
Rubén Justo | a675ad1 | 2023-03-27 00:33:27 +0200 | [diff] [blame] | 586 | if (is_null_oid(&wt->head_oid)) |
| 587 | oldref_usage |= IS_ORPHAN; |
Rubén Justo | 7a6ccdf | 2023-03-27 00:33:17 +0200 | [diff] [blame] | 588 | break; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | if ((copy || !(oldref_usage & IS_HEAD)) && !ref_exists(oldref.buf)) { |
| 593 | if (oldref_usage & IS_HEAD) |
Rubén Justo | bcfc82b | 2022-10-08 02:39:43 +0200 | [diff] [blame] | 594 | die(_("No commit on branch '%s' yet."), oldname); |
| 595 | else |
| 596 | die(_("No branch named '%s'."), oldname); |
| 597 | } |
| 598 | |
Jonathan Nieder | 3f59481 | 2011-11-25 20:30:02 -0600 | [diff] [blame] | 599 | /* |
| 600 | * A command like "git branch -M currentbranch currentbranch" cannot |
| 601 | * cause the worktree to become inconsistent with HEAD, so allow it. |
| 602 | */ |
Junio C Hamano | bc1c9c0 | 2017-10-13 13:45:40 +0900 | [diff] [blame] | 603 | if (!strcmp(oldname, newname)) |
| 604 | validate_branchname(newname, &newref); |
| 605 | else |
| 606 | validate_new_branchname(newname, &newref, force); |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 607 | |
Rubén Justo | d7f4ca6 | 2023-03-27 00:33:09 +0200 | [diff] [blame] | 608 | reject_rebase_or_bisect_branch(worktrees, oldref.buf); |
Nguyễn Thái Ngọc Duy | 14ace5b | 2016-04-22 20:01:36 +0700 | [diff] [blame] | 609 | |
Kaartic Sivaraam | 255073c | 2017-12-01 11:29:33 +0530 | [diff] [blame] | 610 | if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) || |
| 611 | !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) { |
Johannes Schindelin | 033abf9 | 2018-05-02 11:38:39 +0200 | [diff] [blame] | 612 | BUG("expected prefix missing for refs"); |
Kaartic Sivaraam | 255073c | 2017-12-01 11:29:33 +0530 | [diff] [blame] | 613 | } |
| 614 | |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 615 | if (copy) |
| 616 | strbuf_addf(&logmsg, "Branch: copied %s to %s", |
| 617 | oldref.buf, newref.buf); |
| 618 | else |
| 619 | strbuf_addf(&logmsg, "Branch: renamed %s to %s", |
| 620 | oldref.buf, newref.buf); |
Lars Hjemli | 678d0f4 | 2006-11-30 03:16:56 +0100 | [diff] [blame] | 621 | |
Rubén Justo | a675ad1 | 2023-03-27 00:33:27 +0200 | [diff] [blame] | 622 | if (!copy && !(oldref_usage & IS_ORPHAN) && |
Johannes Schindelin | cfaff3a | 2020-12-11 11:36:55 +0000 | [diff] [blame] | 623 | rename_ref(oldref.buf, newref.buf, logmsg.buf)) |
Ævar Arnfjörð Bjarmason | 49df4b0 | 2011-02-22 23:41:34 +0000 | [diff] [blame] | 624 | die(_("Branch rename failed")); |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 625 | if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf)) |
| 626 | die(_("Branch copy failed")); |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 627 | |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 628 | if (recovery) { |
| 629 | if (copy) |
Kaartic Sivaraam | a48ebe9 | 2017-11-18 22:56:47 +0530 | [diff] [blame] | 630 | warning(_("Created a copy of a misnamed branch '%s'"), |
Kaartic Sivaraam | 255073c | 2017-12-01 11:29:33 +0530 | [diff] [blame] | 631 | interpreted_oldname); |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 632 | else |
| 633 | warning(_("Renamed a misnamed branch '%s' away"), |
Kaartic Sivaraam | 255073c | 2017-12-01 11:29:33 +0530 | [diff] [blame] | 634 | interpreted_oldname); |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 635 | } |
Junio C Hamano | cbdffe4 | 2009-03-21 13:27:31 -0700 | [diff] [blame] | 636 | |
Rubén Justo | 3521c63 | 2023-03-27 00:33:40 +0200 | [diff] [blame] | 637 | if (!copy && (oldref_usage & IS_HEAD) && |
Rubén Justo | d7f4ca6 | 2023-03-27 00:33:09 +0200 | [diff] [blame] | 638 | replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf, |
| 639 | logmsg.buf)) |
Ævar Arnfjörð Bjarmason | 49df4b0 | 2011-02-22 23:41:34 +0000 | [diff] [blame] | 640 | die(_("Branch renamed to %s, but HEAD is not updated!"), newname); |
Lars Hjemli | 19eba15 | 2007-04-06 14:13:00 +0200 | [diff] [blame] | 641 | |
Kyle Meyer | 39ee4c6 | 2017-02-20 20:10:35 -0500 | [diff] [blame] | 642 | strbuf_release(&logmsg); |
| 643 | |
Kaartic Sivaraam | 255073c | 2017-12-01 11:29:33 +0530 | [diff] [blame] | 644 | strbuf_addf(&oldsection, "branch.%s", interpreted_oldname); |
Kaartic Sivaraam | 255073c | 2017-12-01 11:29:33 +0530 | [diff] [blame] | 645 | strbuf_addf(&newsection, "branch.%s", interpreted_newname); |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 646 | if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0) |
Ævar Arnfjörð Bjarmason | 49df4b0 | 2011-02-22 23:41:34 +0000 | [diff] [blame] | 647 | die(_("Branch is renamed, but update of config-file failed")); |
Rubén Justo | cfbd173 | 2022-11-17 02:36:52 +0100 | [diff] [blame] | 648 | if (copy && strcmp(interpreted_oldname, interpreted_newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0) |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 649 | die(_("Branch is copied, but update of config-file failed")); |
Rubén Justo | cfbd173 | 2022-11-17 02:36:52 +0100 | [diff] [blame] | 650 | strbuf_release(&oldref); |
| 651 | strbuf_release(&newref); |
Miklos Vajna | da3f78a | 2008-11-17 21:48:37 +0100 | [diff] [blame] | 652 | strbuf_release(&oldsection); |
| 653 | strbuf_release(&newsection); |
Rubén Justo | d7f4ca6 | 2023-03-27 00:33:09 +0200 | [diff] [blame] | 654 | free_worktrees(worktrees); |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 655 | } |
| 656 | |
Jeff King | c10388c | 2017-04-20 17:08:41 -0400 | [diff] [blame] | 657 | static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION") |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 658 | |
| 659 | static int edit_branch_description(const char *branch_name) |
| 660 | { |
Junio C Hamano | e288b3d | 2022-09-30 11:06:22 -0700 | [diff] [blame] | 661 | int exists; |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 662 | struct strbuf buf = STRBUF_INIT; |
| 663 | struct strbuf name = STRBUF_INIT; |
| 664 | |
Junio C Hamano | e288b3d | 2022-09-30 11:06:22 -0700 | [diff] [blame] | 665 | exists = !read_branch_desc(&buf, branch_name); |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 666 | if (!buf.len || buf.buf[buf.len-1] != '\n') |
| 667 | strbuf_addch(&buf, '\n'); |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 668 | strbuf_commented_addf(&buf, |
Vasco Almeida | b18237f | 2016-06-17 21:54:15 +0000 | [diff] [blame] | 669 | _("Please edit the description for the branch\n" |
| 670 | " %s\n" |
| 671 | "Lines starting with '%c' will be stripped.\n"), |
Junio C Hamano | eff80a9 | 2013-01-16 20:18:48 +0100 | [diff] [blame] | 672 | branch_name, comment_line_char); |
Jeff King | c10388c | 2017-04-20 17:08:41 -0400 | [diff] [blame] | 673 | write_file_buf(edit_description(), buf.buf, buf.len); |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 674 | strbuf_reset(&buf); |
Jeff King | c10388c | 2017-04-20 17:08:41 -0400 | [diff] [blame] | 675 | if (launch_editor(edit_description(), &buf, NULL)) { |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 676 | strbuf_release(&buf); |
| 677 | return -1; |
| 678 | } |
Tobias Klauser | 63af4a8 | 2015-10-16 17:16:42 +0200 | [diff] [blame] | 679 | strbuf_stripspace(&buf, 1); |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 680 | |
| 681 | strbuf_addf(&name, "branch.%s.description", branch_name); |
Junio C Hamano | e288b3d | 2022-09-30 11:06:22 -0700 | [diff] [blame] | 682 | if (buf.len || exists) |
| 683 | git_config_set(name.buf, buf.len ? buf.buf : NULL); |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 684 | strbuf_release(&name); |
| 685 | strbuf_release(&buf); |
| 686 | |
Patrick Steinhardt | bd25f89 | 2016-02-22 12:23:25 +0100 | [diff] [blame] | 687 | return 0; |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 688 | } |
| 689 | |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 690 | int cmd_branch(int argc, const char **argv, const char *prefix) |
| 691 | { |
Glen Choo | 6e0a2ca | 2022-01-28 16:04:44 -0800 | [diff] [blame] | 692 | /* possible actions */ |
| 693 | int delete = 0, rename = 0, copy = 0, list = 0, |
| 694 | unset_upstream = 0, show_current = 0, edit_description = 0; |
Carlos Martín Nieto | 6183d82 | 2012-08-20 15:47:38 +0200 | [diff] [blame] | 695 | const char *new_upstream = NULL; |
Glen Choo | 6e0a2ca | 2022-01-28 16:04:44 -0800 | [diff] [blame] | 696 | int noncreate_actions = 0; |
| 697 | /* possible options */ |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 698 | int reflog = 0, quiet = 0, icase = 0, force = 0, |
| 699 | recurse_submodules_explicit = 0; |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 700 | enum branch_track track; |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 701 | struct ref_filter filter; |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 702 | static struct ref_sorting *sorting; |
| 703 | struct string_list sorting_options = STRING_LIST_INIT_DUP; |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 704 | struct ref_format format = REF_FORMAT_INIT; |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 705 | |
| 706 | struct option options[] = { |
Nguyễn Thái Ngọc Duy | 24a8521 | 2012-08-20 19:31:55 +0700 | [diff] [blame] | 707 | OPT_GROUP(N_("Generic options")), |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 708 | OPT__VERBOSE(&filter.verbose, |
Nguyễn Thái Ngọc Duy | 24a8521 | 2012-08-20 19:31:55 +0700 | [diff] [blame] | 709 | N_("show hash and subject, give twice for upstream branch")), |
| 710 | OPT__QUIET(&quiet, N_("suppress informational messages")), |
René Scharfe | 6327f0e | 2022-01-20 13:35:54 +0100 | [diff] [blame] | 711 | OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)", |
Josh Steadmon | d311566 | 2021-12-20 19:30:23 -0800 | [diff] [blame] | 712 | N_("set branch tracking configuration"), |
Josh Steadmon | 15f0028 | 2022-01-18 12:49:46 -0800 | [diff] [blame] | 713 | PARSE_OPT_OPTARG, |
Josh Steadmon | d311566 | 2021-12-20 19:30:23 -0800 | [diff] [blame] | 714 | parse_opt_tracking_mode), |
Nguyễn Thái Ngọc Duy | 3e4a67b | 2018-05-20 17:42:58 +0200 | [diff] [blame] | 715 | OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"), |
| 716 | BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN), |
Vasco Almeida | ab86885 | 2016-04-08 20:02:45 +0000 | [diff] [blame] | 717 | OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")), |
Dimitriy Ryazantcev | 11de8dd | 2019-12-08 11:26:47 +0200 | [diff] [blame] | 718 | OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("unset the upstream info")), |
Nguyễn Thái Ngọc Duy | 24a8521 | 2012-08-20 19:31:55 +0700 | [diff] [blame] | 719 | OPT__COLOR(&branch_use_color, N_("use colored output")), |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 720 | OPT_SET_INT('r', "remotes", &filter.kind, N_("act on remote-tracking branches"), |
| 721 | FILTER_REFS_REMOTES), |
| 722 | OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")), |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 723 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")), |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 724 | OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")), |
Ævar Arnfjörð Bjarmason | ac3f5a3 | 2017-03-24 18:40:57 +0000 | [diff] [blame] | 725 | OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")), |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 726 | OPT__ABBREV(&filter.abbrev), |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 727 | |
Nguyễn Thái Ngọc Duy | 24a8521 | 2012-08-20 19:31:55 +0700 | [diff] [blame] | 728 | OPT_GROUP(N_("Specific git-branch actions:")), |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 729 | OPT_SET_INT('a', "all", &filter.kind, N_("list both remote-tracking and local branches"), |
| 730 | FILTER_REFS_REMOTES | FILTER_REFS_BRANCHES), |
Nguyễn Thái Ngọc Duy | 24a8521 | 2012-08-20 19:31:55 +0700 | [diff] [blame] | 731 | OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1), |
| 732 | OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2), |
| 733 | OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1), |
| 734 | OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2), |
Øystein Walle | aabfdc9 | 2023-04-07 19:53:16 +0200 | [diff] [blame] | 735 | OPT_BOOL(0, "omit-empty", &omit_empty, |
| 736 | N_("do not output a newline after empty formatted refs")), |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 737 | OPT_BIT('c', "copy", ©, N_("copy a branch and its reflog"), 1), |
| 738 | OPT_BIT('C', NULL, ©, N_("copy a branch, even if target exists"), 2), |
Jeff King | a15d598 | 2018-06-22 05:24:59 -0400 | [diff] [blame] | 739 | OPT_BOOL('l', "list", &list, N_("list branch names")), |
Daniels Umanovskis | 0ecb1fc | 2018-10-25 21:04:21 +0200 | [diff] [blame] | 740 | OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")), |
Jeff King | 055930b | 2018-06-22 05:24:14 -0400 | [diff] [blame] | 741 | OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")), |
Stefan Beller | d5d09d4 | 2013-08-03 13:51:19 +0200 | [diff] [blame] | 742 | OPT_BOOL(0, "edit-description", &edit_description, |
| 743 | N_("edit the description for the branch")), |
Nguyễn Thái Ngọc Duy | c01b56a | 2018-02-09 18:01:47 +0700 | [diff] [blame] | 744 | OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE), |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 745 | OPT_MERGED(&filter, N_("print only branches that are merged")), |
| 746 | OPT_NO_MERGED(&filter, N_("print only branches that are not merged")), |
Nguyễn Thái Ngọc Duy | 24a8521 | 2012-08-20 19:31:55 +0700 | [diff] [blame] | 747 | OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")), |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 748 | OPT_REF_SORT(&sorting_options), |
Denton Liu | 203c853 | 2020-04-28 04:36:28 -0400 | [diff] [blame] | 749 | OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"), |
| 750 | N_("print only branches of the object"), parse_opt_object_name), |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 751 | OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 752 | OPT_BOOL(0, "recurse-submodules", &recurse_submodules_explicit, N_("recurse through submodules")), |
Jeff King | 4a68e36 | 2017-07-13 11:01:18 -0400 | [diff] [blame] | 753 | OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 754 | OPT_END(), |
| 755 | }; |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 756 | |
Karthik Nayak | 56b4360 | 2017-01-10 14:19:51 +0530 | [diff] [blame] | 757 | setup_ref_filter_porcelain_msg(); |
| 758 | |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 759 | memset(&filter, 0, sizeof(filter)); |
| 760 | filter.kind = FILTER_REFS_BRANCHES; |
| 761 | filter.abbrev = -1; |
| 762 | |
Nguyễn Thái Ngọc Duy | 1dacfbc | 2010-10-22 01:42:58 -0500 | [diff] [blame] | 763 | if (argc == 2 && !strcmp(argv[1], "-h")) |
| 764 | usage_with_options(builtin_branch_usage, options); |
| 765 | |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 766 | git_config(git_branch_config, &sorting_options); |
Matthias Kestenholz | 6b2f2d9 | 2008-02-18 08:26:03 +0100 | [diff] [blame] | 767 | |
Jay Soffian | 9ed36cf | 2008-02-19 11:24:37 -0500 | [diff] [blame] | 768 | track = git_branch_track; |
Lars Hjemli | c976d41 | 2006-11-28 15:47:40 +0100 | [diff] [blame] | 769 | |
brian m. carlson | 0f2dc72 | 2017-10-15 22:06:55 +0000 | [diff] [blame] | 770 | head = resolve_refdup("HEAD", 0, &head_oid, NULL); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 771 | if (!head) |
Ævar Arnfjörð Bjarmason | 49df4b0 | 2011-02-22 23:41:34 +0000 | [diff] [blame] | 772 | die(_("Failed to resolve HEAD as a valid ref.")); |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 773 | if (!strcmp(head, "HEAD")) |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 774 | filter.detached = 1; |
René Scharfe | e3f1da9 | 2014-10-04 20:54:50 +0200 | [diff] [blame] | 775 | else if (!skip_prefix(head, "refs/heads/", &head)) |
| 776 | die(_("HEAD not found below refs/heads!")); |
Nguyễn Thái Ngọc Duy | ebe31ef | 2012-04-13 17:54:38 +0700 | [diff] [blame] | 777 | |
Stephen Boyd | 3778292 | 2009-05-23 11:53:12 -0700 | [diff] [blame] | 778 | argc = parse_options(argc, argv, prefix, options, builtin_branch_usage, |
| 779 | 0); |
Michael J Gruber | cddd127 | 2011-08-28 16:54:31 +0200 | [diff] [blame] | 780 | |
Daniels Umanovskis | 0ecb1fc | 2018-10-25 21:04:21 +0200 | [diff] [blame] | 781 | if (!delete && !rename && !copy && !edit_description && !new_upstream && |
| 782 | !show_current && !unset_upstream && argc == 0) |
Michael J Gruber | cddd127 | 2011-08-28 16:54:31 +0200 | [diff] [blame] | 783 | list = 1; |
| 784 | |
Aaron Lipman | 21bf933 | 2020-09-15 22:08:40 -0400 | [diff] [blame] | 785 | if (filter.with_commit || filter.no_commit || |
| 786 | filter.reachable_from || filter.unreachable_from || filter.points_at.nr) |
Jeff King | d040350 | 2013-01-31 01:46:11 -0500 | [diff] [blame] | 787 | list = 1; |
| 788 | |
Glen Choo | 6e0a2ca | 2022-01-28 16:04:44 -0800 | [diff] [blame] | 789 | noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream + |
| 790 | !!show_current + !!list + !!edit_description + |
| 791 | !!unset_upstream; |
| 792 | if (noncreate_actions > 1) |
Junio C Hamano | 049716b | 2008-07-08 17:55:47 -0700 | [diff] [blame] | 793 | usage_with_options(builtin_branch_usage, options); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 794 | |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 795 | if (recurse_submodules_explicit) { |
| 796 | if (!submodule_propagate_branches) |
| 797 | die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled")); |
| 798 | if (noncreate_actions) |
| 799 | die(_("--recurse-submodules can only be used to create branches")); |
| 800 | } |
| 801 | |
| 802 | recurse_submodules = |
| 803 | (recurse_submodules || recurse_submodules_explicit) && |
| 804 | submodule_propagate_branches; |
| 805 | |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 806 | if (filter.abbrev == -1) |
| 807 | filter.abbrev = DEFAULT_ABBREV; |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 808 | filter.ignore_case = icase; |
| 809 | |
Nguyễn Thái Ngọc Duy | ebe31ef | 2012-04-13 17:54:38 +0700 | [diff] [blame] | 810 | finalize_colopts(&colopts, -1); |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 811 | if (filter.verbose) { |
Nguyễn Thái Ngọc Duy | ebe31ef | 2012-04-13 17:54:38 +0700 | [diff] [blame] | 812 | if (explicitly_enable_column(colopts)) |
Jean-Noël Avila | 12909b6 | 2022-01-05 20:02:16 +0000 | [diff] [blame] | 813 | die(_("options '%s' and '%s' cannot be used together"), "--column", "--verbose"); |
Nguyễn Thái Ngọc Duy | ebe31ef | 2012-04-13 17:54:38 +0700 | [diff] [blame] | 814 | colopts = 0; |
| 815 | } |
Namhyung Kim | b792c06 | 2011-07-01 15:06:08 +0900 | [diff] [blame] | 816 | |
Michael J Gruber | 356e91f | 2014-12-08 17:28:45 +0100 | [diff] [blame] | 817 | if (force) { |
| 818 | delete *= 2; |
| 819 | rename *= 2; |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 820 | copy *= 2; |
Michael J Gruber | 356e91f | 2014-12-08 17:28:45 +0100 | [diff] [blame] | 821 | } |
| 822 | |
Martin Ågren | d74b541 | 2017-11-19 16:03:49 +0100 | [diff] [blame] | 823 | if (list) |
Martin Ågren | 0ae19de | 2017-11-19 16:03:50 +0100 | [diff] [blame] | 824 | setup_auto_pager("branch", 1); |
Martin Ågren | d74b541 | 2017-11-19 16:03:49 +0100 | [diff] [blame] | 825 | |
Nguyễn Thái Ngọc Duy | 640d040 | 2013-01-28 08:18:14 +0700 | [diff] [blame] | 826 | if (delete) { |
| 827 | if (!argc) |
| 828 | die(_("branch name required")); |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 829 | return delete_branches(argc, argv, delete > 1, filter.kind, quiet); |
Daniels Umanovskis | 0ecb1fc | 2018-10-25 21:04:21 +0200 | [diff] [blame] | 830 | } else if (show_current) { |
| 831 | print_current_branch_name(); |
| 832 | return 0; |
Nguyễn Thái Ngọc Duy | 640d040 | 2013-01-28 08:18:14 +0700 | [diff] [blame] | 833 | } else if (list) { |
Ævar Arnfjörð Bjarmason | ffdd02a | 2021-01-06 11:01:35 +0100 | [diff] [blame] | 834 | /* git branch --list also shows HEAD when it is detached */ |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 835 | if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached) |
| 836 | filter.kind |= FILTER_REFS_DETACHED_HEAD; |
| 837 | filter.name_patterns = argv; |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 838 | /* |
| 839 | * If no sorting parameter is given then we default to sorting |
| 840 | * by 'refname'. This would give us an alphabetically sorted |
| 841 | * array with the 'HEAD' ref at the beginning followed by |
Robert P. J. Day | 30aa96c | 2018-06-07 07:53:36 -0400 | [diff] [blame] | 842 | * local branches 'refs/heads/...' and finally remote-tracking |
Nguyễn Thái Ngọc Duy | 3bb16a8 | 2016-12-04 09:52:25 +0700 | [diff] [blame] | 843 | * branches 'refs/remotes/...'. |
| 844 | */ |
Junio C Hamano | 98e7ab6 | 2021-10-20 12:23:53 -0700 | [diff] [blame] | 845 | sorting = ref_sorting_options(&sorting_options); |
Ævar Arnfjörð Bjarmason | 7c269a7 | 2021-01-07 10:51:51 +0100 | [diff] [blame] | 846 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); |
Ævar Arnfjörð Bjarmason | 2708ce6 | 2021-01-07 10:51:52 +0100 | [diff] [blame] | 847 | ref_sorting_set_sort_flags_all( |
| 848 | sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1); |
Ævar Arnfjörð Bjarmason | d72d4f9 | 2021-10-20 20:27:21 +0200 | [diff] [blame] | 849 | print_ref_list(&filter, sorting, &format, &output); |
Nguyễn Thái Ngọc Duy | ebe31ef | 2012-04-13 17:54:38 +0700 | [diff] [blame] | 850 | print_columns(&output, colopts, NULL); |
| 851 | string_list_clear(&output, 0); |
Ævar Arnfjörð Bjarmason | d72d4f9 | 2021-10-20 20:27:21 +0200 | [diff] [blame] | 852 | ref_sorting_release(sorting); |
Karthik Nayak | ca41799 | 2015-09-24 23:39:08 +0530 | [diff] [blame] | 853 | return 0; |
Tao Qingyun | 2e3c894 | 2018-10-16 22:19:20 +0800 | [diff] [blame] | 854 | } else if (edit_description) { |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 855 | const char *branch_name; |
Junio C Hamano | c2d17ba | 2012-02-05 17:13:36 -0800 | [diff] [blame] | 856 | struct strbuf branch_ref = STRBUF_INIT; |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 857 | struct strbuf buf = STRBUF_INIT; |
| 858 | int ret = 1; /* assume failure */ |
Junio C Hamano | c2d17ba | 2012-02-05 17:13:36 -0800 | [diff] [blame] | 859 | |
Nguyễn Thái Ngọc Duy | 75135b2 | 2013-01-28 08:18:13 +0700 | [diff] [blame] | 860 | if (!argc) { |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 861 | if (filter.detached) |
Nguyễn Thái Ngọc Duy | 045e388 | 2013-01-28 08:18:16 +0700 | [diff] [blame] | 862 | die(_("Cannot give description to detached HEAD")); |
Junio C Hamano | b7200e8 | 2011-09-20 15:10:08 -0700 | [diff] [blame] | 863 | branch_name = head; |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 864 | } else if (argc == 1) { |
| 865 | strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL); |
| 866 | branch_name = buf.buf; |
| 867 | } else { |
Nguyễn Thái Ngọc Duy | 43722c4 | 2013-01-28 08:18:15 +0700 | [diff] [blame] | 868 | die(_("cannot edit description of more than one branch")); |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 869 | } |
Junio C Hamano | c2d17ba | 2012-02-05 17:13:36 -0800 | [diff] [blame] | 870 | |
| 871 | strbuf_addf(&branch_ref, "refs/heads/%s", branch_name); |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 872 | if (!ref_exists(branch_ref.buf)) |
Rubén Justo | 7a6ccdf | 2023-03-27 00:33:17 +0200 | [diff] [blame] | 873 | error((!argc || branch_checked_out(branch_ref.buf)) |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 874 | ? _("No commit on branch '%s' yet.") |
| 875 | : _("No branch named '%s'."), |
| 876 | branch_name); |
| 877 | else if (!edit_branch_description(branch_name)) |
| 878 | ret = 0; /* happy */ |
Junio C Hamano | c2d17ba | 2012-02-05 17:13:36 -0800 | [diff] [blame] | 879 | |
Junio C Hamano | c2d17ba | 2012-02-05 17:13:36 -0800 | [diff] [blame] | 880 | strbuf_release(&branch_ref); |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 881 | strbuf_release(&buf); |
Junio C Hamano | c2d17ba | 2012-02-05 17:13:36 -0800 | [diff] [blame] | 882 | |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 883 | return ret; |
Rubén Justo | 77e7267 | 2022-10-26 01:01:29 +0200 | [diff] [blame] | 884 | } else if (copy || rename) { |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 885 | if (!argc) |
| 886 | die(_("branch name required")); |
Rubén Justo | 77e7267 | 2022-10-26 01:01:29 +0200 | [diff] [blame] | 887 | else if ((argc == 1) && filter.detached) |
| 888 | die(copy? _("cannot copy the current branch while not on any.") |
| 889 | : _("cannot rename the current branch while not on any.")); |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 890 | else if (argc == 1) |
Rubén Justo | 77e7267 | 2022-10-26 01:01:29 +0200 | [diff] [blame] | 891 | copy_or_rename_branch(head, argv[0], copy, copy + rename > 1); |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 892 | else if (argc == 2) |
Rubén Justo | 77e7267 | 2022-10-26 01:01:29 +0200 | [diff] [blame] | 893 | copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1); |
Sahil Dua | 52d59cc | 2017-06-18 23:19:16 +0200 | [diff] [blame] | 894 | else |
Rubén Justo | 77e7267 | 2022-10-26 01:01:29 +0200 | [diff] [blame] | 895 | die(copy? _("too many branches for a copy operation") |
| 896 | : _("too many arguments for a rename operation")); |
Carlos Martín Nieto | 6183d82 | 2012-08-20 15:47:38 +0200 | [diff] [blame] | 897 | } else if (new_upstream) { |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 898 | struct branch *branch; |
| 899 | struct strbuf buf = STRBUF_INIT; |
Carlos Martín Nieto | 6183d82 | 2012-08-20 15:47:38 +0200 | [diff] [blame] | 900 | |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 901 | if (!argc) |
| 902 | branch = branch_get(NULL); |
| 903 | else if (argc == 1) { |
| 904 | strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL); |
| 905 | branch = branch_get(buf.buf); |
| 906 | } else |
Kaartic Sivaraam | f777623 | 2017-08-21 19:06:08 +0530 | [diff] [blame] | 907 | die(_("too many arguments to set new upstream")); |
Nguyễn Thái Ngọc Duy | 8efb889 | 2013-02-23 19:22:27 +0700 | [diff] [blame] | 908 | |
| 909 | if (!branch) { |
| 910 | if (!argc || !strcmp(argv[0], "HEAD")) |
| 911 | die(_("could not set upstream of HEAD to %s when " |
| 912 | "it does not point to any branch."), |
| 913 | new_upstream); |
| 914 | die(_("no such branch '%s'"), argv[0]); |
| 915 | } |
| 916 | |
Rubén Justo | bcfc82b | 2022-10-08 02:39:43 +0200 | [diff] [blame] | 917 | if (!ref_exists(branch->refname)) { |
Rubén Justo | 7a6ccdf | 2023-03-27 00:33:17 +0200 | [diff] [blame] | 918 | if (!argc || branch_checked_out(branch->refname)) |
Rubén Justo | bcfc82b | 2022-10-08 02:39:43 +0200 | [diff] [blame] | 919 | die(_("No commit on branch '%s' yet."), branch->name); |
Carlos Martín Nieto | 6183d82 | 2012-08-20 15:47:38 +0200 | [diff] [blame] | 920 | die(_("branch '%s' does not exist"), branch->name); |
Rubén Justo | bcfc82b | 2022-10-08 02:39:43 +0200 | [diff] [blame] | 921 | } |
Carlos Martín Nieto | 6183d82 | 2012-08-20 15:47:38 +0200 | [diff] [blame] | 922 | |
Glen Choo | e89f151 | 2022-01-28 16:04:41 -0800 | [diff] [blame] | 923 | dwim_and_setup_tracking(the_repository, branch->name, |
| 924 | new_upstream, BRANCH_TRACK_OVERRIDE, |
| 925 | quiet); |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 926 | strbuf_release(&buf); |
Carlos Martín Nieto | b84869e | 2012-08-30 19:23:12 +0200 | [diff] [blame] | 927 | } else if (unset_upstream) { |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 928 | struct branch *branch; |
Carlos Martín Nieto | b84869e | 2012-08-30 19:23:12 +0200 | [diff] [blame] | 929 | struct strbuf buf = STRBUF_INIT; |
| 930 | |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 931 | if (!argc) |
| 932 | branch = branch_get(NULL); |
| 933 | else if (argc == 1) { |
| 934 | strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL); |
| 935 | branch = branch_get(buf.buf); |
| 936 | } else |
Kaartic Sivaraam | f777623 | 2017-08-21 19:06:08 +0530 | [diff] [blame] | 937 | die(_("too many arguments to unset upstream")); |
Nguyễn Thái Ngọc Duy | 8efb889 | 2013-02-23 19:22:27 +0700 | [diff] [blame] | 938 | |
| 939 | if (!branch) { |
| 940 | if (!argc || !strcmp(argv[0], "HEAD")) |
| 941 | die(_("could not unset upstream of HEAD when " |
| 942 | "it does not point to any branch.")); |
| 943 | die(_("no such branch '%s'"), argv[0]); |
| 944 | } |
| 945 | |
Felipe Contreras | 54d07f2 | 2013-10-31 03:25:38 -0600 | [diff] [blame] | 946 | if (!branch_has_merge_config(branch)) |
Carlos Martín Nieto | b84869e | 2012-08-30 19:23:12 +0200 | [diff] [blame] | 947 | die(_("Branch '%s' has no upstream information"), branch->name); |
Carlos Martín Nieto | b84869e | 2012-08-30 19:23:12 +0200 | [diff] [blame] | 948 | |
Rubén Justo | 0dc4e5c | 2022-10-11 01:24:58 +0200 | [diff] [blame] | 949 | strbuf_reset(&buf); |
Carlos Martín Nieto | b84869e | 2012-08-30 19:23:12 +0200 | [diff] [blame] | 950 | strbuf_addf(&buf, "branch.%s.remote", branch->name); |
Derrick Stolee | 504ee12 | 2020-11-25 22:12:49 +0000 | [diff] [blame] | 951 | git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); |
Carlos Martín Nieto | b84869e | 2012-08-30 19:23:12 +0200 | [diff] [blame] | 952 | strbuf_reset(&buf); |
| 953 | strbuf_addf(&buf, "branch.%s.merge", branch->name); |
Derrick Stolee | 504ee12 | 2020-11-25 22:12:49 +0000 | [diff] [blame] | 954 | git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); |
Carlos Martín Nieto | b84869e | 2012-08-30 19:23:12 +0200 | [diff] [blame] | 955 | strbuf_release(&buf); |
Glen Choo | 6e0a2ca | 2022-01-28 16:04:44 -0800 | [diff] [blame] | 956 | } else if (!noncreate_actions && argc > 0 && argc <= 2) { |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 957 | const char *branch_name = argv[0]; |
| 958 | const char *start_name = argc == 2 ? argv[1] : head; |
| 959 | |
Karthik Nayak | 1511b22 | 2015-09-23 23:41:11 +0530 | [diff] [blame] | 960 | if (filter.kind != FILTER_REFS_BRANCHES) |
Philip Oakley | 1fde99c | 2019-05-29 00:16:05 +0100 | [diff] [blame] | 961 | die(_("The -a, and -r, options to 'git branch' do not take a branch name.\n" |
| 962 | "Did you mean to use: -a|-r --list <pattern>?")); |
Carlos Martín Nieto | b347d06 | 2012-08-30 19:23:13 +0200 | [diff] [blame] | 963 | |
| 964 | if (track == BRANCH_TRACK_OVERRIDE) |
Kaartic Sivaraam | 5266884 | 2017-08-17 08:24:24 +0530 | [diff] [blame] | 965 | die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead.")); |
Carlos Martín Nieto | b347d06 | 2012-08-30 19:23:13 +0200 | [diff] [blame] | 966 | |
Glen Choo | 961b130 | 2022-01-28 16:04:45 -0800 | [diff] [blame] | 967 | if (recurse_submodules) { |
| 968 | create_branches_recursively(the_repository, branch_name, |
| 969 | start_name, NULL, force, |
| 970 | reflog, quiet, track, 0); |
| 971 | return 0; |
| 972 | } |
| 973 | create_branch(the_repository, branch_name, start_name, force, 0, |
| 974 | reflog, quiet, track, 0); |
Matthieu Moy | 6e8f993 | 2009-12-30 15:45:31 +0100 | [diff] [blame] | 975 | } else |
Pierre Habouzit | a8dfd5e | 2007-10-07 18:26:21 +0200 | [diff] [blame] | 976 | usage_with_options(builtin_branch_usage, options); |
Lars Hjemli | c31820c | 2006-10-23 23:27:45 +0200 | [diff] [blame] | 977 | |
| 978 | return 0; |
| 979 | } |