]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Builtin "git branch" | |
3 | * | |
4 | * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com> | |
5 | * Based on git-branch.sh by Junio C Hamano. | |
6 | */ | |
7 | ||
8 | #define USE_THE_REPOSITORY_VARIABLE | |
9 | ||
10 | #include "builtin.h" | |
11 | #include "config.h" | |
12 | #include "color.h" | |
13 | #include "editor.h" | |
14 | #include "environment.h" | |
15 | #include "refs.h" | |
16 | #include "commit.h" | |
17 | #include "gettext.h" | |
18 | #include "object-name.h" | |
19 | #include "remote.h" | |
20 | #include "parse-options.h" | |
21 | #include "branch.h" | |
22 | #include "path.h" | |
23 | #include "string-list.h" | |
24 | #include "column.h" | |
25 | #include "utf8.h" | |
26 | #include "ref-filter.h" | |
27 | #include "worktree.h" | |
28 | #include "help.h" | |
29 | #include "advice.h" | |
30 | #include "commit-reach.h" | |
31 | ||
32 | static const char * const builtin_branch_usage[] = { | |
33 | N_("git branch [<options>] [-r | -a] [--merged] [--no-merged]"), | |
34 | N_("git branch [<options>] [-f] [--recurse-submodules] <branch-name> [<start-point>]"), | |
35 | N_("git branch [<options>] [-l] [<pattern>...]"), | |
36 | N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."), | |
37 | N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"), | |
38 | N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"), | |
39 | N_("git branch [<options>] [-r | -a] [--points-at]"), | |
40 | N_("git branch [<options>] [-r | -a] [--format]"), | |
41 | NULL | |
42 | }; | |
43 | ||
44 | static const char *head; | |
45 | static struct object_id head_oid; | |
46 | static int recurse_submodules = 0; | |
47 | static int submodule_propagate_branches = 0; | |
48 | ||
49 | static int branch_use_color = -1; | |
50 | static char branch_colors[][COLOR_MAXLEN] = { | |
51 | GIT_COLOR_RESET, | |
52 | GIT_COLOR_NORMAL, /* PLAIN */ | |
53 | GIT_COLOR_RED, /* REMOTE */ | |
54 | GIT_COLOR_NORMAL, /* LOCAL */ | |
55 | GIT_COLOR_GREEN, /* CURRENT */ | |
56 | GIT_COLOR_BLUE, /* UPSTREAM */ | |
57 | GIT_COLOR_CYAN, /* WORKTREE */ | |
58 | }; | |
59 | enum color_branch { | |
60 | BRANCH_COLOR_RESET = 0, | |
61 | BRANCH_COLOR_PLAIN = 1, | |
62 | BRANCH_COLOR_REMOTE = 2, | |
63 | BRANCH_COLOR_LOCAL = 3, | |
64 | BRANCH_COLOR_CURRENT = 4, | |
65 | BRANCH_COLOR_UPSTREAM = 5, | |
66 | BRANCH_COLOR_WORKTREE = 6 | |
67 | }; | |
68 | ||
69 | static const char *color_branch_slots[] = { | |
70 | [BRANCH_COLOR_RESET] = "reset", | |
71 | [BRANCH_COLOR_PLAIN] = "plain", | |
72 | [BRANCH_COLOR_REMOTE] = "remote", | |
73 | [BRANCH_COLOR_LOCAL] = "local", | |
74 | [BRANCH_COLOR_CURRENT] = "current", | |
75 | [BRANCH_COLOR_UPSTREAM] = "upstream", | |
76 | [BRANCH_COLOR_WORKTREE] = "worktree", | |
77 | }; | |
78 | ||
79 | static struct string_list output = STRING_LIST_INIT_DUP; | |
80 | static unsigned int colopts; | |
81 | ||
82 | define_list_config_array(color_branch_slots); | |
83 | ||
84 | static int git_branch_config(const char *var, const char *value, | |
85 | const struct config_context *ctx, void *cb) | |
86 | { | |
87 | const char *slot_name; | |
88 | ||
89 | if (!strcmp(var, "branch.sort")) { | |
90 | if (!value) | |
91 | return config_error_nonbool(var); | |
92 | string_list_append(cb, value); | |
93 | return 0; | |
94 | } | |
95 | ||
96 | if (starts_with(var, "column.")) | |
97 | return git_column_config(var, value, "branch", &colopts); | |
98 | if (!strcmp(var, "color.branch")) { | |
99 | branch_use_color = git_config_colorbool(var, value); | |
100 | return 0; | |
101 | } | |
102 | if (skip_prefix(var, "color.branch.", &slot_name)) { | |
103 | int slot = LOOKUP_CONFIG(color_branch_slots, slot_name); | |
104 | if (slot < 0) | |
105 | return 0; | |
106 | if (!value) | |
107 | return config_error_nonbool(var); | |
108 | return color_parse(value, branch_colors[slot]); | |
109 | } | |
110 | if (!strcmp(var, "submodule.recurse")) { | |
111 | recurse_submodules = git_config_bool(var, value); | |
112 | return 0; | |
113 | } | |
114 | if (!strcasecmp(var, "submodule.propagateBranches")) { | |
115 | submodule_propagate_branches = git_config_bool(var, value); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | if (git_color_config(var, value, cb) < 0) | |
120 | return -1; | |
121 | ||
122 | return git_default_config(var, value, ctx, cb); | |
123 | } | |
124 | ||
125 | static const char *branch_get_color(enum color_branch ix) | |
126 | { | |
127 | if (want_color(branch_use_color)) | |
128 | return branch_colors[ix]; | |
129 | return ""; | |
130 | } | |
131 | ||
132 | static int branch_merged(int kind, const char *name, | |
133 | struct commit *rev, struct commit *head_rev) | |
134 | { | |
135 | /* | |
136 | * This checks whether the merge bases of branch and HEAD (or | |
137 | * the other branch this branch builds upon) contains the | |
138 | * branch, which means that the branch has already been merged | |
139 | * safely to HEAD (or the other branch). | |
140 | */ | |
141 | struct commit *reference_rev = NULL; | |
142 | const char *reference_name = NULL; | |
143 | void *reference_name_to_free = NULL; | |
144 | int merged; | |
145 | ||
146 | if (kind == FILTER_REFS_BRANCHES) { | |
147 | struct branch *branch = branch_get(name); | |
148 | const char *upstream = branch_get_upstream(branch, NULL); | |
149 | struct object_id oid; | |
150 | ||
151 | if (upstream && | |
152 | (reference_name = reference_name_to_free = | |
153 | refs_resolve_refdup(get_main_ref_store(the_repository), upstream, RESOLVE_REF_READING, | |
154 | &oid, NULL)) != NULL) | |
155 | reference_rev = lookup_commit_reference(the_repository, | |
156 | &oid); | |
157 | } | |
158 | if (!reference_rev) | |
159 | reference_rev = head_rev; | |
160 | ||
161 | merged = reference_rev ? repo_in_merge_bases(the_repository, rev, | |
162 | reference_rev) : 0; | |
163 | if (merged < 0) | |
164 | exit(128); | |
165 | ||
166 | /* | |
167 | * After the safety valve is fully redefined to "check with | |
168 | * upstream, if any, otherwise with HEAD", we should just | |
169 | * return the result of the repo_in_merge_bases() above without | |
170 | * any of the following code, but during the transition period, | |
171 | * a gentle reminder is in order. | |
172 | */ | |
173 | if (head_rev != reference_rev) { | |
174 | int expect = head_rev ? repo_in_merge_bases(the_repository, rev, head_rev) : 0; | |
175 | if (expect < 0) | |
176 | exit(128); | |
177 | if (expect == merged) | |
178 | ; /* okay */ | |
179 | else if (merged) | |
180 | warning(_("deleting branch '%s' that has been merged to\n" | |
181 | " '%s', but not yet merged to HEAD"), | |
182 | name, reference_name); | |
183 | else | |
184 | warning(_("not deleting branch '%s' that is not yet merged to\n" | |
185 | " '%s', even though it is merged to HEAD"), | |
186 | name, reference_name); | |
187 | } | |
188 | free(reference_name_to_free); | |
189 | return merged; | |
190 | } | |
191 | ||
192 | static int check_branch_commit(const char *branchname, const char *refname, | |
193 | const struct object_id *oid, struct commit *head_rev, | |
194 | int kinds, int force) | |
195 | { | |
196 | struct commit *rev = lookup_commit_reference(the_repository, oid); | |
197 | if (!force && !rev) { | |
198 | error(_("couldn't look up commit object for '%s'"), refname); | |
199 | return -1; | |
200 | } | |
201 | if (!force && !branch_merged(kinds, branchname, rev, head_rev)) { | |
202 | error(_("the branch '%s' is not fully merged"), branchname); | |
203 | advise_if_enabled(ADVICE_FORCE_DELETE_BRANCH, | |
204 | _("If you are sure you want to delete it, " | |
205 | "run 'git branch -D %s'"), branchname); | |
206 | return -1; | |
207 | } | |
208 | return 0; | |
209 | } | |
210 | ||
211 | static void delete_branch_config(const char *branchname) | |
212 | { | |
213 | struct strbuf buf = STRBUF_INIT; | |
214 | strbuf_addf(&buf, "branch.%s", branchname); | |
215 | if (repo_config_rename_section(the_repository, buf.buf, NULL) < 0) | |
216 | warning(_("update of config-file failed")); | |
217 | strbuf_release(&buf); | |
218 | } | |
219 | ||
220 | static int delete_branches(int argc, const char **argv, int force, int kinds, | |
221 | int quiet) | |
222 | { | |
223 | struct commit *head_rev = NULL; | |
224 | struct object_id oid; | |
225 | char *name = NULL; | |
226 | const char *fmt; | |
227 | int i; | |
228 | int ret = 0; | |
229 | int remote_branch = 0; | |
230 | struct strbuf bname = STRBUF_INIT; | |
231 | unsigned allowed_interpret; | |
232 | struct string_list refs_to_delete = STRING_LIST_INIT_DUP; | |
233 | struct string_list_item *item; | |
234 | int branch_name_pos; | |
235 | const char *fmt_remotes = "refs/remotes/%s"; | |
236 | ||
237 | switch (kinds) { | |
238 | case FILTER_REFS_REMOTES: | |
239 | fmt = fmt_remotes; | |
240 | /* For subsequent UI messages */ | |
241 | remote_branch = 1; | |
242 | allowed_interpret = INTERPRET_BRANCH_REMOTE; | |
243 | ||
244 | force = 1; | |
245 | break; | |
246 | case FILTER_REFS_BRANCHES: | |
247 | fmt = "refs/heads/%s"; | |
248 | allowed_interpret = INTERPRET_BRANCH_LOCAL; | |
249 | break; | |
250 | default: | |
251 | die(_("cannot use -a with -d")); | |
252 | } | |
253 | branch_name_pos = strcspn(fmt, "%"); | |
254 | ||
255 | if (!force) | |
256 | head_rev = lookup_commit_reference(the_repository, &head_oid); | |
257 | ||
258 | for (i = 0; i < argc; i++, strbuf_reset(&bname)) { | |
259 | char *target = NULL; | |
260 | int flags = 0; | |
261 | ||
262 | copy_branchname(&bname, argv[i], allowed_interpret); | |
263 | free(name); | |
264 | name = mkpathdup(fmt, bname.buf); | |
265 | ||
266 | if (kinds == FILTER_REFS_BRANCHES) { | |
267 | const char *path; | |
268 | if ((path = branch_checked_out(name))) { | |
269 | error(_("cannot delete branch '%s' " | |
270 | "used by worktree at '%s'"), | |
271 | bname.buf, path); | |
272 | ret = 1; | |
273 | continue; | |
274 | } | |
275 | } | |
276 | ||
277 | target = refs_resolve_refdup(get_main_ref_store(the_repository), | |
278 | name, | |
279 | RESOLVE_REF_READING | |
280 | | RESOLVE_REF_NO_RECURSE | |
281 | | RESOLVE_REF_ALLOW_BAD_NAME, | |
282 | &oid, &flags); | |
283 | if (!target) { | |
284 | if (remote_branch) { | |
285 | error(_("remote-tracking branch '%s' not found"), bname.buf); | |
286 | } else { | |
287 | char *virtual_name = mkpathdup(fmt_remotes, bname.buf); | |
288 | char *virtual_target = refs_resolve_refdup(get_main_ref_store(the_repository), | |
289 | virtual_name, | |
290 | RESOLVE_REF_READING | |
291 | | RESOLVE_REF_NO_RECURSE | |
292 | | RESOLVE_REF_ALLOW_BAD_NAME, | |
293 | &oid, | |
294 | &flags); | |
295 | FREE_AND_NULL(virtual_name); | |
296 | ||
297 | if (virtual_target) | |
298 | error(_("branch '%s' not found.\n" | |
299 | "Did you forget --remote?"), | |
300 | bname.buf); | |
301 | else | |
302 | error(_("branch '%s' not found"), bname.buf); | |
303 | FREE_AND_NULL(virtual_target); | |
304 | } | |
305 | ret = 1; | |
306 | continue; | |
307 | } | |
308 | ||
309 | if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) && | |
310 | check_branch_commit(bname.buf, name, &oid, head_rev, kinds, | |
311 | force)) { | |
312 | ret = 1; | |
313 | goto next; | |
314 | } | |
315 | ||
316 | item = string_list_append(&refs_to_delete, name); | |
317 | item->util = xstrdup((flags & REF_ISBROKEN) ? "broken" | |
318 | : (flags & REF_ISSYMREF) ? target | |
319 | : repo_find_unique_abbrev(the_repository, &oid, DEFAULT_ABBREV)); | |
320 | ||
321 | next: | |
322 | free(target); | |
323 | } | |
324 | ||
325 | if (refs_delete_refs(get_main_ref_store(the_repository), NULL, &refs_to_delete, REF_NO_DEREF)) | |
326 | ret = 1; | |
327 | ||
328 | for_each_string_list_item(item, &refs_to_delete) { | |
329 | char *describe_ref = item->util; | |
330 | char *name = item->string; | |
331 | if (!refs_ref_exists(get_main_ref_store(the_repository), name)) { | |
332 | char *refname = name + branch_name_pos; | |
333 | if (!quiet) | |
334 | printf(remote_branch | |
335 | ? _("Deleted remote-tracking branch %s (was %s).\n") | |
336 | : _("Deleted branch %s (was %s).\n"), | |
337 | name + branch_name_pos, describe_ref); | |
338 | ||
339 | delete_branch_config(refname); | |
340 | } | |
341 | free(describe_ref); | |
342 | } | |
343 | string_list_clear(&refs_to_delete, 0); | |
344 | ||
345 | free(name); | |
346 | strbuf_release(&bname); | |
347 | ||
348 | return ret; | |
349 | } | |
350 | ||
351 | static int calc_maxwidth(struct ref_array *refs, int remote_bonus) | |
352 | { | |
353 | int i, max = 0; | |
354 | for (i = 0; i < refs->nr; i++) { | |
355 | struct ref_array_item *it = refs->items[i]; | |
356 | const char *desc = it->refname; | |
357 | int w; | |
358 | ||
359 | skip_prefix(it->refname, "refs/heads/", &desc); | |
360 | skip_prefix(it->refname, "refs/remotes/", &desc); | |
361 | if (it->kind == FILTER_REFS_DETACHED_HEAD) { | |
362 | char *head_desc = get_head_description(); | |
363 | w = utf8_strwidth(head_desc); | |
364 | free(head_desc); | |
365 | } else | |
366 | w = utf8_strwidth(desc); | |
367 | ||
368 | if (it->kind == FILTER_REFS_REMOTES) | |
369 | w += remote_bonus; | |
370 | if (w > max) | |
371 | max = w; | |
372 | } | |
373 | return max; | |
374 | } | |
375 | ||
376 | static const char *quote_literal_for_format(const char *s) | |
377 | { | |
378 | static struct strbuf buf = STRBUF_INIT; | |
379 | ||
380 | strbuf_reset(&buf); | |
381 | while (strbuf_expand_step(&buf, &s)) | |
382 | strbuf_addstr(&buf, "%%"); | |
383 | return buf.buf; | |
384 | } | |
385 | ||
386 | static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix) | |
387 | { | |
388 | struct strbuf fmt = STRBUF_INIT; | |
389 | struct strbuf local = STRBUF_INIT; | |
390 | struct strbuf remote = STRBUF_INIT; | |
391 | ||
392 | strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else)%%(if)%%(worktreepath)%%(then)+ %s%%(else) %s%%(end)%%(end)", | |
393 | branch_get_color(BRANCH_COLOR_CURRENT), | |
394 | branch_get_color(BRANCH_COLOR_WORKTREE), | |
395 | branch_get_color(BRANCH_COLOR_LOCAL)); | |
396 | strbuf_addf(&remote, " %s", | |
397 | branch_get_color(BRANCH_COLOR_REMOTE)); | |
398 | ||
399 | if (filter->verbose) { | |
400 | struct strbuf obname = STRBUF_INIT; | |
401 | ||
402 | if (filter->abbrev < 0) | |
403 | strbuf_addf(&obname, "%%(objectname:short)"); | |
404 | else if (!filter->abbrev) | |
405 | strbuf_addf(&obname, "%%(objectname)"); | |
406 | else | |
407 | strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev); | |
408 | ||
409 | strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth); | |
410 | strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET)); | |
411 | strbuf_addf(&local, " %s ", obname.buf); | |
412 | ||
413 | if (filter->verbose > 1) | |
414 | { | |
415 | strbuf_addf(&local, "%%(if:notequals=*)%%(HEAD)%%(then)%%(if)%%(worktreepath)%%(then)(%s%%(worktreepath)%s) %%(end)%%(end)", | |
416 | branch_get_color(BRANCH_COLOR_WORKTREE), branch_get_color(BRANCH_COLOR_RESET)); | |
417 | strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)" | |
418 | "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)", | |
419 | branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET)); | |
420 | } | |
421 | else | |
422 | strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)"); | |
423 | ||
424 | strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s" | |
425 | "%%(if)%%(symref)%%(then) -> %%(symref:short)" | |
426 | "%%(else) %s %%(contents:subject)%%(end)", | |
427 | maxwidth, quote_literal_for_format(remote_prefix), | |
428 | branch_get_color(BRANCH_COLOR_RESET), obname.buf); | |
429 | strbuf_release(&obname); | |
430 | } else { | |
431 | strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", | |
432 | branch_get_color(BRANCH_COLOR_RESET)); | |
433 | strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)", | |
434 | quote_literal_for_format(remote_prefix), | |
435 | branch_get_color(BRANCH_COLOR_RESET)); | |
436 | } | |
437 | ||
438 | strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf); | |
439 | ||
440 | strbuf_release(&local); | |
441 | strbuf_release(&remote); | |
442 | return strbuf_detach(&fmt, NULL); | |
443 | } | |
444 | ||
445 | static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, | |
446 | struct ref_format *format, struct string_list *output) | |
447 | { | |
448 | int i; | |
449 | struct ref_array array; | |
450 | int maxwidth = 0; | |
451 | const char *remote_prefix = ""; | |
452 | char *to_free = NULL; | |
453 | ||
454 | /* | |
455 | * If we are listing more than just remote branches, | |
456 | * then remote branches will have a "remotes/" prefix. | |
457 | * We need to account for this in the width. | |
458 | */ | |
459 | if (filter->kind != FILTER_REFS_REMOTES) | |
460 | remote_prefix = "remotes/"; | |
461 | ||
462 | memset(&array, 0, sizeof(array)); | |
463 | ||
464 | filter_refs(&array, filter, filter->kind); | |
465 | ||
466 | if (filter->verbose) | |
467 | maxwidth = calc_maxwidth(&array, strlen(remote_prefix)); | |
468 | ||
469 | if (!format->format) | |
470 | format->format = to_free = build_format(filter, maxwidth, remote_prefix); | |
471 | format->use_color = branch_use_color; | |
472 | ||
473 | if (verify_ref_format(format)) | |
474 | die(_("unable to parse format string")); | |
475 | ||
476 | filter_ahead_behind(the_repository, &array); | |
477 | ref_array_sort(sorting, &array); | |
478 | ||
479 | if (column_active(colopts)) { | |
480 | struct strbuf out = STRBUF_INIT, err = STRBUF_INIT; | |
481 | ||
482 | assert(!filter->verbose && "--column and --verbose are incompatible"); | |
483 | ||
484 | for (i = 0; i < array.nr; i++) { | |
485 | strbuf_reset(&err); | |
486 | strbuf_reset(&out); | |
487 | if (format_ref_array_item(array.items[i], format, &out, &err)) | |
488 | die("%s", err.buf); | |
489 | ||
490 | /* format to a string_list to let print_columns() do its job */ | |
491 | string_list_append(output, out.buf); | |
492 | } | |
493 | ||
494 | strbuf_release(&err); | |
495 | strbuf_release(&out); | |
496 | } else { | |
497 | print_formatted_ref_array(&array, format); | |
498 | } | |
499 | ||
500 | ref_array_clear(&array); | |
501 | free(to_free); | |
502 | } | |
503 | ||
504 | static void print_current_branch_name(void) | |
505 | { | |
506 | int flags; | |
507 | const char *refname = refs_resolve_ref_unsafe(get_main_ref_store(the_repository), | |
508 | "HEAD", 0, NULL, &flags); | |
509 | const char *shortname; | |
510 | if (!refname) | |
511 | die(_("could not resolve HEAD")); | |
512 | else if (!(flags & REF_ISSYMREF)) | |
513 | return; | |
514 | else if (skip_prefix(refname, "refs/heads/", &shortname)) | |
515 | puts(shortname); | |
516 | else | |
517 | die(_("HEAD (%s) points outside of refs/heads/"), refname); | |
518 | } | |
519 | ||
520 | static void reject_rebase_or_bisect_branch(struct worktree **worktrees, | |
521 | const char *target) | |
522 | { | |
523 | int i; | |
524 | ||
525 | for (i = 0; worktrees[i]; i++) { | |
526 | struct worktree *wt = worktrees[i]; | |
527 | ||
528 | if (!wt->is_detached) | |
529 | continue; | |
530 | ||
531 | if (is_worktree_being_rebased(wt, target)) | |
532 | die(_("branch %s is being rebased at %s"), | |
533 | target, wt->path); | |
534 | ||
535 | if (is_worktree_being_bisected(wt, target)) | |
536 | die(_("branch %s is being bisected at %s"), | |
537 | target, wt->path); | |
538 | } | |
539 | } | |
540 | ||
541 | /* | |
542 | * Update all per-worktree HEADs pointing at the old ref to point the new ref. | |
543 | * This will be used when renaming a branch. Returns 0 if successful, non-zero | |
544 | * otherwise. | |
545 | */ | |
546 | static int replace_each_worktree_head_symref(struct worktree **worktrees, | |
547 | const char *oldref, const char *newref, | |
548 | const char *logmsg) | |
549 | { | |
550 | int ret = 0; | |
551 | int i; | |
552 | ||
553 | for (i = 0; worktrees[i]; i++) { | |
554 | struct ref_store *refs; | |
555 | ||
556 | if (worktrees[i]->is_detached) | |
557 | continue; | |
558 | if (!worktrees[i]->head_ref) | |
559 | continue; | |
560 | if (strcmp(oldref, worktrees[i]->head_ref)) | |
561 | continue; | |
562 | ||
563 | refs = get_worktree_ref_store(worktrees[i]); | |
564 | if (refs_update_symref(refs, "HEAD", newref, logmsg)) | |
565 | ret = error(_("HEAD of working tree %s is not updated"), | |
566 | worktrees[i]->path); | |
567 | } | |
568 | ||
569 | return ret; | |
570 | } | |
571 | ||
572 | #define IS_HEAD 1 | |
573 | #define IS_ORPHAN 2 | |
574 | ||
575 | static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force) | |
576 | { | |
577 | struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT; | |
578 | struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT; | |
579 | const char *interpreted_oldname = NULL; | |
580 | const char *interpreted_newname = NULL; | |
581 | int recovery = 0, oldref_usage = 0; | |
582 | struct worktree **worktrees = get_worktrees(); | |
583 | ||
584 | if (check_branch_ref(&oldref, oldname)) { | |
585 | /* | |
586 | * Bad name --- this could be an attempt to rename a | |
587 | * ref that we used to allow to be created by accident. | |
588 | */ | |
589 | if (refs_ref_exists(get_main_ref_store(the_repository), oldref.buf)) | |
590 | recovery = 1; | |
591 | else { | |
592 | int code = die_message(_("invalid branch name: '%s'"), oldname); | |
593 | advise_if_enabled(ADVICE_REF_SYNTAX, | |
594 | _("See `man git check-ref-format`")); | |
595 | exit(code); | |
596 | } | |
597 | } | |
598 | ||
599 | for (int i = 0; worktrees[i]; i++) { | |
600 | struct worktree *wt = worktrees[i]; | |
601 | ||
602 | if (wt->head_ref && !strcmp(oldref.buf, wt->head_ref)) { | |
603 | oldref_usage |= IS_HEAD; | |
604 | if (is_null_oid(&wt->head_oid)) | |
605 | oldref_usage |= IS_ORPHAN; | |
606 | break; | |
607 | } | |
608 | } | |
609 | ||
610 | if ((copy || !(oldref_usage & IS_HEAD)) && !refs_ref_exists(get_main_ref_store(the_repository), oldref.buf)) { | |
611 | if (oldref_usage & IS_HEAD) | |
612 | die(_("no commit on branch '%s' yet"), oldname); | |
613 | else | |
614 | die(_("no branch named '%s'"), oldname); | |
615 | } | |
616 | ||
617 | /* | |
618 | * A command like "git branch -M currentbranch currentbranch" cannot | |
619 | * cause the worktree to become inconsistent with HEAD, so allow it. | |
620 | */ | |
621 | if (!strcmp(oldname, newname)) | |
622 | validate_branchname(newname, &newref); | |
623 | else | |
624 | validate_new_branchname(newname, &newref, force); | |
625 | ||
626 | reject_rebase_or_bisect_branch(worktrees, oldref.buf); | |
627 | ||
628 | if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) || | |
629 | !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) { | |
630 | BUG("expected prefix missing for refs"); | |
631 | } | |
632 | ||
633 | if (copy) | |
634 | strbuf_addf(&logmsg, "Branch: copied %s to %s", | |
635 | oldref.buf, newref.buf); | |
636 | else | |
637 | strbuf_addf(&logmsg, "Branch: renamed %s to %s", | |
638 | oldref.buf, newref.buf); | |
639 | ||
640 | if (!copy && !(oldref_usage & IS_ORPHAN) && | |
641 | refs_rename_ref(get_main_ref_store(the_repository), oldref.buf, newref.buf, logmsg.buf)) | |
642 | die(_("branch rename failed")); | |
643 | if (copy && refs_copy_existing_ref(get_main_ref_store(the_repository), oldref.buf, newref.buf, logmsg.buf)) | |
644 | die(_("branch copy failed")); | |
645 | ||
646 | if (recovery) { | |
647 | if (copy) | |
648 | warning(_("created a copy of a misnamed branch '%s'"), | |
649 | interpreted_oldname); | |
650 | else | |
651 | warning(_("renamed a misnamed branch '%s' away"), | |
652 | interpreted_oldname); | |
653 | } | |
654 | ||
655 | if (!copy && (oldref_usage & IS_HEAD) && | |
656 | replace_each_worktree_head_symref(worktrees, oldref.buf, newref.buf, | |
657 | logmsg.buf)) | |
658 | die(_("branch renamed to %s, but HEAD is not updated"), newname); | |
659 | ||
660 | strbuf_release(&logmsg); | |
661 | ||
662 | strbuf_addf(&oldsection, "branch.%s", interpreted_oldname); | |
663 | strbuf_addf(&newsection, "branch.%s", interpreted_newname); | |
664 | if (!copy && repo_config_rename_section(the_repository, oldsection.buf, newsection.buf) < 0) | |
665 | die(_("branch is renamed, but update of config-file failed")); | |
666 | if (copy && strcmp(interpreted_oldname, interpreted_newname) && | |
667 | repo_config_copy_section(the_repository, oldsection.buf, newsection.buf) < 0) | |
668 | die(_("branch is copied, but update of config-file failed")); | |
669 | strbuf_release(&oldref); | |
670 | strbuf_release(&newref); | |
671 | strbuf_release(&oldsection); | |
672 | strbuf_release(&newsection); | |
673 | free_worktrees(worktrees); | |
674 | } | |
675 | ||
676 | static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION") | |
677 | ||
678 | static int edit_branch_description(const char *branch_name) | |
679 | { | |
680 | int exists; | |
681 | struct strbuf buf = STRBUF_INIT; | |
682 | struct strbuf name = STRBUF_INIT; | |
683 | ||
684 | exists = !read_branch_desc(&buf, branch_name); | |
685 | if (!buf.len || buf.buf[buf.len-1] != '\n') | |
686 | strbuf_addch(&buf, '\n'); | |
687 | strbuf_commented_addf(&buf, comment_line_str, | |
688 | _("Please edit the description for the branch\n" | |
689 | " %s\n" | |
690 | "Lines starting with '%s' will be stripped.\n"), | |
691 | branch_name, comment_line_str); | |
692 | write_file_buf(edit_description(), buf.buf, buf.len); | |
693 | strbuf_reset(&buf); | |
694 | if (launch_editor(edit_description(), &buf, NULL)) { | |
695 | strbuf_release(&buf); | |
696 | return -1; | |
697 | } | |
698 | strbuf_stripspace(&buf, comment_line_str); | |
699 | ||
700 | strbuf_addf(&name, "branch.%s.description", branch_name); | |
701 | if (buf.len || exists) | |
702 | git_config_set(name.buf, buf.len ? buf.buf : NULL); | |
703 | strbuf_release(&name); | |
704 | strbuf_release(&buf); | |
705 | ||
706 | return 0; | |
707 | } | |
708 | ||
709 | int cmd_branch(int argc, | |
710 | const char **argv, | |
711 | const char *prefix, | |
712 | struct repository *repo UNUSED) | |
713 | { | |
714 | /* possible actions */ | |
715 | int delete = 0, rename = 0, copy = 0, list = 0, | |
716 | unset_upstream = 0, show_current = 0, edit_description = 0; | |
717 | const char *new_upstream = NULL; | |
718 | int noncreate_actions = 0; | |
719 | /* possible options */ | |
720 | int reflog = 0, quiet = 0, icase = 0, force = 0, | |
721 | recurse_submodules_explicit = 0; | |
722 | enum branch_track track; | |
723 | struct ref_filter filter = REF_FILTER_INIT; | |
724 | static struct ref_sorting *sorting; | |
725 | struct string_list sorting_options = STRING_LIST_INIT_DUP; | |
726 | struct ref_format format = REF_FORMAT_INIT; | |
727 | int ret; | |
728 | ||
729 | struct option options[] = { | |
730 | OPT_GROUP(N_("Generic options")), | |
731 | OPT__VERBOSE(&filter.verbose, | |
732 | N_("show hash and subject, give twice for upstream branch")), | |
733 | OPT__QUIET(&quiet, N_("suppress informational messages")), | |
734 | OPT_CALLBACK_F('t', "track", &track, "(direct|inherit)", | |
735 | N_("set branch tracking configuration"), | |
736 | PARSE_OPT_OPTARG, | |
737 | parse_opt_tracking_mode), | |
738 | OPT_SET_INT_F(0, "set-upstream", &track, N_("do not use"), | |
739 | BRANCH_TRACK_OVERRIDE, PARSE_OPT_HIDDEN), | |
740 | OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")), | |
741 | OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("unset the upstream info")), | |
742 | OPT__COLOR(&branch_use_color, N_("use colored output")), | |
743 | OPT_SET_INT_F('r', "remotes", &filter.kind, N_("act on remote-tracking branches"), | |
744 | FILTER_REFS_REMOTES, | |
745 | PARSE_OPT_NONEG), | |
746 | OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")), | |
747 | OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")), | |
748 | OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")), | |
749 | OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")), | |
750 | OPT__ABBREV(&filter.abbrev), | |
751 | ||
752 | OPT_GROUP(N_("Specific git-branch actions:")), | |
753 | OPT_SET_INT_F('a', "all", &filter.kind, N_("list both remote-tracking and local branches"), | |
754 | FILTER_REFS_REMOTES | FILTER_REFS_BRANCHES, | |
755 | PARSE_OPT_NONEG), | |
756 | OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1), | |
757 | OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2), | |
758 | OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1), | |
759 | OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2), | |
760 | OPT_BOOL(0, "omit-empty", &format.array_opts.omit_empty, | |
761 | N_("do not output a newline after empty formatted refs")), | |
762 | OPT_BIT('c', "copy", ©, N_("copy a branch and its reflog"), 1), | |
763 | OPT_BIT('C', NULL, ©, N_("copy a branch, even if target exists"), 2), | |
764 | OPT_BOOL('l', "list", &list, N_("list branch names")), | |
765 | OPT_BOOL(0, "show-current", &show_current, N_("show current branch name")), | |
766 | OPT_BOOL(0, "create-reflog", &reflog, N_("create the branch's reflog")), | |
767 | OPT_BOOL(0, "edit-description", &edit_description, | |
768 | N_("edit the description for the branch")), | |
769 | OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE), | |
770 | OPT_MERGED(&filter, N_("print only branches that are merged")), | |
771 | OPT_NO_MERGED(&filter, N_("print only branches that are not merged")), | |
772 | OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")), | |
773 | OPT_REF_SORT(&sorting_options), | |
774 | OPT_CALLBACK(0, "points-at", &filter.points_at, N_("object"), | |
775 | N_("print only branches of the object"), parse_opt_object_name), | |
776 | OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")), | |
777 | OPT_BOOL(0, "recurse-submodules", &recurse_submodules_explicit, N_("recurse through submodules")), | |
778 | OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")), | |
779 | OPT_END(), | |
780 | }; | |
781 | ||
782 | setup_ref_filter_porcelain_msg(); | |
783 | ||
784 | filter.kind = FILTER_REFS_BRANCHES; | |
785 | filter.abbrev = -1; | |
786 | ||
787 | show_usage_with_options_if_asked(argc, argv, | |
788 | builtin_branch_usage, options); | |
789 | ||
790 | /* | |
791 | * Try to set sort keys from config. If config does not set any, | |
792 | * fall back on default (refname) sorting. | |
793 | */ | |
794 | git_config(git_branch_config, &sorting_options); | |
795 | if (!sorting_options.nr) | |
796 | string_list_append(&sorting_options, "refname"); | |
797 | ||
798 | track = git_branch_track; | |
799 | ||
800 | head = refs_resolve_refdup(get_main_ref_store(the_repository), "HEAD", | |
801 | 0, &head_oid, NULL); | |
802 | if (!head) | |
803 | die(_("failed to resolve HEAD as a valid ref")); | |
804 | if (!strcmp(head, "HEAD")) | |
805 | filter.detached = 1; | |
806 | else if (!skip_prefix(head, "refs/heads/", &head)) | |
807 | die(_("HEAD not found below refs/heads!")); | |
808 | ||
809 | argc = parse_options(argc, argv, prefix, options, builtin_branch_usage, | |
810 | 0); | |
811 | ||
812 | if (!delete && !rename && !copy && !edit_description && !new_upstream && | |
813 | !show_current && !unset_upstream && argc == 0) | |
814 | list = 1; | |
815 | ||
816 | if (filter.with_commit || filter.no_commit || | |
817 | filter.reachable_from || filter.unreachable_from || filter.points_at.nr) | |
818 | list = 1; | |
819 | ||
820 | noncreate_actions = !!delete + !!rename + !!copy + !!new_upstream + | |
821 | !!show_current + !!list + !!edit_description + | |
822 | !!unset_upstream; | |
823 | if (noncreate_actions > 1) | |
824 | usage_with_options(builtin_branch_usage, options); | |
825 | ||
826 | if (recurse_submodules_explicit) { | |
827 | if (!submodule_propagate_branches) | |
828 | die(_("branch with --recurse-submodules can only be used if submodule.propagateBranches is enabled")); | |
829 | if (noncreate_actions) | |
830 | die(_("--recurse-submodules can only be used to create branches")); | |
831 | } | |
832 | ||
833 | recurse_submodules = | |
834 | (recurse_submodules || recurse_submodules_explicit) && | |
835 | submodule_propagate_branches; | |
836 | ||
837 | if (filter.abbrev == -1) | |
838 | filter.abbrev = DEFAULT_ABBREV; | |
839 | filter.ignore_case = icase; | |
840 | ||
841 | finalize_colopts(&colopts, -1); | |
842 | if (filter.verbose) { | |
843 | if (explicitly_enable_column(colopts)) | |
844 | die(_("options '%s' and '%s' cannot be used together"), "--column", "--verbose"); | |
845 | colopts = 0; | |
846 | } | |
847 | ||
848 | if (force) { | |
849 | delete *= 2; | |
850 | rename *= 2; | |
851 | copy *= 2; | |
852 | } | |
853 | ||
854 | if (list) | |
855 | setup_auto_pager("branch", 1); | |
856 | ||
857 | if (delete) { | |
858 | if (!argc) | |
859 | die(_("branch name required")); | |
860 | ret = delete_branches(argc, argv, delete > 1, filter.kind, quiet); | |
861 | goto out; | |
862 | } else if (show_current) { | |
863 | print_current_branch_name(); | |
864 | ret = 0; | |
865 | goto out; | |
866 | } else if (list) { | |
867 | /* git branch --list also shows HEAD when it is detached */ | |
868 | if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached) | |
869 | filter.kind |= FILTER_REFS_DETACHED_HEAD; | |
870 | filter.name_patterns = argv; | |
871 | /* | |
872 | * If no sorting parameter is given then we default to sorting | |
873 | * by 'refname'. This would give us an alphabetically sorted | |
874 | * array with the 'HEAD' ref at the beginning followed by | |
875 | * local branches 'refs/heads/...' and finally remote-tracking | |
876 | * branches 'refs/remotes/...'. | |
877 | */ | |
878 | sorting = ref_sorting_options(&sorting_options); | |
879 | ref_sorting_set_sort_flags_all(sorting, REF_SORTING_ICASE, icase); | |
880 | ref_sorting_set_sort_flags_all( | |
881 | sorting, REF_SORTING_DETACHED_HEAD_FIRST, 1); | |
882 | print_ref_list(&filter, sorting, &format, &output); | |
883 | print_columns(&output, colopts, NULL); | |
884 | string_list_clear(&output, 0); | |
885 | ref_sorting_release(sorting); | |
886 | ref_filter_clear(&filter); | |
887 | ||
888 | ret = 0; | |
889 | goto out; | |
890 | } else if (edit_description) { | |
891 | const char *branch_name; | |
892 | struct strbuf branch_ref = STRBUF_INIT; | |
893 | struct strbuf buf = STRBUF_INIT; | |
894 | ||
895 | if (!argc) { | |
896 | if (filter.detached) | |
897 | die(_("cannot give description to detached HEAD")); | |
898 | branch_name = head; | |
899 | } else if (argc == 1) { | |
900 | copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL); | |
901 | branch_name = buf.buf; | |
902 | } else { | |
903 | die(_("cannot edit description of more than one branch")); | |
904 | } | |
905 | ||
906 | strbuf_addf(&branch_ref, "refs/heads/%s", branch_name); | |
907 | if (!refs_ref_exists(get_main_ref_store(the_repository), branch_ref.buf)) { | |
908 | error((!argc || branch_checked_out(branch_ref.buf)) | |
909 | ? _("no commit on branch '%s' yet") | |
910 | : _("no branch named '%s'"), | |
911 | branch_name); | |
912 | ret = 1; | |
913 | } else if (!edit_branch_description(branch_name)) { | |
914 | ret = 0; /* happy */ | |
915 | } else { | |
916 | ret = 1; | |
917 | } | |
918 | ||
919 | strbuf_release(&branch_ref); | |
920 | strbuf_release(&buf); | |
921 | ||
922 | goto out; | |
923 | } else if (copy || rename) { | |
924 | if (!argc) | |
925 | die(_("branch name required")); | |
926 | else if ((argc == 1) && filter.detached) | |
927 | die(copy? _("cannot copy the current branch while not on any") | |
928 | : _("cannot rename the current branch while not on any")); | |
929 | else if (argc == 1) | |
930 | copy_or_rename_branch(head, argv[0], copy, copy + rename > 1); | |
931 | else if (argc == 2) | |
932 | copy_or_rename_branch(argv[0], argv[1], copy, copy + rename > 1); | |
933 | else | |
934 | die(copy? _("too many branches for a copy operation") | |
935 | : _("too many arguments for a rename operation")); | |
936 | } else if (new_upstream) { | |
937 | struct branch *branch; | |
938 | struct strbuf buf = STRBUF_INIT; | |
939 | ||
940 | if (!argc) | |
941 | branch = branch_get(NULL); | |
942 | else if (argc == 1) { | |
943 | copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL); | |
944 | branch = branch_get(buf.buf); | |
945 | } else | |
946 | die(_("too many arguments to set new upstream")); | |
947 | ||
948 | if (!branch) { | |
949 | if (!argc || !strcmp(argv[0], "HEAD")) | |
950 | die(_("could not set upstream of HEAD to %s when " | |
951 | "it does not point to any branch"), | |
952 | new_upstream); | |
953 | die(_("no such branch '%s'"), argv[0]); | |
954 | } | |
955 | ||
956 | if (!refs_ref_exists(get_main_ref_store(the_repository), branch->refname)) { | |
957 | if (!argc || branch_checked_out(branch->refname)) | |
958 | die(_("no commit on branch '%s' yet"), branch->name); | |
959 | die(_("branch '%s' does not exist"), branch->name); | |
960 | } | |
961 | ||
962 | dwim_and_setup_tracking(the_repository, branch->name, | |
963 | new_upstream, BRANCH_TRACK_OVERRIDE, | |
964 | quiet); | |
965 | strbuf_release(&buf); | |
966 | } else if (unset_upstream) { | |
967 | struct branch *branch; | |
968 | struct strbuf buf = STRBUF_INIT; | |
969 | ||
970 | if (!argc) | |
971 | branch = branch_get(NULL); | |
972 | else if (argc == 1) { | |
973 | copy_branchname(&buf, argv[0], INTERPRET_BRANCH_LOCAL); | |
974 | branch = branch_get(buf.buf); | |
975 | } else | |
976 | die(_("too many arguments to unset upstream")); | |
977 | ||
978 | if (!branch) { | |
979 | if (!argc || !strcmp(argv[0], "HEAD")) | |
980 | die(_("could not unset upstream of HEAD when " | |
981 | "it does not point to any branch")); | |
982 | die(_("no such branch '%s'"), argv[0]); | |
983 | } | |
984 | ||
985 | if (!branch_has_merge_config(branch)) | |
986 | die(_("branch '%s' has no upstream information"), branch->name); | |
987 | ||
988 | strbuf_reset(&buf); | |
989 | strbuf_addf(&buf, "branch.%s.remote", branch->name); | |
990 | git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); | |
991 | strbuf_reset(&buf); | |
992 | strbuf_addf(&buf, "branch.%s.merge", branch->name); | |
993 | git_config_set_multivar(buf.buf, NULL, NULL, CONFIG_FLAGS_MULTI_REPLACE); | |
994 | strbuf_release(&buf); | |
995 | } else if (!noncreate_actions && argc > 0 && argc <= 2) { | |
996 | const char *branch_name = argv[0]; | |
997 | const char *start_name = argc == 2 ? argv[1] : head; | |
998 | ||
999 | if (filter.kind != FILTER_REFS_BRANCHES) | |
1000 | die(_("the -a, and -r, options to 'git branch' do not take a branch name.\n" | |
1001 | "Did you mean to use: -a|-r --list <pattern>?")); | |
1002 | ||
1003 | if (track == BRANCH_TRACK_OVERRIDE) | |
1004 | die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead")); | |
1005 | ||
1006 | if (recurse_submodules) { | |
1007 | create_branches_recursively(the_repository, branch_name, | |
1008 | start_name, NULL, force, | |
1009 | reflog, quiet, track, 0); | |
1010 | ret = 0; | |
1011 | goto out; | |
1012 | } | |
1013 | create_branch(the_repository, branch_name, start_name, force, 0, | |
1014 | reflog, quiet, track, 0); | |
1015 | } else | |
1016 | usage_with_options(builtin_branch_usage, options); | |
1017 | ||
1018 | ret = 0; | |
1019 | ||
1020 | out: | |
1021 | string_list_clear(&sorting_options, 0); | |
1022 | return ret; | |
1023 | } |