THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

823 lines
25KB

  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. #include "cache.h"
  8. #include "config.h"
  9. #include "color.h"
  10. #include "refs.h"
  11. #include "commit.h"
  12. #include "builtin.h"
  13. #include "remote.h"
  14. #include "parse-options.h"
  15. #include "branch.h"
  16. #include "diff.h"
  17. #include "revision.h"
  18. #include "string-list.h"
  19. #include "column.h"
  20. #include "utf8.h"
  21. #include "wt-status.h"
  22. #include "ref-filter.h"
  23. #include "worktree.h"
  24. static const char * const builtin_branch_usage[] = {
  25. N_("git branch [<options>] [-r | -a] [--merged | --no-merged]"),
  26. N_("git branch [<options>] [-l] [-f] <branch-name> [<start-point>]"),
  27. N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
  28. N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
  29. N_("git branch [<options>] (-c | -C) [<old-branch>] <new-branch>"),
  30. N_("git branch [<options>] [-r | -a] [--points-at]"),
  31. N_("git branch [<options>] [-r | -a] [--format]"),
  32. NULL
  33. };
  34. static const char *head;
  35. static struct object_id head_oid;
  36. static int branch_use_color = -1;
  37. static char branch_colors[][COLOR_MAXLEN] = {
  38. GIT_COLOR_RESET,
  39. GIT_COLOR_NORMAL, /* PLAIN */
  40. GIT_COLOR_RED, /* REMOTE */
  41. GIT_COLOR_NORMAL, /* LOCAL */
  42. GIT_COLOR_GREEN, /* CURRENT */
  43. GIT_COLOR_BLUE, /* UPSTREAM */
  44. };
  45. enum color_branch {
  46. BRANCH_COLOR_RESET = 0,
  47. BRANCH_COLOR_PLAIN = 1,
  48. BRANCH_COLOR_REMOTE = 2,
  49. BRANCH_COLOR_LOCAL = 3,
  50. BRANCH_COLOR_CURRENT = 4,
  51. BRANCH_COLOR_UPSTREAM = 5
  52. };
  53. static struct string_list output = STRING_LIST_INIT_DUP;
  54. static unsigned int colopts;
  55. static int parse_branch_color_slot(const char *slot)
  56. {
  57. if (!strcasecmp(slot, "plain"))
  58. return BRANCH_COLOR_PLAIN;
  59. if (!strcasecmp(slot, "reset"))
  60. return BRANCH_COLOR_RESET;
  61. if (!strcasecmp(slot, "remote"))
  62. return BRANCH_COLOR_REMOTE;
  63. if (!strcasecmp(slot, "local"))
  64. return BRANCH_COLOR_LOCAL;
  65. if (!strcasecmp(slot, "current"))
  66. return BRANCH_COLOR_CURRENT;
  67. if (!strcasecmp(slot, "upstream"))
  68. return BRANCH_COLOR_UPSTREAM;
  69. return -1;
  70. }
  71. static int git_branch_config(const char *var, const char *value, void *cb)
  72. {
  73. const char *slot_name;
  74. if (starts_with(var, "column."))
  75. return git_column_config(var, value, "branch", &colopts);
  76. if (!strcmp(var, "color.branch")) {
  77. branch_use_color = git_config_colorbool(var, value);
  78. return 0;
  79. }
  80. if (skip_prefix(var, "color.branch.", &slot_name)) {
  81. int slot = parse_branch_color_slot(slot_name);
  82. if (slot < 0)
  83. return 0;
  84. if (!value)
  85. return config_error_nonbool(var);
  86. return color_parse(value, branch_colors[slot]);
  87. }
  88. return git_color_default_config(var, value, cb);
  89. }
  90. static const char *branch_get_color(enum color_branch ix)
  91. {
  92. if (want_color(branch_use_color))
  93. return branch_colors[ix];
  94. return "";
  95. }
  96. static int branch_merged(int kind, const char *name,
  97. struct commit *rev, struct commit *head_rev)
  98. {
  99. /*
  100. * This checks whether the merge bases of branch and HEAD (or
  101. * the other branch this branch builds upon) contains the
  102. * branch, which means that the branch has already been merged
  103. * safely to HEAD (or the other branch).
  104. */
  105. struct commit *reference_rev = NULL;
  106. const char *reference_name = NULL;
  107. void *reference_name_to_free = NULL;
  108. int merged;
  109. if (kind == FILTER_REFS_BRANCHES) {
  110. struct branch *branch = branch_get(name);
  111. const char *upstream = branch_get_upstream(branch, NULL);
  112. struct object_id oid;
  113. if (upstream &&
  114. (reference_name = reference_name_to_free =
  115. resolve_refdup(upstream, RESOLVE_REF_READING,
  116. &oid, NULL)) != NULL)
  117. reference_rev = lookup_commit_reference(&oid);
  118. }
  119. if (!reference_rev)
  120. reference_rev = head_rev;
  121. merged = in_merge_bases(rev, reference_rev);
  122. /*
  123. * After the safety valve is fully redefined to "check with
  124. * upstream, if any, otherwise with HEAD", we should just
  125. * return the result of the in_merge_bases() above without
  126. * any of the following code, but during the transition period,
  127. * a gentle reminder is in order.
  128. */
  129. if ((head_rev != reference_rev) &&
  130. in_merge_bases(rev, head_rev) != merged) {
  131. if (merged)
  132. warning(_("deleting branch '%s' that has been merged to\n"
  133. " '%s', but not yet merged to HEAD."),
  134. name, reference_name);
  135. else
  136. warning(_("not deleting branch '%s' that is not yet merged to\n"
  137. " '%s', even though it is merged to HEAD."),
  138. name, reference_name);
  139. }
  140. free(reference_name_to_free);
  141. return merged;
  142. }
  143. static int check_branch_commit(const char *branchname, const char *refname,
  144. const struct object_id *oid, struct commit *head_rev,
  145. int kinds, int force)
  146. {
  147. struct commit *rev = lookup_commit_reference(oid);
  148. if (!rev) {
  149. error(_("Couldn't look up commit object for '%s'"), refname);
  150. return -1;
  151. }
  152. if (!force && !branch_merged(kinds, branchname, rev, head_rev)) {
  153. error(_("The branch '%s' is not fully merged.\n"
  154. "If you are sure you want to delete it, "
  155. "run 'git branch -D %s'."), branchname, branchname);
  156. return -1;
  157. }
  158. return 0;
  159. }
  160. static void delete_branch_config(const char *branchname)
  161. {
  162. struct strbuf buf = STRBUF_INIT;
  163. strbuf_addf(&buf, "branch.%s", branchname);
  164. if (git_config_rename_section(buf.buf, NULL) < 0)
  165. warning(_("Update of config-file failed"));
  166. strbuf_release(&buf);
  167. }
  168. static int delete_branches(int argc, const char **argv, int force, int kinds,
  169. int quiet)
  170. {
  171. struct commit *head_rev = NULL;
  172. struct object_id oid;
  173. char *name = NULL;
  174. const char *fmt;
  175. int i;
  176. int ret = 0;
  177. int remote_branch = 0;
  178. struct strbuf bname = STRBUF_INIT;
  179. unsigned allowed_interpret;
  180. switch (kinds) {
  181. case FILTER_REFS_REMOTES:
  182. fmt = "refs/remotes/%s";
  183. /* For subsequent UI messages */
  184. remote_branch = 1;
  185. allowed_interpret = INTERPRET_BRANCH_REMOTE;
  186. force = 1;
  187. break;
  188. case FILTER_REFS_BRANCHES:
  189. fmt = "refs/heads/%s";
  190. allowed_interpret = INTERPRET_BRANCH_LOCAL;
  191. break;
  192. default:
  193. die(_("cannot use -a with -d"));
  194. }
  195. if (!force) {
  196. head_rev = lookup_commit_reference(&head_oid);
  197. if (!head_rev)
  198. die(_("Couldn't look up commit object for HEAD"));
  199. }
  200. for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
  201. char *target = NULL;
  202. int flags = 0;
  203. strbuf_branchname(&bname, argv[i], allowed_interpret);
  204. free(name);
  205. name = mkpathdup(fmt, bname.buf);
  206. if (kinds == FILTER_REFS_BRANCHES) {
  207. const struct worktree *wt =
  208. find_shared_symref("HEAD", name);
  209. if (wt) {
  210. error(_("Cannot delete branch '%s' "
  211. "checked out at '%s'"),
  212. bname.buf, wt->path);
  213. ret = 1;
  214. continue;
  215. }
  216. }
  217. target = resolve_refdup(name,
  218. RESOLVE_REF_READING
  219. | RESOLVE_REF_NO_RECURSE
  220. | RESOLVE_REF_ALLOW_BAD_NAME,
  221. &oid, &flags);
  222. if (!target) {
  223. error(remote_branch
  224. ? _("remote-tracking branch '%s' not found.")
  225. : _("branch '%s' not found."), bname.buf);
  226. ret = 1;
  227. continue;
  228. }
  229. if (!(flags & (REF_ISSYMREF|REF_ISBROKEN)) &&
  230. check_branch_commit(bname.buf, name, &oid, head_rev, kinds,
  231. force)) {
  232. ret = 1;
  233. goto next;
  234. }
  235. if (delete_ref(NULL, name, is_null_oid(&oid) ? NULL : &oid,
  236. REF_NO_DEREF)) {
  237. error(remote_branch
  238. ? _("Error deleting remote-tracking branch '%s'")
  239. : _("Error deleting branch '%s'"),
  240. bname.buf);
  241. ret = 1;
  242. goto next;
  243. }
  244. if (!quiet) {
  245. printf(remote_branch
  246. ? _("Deleted remote-tracking branch %s (was %s).\n")
  247. : _("Deleted branch %s (was %s).\n"),
  248. bname.buf,
  249. (flags & REF_ISBROKEN) ? "broken"
  250. : (flags & REF_ISSYMREF) ? target
  251. : find_unique_abbrev(&oid, DEFAULT_ABBREV));
  252. }
  253. delete_branch_config(bname.buf);
  254. next:
  255. free(target);
  256. }
  257. free(name);
  258. strbuf_release(&bname);
  259. return ret;
  260. }
  261. static int calc_maxwidth(struct ref_array *refs, int remote_bonus)
  262. {
  263. int i, max = 0;
  264. for (i = 0; i < refs->nr; i++) {
  265. struct ref_array_item *it = refs->items[i];
  266. const char *desc = it->refname;
  267. int w;
  268. skip_prefix(it->refname, "refs/heads/", &desc);
  269. skip_prefix(it->refname, "refs/remotes/", &desc);
  270. if (it->kind == FILTER_REFS_DETACHED_HEAD) {
  271. char *head_desc = get_head_description();
  272. w = utf8_strwidth(head_desc);
  273. free(head_desc);
  274. } else
  275. w = utf8_strwidth(desc);
  276. if (it->kind == FILTER_REFS_REMOTES)
  277. w += remote_bonus;
  278. if (w > max)
  279. max = w;
  280. }
  281. return max;
  282. }
  283. static const char *quote_literal_for_format(const char *s)
  284. {
  285. static struct strbuf buf = STRBUF_INIT;
  286. strbuf_reset(&buf);
  287. while (*s) {
  288. const char *ep = strchrnul(s, '%');
  289. if (s < ep)
  290. strbuf_add(&buf, s, ep - s);
  291. if (*ep == '%') {
  292. strbuf_addstr(&buf, "%%");
  293. s = ep + 1;
  294. } else {
  295. s = ep;
  296. }
  297. }
  298. return buf.buf;
  299. }
  300. static char *build_format(struct ref_filter *filter, int maxwidth, const char *remote_prefix)
  301. {
  302. struct strbuf fmt = STRBUF_INIT;
  303. struct strbuf local = STRBUF_INIT;
  304. struct strbuf remote = STRBUF_INIT;
  305. strbuf_addf(&local, "%%(if)%%(HEAD)%%(then)* %s%%(else) %s%%(end)",
  306. branch_get_color(BRANCH_COLOR_CURRENT),
  307. branch_get_color(BRANCH_COLOR_LOCAL));
  308. strbuf_addf(&remote, " %s",
  309. branch_get_color(BRANCH_COLOR_REMOTE));
  310. if (filter->verbose) {
  311. struct strbuf obname = STRBUF_INIT;
  312. if (filter->abbrev < 0)
  313. strbuf_addf(&obname, "%%(objectname:short)");
  314. else if (!filter->abbrev)
  315. strbuf_addf(&obname, "%%(objectname)");
  316. else
  317. strbuf_addf(&obname, "%%(objectname:short=%d)", filter->abbrev);
  318. strbuf_addf(&local, "%%(align:%d,left)%%(refname:lstrip=2)%%(end)", maxwidth);
  319. strbuf_addstr(&local, branch_get_color(BRANCH_COLOR_RESET));
  320. strbuf_addf(&local, " %s ", obname.buf);
  321. if (filter->verbose > 1)
  322. strbuf_addf(&local, "%%(if)%%(upstream)%%(then)[%s%%(upstream:short)%s%%(if)%%(upstream:track)"
  323. "%%(then): %%(upstream:track,nobracket)%%(end)] %%(end)%%(contents:subject)",
  324. branch_get_color(BRANCH_COLOR_UPSTREAM), branch_get_color(BRANCH_COLOR_RESET));
  325. else
  326. strbuf_addf(&local, "%%(if)%%(upstream:track)%%(then)%%(upstream:track) %%(end)%%(contents:subject)");
  327. strbuf_addf(&remote, "%%(align:%d,left)%s%%(refname:lstrip=2)%%(end)%s"
  328. "%%(if)%%(symref)%%(then) -> %%(symref:short)"
  329. "%%(else) %s %%(contents:subject)%%(end)",
  330. maxwidth, quote_literal_for_format(remote_prefix),
  331. branch_get_color(BRANCH_COLOR_RESET), obname.buf);
  332. strbuf_release(&obname);
  333. } else {
  334. strbuf_addf(&local, "%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
  335. branch_get_color(BRANCH_COLOR_RESET));
  336. strbuf_addf(&remote, "%s%%(refname:lstrip=2)%s%%(if)%%(symref)%%(then) -> %%(symref:short)%%(end)",
  337. quote_literal_for_format(remote_prefix),
  338. branch_get_color(BRANCH_COLOR_RESET));
  339. }
  340. strbuf_addf(&fmt, "%%(if:notequals=refs/remotes)%%(refname:rstrip=-2)%%(then)%s%%(else)%s%%(end)", local.buf, remote.buf);
  341. strbuf_release(&local);
  342. strbuf_release(&remote);
  343. return strbuf_detach(&fmt, NULL);
  344. }
  345. static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, struct ref_format *format)
  346. {
  347. int i;
  348. struct ref_array array;
  349. int maxwidth = 0;
  350. const char *remote_prefix = "";
  351. struct strbuf out = STRBUF_INIT;
  352. char *to_free = NULL;
  353. /*
  354. * If we are listing more than just remote branches,
  355. * then remote branches will have a "remotes/" prefix.
  356. * We need to account for this in the width.
  357. */
  358. if (filter->kind != FILTER_REFS_REMOTES)
  359. remote_prefix = "remotes/";
  360. memset(&array, 0, sizeof(array));
  361. filter_refs(&array, filter, filter->kind | FILTER_REFS_INCLUDE_BROKEN);
  362. if (filter->verbose)
  363. maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
  364. if (!format->format)
  365. format->format = to_free = build_format(filter, maxwidth, remote_prefix);
  366. format->use_color = branch_use_color;
  367. if (verify_ref_format(format))
  368. die(_("unable to parse format string"));
  369. ref_array_sort(sorting, &array);
  370. for (i = 0; i < array.nr; i++) {
  371. format_ref_array_item(array.items[i], format, &out);
  372. if (column_active(colopts)) {
  373. assert(!filter->verbose && "--column and --verbose are incompatible");
  374. /* format to a string_list to let print_columns() do its job */
  375. string_list_append(&output, out.buf);
  376. } else {
  377. fwrite(out.buf, 1, out.len, stdout);
  378. putchar('\n');
  379. }
  380. strbuf_release(&out);
  381. }
  382. ref_array_clear(&array);
  383. free(to_free);
  384. }
  385. static void reject_rebase_or_bisect_branch(const char *target)
  386. {
  387. struct worktree **worktrees = get_worktrees(0);
  388. int i;
  389. for (i = 0; worktrees[i]; i++) {
  390. struct worktree *wt = worktrees[i];
  391. if (!wt->is_detached)
  392. continue;
  393. if (is_worktree_being_rebased(wt, target))
  394. die(_("Branch %s is being rebased at %s"),
  395. target, wt->path);
  396. if (is_worktree_being_bisected(wt, target))
  397. die(_("Branch %s is being bisected at %s"),
  398. target, wt->path);
  399. }
  400. free_worktrees(worktrees);
  401. }
  402. static void copy_or_rename_branch(const char *oldname, const char *newname, int copy, int force)
  403. {
  404. struct strbuf oldref = STRBUF_INIT, newref = STRBUF_INIT, logmsg = STRBUF_INIT;
  405. struct strbuf oldsection = STRBUF_INIT, newsection = STRBUF_INIT;
  406. const char *interpreted_oldname = NULL;
  407. const char *interpreted_newname = NULL;
  408. int recovery = 0;
  409. if (!oldname) {
  410. if (copy)
  411. die(_("cannot copy the current branch while not on any."));
  412. else
  413. die(_("cannot rename the current branch while not on any."));
  414. }
  415. if (strbuf_check_branch_ref(&oldref, oldname)) {
  416. /*
  417. * Bad name --- this could be an attempt to rename a
  418. * ref that we used to allow to be created by accident.
  419. */
  420. if (ref_exists(oldref.buf))
  421. recovery = 1;
  422. else
  423. die(_("Invalid branch name: '%s'"), oldname);
  424. }
  425. /*
  426. * A command like "git branch -M currentbranch currentbranch" cannot
  427. * cause the worktree to become inconsistent with HEAD, so allow it.
  428. */
  429. if (!strcmp(oldname, newname))
  430. validate_branchname(newname, &newref);
  431. else
  432. validate_new_branchname(newname, &newref, force);
  433. reject_rebase_or_bisect_branch(oldref.buf);
  434. if (!skip_prefix(oldref.buf, "refs/heads/", &interpreted_oldname) ||
  435. !skip_prefix(newref.buf, "refs/heads/", &interpreted_newname)) {
  436. BUG("expected prefix missing for refs");
  437. }
  438. if (copy)
  439. strbuf_addf(&logmsg, "Branch: copied %s to %s",
  440. oldref.buf, newref.buf);
  441. else
  442. strbuf_addf(&logmsg, "Branch: renamed %s to %s",
  443. oldref.buf, newref.buf);
  444. if (!copy && rename_ref(oldref.buf, newref.buf, logmsg.buf))
  445. die(_("Branch rename failed"));
  446. if (copy && copy_existing_ref(oldref.buf, newref.buf, logmsg.buf))
  447. die(_("Branch copy failed"));
  448. if (recovery) {
  449. if (copy)
  450. warning(_("Created a copy of a misnamed branch '%s'"),
  451. interpreted_oldname);
  452. else
  453. warning(_("Renamed a misnamed branch '%s' away"),
  454. interpreted_oldname);
  455. }
  456. if (!copy &&
  457. replace_each_worktree_head_symref(oldref.buf, newref.buf, logmsg.buf))
  458. die(_("Branch renamed to %s, but HEAD is not updated!"), newname);
  459. strbuf_release(&logmsg);
  460. strbuf_addf(&oldsection, "branch.%s", interpreted_oldname);
  461. strbuf_release(&oldref);
  462. strbuf_addf(&newsection, "branch.%s", interpreted_newname);
  463. strbuf_release(&newref);
  464. if (!copy && git_config_rename_section(oldsection.buf, newsection.buf) < 0)
  465. die(_("Branch is renamed, but update of config-file failed"));
  466. if (copy && strcmp(oldname, newname) && git_config_copy_section(oldsection.buf, newsection.buf) < 0)
  467. die(_("Branch is copied, but update of config-file failed"));
  468. strbuf_release(&oldsection);
  469. strbuf_release(&newsection);
  470. }
  471. static GIT_PATH_FUNC(edit_description, "EDIT_DESCRIPTION")
  472. static int edit_branch_description(const char *branch_name)
  473. {
  474. struct strbuf buf = STRBUF_INIT;
  475. struct strbuf name = STRBUF_INIT;
  476. read_branch_desc(&buf, branch_name);
  477. if (!buf.len || buf.buf[buf.len-1] != '\n')
  478. strbuf_addch(&buf, '\n');
  479. strbuf_commented_addf(&buf,
  480. _("Please edit the description for the branch\n"
  481. " %s\n"
  482. "Lines starting with '%c' will be stripped.\n"),
  483. branch_name, comment_line_char);
  484. write_file_buf(edit_description(), buf.buf, buf.len);
  485. strbuf_reset(&buf);
  486. if (launch_editor(edit_description(), &buf, NULL)) {
  487. strbuf_release(&buf);
  488. return -1;
  489. }
  490. strbuf_stripspace(&buf, 1);
  491. strbuf_addf(&name, "branch.%s.description", branch_name);
  492. git_config_set(name.buf, buf.len ? buf.buf : NULL);
  493. strbuf_release(&name);
  494. strbuf_release(&buf);
  495. return 0;
  496. }
  497. int cmd_branch(int argc, const char **argv, const char *prefix)
  498. {
  499. int delete = 0, rename = 0, copy = 0, force = 0, list = 0;
  500. int reflog = 0, edit_description = 0;
  501. int quiet = 0, unset_upstream = 0;
  502. const char *new_upstream = NULL;
  503. enum branch_track track;
  504. struct ref_filter filter;
  505. int icase = 0;
  506. static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
  507. struct ref_format format = REF_FORMAT_INIT;
  508. struct option options[] = {
  509. OPT_GROUP(N_("Generic options")),
  510. OPT__VERBOSE(&filter.verbose,
  511. N_("show hash and subject, give twice for upstream branch")),
  512. OPT__QUIET(&quiet, N_("suppress informational messages")),
  513. OPT_SET_INT('t', "track", &track, N_("set up tracking mode (see git-pull(1))"),
  514. BRANCH_TRACK_EXPLICIT),
  515. { OPTION_SET_INT, 0, "set-upstream", &track, NULL, N_("do not use"),
  516. PARSE_OPT_NOARG | PARSE_OPT_HIDDEN, NULL, BRANCH_TRACK_OVERRIDE },
  517. OPT_STRING('u', "set-upstream-to", &new_upstream, N_("upstream"), N_("change the upstream info")),
  518. OPT_BOOL(0, "unset-upstream", &unset_upstream, N_("Unset the upstream info")),
  519. OPT__COLOR(&branch_use_color, N_("use colored output")),
  520. OPT_SET_INT('r', "remotes", &filter.kind, N_("act on remote-tracking branches"),
  521. FILTER_REFS_REMOTES),
  522. OPT_CONTAINS(&filter.with_commit, N_("print only branches that contain the commit")),
  523. OPT_NO_CONTAINS(&filter.no_commit, N_("print only branches that don't contain the commit")),
  524. OPT_WITH(&filter.with_commit, N_("print only branches that contain the commit")),
  525. OPT_WITHOUT(&filter.no_commit, N_("print only branches that don't contain the commit")),
  526. OPT__ABBREV(&filter.abbrev),
  527. OPT_GROUP(N_("Specific git-branch actions:")),
  528. OPT_SET_INT('a', "all", &filter.kind, N_("list both remote-tracking and local branches"),
  529. FILTER_REFS_REMOTES | FILTER_REFS_BRANCHES),
  530. OPT_BIT('d', "delete", &delete, N_("delete fully merged branch"), 1),
  531. OPT_BIT('D', NULL, &delete, N_("delete branch (even if not merged)"), 2),
  532. OPT_BIT('m', "move", &rename, N_("move/rename a branch and its reflog"), 1),
  533. OPT_BIT('M', NULL, &rename, N_("move/rename a branch, even if target exists"), 2),
  534. OPT_BIT('c', "copy", &copy, N_("copy a branch and its reflog"), 1),
  535. OPT_BIT('C', NULL, &copy, N_("copy a branch, even if target exists"), 2),
  536. OPT_BOOL(0, "list", &list, N_("list branch names")),
  537. OPT_BOOL('l', "create-reflog", &reflog, N_("create the branch's reflog")),
  538. OPT_BOOL(0, "edit-description", &edit_description,
  539. N_("edit the description for the branch")),
  540. OPT__FORCE(&force, N_("force creation, move/rename, deletion"), PARSE_OPT_NOCOMPLETE),
  541. OPT_MERGED(&filter, N_("print only branches that are merged")),
  542. OPT_NO_MERGED(&filter, N_("print only branches that are not merged")),
  543. OPT_COLUMN(0, "column", &colopts, N_("list branches in columns")),
  544. OPT_CALLBACK(0 , "sort", sorting_tail, N_("key"),
  545. N_("field name to sort on"), &parse_opt_ref_sorting),
  546. {
  547. OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
  548. N_("print only branches of the object"), 0, parse_opt_object_name
  549. },
  550. OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
  551. OPT_STRING( 0 , "format", &format.format, N_("format"), N_("format to use for the output")),
  552. OPT_END(),
  553. };
  554. setup_ref_filter_porcelain_msg();
  555. memset(&filter, 0, sizeof(filter));
  556. filter.kind = FILTER_REFS_BRANCHES;
  557. filter.abbrev = -1;
  558. if (argc == 2 && !strcmp(argv[1], "-h"))
  559. usage_with_options(builtin_branch_usage, options);
  560. git_config(git_branch_config, NULL);
  561. track = git_branch_track;
  562. head = resolve_refdup("HEAD", 0, &head_oid, NULL);
  563. if (!head)
  564. die(_("Failed to resolve HEAD as a valid ref."));
  565. if (!strcmp(head, "HEAD"))
  566. filter.detached = 1;
  567. else if (!skip_prefix(head, "refs/heads/", &head))
  568. die(_("HEAD not found below refs/heads!"));
  569. argc = parse_options(argc, argv, prefix, options, builtin_branch_usage,
  570. 0);
  571. if (!delete && !rename && !copy && !edit_description && !new_upstream && !unset_upstream && argc == 0)
  572. list = 1;
  573. if (filter.with_commit || filter.merge != REF_FILTER_MERGED_NONE || filter.points_at.nr ||
  574. filter.no_commit)
  575. list = 1;
  576. if (!!delete + !!rename + !!copy + !!new_upstream +
  577. list + unset_upstream > 1)
  578. usage_with_options(builtin_branch_usage, options);
  579. if (filter.abbrev == -1)
  580. filter.abbrev = DEFAULT_ABBREV;
  581. filter.ignore_case = icase;
  582. finalize_colopts(&colopts, -1);
  583. if (filter.verbose) {
  584. if (explicitly_enable_column(colopts))
  585. die(_("--column and --verbose are incompatible"));
  586. colopts = 0;
  587. }
  588. if (force) {
  589. delete *= 2;
  590. rename *= 2;
  591. copy *= 2;
  592. }
  593. if (list)
  594. setup_auto_pager("branch", 1);
  595. if (delete) {
  596. if (!argc)
  597. die(_("branch name required"));
  598. return delete_branches(argc, argv, delete > 1, filter.kind, quiet);
  599. } else if (list) {
  600. /* git branch --local also shows HEAD when it is detached */
  601. if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
  602. filter.kind |= FILTER_REFS_DETACHED_HEAD;
  603. filter.name_patterns = argv;
  604. /*
  605. * If no sorting parameter is given then we default to sorting
  606. * by 'refname'. This would give us an alphabetically sorted
  607. * array with the 'HEAD' ref at the beginning followed by
  608. * local branches 'refs/heads/...' and finally remote-tacking
  609. * branches 'refs/remotes/...'.
  610. */
  611. if (!sorting)
  612. sorting = ref_default_sorting();
  613. sorting->ignore_case = icase;
  614. print_ref_list(&filter, sorting, &format);
  615. print_columns(&output, colopts, NULL);
  616. string_list_clear(&output, 0);
  617. return 0;
  618. }
  619. else if (edit_description) {
  620. const char *branch_name;
  621. struct strbuf branch_ref = STRBUF_INIT;
  622. if (!argc) {
  623. if (filter.detached)
  624. die(_("Cannot give description to detached HEAD"));
  625. branch_name = head;
  626. } else if (argc == 1)
  627. branch_name = argv[0];
  628. else
  629. die(_("cannot edit description of more than one branch"));
  630. strbuf_addf(&branch_ref, "refs/heads/%s", branch_name);
  631. if (!ref_exists(branch_ref.buf)) {
  632. strbuf_release(&branch_ref);
  633. if (!argc)
  634. return error(_("No commit on branch '%s' yet."),
  635. branch_name);
  636. else
  637. return error(_("No branch named '%s'."),
  638. branch_name);
  639. }
  640. strbuf_release(&branch_ref);
  641. if (edit_branch_description(branch_name))
  642. return 1;
  643. } else if (copy) {
  644. if (!argc)
  645. die(_("branch name required"));
  646. else if (argc == 1)
  647. copy_or_rename_branch(head, argv[0], 1, copy > 1);
  648. else if (argc == 2)
  649. copy_or_rename_branch(argv[0], argv[1], 1, copy > 1);
  650. else
  651. die(_("too many branches for a copy operation"));
  652. } else if (rename) {
  653. if (!argc)
  654. die(_("branch name required"));
  655. else if (argc == 1)
  656. copy_or_rename_branch(head, argv[0], 0, rename > 1);
  657. else if (argc == 2)
  658. copy_or_rename_branch(argv[0], argv[1], 0, rename > 1);
  659. else
  660. die(_("too many arguments for a rename operation"));
  661. } else if (new_upstream) {
  662. struct branch *branch = branch_get(argv[0]);
  663. if (argc > 1)
  664. die(_("too many arguments to set new upstream"));
  665. if (!branch) {
  666. if (!argc || !strcmp(argv[0], "HEAD"))
  667. die(_("could not set upstream of HEAD to %s when "
  668. "it does not point to any branch."),
  669. new_upstream);
  670. die(_("no such branch '%s'"), argv[0]);
  671. }
  672. if (!ref_exists(branch->refname))
  673. die(_("branch '%s' does not exist"), branch->name);
  674. /*
  675. * create_branch takes care of setting up the tracking
  676. * info and making sure new_upstream is correct
  677. */
  678. create_branch(branch->name, new_upstream, 0, 0, 0, quiet, BRANCH_TRACK_OVERRIDE);
  679. } else if (unset_upstream) {
  680. struct branch *branch = branch_get(argv[0]);
  681. struct strbuf buf = STRBUF_INIT;
  682. if (argc > 1)
  683. die(_("too many arguments to unset upstream"));
  684. if (!branch) {
  685. if (!argc || !strcmp(argv[0], "HEAD"))
  686. die(_("could not unset upstream of HEAD when "
  687. "it does not point to any branch."));
  688. die(_("no such branch '%s'"), argv[0]);
  689. }
  690. if (!branch_has_merge_config(branch))
  691. die(_("Branch '%s' has no upstream information"), branch->name);
  692. strbuf_addf(&buf, "branch.%s.remote", branch->name);
  693. git_config_set_multivar(buf.buf, NULL, NULL, 1);
  694. strbuf_reset(&buf);
  695. strbuf_addf(&buf, "branch.%s.merge", branch->name);
  696. git_config_set_multivar(buf.buf, NULL, NULL, 1);
  697. strbuf_release(&buf);
  698. } else if (argc > 0 && argc <= 2) {
  699. struct branch *branch = branch_get(argv[0]);
  700. if (!branch)
  701. die(_("no such branch '%s'"), argv[0]);
  702. if (filter.kind != FILTER_REFS_BRANCHES)
  703. die(_("-a and -r options to 'git branch' do not make sense with a branch name"));
  704. if (track == BRANCH_TRACK_OVERRIDE)
  705. die(_("the '--set-upstream' option is no longer supported. Please use '--track' or '--set-upstream-to' instead."));
  706. create_branch(argv[0], (argc == 2) ? argv[1] : head,
  707. force, 0, reflog, quiet, track);
  708. } else
  709. usage_with_options(builtin_branch_usage, options);
  710. return 0;
  711. }