The third batch
[git/debian.git] / branch.c
blob5d20a2e8484b22f321a60e01d63cb4c4bd8e88fe
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"
12 struct tracking {
13 struct refspec_item spec;
14 struct string_list *srcs;
15 const char *remote;
16 int matches;
19 static int find_tracked_branch(struct remote *remote, void *priv)
21 struct tracking *tracking = priv;
23 if (!remote_find_tracking(remote, &tracking->spec)) {
24 if (++tracking->matches == 1) {
25 string_list_append(tracking->srcs, tracking->spec.src);
26 tracking->remote = remote->name;
27 } else {
28 free(tracking->spec.src);
29 string_list_clear(tracking->srcs, 0);
31 tracking->spec.src = NULL;
34 return 0;
37 static int should_setup_rebase(const char *origin)
39 switch (autorebase) {
40 case AUTOREBASE_NEVER:
41 return 0;
42 case AUTOREBASE_LOCAL:
43 return origin == NULL;
44 case AUTOREBASE_REMOTE:
45 return origin != NULL;
46 case AUTOREBASE_ALWAYS:
47 return 1;
49 return 0;
52 /**
53 * Install upstream tracking configuration for a branch; specifically, add
54 * `branch.<name>.remote` and `branch.<name>.merge` entries.
56 * `flag` contains integer flags for options; currently only
57 * BRANCH_CONFIG_VERBOSE is checked.
59 * `local` is the name of the branch whose configuration we're installing.
61 * `origin` is the name of the remote owning the upstream branches. NULL means
62 * the upstream branches are local to this repo.
64 * `remotes` is a list of refs that are upstream of local
66 static int install_branch_config_multiple_remotes(int flag, const char *local,
67 const char *origin, struct string_list *remotes)
69 const char *shortname = NULL;
70 struct strbuf key = STRBUF_INIT;
71 struct string_list_item *item;
72 int rebasing = should_setup_rebase(origin);
74 if (!remotes->nr)
75 BUG("must provide at least one remote for branch config");
76 if (rebasing && remotes->nr > 1)
77 die(_("cannot inherit upstream tracking configuration of "
78 "multiple refs when rebasing is requested"));
81 * If the new branch is trying to track itself, something has gone
82 * wrong. Warn the user and don't proceed any further.
84 if (!origin)
85 for_each_string_list_item(item, remotes)
86 if (skip_prefix(item->string, "refs/heads/", &shortname)
87 && !strcmp(local, shortname)) {
88 warning(_("not setting branch '%s' as its own upstream"),
89 local);
90 return 0;
93 strbuf_addf(&key, "branch.%s.remote", local);
94 if (git_config_set_gently(key.buf, origin ? origin : ".") < 0)
95 goto out_err;
97 strbuf_reset(&key);
98 strbuf_addf(&key, "branch.%s.merge", local);
100 * We want to overwrite any existing config with all the branches in
101 * "remotes". Override any existing config, then write our branches. If
102 * more than one is provided, use CONFIG_REGEX_NONE to preserve what
103 * we've written so far.
105 if (git_config_set_gently(key.buf, NULL) < 0)
106 goto out_err;
107 for_each_string_list_item(item, remotes)
108 if (git_config_set_multivar_gently(key.buf, item->string, CONFIG_REGEX_NONE, 0) < 0)
109 goto out_err;
111 if (rebasing) {
112 strbuf_reset(&key);
113 strbuf_addf(&key, "branch.%s.rebase", local);
114 if (git_config_set_gently(key.buf, "true") < 0)
115 goto out_err;
117 strbuf_release(&key);
119 if (flag & BRANCH_CONFIG_VERBOSE) {
120 struct strbuf tmp_ref_name = STRBUF_INIT;
121 struct string_list friendly_ref_names = STRING_LIST_INIT_DUP;
123 for_each_string_list_item(item, remotes) {
124 shortname = item->string;
125 skip_prefix(shortname, "refs/heads/", &shortname);
126 if (origin) {
127 strbuf_addf(&tmp_ref_name, "%s/%s",
128 origin, shortname);
129 string_list_append_nodup(
130 &friendly_ref_names,
131 strbuf_detach(&tmp_ref_name, NULL));
132 } else {
133 string_list_append(
134 &friendly_ref_names, shortname);
138 if (remotes->nr == 1) {
140 * Rebasing is only allowed in the case of a single
141 * upstream branch.
143 printf_ln(rebasing ?
144 _("branch '%s' set up to track '%s' by rebasing.") :
145 _("branch '%s' set up to track '%s'."),
146 local, friendly_ref_names.items[0].string);
147 } else {
148 printf_ln(_("branch '%s' set up to track:"), local);
149 for_each_string_list_item(item, &friendly_ref_names)
150 printf_ln(" %s", item->string);
153 string_list_clear(&friendly_ref_names, 0);
156 return 0;
158 out_err:
159 strbuf_release(&key);
160 error(_("unable to write upstream branch configuration"));
162 advise(_("\nAfter fixing the error cause you may try to fix up\n"
163 "the remote tracking information by invoking:"));
164 if (remotes->nr == 1)
165 advise(" git branch --set-upstream-to=%s%s%s",
166 origin ? origin : "",
167 origin ? "/" : "",
168 remotes->items[0].string);
169 else {
170 advise(" git config --add branch.\"%s\".remote %s",
171 local, origin ? origin : ".");
172 for_each_string_list_item(item, remotes)
173 advise(" git config --add branch.\"%s\".merge %s",
174 local, item->string);
177 return -1;
180 int install_branch_config(int flag, const char *local, const char *origin,
181 const char *remote)
183 int ret;
184 struct string_list remotes = STRING_LIST_INIT_DUP;
186 string_list_append(&remotes, remote);
187 ret = install_branch_config_multiple_remotes(flag, local, origin, &remotes);
188 string_list_clear(&remotes, 0);
189 return ret;
192 static int inherit_tracking(struct tracking *tracking, const char *orig_ref)
194 const char *bare_ref;
195 struct branch *branch;
196 int i;
198 bare_ref = orig_ref;
199 skip_prefix(orig_ref, "refs/heads/", &bare_ref);
201 branch = branch_get(bare_ref);
202 if (!branch->remote_name) {
203 warning(_("asked to inherit tracking from '%s', but no remote is set"),
204 bare_ref);
205 return -1;
208 if (branch->merge_nr < 1 || !branch->merge_name || !branch->merge_name[0]) {
209 warning(_("asked to inherit tracking from '%s', but no merge configuration is set"),
210 bare_ref);
211 return -1;
214 tracking->remote = xstrdup(branch->remote_name);
215 for (i = 0; i < branch->merge_nr; i++)
216 string_list_append(tracking->srcs, branch->merge_name[i]);
217 return 0;
221 * This is called when new_ref is branched off of orig_ref, and tries
222 * to infer the settings for branch.<new_ref>.{remote,merge} from the
223 * config.
225 static void setup_tracking(const char *new_ref, const char *orig_ref,
226 enum branch_track track, int quiet)
228 struct tracking tracking;
229 struct string_list tracking_srcs = STRING_LIST_INIT_DUP;
230 int config_flags = quiet ? 0 : BRANCH_CONFIG_VERBOSE;
232 memset(&tracking, 0, sizeof(tracking));
233 tracking.spec.dst = (char *)orig_ref;
234 tracking.srcs = &tracking_srcs;
235 if (track != BRANCH_TRACK_INHERIT)
236 for_each_remote(find_tracked_branch, &tracking);
237 else if (inherit_tracking(&tracking, orig_ref))
238 return;
240 if (!tracking.matches)
241 switch (track) {
242 case BRANCH_TRACK_ALWAYS:
243 case BRANCH_TRACK_EXPLICIT:
244 case BRANCH_TRACK_OVERRIDE:
245 case BRANCH_TRACK_INHERIT:
246 break;
247 default:
248 return;
251 if (tracking.matches > 1)
252 die(_("not tracking: ambiguous information for ref %s"),
253 orig_ref);
255 if (tracking.srcs->nr < 1)
256 string_list_append(tracking.srcs, orig_ref);
257 if (install_branch_config_multiple_remotes(config_flags, new_ref,
258 tracking.remote, tracking.srcs) < 0)
259 exit(-1);
261 string_list_clear(tracking.srcs, 0);
264 int read_branch_desc(struct strbuf *buf, const char *branch_name)
266 char *v = NULL;
267 struct strbuf name = STRBUF_INIT;
268 strbuf_addf(&name, "branch.%s.description", branch_name);
269 if (git_config_get_string(name.buf, &v)) {
270 strbuf_release(&name);
271 return -1;
273 strbuf_addstr(buf, v);
274 free(v);
275 strbuf_release(&name);
276 return 0;
280 * Check if 'name' can be a valid name for a branch; die otherwise.
281 * Return 1 if the named branch already exists; return 0 otherwise.
282 * Fill ref with the full refname for the branch.
284 int validate_branchname(const char *name, struct strbuf *ref)
286 if (strbuf_check_branch_ref(ref, name))
287 die(_("'%s' is not a valid branch name"), name);
289 return ref_exists(ref->buf);
293 * Check if a branch 'name' can be created as a new branch; die otherwise.
294 * 'force' can be used when it is OK for the named branch already exists.
295 * Return 1 if the named branch already exists; return 0 otherwise.
296 * Fill ref with the full refname for the branch.
298 int validate_new_branchname(const char *name, struct strbuf *ref, int force)
300 struct worktree **worktrees;
301 const struct worktree *wt;
303 if (!validate_branchname(name, ref))
304 return 0;
306 if (!force)
307 die(_("a branch named '%s' already exists"),
308 ref->buf + strlen("refs/heads/"));
310 worktrees = get_worktrees();
311 wt = find_shared_symref(worktrees, "HEAD", ref->buf);
312 if (wt && !wt->is_bare)
313 die(_("cannot force update the branch '%s' "
314 "checked out at '%s'"),
315 ref->buf + strlen("refs/heads/"), wt->path);
316 free_worktrees(worktrees);
318 return 1;
321 static int check_tracking_branch(struct remote *remote, void *cb_data)
323 char *tracking_branch = cb_data;
324 struct refspec_item query;
325 memset(&query, 0, sizeof(struct refspec_item));
326 query.dst = tracking_branch;
327 return !remote_find_tracking(remote, &query);
330 static int validate_remote_tracking_branch(char *ref)
332 return !for_each_remote(check_tracking_branch, ref);
335 static const char upstream_not_branch[] =
336 N_("cannot set up tracking information; starting point '%s' is not a branch");
337 static const char upstream_missing[] =
338 N_("the requested upstream branch '%s' does not exist");
339 static const char upstream_advice[] =
340 N_("\n"
341 "If you are planning on basing your work on an upstream\n"
342 "branch that already exists at the remote, you may need to\n"
343 "run \"git fetch\" to retrieve it.\n"
344 "\n"
345 "If you are planning to push out a new local branch that\n"
346 "will track its remote counterpart, you may want to use\n"
347 "\"git push -u\" to set the upstream config as you push.");
349 void create_branch(struct repository *r,
350 const char *name, const char *start_name,
351 int force, int clobber_head_ok, int reflog,
352 int quiet, enum branch_track track)
354 struct commit *commit;
355 struct object_id oid;
356 char *real_ref;
357 struct strbuf ref = STRBUF_INIT;
358 int forcing = 0;
359 int dont_change_ref = 0;
360 int explicit_tracking = 0;
362 if (track == BRANCH_TRACK_EXPLICIT || track == BRANCH_TRACK_OVERRIDE)
363 explicit_tracking = 1;
365 if ((track == BRANCH_TRACK_OVERRIDE || clobber_head_ok)
366 ? validate_branchname(name, &ref)
367 : validate_new_branchname(name, &ref, force)) {
368 if (!force)
369 dont_change_ref = 1;
370 else
371 forcing = 1;
374 real_ref = NULL;
375 if (get_oid_mb(start_name, &oid)) {
376 if (explicit_tracking) {
377 if (advice_enabled(ADVICE_SET_UPSTREAM_FAILURE)) {
378 error(_(upstream_missing), start_name);
379 advise(_(upstream_advice));
380 exit(1);
382 die(_(upstream_missing), start_name);
384 die(_("not a valid object name: '%s'"), start_name);
387 switch (dwim_ref(start_name, strlen(start_name), &oid, &real_ref, 0)) {
388 case 0:
389 /* Not branching from any existing branch */
390 if (explicit_tracking)
391 die(_(upstream_not_branch), start_name);
392 break;
393 case 1:
394 /* Unique completion -- good, only if it is a real branch */
395 if (!starts_with(real_ref, "refs/heads/") &&
396 validate_remote_tracking_branch(real_ref)) {
397 if (explicit_tracking)
398 die(_(upstream_not_branch), start_name);
399 else
400 FREE_AND_NULL(real_ref);
402 break;
403 default:
404 die(_("ambiguous object name: '%s'"), start_name);
405 break;
408 if ((commit = lookup_commit_reference(r, &oid)) == NULL)
409 die(_("not a valid branch point: '%s'"), start_name);
410 oidcpy(&oid, &commit->object.oid);
412 if (reflog)
413 log_all_ref_updates = LOG_REFS_NORMAL;
415 if (!dont_change_ref) {
416 struct ref_transaction *transaction;
417 struct strbuf err = STRBUF_INIT;
418 char *msg;
420 if (forcing)
421 msg = xstrfmt("branch: Reset to %s", start_name);
422 else
423 msg = xstrfmt("branch: Created from %s", start_name);
425 transaction = ref_transaction_begin(&err);
426 if (!transaction ||
427 ref_transaction_update(transaction, ref.buf,
428 &oid, forcing ? NULL : null_oid(),
429 0, msg, &err) ||
430 ref_transaction_commit(transaction, &err))
431 die("%s", err.buf);
432 ref_transaction_free(transaction);
433 strbuf_release(&err);
434 free(msg);
437 if (real_ref && track)
438 setup_tracking(ref.buf + 11, real_ref, track, quiet);
440 strbuf_release(&ref);
441 free(real_ref);
444 void remove_merge_branch_state(struct repository *r)
446 unlink(git_path_merge_head(r));
447 unlink(git_path_merge_rr(r));
448 unlink(git_path_merge_msg(r));
449 unlink(git_path_merge_mode(r));
450 unlink(git_path_auto_merge(r));
451 save_autostash(git_path_merge_autostash(r));
454 void remove_branch_state(struct repository *r, int verbose)
456 sequencer_post_commit_cleanup(r, verbose);
457 unlink(git_path_squash_msg(r));
458 remove_merge_branch_state(r);
461 void die_if_checked_out(const char *branch, int ignore_current_worktree)
463 struct worktree **worktrees = get_worktrees();
464 const struct worktree *wt;
466 wt = find_shared_symref(worktrees, "HEAD", branch);
467 if (wt && (!ignore_current_worktree || !wt->is_current)) {
468 skip_prefix(branch, "refs/heads/", &branch);
469 die(_("'%s' is already checked out at '%s'"), branch, wt->path);
472 free_worktrees(worktrees);
475 int replace_each_worktree_head_symref(const char *oldref, const char *newref,
476 const char *logmsg)
478 int ret = 0;
479 struct worktree **worktrees = get_worktrees();
480 int i;
482 for (i = 0; worktrees[i]; i++) {
483 struct ref_store *refs;
485 if (worktrees[i]->is_detached)
486 continue;
487 if (!worktrees[i]->head_ref)
488 continue;
489 if (strcmp(oldref, worktrees[i]->head_ref))
490 continue;
492 refs = get_worktree_ref_store(worktrees[i]);
493 if (refs_create_symref(refs, "HEAD", newref, logmsg))
494 ret = error(_("HEAD of working tree %s is not updated"),
495 worktrees[i]->path);
498 free_worktrees(worktrees);
499 return ret;