1 #include "git-compat-util.h"
15 static int find_tracked_branch(struct remote
*remote
, void *priv
)
17 struct tracking
*tracking
= priv
;
19 if (!remote_find_tracking(remote
, &tracking
->spec
)) {
20 if (++tracking
->matches
== 1) {
21 tracking
->src
= tracking
->spec
.src
;
22 tracking
->remote
= remote
->name
;
24 free(tracking
->spec
.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 void install_branch_config(int flag
, const char *local
, const char *origin
, const char *remote
)
53 const char *shortname
= NULL
;
54 struct strbuf key
= STRBUF_INIT
;
55 int rebasing
= should_setup_rebase(origin
);
57 if (skip_prefix(remote
, "refs/heads/", &shortname
)
58 && !strcmp(local
, shortname
)
60 warning(_("Not setting branch %s as its own upstream."),
65 strbuf_addf(&key
, "branch.%s.remote", local
);
66 git_config_set(key
.buf
, origin
? origin
: ".");
69 strbuf_addf(&key
, "branch.%s.merge", local
);
70 git_config_set(key
.buf
, remote
);
74 strbuf_addf(&key
, "branch.%s.rebase", local
);
75 git_config_set(key
.buf
, "true");
79 if (flag
& BRANCH_CONFIG_VERBOSE
) {
83 _("Branch %s set up to track remote branch %s from %s by rebasing.") :
84 _("Branch %s set up to track remote branch %s from %s."),
85 local
, shortname
, origin
);
88 _("Branch %s set up to track local branch %s by rebasing.") :
89 _("Branch %s set up to track local branch %s."),
94 _("Branch %s set up to track remote ref %s by rebasing.") :
95 _("Branch %s set up to track remote ref %s."),
99 _("Branch %s set up to track local ref %s by rebasing.") :
100 _("Branch %s set up to track local ref %s."),
107 * This is called when new_ref is branched off of orig_ref, and tries
108 * to infer the settings for branch.<new_ref>.{remote,merge} from the
111 static int setup_tracking(const char *new_ref
, const char *orig_ref
,
112 enum branch_track track
, int quiet
)
114 struct tracking tracking
;
115 int config_flags
= quiet
? 0 : BRANCH_CONFIG_VERBOSE
;
117 memset(&tracking
, 0, sizeof(tracking
));
118 tracking
.spec
.dst
= (char *)orig_ref
;
119 if (for_each_remote(find_tracked_branch
, &tracking
))
122 if (!tracking
.matches
)
124 case BRANCH_TRACK_ALWAYS
:
125 case BRANCH_TRACK_EXPLICIT
:
126 case BRANCH_TRACK_OVERRIDE
:
132 if (tracking
.matches
> 1)
133 return error(_("Not tracking: ambiguous information for ref %s"),
136 install_branch_config(config_flags
, new_ref
, tracking
.remote
,
137 tracking
.src
? tracking
.src
: orig_ref
);
143 int read_branch_desc(struct strbuf
*buf
, const char *branch_name
)
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
);
152 strbuf_addstr(buf
, v
);
154 strbuf_release(&name
);
158 int validate_new_branchname(const char *name
, struct strbuf
*ref
,
159 int force
, int attr_only
)
161 if (strbuf_check_branch_ref(ref
, name
))
162 die(_("'%s' is not a valid branch name."), name
);
164 if (!ref_exists(ref
->buf
))
166 else if (!force
&& !attr_only
)
167 die(_("A branch named '%s' already exists."), ref
->buf
+ strlen("refs/heads/"));
171 unsigned char sha1
[20];
173 head
= resolve_ref_unsafe("HEAD", 0, sha1
, NULL
);
174 if (!is_bare_repository() && head
&& !strcmp(head
, ref
->buf
))
175 die(_("Cannot force update the current branch."));
180 static int check_tracking_branch(struct remote
*remote
, void *cb_data
)
182 char *tracking_branch
= cb_data
;
183 struct refspec query
;
184 memset(&query
, 0, sizeof(struct refspec
));
185 query
.dst
= tracking_branch
;
186 return !remote_find_tracking(remote
, &query
);
189 static int validate_remote_tracking_branch(char *ref
)
191 return !for_each_remote(check_tracking_branch
, ref
);
194 static const char upstream_not_branch
[] =
195 N_("Cannot setup tracking information; starting point '%s' is not a branch.");
196 static const char upstream_missing
[] =
197 N_("the requested upstream branch '%s' does not exist");
198 static const char upstream_advice
[] =
200 "If you are planning on basing your work on an upstream\n"
201 "branch that already exists at the remote, you may need to\n"
202 "run \"git fetch\" to retrieve it.\n"
204 "If you are planning to push out a new local branch that\n"
205 "will track its remote counterpart, you may want to use\n"
206 "\"git push -u\" to set the upstream config as you push.");
208 void create_branch(const char *head
,
209 const char *name
, const char *start_name
,
210 int force
, int reflog
, int clobber_head
,
211 int quiet
, enum branch_track track
)
213 struct commit
*commit
;
214 unsigned char sha1
[20];
215 char *real_ref
, msg
[PATH_MAX
+ 20];
216 struct strbuf ref
= STRBUF_INIT
;
218 int dont_change_ref
= 0;
219 int explicit_tracking
= 0;
221 if (track
== BRANCH_TRACK_EXPLICIT
|| track
== BRANCH_TRACK_OVERRIDE
)
222 explicit_tracking
= 1;
224 if (validate_new_branchname(name
, &ref
, force
,
225 track
== BRANCH_TRACK_OVERRIDE
||
234 if (get_sha1(start_name
, sha1
)) {
235 if (explicit_tracking
) {
236 if (advice_set_upstream_failure
) {
237 error(_(upstream_missing
), start_name
);
238 advise(_(upstream_advice
));
241 die(_(upstream_missing
), start_name
);
243 die(_("Not a valid object name: '%s'."), start_name
);
246 switch (dwim_ref(start_name
, strlen(start_name
), sha1
, &real_ref
)) {
248 /* Not branching from any existing branch */
249 if (explicit_tracking
)
250 die(_(upstream_not_branch
), start_name
);
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
);
263 die(_("Ambiguous object name: '%s'."), start_name
);
267 if ((commit
= lookup_commit_reference(sha1
)) == NULL
)
268 die(_("Not a valid branch point: '%s'."), start_name
);
269 hashcpy(sha1
, commit
->object
.sha1
);
272 snprintf(msg
, sizeof msg
, "branch: Reset to %s",
274 else if (!dont_change_ref
)
275 snprintf(msg
, sizeof msg
, "branch: Created from %s",
279 log_all_ref_updates
= 1;
281 if (!dont_change_ref
) {
282 struct ref_transaction
*transaction
;
283 struct strbuf err
= STRBUF_INIT
;
285 transaction
= ref_transaction_begin(&err
);
287 ref_transaction_update(transaction
, ref
.buf
,
288 sha1
, forcing
? NULL
: null_sha1
,
290 ref_transaction_commit(transaction
, &err
))
292 ref_transaction_free(transaction
);
293 strbuf_release(&err
);
296 if (real_ref
&& track
)
297 setup_tracking(ref
.buf
+ 11, real_ref
, track
, quiet
);
299 strbuf_release(&ref
);
303 void remove_branch_state(void)
305 unlink(git_path_cherry_pick_head());
306 unlink(git_path_revert_head());
307 unlink(git_path_merge_head());
308 unlink(git_path_merge_rr());
309 unlink(git_path_merge_msg());
310 unlink(git_path_merge_mode());
311 unlink(git_path_squash_msg());
314 static char *find_linked_symref(const char *symref
, const char *branch
,
317 struct strbuf sb
= STRBUF_INIT
;
318 struct strbuf path
= STRBUF_INIT
;
319 struct strbuf gitdir
= STRBUF_INIT
;
320 char *existing
= NULL
;
323 * $GIT_COMMON_DIR/$symref (e.g. HEAD) is practically outside
324 * $GIT_DIR so resolve_ref_unsafe() won't work (it uses
325 * git_path). Parse the ref ourselves.
328 strbuf_addf(&path
, "%s/worktrees/%s/%s", get_git_common_dir(), id
, symref
);
330 strbuf_addf(&path
, "%s/%s", get_git_common_dir(), symref
);
332 if (!strbuf_readlink(&sb
, path
.buf
, 0)) {
333 if (!starts_with(sb
.buf
, "refs/") ||
334 check_refname_format(sb
.buf
, 0))
336 } else if (strbuf_read_file(&sb
, path
.buf
, 0) >= 0 &&
337 starts_with(sb
.buf
, "ref:")) {
338 strbuf_remove(&sb
, 0, strlen("ref:"));
342 if (strcmp(sb
.buf
, branch
))
346 strbuf_addf(&path
, "%s/worktrees/%s/gitdir", get_git_common_dir(), id
);
347 if (strbuf_read_file(&gitdir
, path
.buf
, 0) <= 0)
349 strbuf_rtrim(&gitdir
);
351 strbuf_addstr(&gitdir
, get_git_common_dir());
352 strbuf_strip_suffix(&gitdir
, ".git");
354 existing
= strbuf_detach(&gitdir
, NULL
);
356 strbuf_release(&path
);
358 strbuf_release(&gitdir
);
363 char *find_shared_symref(const char *symref
, const char *target
)
365 struct strbuf path
= STRBUF_INIT
;
370 if ((existing
= find_linked_symref(symref
, target
, NULL
)))
373 strbuf_addf(&path
, "%s/worktrees", get_git_common_dir());
374 dir
= opendir(path
.buf
);
375 strbuf_release(&path
);
379 while ((d
= readdir(dir
)) != NULL
) {
380 if (!strcmp(d
->d_name
, ".") || !strcmp(d
->d_name
, ".."))
382 existing
= find_linked_symref(symref
, target
, d
->d_name
);
392 void die_if_checked_out(const char *branch
)
396 existing
= find_shared_symref("HEAD", branch
);
398 skip_prefix(branch
, "refs/heads/", &branch
);
399 die(_("'%s' is already checked out at '%s'"), branch
, existing
);