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