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