1 #include "git-compat-util.h"
12 struct refspec_item spec
;
18 static int find_tracked_branch(struct remote
*remote
, void *priv
)
20 struct tracking
*tracking
= priv
;
22 if (!remote_find_tracking(remote
, &tracking
->spec
)) {
23 if (++tracking
->matches
== 1) {
24 tracking
->src
= tracking
->spec
.src
;
25 tracking
->remote
= remote
->name
;
27 free(tracking
->spec
.src
);
28 FREE_AND_NULL(tracking
->src
);
30 tracking
->spec
.src
= NULL
;
36 static int should_setup_rebase(const char *origin
)
39 case AUTOREBASE_NEVER
:
41 case AUTOREBASE_LOCAL
:
42 return origin
== NULL
;
43 case AUTOREBASE_REMOTE
:
44 return origin
!= NULL
;
45 case AUTOREBASE_ALWAYS
:
51 static const char tracking_advice
[] =
53 "After fixing the error cause you may try to fix up\n"
54 "the remote tracking information by invoking\n"
55 "\"git branch --set-upstream-to=%s%s%s\".");
57 int install_branch_config(int flag
, const char *local
, const char *origin
, const char *remote
)
59 const char *shortname
= NULL
;
60 struct strbuf key
= STRBUF_INIT
;
61 int rebasing
= should_setup_rebase(origin
);
63 if (skip_prefix(remote
, "refs/heads/", &shortname
)
64 && !strcmp(local
, shortname
)
66 warning(_("Not setting branch %s as its own upstream."),
71 strbuf_addf(&key
, "branch.%s.remote", local
);
72 if (git_config_set_gently(key
.buf
, origin
? origin
: ".") < 0)
76 strbuf_addf(&key
, "branch.%s.merge", local
);
77 if (git_config_set_gently(key
.buf
, remote
) < 0)
82 strbuf_addf(&key
, "branch.%s.rebase", local
);
83 if (git_config_set_gently(key
.buf
, "true") < 0)
88 if (flag
& BRANCH_CONFIG_VERBOSE
) {
92 _("Branch '%s' set up to track remote branch '%s' from '%s' by rebasing.") :
93 _("Branch '%s' set up to track remote branch '%s' from '%s'."),
94 local
, shortname
, origin
);
97 _("Branch '%s' set up to track local branch '%s' by rebasing.") :
98 _("Branch '%s' set up to track local branch '%s'."),
103 _("Branch '%s' set up to track remote ref '%s' by rebasing.") :
104 _("Branch '%s' set up to track remote ref '%s'."),
108 _("Branch '%s' set up to track local ref '%s' by rebasing.") :
109 _("Branch '%s' set up to track local ref '%s'."),
117 strbuf_release(&key
);
118 error(_("Unable to write upstream branch configuration"));
120 advise(_(tracking_advice
),
121 origin
? origin
: "",
123 shortname
? shortname
: remote
);
129 * This is called when new_ref is branched off of orig_ref, and tries
130 * to infer the settings for branch.<new_ref>.{remote,merge} from the
133 static void setup_tracking(const char *new_ref
, const char *orig_ref
,
134 enum branch_track track
, int quiet
)
136 struct tracking tracking
;
137 int config_flags
= quiet
? 0 : BRANCH_CONFIG_VERBOSE
;
139 memset(&tracking
, 0, sizeof(tracking
));
140 tracking
.spec
.dst
= (char *)orig_ref
;
141 if (for_each_remote(find_tracked_branch
, &tracking
))
144 if (!tracking
.matches
)
146 case BRANCH_TRACK_ALWAYS
:
147 case BRANCH_TRACK_EXPLICIT
:
148 case BRANCH_TRACK_OVERRIDE
:
154 if (tracking
.matches
> 1)
155 die(_("Not tracking: ambiguous information for ref %s"),
158 if (install_branch_config(config_flags
, new_ref
, tracking
.remote
,
159 tracking
.src
? tracking
.src
: orig_ref
) < 0)
165 int read_branch_desc(struct strbuf
*buf
, const char *branch_name
)
168 struct strbuf name
= STRBUF_INIT
;
169 strbuf_addf(&name
, "branch.%s.description", branch_name
);
170 if (git_config_get_string(name
.buf
, &v
)) {
171 strbuf_release(&name
);
174 strbuf_addstr(buf
, v
);
176 strbuf_release(&name
);
181 * Check if 'name' can be a valid name for a branch; die otherwise.
182 * Return 1 if the named branch already exists; return 0 otherwise.
183 * Fill ref with the full refname for the branch.
185 int validate_branchname(const char *name
, struct strbuf
*ref
)
187 if (strbuf_check_branch_ref(ref
, name
))
188 die(_("'%s' is not a valid branch name."), name
);
190 return ref_exists(ref
->buf
);
194 * Check if a branch 'name' can be created as a new branch; die otherwise.
195 * 'force' can be used when it is OK for the named branch already exists.
196 * Return 1 if the named branch already exists; return 0 otherwise.
197 * Fill ref with the full refname for the branch.
199 int validate_new_branchname(const char *name
, struct strbuf
*ref
, int force
)
203 if (!validate_branchname(name
, ref
))
207 die(_("A branch named '%s' already exists."),
208 ref
->buf
+ strlen("refs/heads/"));
210 head
= resolve_ref_unsafe("HEAD", 0, NULL
, NULL
);
211 if (!is_bare_repository() && head
&& !strcmp(head
, ref
->buf
))
212 die(_("Cannot force update the current branch."));
217 static int check_tracking_branch(struct remote
*remote
, void *cb_data
)
219 char *tracking_branch
= cb_data
;
220 struct refspec_item query
;
221 memset(&query
, 0, sizeof(struct refspec_item
));
222 query
.dst
= tracking_branch
;
223 return !remote_find_tracking(remote
, &query
);
226 static int validate_remote_tracking_branch(char *ref
)
228 return !for_each_remote(check_tracking_branch
, ref
);
231 static const char upstream_not_branch
[] =
232 N_("Cannot setup tracking information; starting point '%s' is not a branch.");
233 static const char upstream_missing
[] =
234 N_("the requested upstream branch '%s' does not exist");
235 static const char upstream_advice
[] =
237 "If you are planning on basing your work on an upstream\n"
238 "branch that already exists at the remote, you may need to\n"
239 "run \"git fetch\" to retrieve it.\n"
241 "If you are planning to push out a new local branch that\n"
242 "will track its remote counterpart, you may want to use\n"
243 "\"git push -u\" to set the upstream config as you push.");
245 void create_branch(const char *name
, const char *start_name
,
246 int force
, int clobber_head_ok
, int reflog
,
247 int quiet
, enum branch_track track
)
249 struct commit
*commit
;
250 struct object_id oid
;
252 struct strbuf ref
= STRBUF_INIT
;
254 int dont_change_ref
= 0;
255 int explicit_tracking
= 0;
257 if (track
== BRANCH_TRACK_EXPLICIT
|| track
== BRANCH_TRACK_OVERRIDE
)
258 explicit_tracking
= 1;
260 if ((track
== BRANCH_TRACK_OVERRIDE
|| clobber_head_ok
)
261 ? validate_branchname(name
, &ref
)
262 : validate_new_branchname(name
, &ref
, force
)) {
270 if (get_oid(start_name
, &oid
)) {
271 if (explicit_tracking
) {
272 if (advice_set_upstream_failure
) {
273 error(_(upstream_missing
), start_name
);
274 advise(_(upstream_advice
));
277 die(_(upstream_missing
), start_name
);
279 die(_("Not a valid object name: '%s'."), start_name
);
282 switch (dwim_ref(start_name
, strlen(start_name
), &oid
, &real_ref
)) {
284 /* Not branching from any existing branch */
285 if (explicit_tracking
)
286 die(_(upstream_not_branch
), start_name
);
289 /* Unique completion -- good, only if it is a real branch */
290 if (!starts_with(real_ref
, "refs/heads/") &&
291 validate_remote_tracking_branch(real_ref
)) {
292 if (explicit_tracking
)
293 die(_(upstream_not_branch
), start_name
);
299 die(_("Ambiguous object name: '%s'."), start_name
);
303 if ((commit
= lookup_commit_reference(the_repository
, &oid
)) == NULL
)
304 die(_("Not a valid branch point: '%s'."), start_name
);
305 oidcpy(&oid
, &commit
->object
.oid
);
308 log_all_ref_updates
= LOG_REFS_NORMAL
;
310 if (!dont_change_ref
) {
311 struct ref_transaction
*transaction
;
312 struct strbuf err
= STRBUF_INIT
;
316 msg
= xstrfmt("branch: Reset to %s", start_name
);
318 msg
= xstrfmt("branch: Created from %s", start_name
);
320 transaction
= ref_transaction_begin(&err
);
322 ref_transaction_update(transaction
, ref
.buf
,
323 &oid
, forcing
? NULL
: &null_oid
,
325 ref_transaction_commit(transaction
, &err
))
327 ref_transaction_free(transaction
);
328 strbuf_release(&err
);
332 if (real_ref
&& track
)
333 setup_tracking(ref
.buf
+ 11, real_ref
, track
, quiet
);
335 strbuf_release(&ref
);
339 void remove_branch_state(void)
341 unlink(git_path_cherry_pick_head(the_repository
));
342 unlink(git_path_revert_head(the_repository
));
343 unlink(git_path_merge_head(the_repository
));
344 unlink(git_path_merge_rr(the_repository
));
345 unlink(git_path_merge_msg(the_repository
));
346 unlink(git_path_merge_mode(the_repository
));
347 unlink(git_path_squash_msg(the_repository
));
350 void die_if_checked_out(const char *branch
, int ignore_current_worktree
)
352 const struct worktree
*wt
;
354 wt
= find_shared_symref("HEAD", branch
);
355 if (!wt
|| (ignore_current_worktree
&& wt
->is_current
))
357 skip_prefix(branch
, "refs/heads/", &branch
);
358 die(_("'%s' is already checked out at '%s'"),
362 int replace_each_worktree_head_symref(const char *oldref
, const char *newref
,
366 struct worktree
**worktrees
= get_worktrees(0);
369 for (i
= 0; worktrees
[i
]; i
++) {
370 struct ref_store
*refs
;
372 if (worktrees
[i
]->is_detached
)
374 if (!worktrees
[i
]->head_ref
)
376 if (strcmp(oldref
, worktrees
[i
]->head_ref
))
379 refs
= get_worktree_ref_store(worktrees
[i
]);
380 if (refs_create_symref(refs
, "HEAD", newref
, logmsg
))
381 ret
= error(_("HEAD of working tree %s is not updated"),
385 free_worktrees(worktrees
);