]>
Commit | Line | Data |
---|---|---|
c31820c2 LH |
1 | /* |
2 | * Builtin "git branch" | |
3 | * | |
d972cce0 | 4 | * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com> |
c31820c2 LH |
5 | * Based on git-branch.sh by Junio C Hamano. |
6 | */ | |
7 | ||
bc5c5ec0 | 8 | #include "builtin.h" |
b2141fc1 | 9 | #include "config.h" |
85023577 | 10 | #include "color.h" |
4e120823 | 11 | #include "editor.h" |
7ee24e18 | 12 | #include "environment.h" |
c31820c2 LH |
13 | #include "refs.h" |
14 | #include "commit.h" | |
f394e093 | 15 | #include "gettext.h" |
dabab1d6 | 16 | #include "object-name.h" |
6f084a56 | 17 | #include "remote.h" |
a8dfd5ea | 18 | #include "parse-options.h" |
e496c003 | 19 | #include "branch.h" |
c339932b | 20 | #include "path.h" |
ebe31ef2 NTND |
21 | #include "string-list.h" |
22 | #include "column.h" | |
1452bd64 | 23 | #include "utf8.h" |
1511b22d | 24 | #include "ref-filter.h" |
f292244c | 25 | #include "worktree.h" |
3ac68a93 | 26 | #include "help.h" |
bec9bb4b | 27 | #include "advice.h" |
64043556 | 28 | #include "commit-reach.h" |
a8dfd5ea PH |
29 | |
30 | static const char * const builtin_branch_usage[] = { | |
21bf9339 | 31 | N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"), |
961b130d GC |
32 | N_("git branch [<options>] [-f] [--recurse-submodules] <branch-name> [<start-point>]"), |
33 | N_("git branch [<options>] [-l] [<pattern>...]"), | |
9c9b4f2f AH |
34 | N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."), |
35 | N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"), | |
52d59cc6 | 36 | N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"), |
aa3bc55e | 37 | N_("git branch [<options>] [-r | -a] [--points-at]"), |
3d9e4ce3 | 38 | N_("git branch [<options>] [-r | -a] [--format]"), |
a8dfd5ea PH |
39 | NULL |
40 | }; | |
c31820c2 | 41 | |
c31820c2 | 42 | static const char *head; |
0c77cd24 | 43 | static struct object_id head_oid; |
961b130d GC |
44 | static int recurse_submodules = 0; |
45 | static int submodule_propagate_branches = 0; | |
c31820c2 | 46 | |
6b2f2d98 | 47 | static int branch_use_color = -1; |
a1158cae | 48 | static char branch_colors[][COLOR_MAXLEN] = { |
dc6ebd4c | 49 | GIT_COLOR_RESET, |
949af068 KN |
50 | GIT_COLOR_NORMAL, /* PLAIN */ |
51 | GIT_COLOR_RED, /* REMOTE */ | |
52 | GIT_COLOR_NORMAL, /* LOCAL */ | |
53 | GIT_COLOR_GREEN, /* CURRENT */ | |
54 | GIT_COLOR_BLUE, /* UPSTREAM */ | |
ab313814 | 55 | GIT_COLOR_CYAN, /* WORKTREE */ |
a1158cae AP |
56 | }; |
57 | enum color_branch { | |
74bb2dfb AL |
58 | BRANCH_COLOR_RESET = 0, |
59 | BRANCH_COLOR_PLAIN = 1, | |
60 | BRANCH_COLOR_REMOTE = 2, | |
61 | BRANCH_COLOR_LOCAL = 3, | |
dbda21fa | 62 | BRANCH_COLOR_CURRENT = 4, |
ab313814 NB |
63 | BRANCH_COLOR_UPSTREAM = 5, |
64 | BRANCH_COLOR_WORKTREE = 6 | |
a1158cae AP |
65 | }; |
66 | ||
a73b3680 NTND |
67 | static 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", | |
ab313814 | 74 | [BRANCH_COLOR_WORKTREE] = "worktree", |
a73b3680 NTND |
75 | }; |
76 | ||
ebe31ef2 NTND |
77 | static struct string_list output = STRING_LIST_INIT_DUP; |
78 | static unsigned int colopts; | |
79 | ||
3ac68a93 | 80 | define_list_config_array(color_branch_slots); |
a1158cae | 81 | |
a4e7e317 GC |
82 | static int git_branch_config(const char *var, const char *value, |
83 | const struct config_context *ctx, void *cb) | |
a1158cae | 84 | { |
e3f1da98 | 85 | const char *slot_name; |
560ae1c1 SM |
86 | |
87 | if (!strcmp(var, "branch.sort")) { | |
88 | if (!value) | |
89 | return config_error_nonbool(var); | |
98e7ab6d | 90 | string_list_append(cb, value); |
560ae1c1 SM |
91 | return 0; |
92 | } | |
e3f1da98 | 93 | |
59556548 | 94 | if (starts_with(var, "column.")) |
ebe31ef2 | 95 | return git_column_config(var, value, "branch", &colopts); |
a1158cae | 96 | if (!strcmp(var, "color.branch")) { |
e269eb79 | 97 | branch_use_color = git_config_colorbool(var, value); |
a1158cae AP |
98 | return 0; |
99 | } | |
e3f1da98 | 100 | if (skip_prefix(var, "color.branch.", &slot_name)) { |
a73b3680 | 101 | int slot = LOOKUP_CONFIG(color_branch_slots, slot_name); |
8b8e8624 JK |
102 | if (slot < 0) |
103 | return 0; | |
5768c98a JH |
104 | if (!value) |
105 | return config_error_nonbool(var); | |
f6c5a296 | 106 | return color_parse(value, branch_colors[slot]); |
a1158cae | 107 | } |
961b130d GC |
108 | 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 | ||
97eeeea2 GC |
117 | if (git_color_config(var, value, cb) < 0) |
118 | return -1; | |
119 | ||
a4e7e317 | 120 | return git_default_config(var, value, ctx, cb); |
a1158cae AP |
121 | } |
122 | ||
52fae7de | 123 | static const char *branch_get_color(enum color_branch ix) |
a1158cae | 124 | { |
daa0c3d9 | 125 | if (want_color(branch_use_color)) |
a1158cae AP |
126 | return branch_colors[ix]; |
127 | return ""; | |
128 | } | |
129 | ||
99c419c9 JH |
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; | |
96ec7b1e | 141 | void *reference_name_to_free = NULL; |
99c419c9 JH |
142 | int merged; |
143 | ||
1511b22d | 144 | if (kind == FILTER_REFS_BRANCHES) { |
99c419c9 | 145 | struct branch *branch = branch_get(name); |
3a429d0a | 146 | const char *upstream = branch_get_upstream(branch, NULL); |
0c77cd24 | 147 | struct object_id oid; |
99c419c9 | 148 | |
a9f9f8cc | 149 | if (upstream && |
96ec7b1e | 150 | (reference_name = reference_name_to_free = |
a9f9f8cc | 151 | resolve_refdup(upstream, RESOLVE_REF_READING, |
0f2dc722 | 152 | &oid, NULL)) != NULL) |
2122f675 SB |
153 | reference_rev = lookup_commit_reference(the_repository, |
154 | &oid); | |
99c419c9 JH |
155 | } |
156 | if (!reference_rev) | |
157 | reference_rev = head_rev; | |
158 | ||
cb338c23 ÆAB |
159 | merged = reference_rev ? repo_in_merge_bases(the_repository, rev, |
160 | reference_rev) : 0; | |
24876ebf JS |
161 | if (merged < 0) |
162 | exit(128); | |
99c419c9 JH |
163 | |
164 | /* | |
165 | * After the safety valve is fully redefined to "check with | |
166 | * upstream, if any, otherwise with HEAD", we should just | |
c7c33f50 | 167 | * return the result of the repo_in_merge_bases() above without |
99c419c9 JH |
168 | * any of the following code, but during the transition period, |
169 | * a gentle reminder is in order. | |
170 | */ | |
24876ebf JS |
171 | 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) | |
49df4b02 | 178 | warning(_("deleting branch '%s' that has been merged to\n" |
12b99928 | 179 | " '%s', but not yet merged to HEAD"), |
99c419c9 JH |
180 | name, reference_name); |
181 | else | |
49df4b02 | 182 | warning(_("not deleting branch '%s' that is not yet merged to\n" |
12b99928 | 183 | " '%s', even though it is merged to HEAD"), |
99c419c9 JH |
184 | name, reference_name); |
185 | } | |
96ec7b1e | 186 | free(reference_name_to_free); |
99c419c9 JH |
187 | return merged; |
188 | } | |
189 | ||
f5d0e162 | 190 | static int check_branch_commit(const char *branchname, const char *refname, |
0c77cd24 | 191 | const struct object_id *oid, struct commit *head_rev, |
f5d0e162 RS |
192 | int kinds, int force) |
193 | { | |
2122f675 | 194 | struct commit *rev = lookup_commit_reference(the_repository, oid); |
597a9774 | 195 | if (!force && !rev) { |
12b99928 | 196 | error(_("couldn't look up commit object for '%s'"), refname); |
f5d0e162 RS |
197 | return -1; |
198 | } | |
199 | if (!force && !branch_merged(kinds, branchname, rev, head_rev)) { | |
bec9bb4b RJ |
200 | 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); | |
f5d0e162 RS |
204 | return -1; |
205 | } | |
206 | return 0; | |
207 | } | |
208 | ||
22ed7927 RS |
209 | static 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) | |
12b99928 | 214 | warning(_("update of config-file failed")); |
22ed7927 RS |
215 | strbuf_release(&buf); |
216 | } | |
217 | ||
d65ddf19 JK |
218 | static int delete_branches(int argc, const char **argv, int force, int kinds, |
219 | int quiet) | |
c31820c2 | 220 | { |
f5d0e162 | 221 | struct commit *head_rev = NULL; |
0c77cd24 | 222 | struct object_id oid; |
b8e9a00d | 223 | char *name = NULL; |
c179837c | 224 | const char *fmt; |
c31820c2 | 225 | int i; |
b8e9a00d | 226 | int ret = 0; |
c179837c | 227 | int remote_branch = 0; |
8415d5c7 | 228 | struct strbuf bname = STRBUF_INIT; |
6b145e01 | 229 | unsigned allowed_interpret; |
81989077 PH |
230 | struct string_list refs_to_delete = STRING_LIST_INIT_DUP; |
231 | struct string_list_item *item; | |
232 | int branch_name_pos; | |
4c643fb3 | 233 | const char *fmt_remotes = "refs/remotes/%s"; |
c31820c2 | 234 | |
f3d985c3 | 235 | switch (kinds) { |
1511b22d | 236 | case FILTER_REFS_REMOTES: |
4c643fb3 | 237 | fmt = fmt_remotes; |
c179837c ÆAB |
238 | /* For subsequent UI messages */ |
239 | remote_branch = 1; | |
6b145e01 | 240 | allowed_interpret = INTERPRET_BRANCH_REMOTE; |
c179837c | 241 | |
f3d985c3 JH |
242 | force = 1; |
243 | break; | |
1511b22d | 244 | case FILTER_REFS_BRANCHES: |
f3d985c3 | 245 | fmt = "refs/heads/%s"; |
6b145e01 | 246 | allowed_interpret = INTERPRET_BRANCH_LOCAL; |
f3d985c3 JH |
247 | break; |
248 | default: | |
49df4b02 | 249 | die(_("cannot use -a with -d")); |
f3d985c3 | 250 | } |
81989077 | 251 | branch_name_pos = strcspn(fmt, "%"); |
c31820c2 | 252 | |
eb20e63f | 253 | if (!force) |
2122f675 | 254 | head_rev = lookup_commit_reference(the_repository, &head_oid); |
c8dd491f | 255 | |
a9155c50 | 256 | for (i = 0; i < argc; i++, strbuf_reset(&bname)) { |
8bb04553 | 257 | char *target = NULL; |
0fe700e3 RS |
258 | int flags = 0; |
259 | ||
6b145e01 | 260 | strbuf_branchname(&bname, argv[i], allowed_interpret); |
8e0f7003 | 261 | free(name); |
4e2d094d | 262 | name = mkpathdup(fmt, bname.buf); |
f292244c KY |
263 | |
264 | if (kinds == FILTER_REFS_BRANCHES) { | |
b489b9d9 DS |
265 | const char *path; |
266 | if ((path = branch_checked_out(name))) { | |
12b99928 | 267 | error(_("cannot delete branch '%s' " |
92edf618 | 268 | "used by worktree at '%s'"), |
b489b9d9 | 269 | bname.buf, path); |
f292244c KY |
270 | ret = 1; |
271 | continue; | |
272 | } | |
273 | } | |
274 | ||
8bb04553 MH |
275 | target = resolve_refdup(name, |
276 | RESOLVE_REF_READING | |
277 | | RESOLVE_REF_NO_RECURSE | |
278 | | RESOLVE_REF_ALLOW_BAD_NAME, | |
0f2dc722 | 279 | &oid, &flags); |
18f29fc6 | 280 | if (!target) { |
4c643fb3 | 281 | if (remote_branch) { |
12b99928 | 282 | error(_("remote-tracking branch '%s' not found"), bname.buf); |
4c643fb3 CM |
283 | } 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 | |
12b99928 | 297 | error(_("branch '%s' not found"), bname.buf); |
4c643fb3 CM |
298 | FREE_AND_NULL(virtual_target); |
299 | } | |
b8e9a00d QT |
300 | ret = 1; |
301 | continue; | |
302 | } | |
c31820c2 | 303 | |
d0f810f0 | 304 | if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) && |
0c77cd24 | 305 | check_branch_commit(bname.buf, name, &oid, head_rev, kinds, |
f5d0e162 | 306 | force)) { |
b8e9a00d | 307 | ret = 1; |
8bb04553 | 308 | goto next; |
c31820c2 LH |
309 | } |
310 | ||
81989077 PH |
311 | item = string_list_append(&refs_to_delete, name); |
312 | item->util = xstrdup((flags & REF_ISBROKEN) ? "broken" | |
313 | : (flags & REF_ISSYMREF) ? target | |
d850b7a5 | 314 | : repo_find_unique_abbrev(the_repository, &oid, DEFAULT_ABBREV)); |
8bb04553 MH |
315 | |
316 | next: | |
317 | free(target); | |
c31820c2 | 318 | } |
c31820c2 | 319 | |
81989077 PH |
320 | 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 | ||
8e0f7003 | 340 | free(name); |
a9155c50 | 341 | strbuf_release(&bname); |
b8e9a00d | 342 | |
a9155c50 | 343 | return ret; |
c31820c2 | 344 | } |
c31820c2 | 345 | |
949af068 | 346 | static int calc_maxwidth(struct ref_array *refs, int remote_bonus) |
94fcb730 | 347 | { |
949af068 KN |
348 | int i, max = 0; |
349 | for (i = 0; i < refs->nr; i++) { | |
350 | struct ref_array_item *it = refs->items[i]; | |
351 | const char *desc = it->refname; | |
352 | int w; | |
dbda21fa | 353 | |
949af068 KN |
354 | skip_prefix(it->refname, "refs/heads/", &desc); |
355 | skip_prefix(it->refname, "refs/remotes/", &desc); | |
356 | 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); | |
21dfc09d | 362 | |
949af068 KN |
363 | if (it->kind == FILTER_REFS_REMOTES) |
364 | w += remote_bonus; | |
365 | if (w > max) | |
366 | max = w; | |
21dfc09d | 367 | } |
949af068 | 368 | return max; |
94fcb730 JH |
369 | } |
370 | ||
949af068 | 371 | static const char *quote_literal_for_format(const char *s) |
6e0332ec | 372 | { |
949af068 KN |
373 | static struct strbuf buf = STRBUF_INIT; |
374 | ||
375 | strbuf_reset(&buf); | |
44ccb337 RS |
376 | while (strbuf_expand_step(&buf, &s)) |
377 | strbuf_addstr(&buf, "%%"); | |
949af068 | 378 | return buf.buf; |
6e0332ec JN |
379 | } |
380 | ||
949af068 | 381 | static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix) |
75e6e213 | 382 | { |
949af068 KN |
383 | struct strbuf fmt = STRBUF_INIT; |
384 | struct strbuf local = STRBUF_INIT; | |
385 | struct strbuf remote = STRBUF_INIT; | |
75e6e213 | 386 | |
ab313814 NB |
387 | 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)); | |
7ca260ab JK |
391 | strbuf_addf(&remote, " %s", |
392 | branch_get_color(BRANCH_COLOR_REMOTE)); | |
75e6e213 | 393 | |
1511b22d | 394 | if (filter->verbose) { |
ac5bbc02 JH |
395 | 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 | ||
949af068 | 404 | strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth); |
72d4a9a7 | 405 | strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET)); |
ac5bbc02 | 406 | strbuf_addf(&local, " %s ", obname.buf); |
949af068 KN |
407 | |
408 | if (filter->verbose > 1) | |
6e938146 NB |
409 | { |
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)); | |
949af068 KN |
412 | 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)); | |
6e938146 | 415 | } |
949af068 KN |
416 | else |
417 | strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)"); | |
418 | ||
7ca260ab | 419 | strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s" |
ac5bbc02 JH |
420 | "%%(if)%%(symref)%%(then) -> %%(symref:short)" |
421 | "%%(else) %s %%(contents:subject)%%(end)", | |
7ca260ab | 422 | maxwidth, quote_literal_for_format(remote_prefix), |
ac5bbc02 JH |
423 | branch_get_color(BRANCH_COLOR_RESET), obname.buf); |
424 | strbuf_release(&obname); | |
ebe31ef2 | 425 | } else { |
949af068 KN |
426 | strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", |
427 | branch_get_color(BRANCH_COLOR_RESET)); | |
7ca260ab JK |
428 | strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", |
429 | quote_literal_for_format(remote_prefix), | |
949af068 | 430 | branch_get_color(BRANCH_COLOR_RESET)); |
ebe31ef2 | 431 | } |
75e6e213 | 432 | |
949af068 | 433 | strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf); |
aedcb7dc | 434 | |
949af068 KN |
435 | strbuf_release(&local); |
436 | strbuf_release(&remote); | |
437 | return strbuf_detach(&fmt, NULL); | |
7e9ff00b LT |
438 | } |
439 | ||
d72d4f92 ÆAB |
440 | static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, |
441 | struct ref_format *format, struct string_list *output) | |
c31820c2 LH |
442 | { |
443 | int i; | |
1511b22d | 444 | struct ref_array array; |
1051e40d KN |
445 | int maxwidth = 0; |
446 | const char *remote_prefix = ""; | |
3d9e4ce3 | 447 | char *to_free = NULL; |
1051e40d | 448 | |
35257aa0 | 449 | /* |
1051e40d KN |
450 | * 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. | |
35257aa0 | 453 | */ |
1511b22d | 454 | if (filter->kind != FILTER_REFS_REMOTES) |
1051e40d | 455 | remote_prefix = "remotes/"; |
8376a704 | 456 | |
1511b22d | 457 | memset(&array, 0, sizeof(array)); |
8376a704 | 458 | |
1763334c | 459 | filter_refs(&array, filter, filter->kind); |
c31820c2 | 460 | |
1511b22d KN |
461 | if (filter->verbose) |
462 | maxwidth = calc_maxwidth(&array, strlen(remote_prefix)); | |
bfcc9214 | 463 | |
4a68e36d JK |
464 | if (!format->format) |
465 | format->format = to_free = build_format(filter, maxwidth, remote_prefix); | |
11b087ad | 466 | format->use_color = branch_use_color; |
2eda0102 JK |
467 | |
468 | if (verify_ref_format(format)) | |
469 | die(_("unable to parse format string")); | |
949af068 | 470 | |
49abcd21 | 471 | filter_ahead_behind(the_repository, format, &array); |
aedcb7dc | 472 | ref_array_sort(sorting, &array); |
1603ade8 | 473 | |
e7574b0c VD |
474 | 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 */ | |
d72d4f92 | 486 | string_list_append(output, out.buf); |
949af068 | 487 | } |
e7574b0c VD |
488 | |
489 | strbuf_release(&err); | |
490 | strbuf_release(&out); | |
491 | } else { | |
492 | print_formatted_ref_array(&array, format); | |
949af068 | 493 | } |
0e9716e6 | 494 | |
1511b22d | 495 | ref_array_clear(&array); |
3d9e4ce3 | 496 | free(to_free); |
c31820c2 LH |
497 | } |
498 | ||
0ecb1fc7 DU |
499 | static 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 | ||
d7f4ca61 RJ |
514 | static void reject_rebase_or_bisect_branch(struct worktree **worktrees, |
515 | const char *target) | |
14ace5b7 | 516 | { |
14ace5b7 NTND |
517 | 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)) | |
12b99928 | 526 | die(_("branch %s is being rebased at %s"), |
14ace5b7 NTND |
527 | target, wt->path); |
528 | ||
529 | if (is_worktree_being_bisected(wt, target)) | |
12b99928 | 530 | die(_("branch %s is being bisected at %s"), |
14ace5b7 NTND |
531 | target, wt->path); |
532 | } | |
14ace5b7 | 533 | } |
14ace5b7 | 534 | |
2e8af499 RJ |
535 | /* |
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 | */ | |
d7f4ca61 RJ |
540 | static int replace_each_worktree_head_symref(struct worktree **worktrees, |
541 | const char *oldref, const char *newref, | |
2e8af499 RJ |
542 | const char *logmsg) |
543 | { | |
544 | int ret = 0; | |
2e8af499 RJ |
545 | 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 | ||
2e8af499 | 563 | return ret; |
14ace5b7 NTND |
564 | } |
565 | ||
7a6ccdfb | 566 | #define IS_HEAD 1 |
a675ad17 | 567 | #define IS_ORPHAN 2 |
7a6ccdfb | 568 | |
52d59cc6 | 569 | static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force) |
c976d415 | 570 | { |
da3f78a2 | 571 | struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; |
da3f78a2 | 572 | struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; |
255073ca KS |
573 | const char *interpreted_oldname = NULL; |
574 | const char *interpreted_newname = NULL; | |
7a6ccdfb | 575 | int recovery = 0, oldref_usage = 0; |
d7f4ca61 | 576 | struct worktree **worktrees = get_worktrees(); |
c976d415 | 577 | |
cbdffe40 JH |
578 | 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 | */ | |
c6893323 | 583 | if (ref_exists(oldref.buf)) |
cbdffe40 | 584 | recovery = 1; |
8fbd903e KH |
585 | 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 | } | |
cbdffe40 | 591 | } |
c976d415 | 592 | |
7a6ccdfb RJ |
593 | 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; | |
a675ad17 RJ |
598 | if (is_null_oid(&wt->head_oid)) |
599 | oldref_usage |= IS_ORPHAN; | |
7a6ccdfb RJ |
600 | break; |
601 | } | |
602 | } | |
603 | ||
604 | if ((copy || !(oldref_usage & IS_HEAD)) && !ref_exists(oldref.buf)) { | |
605 | if (oldref_usage & IS_HEAD) | |
12b99928 | 606 | die(_("no commit on branch '%s' yet"), oldname); |
bcfc82bd | 607 | else |
12b99928 | 608 | die(_("no branch named '%s'"), oldname); |
bcfc82bd RJ |
609 | } |
610 | ||
3f59481e JN |
611 | /* |
612 | * A command like "git branch -M currentbranch currentbranch" cannot | |
613 | * cause the worktree to become inconsistent with HEAD, so allow it. | |
614 | */ | |
bc1c9c0e JH |
615 | if (!strcmp(oldname, newname)) |
616 | validate_branchname(newname, &newref); | |
617 | else | |
618 | validate_new_branchname(newname, &newref, force); | |
c976d415 | 619 | |
d7f4ca61 | 620 | reject_rebase_or_bisect_branch(worktrees, oldref.buf); |
14ace5b7 | 621 | |
255073ca KS |
622 | if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) || |
623 | !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) { | |
033abf97 | 624 | BUG("expected prefix missing for refs"); |
255073ca KS |
625 | } |
626 | ||
52d59cc6 SD |
627 | 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); | |
678d0f4c | 633 | |
a675ad17 | 634 | if (!copy && !(oldref_usage & IS_ORPHAN) && |
cfaff3aa | 635 | rename_ref(oldref.buf, newref.buf, logmsg.buf)) |
12b99928 | 636 | die(_("branch rename failed")); |
52d59cc6 | 637 | if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf)) |
12b99928 | 638 | die(_("branch copy failed")); |
c976d415 | 639 | |
52d59cc6 SD |
640 | if (recovery) { |
641 | if (copy) | |
12b99928 | 642 | warning(_("created a copy of a misnamed branch '%s'"), |
255073ca | 643 | interpreted_oldname); |
52d59cc6 | 644 | else |
12b99928 | 645 | warning(_("renamed a misnamed branch '%s' away"), |
255073ca | 646 | interpreted_oldname); |
52d59cc6 | 647 | } |
cbdffe40 | 648 | |
3521c632 | 649 | if (!copy && (oldref_usage & IS_HEAD) && |
d7f4ca61 RJ |
650 | replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf, |
651 | logmsg.buf)) | |
12b99928 | 652 | die(_("branch renamed to %s, but HEAD is not updated"), newname); |
19eba151 | 653 | |
39ee4c6c KM |
654 | strbuf_release(&logmsg); |
655 | ||
255073ca | 656 | strbuf_addf(&oldsection, "branch.%s", interpreted_oldname); |
255073ca | 657 | strbuf_addf(&newsection, "branch.%s", interpreted_newname); |
52d59cc6 | 658 | if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0) |
12b99928 | 659 | die(_("branch is renamed, but update of config-file failed")); |
cfbd173c | 660 | if (copy && strcmp(interpreted_oldname, interpreted_newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0) |
12b99928 | 661 | die(_("branch is copied, but update of config-file failed")); |
cfbd173c RJ |
662 | strbuf_release(&oldref); |
663 | strbuf_release(&newref); | |
da3f78a2 MV |
664 | strbuf_release(&oldsection); |
665 | strbuf_release(&newsection); | |
d7f4ca61 | 666 | free_worktrees(worktrees); |
c976d415 LH |
667 | } |
668 | ||
c10388c7 | 669 | static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION") |
b7200e83 JH |
670 | |
671 | static int edit_branch_description(const char *branch_name) | |
672 | { | |
e288b3de | 673 | int exists; |
b7200e83 JH |
674 | struct strbuf buf = STRBUF_INIT; |
675 | struct strbuf name = STRBUF_INIT; | |
676 | ||
e288b3de | 677 | exists = !read_branch_desc(&buf, branch_name); |
b7200e83 JH |
678 | if (!buf.len || buf.buf[buf.len-1] != '\n') |
679 | strbuf_addch(&buf, '\n'); | |
787cb8a4 | 680 | strbuf_commented_addf(&buf, comment_line_char, |
b18237f4 VA |
681 | _("Please edit the description for the branch\n" |
682 | " %s\n" | |
683 | "Lines starting with '%c' will be stripped.\n"), | |
eff80a9f | 684 | branch_name, comment_line_char); |
c10388c7 | 685 | write_file_buf(edit_description(), buf.buf, buf.len); |
b7200e83 | 686 | strbuf_reset(&buf); |
c10388c7 | 687 | if (launch_editor(edit_description(), &buf, NULL)) { |
b7200e83 JH |
688 | strbuf_release(&buf); |
689 | return -1; | |
690 | } | |
787cb8a4 | 691 | strbuf_stripspace(&buf, comment_line_char); |
b7200e83 JH |
692 | |
693 | strbuf_addf(&name, "branch.%s.description", branch_name); | |
e288b3de JH |
694 | if (buf.len || exists) |
695 | git_config_set(name.buf, buf.len ? buf.buf : NULL); | |
b7200e83 JH |
696 | strbuf_release(&name); |
697 | strbuf_release(&buf); | |
698 | ||
bd25f890 | 699 | return 0; |
b7200e83 JH |
700 | } |
701 | ||
c31820c2 LH |
702 | int cmd_branch(int argc, const char **argv, const char *prefix) |
703 | { | |
6e0a2ca0 GC |
704 | /* possible actions */ |
705 | int delete = 0, rename = 0, copy = 0, list = 0, | |
706 | unset_upstream = 0, show_current = 0, edit_description = 0; | |
6183d826 | 707 | const char *new_upstream = NULL; |
6e0a2ca0 GC |
708 | int noncreate_actions = 0; |
709 | /* possible options */ | |
961b130d GC |
710 | int reflog = 0, quiet = 0, icase = 0, force = 0, |
711 | recurse_submodules_explicit = 0; | |
9ed36cfa | 712 | enum branch_track track; |
b9f7daa6 | 713 | struct ref_filter filter = REF_FILTER_INIT; |
98e7ab6d JH |
714 | static struct ref_sorting *sorting; |
715 | struct string_list sorting_options = STRING_LIST_INIT_DUP; | |
4a68e36d | 716 | struct ref_format format = REF_FORMAT_INIT; |
a8dfd5ea PH |
717 | |
718 | struct option options[] = { | |
24a85214 | 719 | OPT_GROUP(N_("Generic options")), |
1511b22d | 720 | OPT__VERBOSE(&filter.verbose, |
24a85214 NTND |
721 | N_("show hash and subject, give twice for upstream branch")), |
722 | OPT__QUIET(&quiet, N_("suppress informational messages")), | |
6327f0ef | 723 | OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)", |
d3115660 | 724 | N_("set branch tracking configuration"), |
15f00281 | 725 | PARSE_OPT_OPTARG, |
d3115660 | 726 | parse_opt_tracking_mode), |
3e4a67b4 NTND |
727 | OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"), |
728 | BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN), | |
ab86885a | 729 | OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")), |
11de8dd7 | 730 | OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("unset the upstream info")), |
24a85214 | 731 | OPT__COLOR(&branch_use_color, N_("use colored output")), |
e12cb98e JH |
732 | OPT_SET_INT_F('r', "remotes", &filter.kind, N_("act on remote-tracking branches"), |
733 | FILTER_REFS_REMOTES, | |
734 | PARSE_OPT_NONEG), | |
1511b22d | 735 | OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")), |
ac3f5a34 | 736 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")), |
1511b22d | 737 | OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")), |
ac3f5a34 | 738 | OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")), |
1511b22d | 739 | OPT__ABBREV(&filter.abbrev), |
a8dfd5ea | 740 | |
24a85214 | 741 | OPT_GROUP(N_("Specific git-branch actions:")), |
e12cb98e JH |
742 | 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), | |
24a85214 NTND |
745 | 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), | |
9d4fcfe1 | 749 | OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty, |
aabfdc95 | 750 | N_("do not output a newline after empty formatted refs")), |
52d59cc6 SD |
751 | OPT_BIT('c', "copy", ©, N_("copy a branch and its reflog"), 1), |
752 | OPT_BIT('C', NULL, ©, N_("copy a branch, even if target exists"), 2), | |
a15d5981 | 753 | OPT_BOOL('l', "list", &list, N_("list branch names")), |
0ecb1fc7 | 754 | OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")), |
055930bc | 755 | OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")), |
d5d09d47 SB |
756 | OPT_BOOL(0, "edit-description", &edit_description, |
757 | N_("edit the description for the branch")), | |
c01b56a3 | 758 | OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE), |
1511b22d KN |
759 | OPT_MERGED(&filter, N_("print only branches that are merged")), |
760 | OPT_NO_MERGED(&filter, N_("print only branches that are not merged")), | |
24a85214 | 761 | OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")), |
98e7ab6d | 762 | OPT_REF_SORT(&sorting_options), |
203c8533 DL |
763 | OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"), |
764 | N_("print only branches of the object"), parse_opt_object_name), | |
3bb16a8b | 765 | OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), |
961b130d | 766 | OPT_BOOL(0, "recurse-submodules", &recurse_submodules_explicit, N_("recurse through submodules")), |
4a68e36d | 767 | OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), |
a8dfd5ea PH |
768 | OPT_END(), |
769 | }; | |
c31820c2 | 770 | |
56b43607 KN |
771 | setup_ref_filter_porcelain_msg(); |
772 | ||
1511b22d KN |
773 | filter.kind = FILTER_REFS_BRANCHES; |
774 | filter.abbrev = -1; | |
775 | ||
1dacfbcf NTND |
776 | if (argc == 2 && !strcmp(argv[1], "-h")) |
777 | usage_with_options(builtin_branch_usage, options); | |
778 | ||
56d26ade VD |
779 | /* |
780 | * Try to set sort keys from config. If config does not set any, | |
781 | * fall back on default (refname) sorting. | |
782 | */ | |
98e7ab6d | 783 | git_config(git_branch_config, &sorting_options); |
56d26ade VD |
784 | if (!sorting_options.nr) |
785 | string_list_append(&sorting_options, "refname"); | |
6b2f2d98 | 786 | |
9ed36cfa | 787 | track = git_branch_track; |
c976d415 | 788 | |
0f2dc722 | 789 | head = resolve_refdup("HEAD", 0, &head_oid, NULL); |
c31820c2 | 790 | if (!head) |
12b99928 | 791 | die(_("failed to resolve HEAD as a valid ref")); |
e3f1da98 | 792 | if (!strcmp(head, "HEAD")) |
1511b22d | 793 | filter.detached = 1; |
e3f1da98 RS |
794 | else if (!skip_prefix(head, "refs/heads/", &head)) |
795 | die(_("HEAD not found below refs/heads!")); | |
ebe31ef2 | 796 | |
37782920 SB |
797 | argc = parse_options(argc, argv, prefix, options, builtin_branch_usage, |
798 | 0); | |
cddd127b | 799 | |
0ecb1fc7 DU |
800 | if (!delete && !rename && !copy && !edit_description && !new_upstream && |
801 | !show_current && !unset_upstream && argc == 0) | |
cddd127b MG |
802 | list = 1; |
803 | ||
21bf9339 AL |
804 | if (filter.with_commit || filter.no_commit || |
805 | filter.reachable_from || filter.unreachable_from || filter.points_at.nr) | |
d0403508 JK |
806 | list = 1; |
807 | ||
6e0a2ca0 GC |
808 | noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream + |
809 | !!show_current + !!list + !!edit_description + | |
810 | !!unset_upstream; | |
811 | if (noncreate_actions > 1) | |
049716b3 | 812 | usage_with_options(builtin_branch_usage, options); |
c31820c2 | 813 | |
961b130d GC |
814 | 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 | ||
1511b22d KN |
825 | if (filter.abbrev == -1) |
826 | filter.abbrev = DEFAULT_ABBREV; | |
3bb16a8b NTND |
827 | filter.ignore_case = icase; |
828 | ||
ebe31ef2 | 829 | finalize_colopts(&colopts, -1); |
1511b22d | 830 | if (filter.verbose) { |
ebe31ef2 | 831 | if (explicitly_enable_column(colopts)) |
12909b6b | 832 | die(_("options '%s' and '%s' cannot be used together"), "--column", "--verbose"); |
ebe31ef2 NTND |
833 | colopts = 0; |
834 | } | |
b792c067 | 835 | |
356e91f2 MG |
836 | if (force) { |
837 | delete *= 2; | |
838 | rename *= 2; | |
52d59cc6 | 839 | copy *= 2; |
356e91f2 MG |
840 | } |
841 | ||
d74b541e | 842 | if (list) |
0ae19de7 | 843 | setup_auto_pager("branch", 1); |
d74b541e | 844 | |
2935a978 RJ |
845 | UNLEAK(sorting_options); |
846 | ||
640d0401 NTND |
847 | if (delete) { |
848 | if (!argc) | |
849 | die(_("branch name required")); | |
1511b22d | 850 | return delete_branches(argc, argv, delete > 1, filter.kind, quiet); |
0ecb1fc7 DU |
851 | } else if (show_current) { |
852 | print_current_branch_name(); | |
853 | return 0; | |
640d0401 | 854 | } else if (list) { |
ffdd02a5 | 855 | /* git branch --list also shows HEAD when it is detached */ |
1511b22d KN |
856 | if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached) |
857 | filter.kind |= FILTER_REFS_DETACHED_HEAD; | |
858 | filter.name_patterns = argv; | |
3bb16a8b NTND |
859 | /* |
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 | |
30aa96cd | 863 | * local branches 'refs/heads/...' and finally remote-tracking |
3bb16a8b NTND |
864 | * branches 'refs/remotes/...'. |
865 | */ | |
98e7ab6d | 866 | sorting = ref_sorting_options(&sorting_options); |
7c269a7b | 867 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); |
2708ce62 ÆAB |
868 | ref_sorting_set_sort_flags_all( |
869 | sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1); | |
d72d4f92 | 870 | print_ref_list(&filter, sorting, &format, &output); |
ebe31ef2 NTND |
871 | print_columns(&output, colopts, NULL); |
872 | string_list_clear(&output, 0); | |
d72d4f92 | 873 | ref_sorting_release(sorting); |
b571fb98 | 874 | ref_filter_clear(&filter); |
ca417990 | 875 | return 0; |
2e3c894f | 876 | } else if (edit_description) { |
b7200e83 | 877 | const char *branch_name; |
c2d17ba3 | 878 | struct strbuf branch_ref = STRBUF_INIT; |
0dc4e5c5 RJ |
879 | struct strbuf buf = STRBUF_INIT; |
880 | int ret = 1; /* assume failure */ | |
c2d17ba3 | 881 | |
75135b23 | 882 | if (!argc) { |
1511b22d | 883 | if (filter.detached) |
12b99928 | 884 | die(_("cannot give description to detached HEAD")); |
b7200e83 | 885 | branch_name = head; |
0dc4e5c5 RJ |
886 | } else if (argc == 1) { |
887 | strbuf_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL); | |
888 | branch_name = buf.buf; | |
889 | } else { | |
43722c4d | 890 | die(_("cannot edit description of more than one branch")); |
0dc4e5c5 | 891 | } |
c2d17ba3 JH |
892 | |
893 | strbuf_addf(&branch_ref, "refs/heads/%s", branch_name); | |
0dc4e5c5 | 894 | if (!ref_exists(branch_ref.buf)) |
7a6ccdfb | 895 | error((!argc || branch_checked_out(branch_ref.buf)) |
12b99928 IJI |
896 | ? _("no commit on branch '%s' yet") |
897 | : _("no branch named '%s'"), | |
0dc4e5c5 RJ |
898 | branch_name); |
899 | else if (!edit_branch_description(branch_name)) | |
900 | ret = 0; /* happy */ | |
901 | ||
c2d17ba3 | 902 | strbuf_release(&branch_ref); |
0dc4e5c5 | 903 | strbuf_release(&buf); |
c2d17ba3 | 904 | |
0dc4e5c5 | 905 | return ret; |
77e7267e | 906 | } else if (copy || rename) { |
d1520c4b JM |
907 | if (!argc) |
908 | die(_("branch name required")); | |
77e7267e | 909 | else if ((argc == 1) && filter.detached) |
12b99928 IJI |
910 | die(copy? _("cannot copy the current branch while not on any") |
911 | : _("cannot rename the current branch while not on any")); | |
d1520c4b | 912 | else if (argc == 1) |
77e7267e | 913 | copy_or_rename_branch(head, argv[0], copy, copy + rename > 1); |
3706ed29 | 914 | else if (argc == 2) |
77e7267e | 915 | copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1); |
3706ed29 | 916 | else |
77e7267e RJ |
917 | die(copy? _("too many branches for a copy operation") |
918 | : _("too many arguments for a rename operation")); | |
6183d826 | 919 | } else if (new_upstream) { |
0dc4e5c5 RJ |
920 | struct branch *branch; |
921 | struct strbuf buf = STRBUF_INIT; | |
6183d826 | 922 | |
0dc4e5c5 RJ |
923 | 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 | |
f7776235 | 929 | die(_("too many arguments to set new upstream")); |
8efb8899 NTND |
930 | |
931 | if (!branch) { | |
932 | if (!argc || !strcmp(argv[0], "HEAD")) | |
933 | die(_("could not set upstream of HEAD to %s when " | |
12b99928 | 934 | "it does not point to any branch"), |
8efb8899 NTND |
935 | new_upstream); |
936 | die(_("no such branch '%s'"), argv[0]); | |
937 | } | |
938 | ||
bcfc82bd | 939 | if (!ref_exists(branch->refname)) { |
7a6ccdfb | 940 | if (!argc || branch_checked_out(branch->refname)) |
12b99928 | 941 | die(_("no commit on branch '%s' yet"), branch->name); |
6183d826 | 942 | die(_("branch '%s' does not exist"), branch->name); |
bcfc82bd | 943 | } |
6183d826 | 944 | |
e89f151d GC |
945 | dwim_and_setup_tracking(the_repository, branch->name, |
946 | new_upstream, BRANCH_TRACK_OVERRIDE, | |
947 | quiet); | |
0dc4e5c5 | 948 | strbuf_release(&buf); |
b84869ef | 949 | } else if (unset_upstream) { |
0dc4e5c5 | 950 | struct branch *branch; |
b84869ef CMN |
951 | struct strbuf buf = STRBUF_INIT; |
952 | ||
0dc4e5c5 RJ |
953 | 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 | |
f7776235 | 959 | die(_("too many arguments to unset upstream")); |
8efb8899 NTND |
960 | |
961 | if (!branch) { | |
962 | if (!argc || !strcmp(argv[0], "HEAD")) | |
963 | die(_("could not unset upstream of HEAD when " | |
12b99928 | 964 | "it does not point to any branch")); |
8efb8899 NTND |
965 | die(_("no such branch '%s'"), argv[0]); |
966 | } | |
967 | ||
54d07f2e | 968 | if (!branch_has_merge_config(branch)) |
12b99928 | 969 | die(_("branch '%s' has no upstream information"), branch->name); |
b84869ef | 970 | |
0dc4e5c5 | 971 | strbuf_reset(&buf); |
b84869ef | 972 | strbuf_addf(&buf, "branch.%s.remote", branch->name); |
504ee129 | 973 | git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); |
b84869ef CMN |
974 | strbuf_reset(&buf); |
975 | strbuf_addf(&buf, "branch.%s.merge", branch->name); | |
504ee129 | 976 | git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); |
b84869ef | 977 | strbuf_release(&buf); |
6e0a2ca0 | 978 | } else if (!noncreate_actions && argc > 0 && argc <= 2) { |
961b130d GC |
979 | const char *branch_name = argv[0]; |
980 | const char *start_name = argc == 2 ? argv[1] : head; | |
981 | ||
1511b22d | 982 | if (filter.kind != FILTER_REFS_BRANCHES) |
12b99928 | 983 | die(_("the -a, and -r, options to 'git branch' do not take a branch name.\n" |
1fde99cf | 984 | "Did you mean to use: -a|-r --list <pattern>?")); |
b347d06b CMN |
985 | |
986 | if (track == BRANCH_TRACK_OVERRIDE) | |
12b99928 | 987 | die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead")); |
b347d06b | 988 | |
961b130d GC |
989 | 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); | |
6e8f993a | 997 | } else |
a8dfd5ea | 998 | usage_with_options(builtin_branch_usage, options); |
c31820c2 LH |
999 | |
1000 | return 0; | |
1001 | } |