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.

394 lines
10KB

  1. #include "git-compat-util.h"
  2. #include "cache.h"
  3. #include "config.h"
  4. #include "branch.h"
  5. #include "refs.h"
  6. #include "refspec.h"
  7. #include "remote.h"
  8. #include "sequencer.h"
  9. #include "commit.h"
  10. #include "worktree.h"
  11. struct tracking {
  12. struct refspec_item spec;
  13. char *src;
  14. const char *remote;
  15. int matches;
  16. };
  17. static int find_tracked_branch(struct remote *remote, void *priv)
  18. {
  19. struct tracking *tracking = priv;
  20. if (!remote_find_tracking(remote, &tracking->spec)) {
  21. if (++tracking->matches == 1) {
  22. tracking->src = tracking->spec.src;
  23. tracking->remote = remote->name;
  24. } else {
  25. free(tracking->spec.src);
  26. FREE_AND_NULL(tracking->src);
  27. }
  28. tracking->spec.src = NULL;
  29. }
  30. return 0;
  31. }
  32. static int should_setup_rebase(const char *origin)
  33. {
  34. switch (autorebase) {
  35. case AUTOREBASE_NEVER:
  36. return 0;
  37. case AUTOREBASE_LOCAL:
  38. return origin == NULL;
  39. case AUTOREBASE_REMOTE:
  40. return origin != NULL;
  41. case AUTOREBASE_ALWAYS:
  42. return 1;
  43. }
  44. return 0;
  45. }
  46. static const char tracking_advice[] =
  47. N_("\n"
  48. "After fixing the error cause you may try to fix up\n"
  49. "the remote tracking information by invoking\n"
  50. "\"git branch --set-upstream-to=%s%s%s\".");
  51. int install_branch_config(int flag, const char *local, const char *origin, const char *remote)
  52. {
  53. const char *shortname = NULL;
  54. struct strbuf key = STRBUF_INIT;
  55. int rebasing = should_setup_rebase(origin);
  56. if (skip_prefix(remote, "refs/heads/", &shortname)
  57. && !strcmp(local, shortname)
  58. && !origin) {
  59. warning(_("Not setting branch %s as its own upstream."),
  60. local);
  61. return 0;
  62. }
  63. strbuf_addf(&key, "branch.%s.remote", local);
  64. if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
  65. goto out_err;
  66. strbuf_reset(&key);
  67. strbuf_addf(&key, "branch.%s.merge", local);
  68. if (git_config_set_gently(key.buf, remote) < 0)
  69. goto out_err;
  70. if (rebasing) {
  71. strbuf_reset(&key);
  72. strbuf_addf(&key, "branch.%s.rebase", local);
  73. if (git_config_set_gently(key.buf, "true") < 0)
  74. goto out_err;
  75. }
  76. strbuf_release(&key);
  77. if (flag & BRANCH_CONFIG_VERBOSE) {
  78. if (shortname) {
  79. if (origin)
  80. printf_ln(rebasing ?
  81. _("Branch '%s' set up to track remote branch '%s' from '%s' by rebasing.") :
  82. _("Branch '%s' set up to track remote branch '%s' from '%s'."),
  83. local, shortname, origin);
  84. else
  85. printf_ln(rebasing ?
  86. _("Branch '%s' set up to track local branch '%s' by rebasing.") :
  87. _("Branch '%s' set up to track local branch '%s'."),
  88. local, shortname);
  89. } else {
  90. if (origin)
  91. printf_ln(rebasing ?
  92. _("Branch '%s' set up to track remote ref '%s' by rebasing.") :
  93. _("Branch '%s' set up to track remote ref '%s'."),
  94. local, remote);
  95. else
  96. printf_ln(rebasing ?
  97. _("Branch '%s' set up to track local ref '%s' by rebasing.") :
  98. _("Branch '%s' set up to track local ref '%s'."),
  99. local, remote);
  100. }
  101. }
  102. return 0;
  103. out_err:
  104. strbuf_release(&key);
  105. error(_("Unable to write upstream branch configuration"));
  106. advise(_(tracking_advice),
  107. origin ? origin : "",
  108. origin ? "/" : "",
  109. shortname ? shortname : remote);
  110. return -1;
  111. }
  112. /*
  113. * This is called when new_ref is branched off of orig_ref, and tries
  114. * to infer the settings for branch.<new_ref>.{remote,merge} from the
  115. * config.
  116. */
  117. static void setup_tracking(const char *new_ref, const char *orig_ref,
  118. enum branch_track track, int quiet)
  119. {
  120. struct tracking tracking;
  121. int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
  122. memset(&tracking, 0, sizeof(tracking));
  123. tracking.spec.dst = (char *)orig_ref;
  124. if (for_each_remote(find_tracked_branch, &tracking))
  125. return;
  126. if (!tracking.matches)
  127. switch (track) {
  128. case BRANCH_TRACK_ALWAYS:
  129. case BRANCH_TRACK_EXPLICIT:
  130. case BRANCH_TRACK_OVERRIDE:
  131. break;
  132. default:
  133. return;
  134. }
  135. if (tracking.matches > 1)
  136. die(_("Not tracking: ambiguous information for ref %s"),
  137. orig_ref);
  138. if (install_branch_config(config_flags, new_ref, tracking.remote,
  139. tracking.src ? tracking.src : orig_ref) < 0)
  140. exit(-1);
  141. free(tracking.src);
  142. }
  143. int read_branch_desc(struct strbuf *buf, const char *branch_name)
  144. {
  145. char *v = NULL;
  146. struct strbuf name = STRBUF_INIT;
  147. strbuf_addf(&name, "branch.%s.description", branch_name);
  148. if (git_config_get_string(name.buf, &v)) {
  149. strbuf_release(&name);
  150. return -1;
  151. }
  152. strbuf_addstr(buf, v);
  153. free(v);
  154. strbuf_release(&name);
  155. return 0;
  156. }
  157. /*
  158. * Check if 'name' can be a valid name for a branch; die otherwise.
  159. * Return 1 if the named branch already exists; return 0 otherwise.
  160. * Fill ref with the full refname for the branch.
  161. */
  162. int validate_branchname(const char *name, struct strbuf *ref)
  163. {
  164. if (strbuf_check_branch_ref(ref, name))
  165. die(_("'%s' is not a valid branch name."), name);
  166. return ref_exists(ref->buf);
  167. }
  168. /*
  169. * Check if a branch 'name' can be created as a new branch; die otherwise.
  170. * 'force' can be used when it is OK for the named branch already exists.
  171. * Return 1 if the named branch already exists; return 0 otherwise.
  172. * Fill ref with the full refname for the branch.
  173. */
  174. int validate_new_branchname(const char *name, struct strbuf *ref, int force)
  175. {
  176. const char *head;
  177. if (!validate_branchname(name, ref))
  178. return 0;
  179. if (!force)
  180. die(_("A branch named '%s' already exists."),
  181. ref->buf + strlen("refs/heads/"));
  182. head = resolve_ref_unsafe("HEAD", 0, NULL, NULL);
  183. if (!is_bare_repository() && head && !strcmp(head, ref->buf))
  184. die(_("Cannot force update the current branch."));
  185. return 1;
  186. }
  187. static int check_tracking_branch(struct remote *remote, void *cb_data)
  188. {
  189. char *tracking_branch = cb_data;
  190. struct refspec_item query;
  191. memset(&query, 0, sizeof(struct refspec_item));
  192. query.dst = tracking_branch;
  193. return !remote_find_tracking(remote, &query);
  194. }
  195. static int validate_remote_tracking_branch(char *ref)
  196. {
  197. return !for_each_remote(check_tracking_branch, ref);
  198. }
  199. static const char upstream_not_branch[] =
  200. N_("Cannot setup tracking information; starting point '%s' is not a branch.");
  201. static const char upstream_missing[] =
  202. N_("the requested upstream branch '%s' does not exist");
  203. static const char upstream_advice[] =
  204. N_("\n"
  205. "If you are planning on basing your work on an upstream\n"
  206. "branch that already exists at the remote, you may need to\n"
  207. "run \"git fetch\" to retrieve it.\n"
  208. "\n"
  209. "If you are planning to push out a new local branch that\n"
  210. "will track its remote counterpart, you may want to use\n"
  211. "\"git push -u\" to set the upstream config as you push.");
  212. void create_branch(struct repository *r,
  213. const char *name, const char *start_name,
  214. int force, int clobber_head_ok, int reflog,
  215. int quiet, enum branch_track track)
  216. {
  217. struct commit *commit;
  218. struct object_id oid;
  219. char *real_ref;
  220. struct strbuf ref = STRBUF_INIT;
  221. int forcing = 0;
  222. int dont_change_ref = 0;
  223. int explicit_tracking = 0;
  224. if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
  225. explicit_tracking = 1;
  226. if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
  227. ? validate_branchname(name, &ref)
  228. : validate_new_branchname(name, &ref, force)) {
  229. if (!force)
  230. dont_change_ref = 1;
  231. else
  232. forcing = 1;
  233. }
  234. real_ref = NULL;
  235. if (get_oid_mb(start_name, &oid)) {
  236. if (explicit_tracking) {
  237. if (advice_set_upstream_failure) {
  238. error(_(upstream_missing), start_name);
  239. advise(_(upstream_advice));
  240. exit(1);
  241. }
  242. die(_(upstream_missing), start_name);
  243. }
  244. die(_("Not a valid object name: '%s'."), start_name);
  245. }
  246. switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref)) {
  247. case 0:
  248. /* Not branching from any existing branch */
  249. if (explicit_tracking)
  250. die(_(upstream_not_branch), start_name);
  251. break;
  252. case 1:
  253. /* Unique completion -- good, only if it is a real branch */
  254. if (!starts_with(real_ref, "refs/heads/") &&
  255. validate_remote_tracking_branch(real_ref)) {
  256. if (explicit_tracking)
  257. die(_(upstream_not_branch), start_name);
  258. else
  259. real_ref = NULL;
  260. }
  261. break;
  262. default:
  263. die(_("Ambiguous object name: '%s'."), start_name);
  264. break;
  265. }
  266. if ((commit = lookup_commit_reference(r, &oid)) == NULL)
  267. die(_("Not a valid branch point: '%s'."), start_name);
  268. oidcpy(&oid, &commit->object.oid);
  269. if (reflog)
  270. log_all_ref_updates = LOG_REFS_NORMAL;
  271. if (!dont_change_ref) {
  272. struct ref_transaction *transaction;
  273. struct strbuf err = STRBUF_INIT;
  274. char *msg;
  275. if (forcing)
  276. msg = xstrfmt("branch: Reset to %s", start_name);
  277. else
  278. msg = xstrfmt("branch: Created from %s", start_name);
  279. transaction = ref_transaction_begin(&err);
  280. if (!transaction ||
  281. ref_transaction_update(transaction, ref.buf,
  282. &oid, forcing ? NULL : &null_oid,
  283. 0, msg, &err) ||
  284. ref_transaction_commit(transaction, &err))
  285. die("%s", err.buf);
  286. ref_transaction_free(transaction);
  287. strbuf_release(&err);
  288. free(msg);
  289. }
  290. if (real_ref && track)
  291. setup_tracking(ref.buf + 11, real_ref, track, quiet);
  292. strbuf_release(&ref);
  293. free(real_ref);
  294. }
  295. void remove_merge_branch_state(struct repository *r)
  296. {
  297. unlink(git_path_merge_head(r));
  298. unlink(git_path_merge_rr(r));
  299. unlink(git_path_merge_msg(r));
  300. unlink(git_path_merge_mode(r));
  301. }
  302. void remove_branch_state(struct repository *r, int verbose)
  303. {
  304. sequencer_post_commit_cleanup(r, verbose);
  305. unlink(git_path_squash_msg(r));
  306. remove_merge_branch_state(r);
  307. }
  308. void die_if_checked_out(const char *branch, int ignore_current_worktree)
  309. {
  310. const struct worktree *wt;
  311. wt = find_shared_symref("HEAD", branch);
  312. if (!wt || (ignore_current_worktree && wt->is_current))
  313. return;
  314. skip_prefix(branch, "refs/heads/", &branch);
  315. die(_("'%s' is already checked out at '%s'"),
  316. branch, wt->path);
  317. }
  318. int replace_each_worktree_head_symref(const char *oldref, const char *newref,
  319. const char *logmsg)
  320. {
  321. int ret = 0;
  322. struct worktree **worktrees = get_worktrees(0);
  323. int i;
  324. for (i = 0; worktrees[i]; i++) {
  325. struct ref_store *refs;
  326. if (worktrees[i]->is_detached)
  327. continue;
  328. if (!worktrees[i]->head_ref)
  329. continue;
  330. if (strcmp(oldref, worktrees[i]->head_ref))
  331. continue;
  332. refs = get_worktree_ref_store(worktrees[i]);
  333. if (refs_create_symref(refs, "HEAD", newref, logmsg))
  334. ret = error(_("HEAD of working tree %s is not updated"),
  335. worktrees[i]->path);
  336. }
  337. free_worktrees(worktrees);
  338. return ret;
  339. }