1 #include "git-compat-util.h"
16 static int find_tracked_branch(struct remote
*remote
, void *priv
)
18 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
;
25 free(tracking
->spec
.src
);
31 tracking
->spec
.src
= NULL
;
37 static int should_setup_rebase(const char *origin
)
40 case AUTOREBASE_NEVER
:
42 case AUTOREBASE_LOCAL
:
43 return origin
== NULL
;
44 case AUTOREBASE_REMOTE
:
45 return origin
!= NULL
;
46 case AUTOREBASE_ALWAYS
:
52 static const char tracking_advice
[] =
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\".");
58 int install_branch_config(int flag
, const char *local
, const char *origin
, const char *remote
)
60 const char *shortname
= NULL
;
61 struct strbuf key
= STRBUF_INIT
;
62 int rebasing
= should_setup_rebase(origin
);
64 if (skip_prefix(remote
, "refs/heads/", &shortname
)
65 && !strcmp(local
, shortname
)
67 warning(_("Not setting branch %s as its own upstream."),
72 strbuf_addf(&key
, "branch.%s.remote", local
);
73 if (git_config_set_gently(key
.buf
, origin
? origin
: ".") < 0)
77 strbuf_addf(&key
, "branch.%s.merge", local
);
78 if (git_config_set_gently(key
.buf
, remote
) < 0)
83 strbuf_addf(&key
, "branch.%s.rebase", local
);
84 if (git_config_set_gently(key
.buf
, "true") < 0)
89 if (flag
& BRANCH_CONFIG_VERBOSE
) {
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
);
98 _("Branch %s set up to track local branch %s by rebasing.") :
99 _("Branch %s set up to track local branch %s."),
104 _("Branch %s set up to track remote ref %s by rebasing.") :
105 _("Branch %s set up to track remote ref %s."),
109 _("Branch %s set up to track local ref %s by rebasing.") :
110 _("Branch %s set up to track local ref %s."),
118 strbuf_release(&key
);
119 error(_("Unable to write upstream branch configuration"));
121 advise(_(tracking_advice
),
122 origin
? origin
: "",
124 shortname
? shortname
: remote
);
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
134 static void setup_tracking(const char *new_ref
, const char *orig_ref
,
135 enum branch_track track
, int quiet
)
137 struct tracking tracking
;
138 int config_flags
= quiet
? 0 : BRANCH_CONFIG_VERBOSE
;
140 memset(&tracking
, 0, sizeof(tracking
));
141 tracking
.spec
.dst
= (char *)orig_ref
;
142 if (for_each_remote(find_tracked_branch
, &tracking
))
145 if (!tracking
.matches
)
147 case BRANCH_TRACK_ALWAYS
:
148 case BRANCH_TRACK_EXPLICIT
:
149 case BRANCH_TRACK_OVERRIDE
:
155 if (tracking
.matches
> 1)
156 die(_("Not tracking: ambiguous information for ref %s"),
159 if (install_branch_config(config_flags
, new_ref
, tracking
.remote
,
160 tracking
.src
? tracking
.src
: orig_ref
) < 0)
166 int read_branch_desc(struct strbuf
*buf
, const char *branch_name
)
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
);
175 strbuf_addstr(buf
, v
);
177 strbuf_release(&name
);
181 int validate_new_branchname(const char *name
, struct strbuf
*ref
,
182 int force
, int attr_only
)
184 if (strbuf_check_branch_ref(ref
, name
))
185 die(_("'%s' is not a valid branch name."), name
);
187 if (!ref_exists(ref
->buf
))
189 else if (!force
&& !attr_only
)
190 die(_("A branch named '%s' already exists."), ref
->buf
+ strlen("refs/heads/"));
194 struct object_id oid
;
196 head
= resolve_ref_unsafe("HEAD", 0, oid
.hash
, NULL
);
197 if (!is_bare_repository() && head
&& !strcmp(head
, ref
->buf
))
198 die(_("Cannot force update the current branch."));
203 static int check_tracking_branch(struct remote
*remote
, void *cb_data
)
205 char *tracking_branch
= cb_data
;
206 struct refspec query
;
207 memset(&query
, 0, sizeof(struct refspec
));
208 query
.dst
= tracking_branch
;
209 return !remote_find_tracking(remote
, &query
);
212 static int validate_remote_tracking_branch(char *ref
)
214 return !for_each_remote(check_tracking_branch
, ref
);
217 static const char upstream_not_branch
[] =
218 N_("Cannot setup tracking information; starting point '%s' is not a branch.");
219 static const char upstream_missing
[] =
220 N_("the requested upstream branch '%s' does not exist");
221 static const char upstream_advice
[] =
223 "If you are planning on basing your work on an upstream\n"
224 "branch that already exists at the remote, you may need to\n"
225 "run \"git fetch\" to retrieve it.\n"
227 "If you are planning to push out a new local branch that\n"
228 "will track its remote counterpart, you may want to use\n"
229 "\"git push -u\" to set the upstream config as you push.");
231 void create_branch(const char *name
, const char *start_name
,
232 int force
, int reflog
, int clobber_head
,
233 int quiet
, enum branch_track track
)
235 struct commit
*commit
;
236 struct object_id oid
;
238 struct strbuf ref
= STRBUF_INIT
;
240 int dont_change_ref
= 0;
241 int explicit_tracking
= 0;
243 if (track
== BRANCH_TRACK_EXPLICIT
|| track
== BRANCH_TRACK_OVERRIDE
)
244 explicit_tracking
= 1;
246 if (validate_new_branchname(name
, &ref
, force
,
247 track
== BRANCH_TRACK_OVERRIDE
||
256 if (get_oid(start_name
, &oid
)) {
257 if (explicit_tracking
) {
258 if (advice_set_upstream_failure
) {
259 error(_(upstream_missing
), start_name
);
260 advise(_(upstream_advice
));
263 die(_(upstream_missing
), start_name
);
265 die(_("Not a valid object name: '%s'."), start_name
);
268 switch (dwim_ref(start_name
, strlen(start_name
), oid
.hash
, &real_ref
)) {
270 /* Not branching from any existing branch */
271 if (explicit_tracking
)
272 die(_(upstream_not_branch
), start_name
);
275 /* Unique completion -- good, only if it is a real branch */
276 if (!starts_with(real_ref
, "refs/heads/") &&
277 validate_remote_tracking_branch(real_ref
)) {
278 if (explicit_tracking
)
279 die(_(upstream_not_branch
), start_name
);
285 die(_("Ambiguous object name: '%s'."), start_name
);
289 if ((commit
= lookup_commit_reference(&oid
)) == NULL
)
290 die(_("Not a valid branch point: '%s'."), start_name
);
291 oidcpy(&oid
, &commit
->object
.oid
);
294 log_all_ref_updates
= LOG_REFS_NORMAL
;
296 if (!dont_change_ref
) {
297 struct ref_transaction
*transaction
;
298 struct strbuf err
= STRBUF_INIT
;
302 msg
= xstrfmt("branch: Reset to %s", start_name
);
304 msg
= xstrfmt("branch: Created from %s", start_name
);
306 transaction
= ref_transaction_begin(&err
);
308 ref_transaction_update(transaction
, ref
.buf
,
309 oid
.hash
, forcing
? NULL
: null_sha1
,
311 ref_transaction_commit(transaction
, &err
))
313 ref_transaction_free(transaction
);
314 strbuf_release(&err
);
318 if (real_ref
&& track
)
319 setup_tracking(ref
.buf
+ 11, real_ref
, track
, quiet
);
321 strbuf_release(&ref
);
325 void remove_branch_state(void)
327 unlink(git_path_cherry_pick_head());
328 unlink(git_path_revert_head());
329 unlink(git_path_merge_head());
330 unlink(git_path_merge_rr());
331 unlink(git_path_merge_msg());
332 unlink(git_path_merge_mode());
333 unlink(git_path_squash_msg());
336 void die_if_checked_out(const char *branch
, int ignore_current_worktree
)
338 const struct worktree
*wt
;
340 wt
= find_shared_symref("HEAD", branch
);
341 if (!wt
|| (ignore_current_worktree
&& wt
->is_current
))
343 skip_prefix(branch
, "refs/heads/", &branch
);
344 die(_("'%s' is already checked out at '%s'"),
348 int replace_each_worktree_head_symref(const char *oldref
, const char *newref
,
352 struct worktree
**worktrees
= get_worktrees(0);
355 for (i
= 0; worktrees
[i
]; i
++) {
356 if (worktrees
[i
]->is_detached
)
358 if (strcmp(oldref
, worktrees
[i
]->head_ref
))
361 if (set_worktree_head_symref(get_worktree_git_dir(worktrees
[i
]),
364 error(_("HEAD of working tree %s is not updated"),
369 free_worktrees(worktrees
);