branch.c 26 KB

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