14 static int find_tracked_branch(struct remote
*remote
, void *priv
)
16 struct tracking
*tracking
= priv
;
18 if (!remote_find_tracking(remote
, &tracking
->spec
)) {
19 if (++tracking
->matches
== 1) {
20 tracking
->src
= tracking
->spec
.src
;
21 tracking
->remote
= remote
->name
;
23 free(tracking
->spec
.src
);
29 tracking
->spec
.src
= NULL
;
35 static int should_setup_rebase(const char *origin
)
38 case AUTOREBASE_NEVER
:
40 case AUTOREBASE_LOCAL
:
41 return origin
== NULL
;
42 case AUTOREBASE_REMOTE
:
43 return origin
!= NULL
;
44 case AUTOREBASE_ALWAYS
:
50 void install_branch_config(int flag
, const char *local
, const char *origin
, const char *remote
)
52 const char *shortname
= remote
+ 11;
53 int remote_is_branch
= !prefixcmp(remote
, "refs/heads/");
54 struct strbuf key
= STRBUF_INIT
;
55 int rebasing
= should_setup_rebase(origin
);
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");
78 if (flag
& BRANCH_CONFIG_VERBOSE
) {
81 strbuf_addstr(&key
, origin
? "remote" : "local");
83 /* Are we tracking a proper "branch"? */
84 if (remote_is_branch
) {
85 strbuf_addf(&key
, " branch %s", shortname
);
87 strbuf_addf(&key
, " from %s", origin
);
90 strbuf_addf(&key
, " ref %s", remote
);
91 printf("Branch %s set up to track %s%s.\n",
93 rebasing
? " by rebasing" : "");
99 * This is called when new_ref is branched off of orig_ref, and tries
100 * to infer the settings for branch.<new_ref>.{remote,merge} from the
103 static int setup_tracking(const char *new_ref
, const char *orig_ref
,
104 enum branch_track track
)
106 struct tracking tracking
;
108 if (strlen(new_ref
) > 1024 - 7 - 7 - 1)
109 return error("Tracking not set up: name too long: %s",
112 memset(&tracking
, 0, sizeof(tracking
));
113 tracking
.spec
.dst
= (char *)orig_ref
;
114 if (for_each_remote(find_tracked_branch
, &tracking
))
117 if (!tracking
.matches
)
119 case BRANCH_TRACK_ALWAYS
:
120 case BRANCH_TRACK_EXPLICIT
:
121 case BRANCH_TRACK_OVERRIDE
:
127 if (tracking
.matches
> 1)
128 return error("Not tracking: ambiguous information for ref %s",
131 install_branch_config(BRANCH_CONFIG_VERBOSE
, new_ref
, tracking
.remote
,
132 tracking
.src
? tracking
.src
: orig_ref
);
138 struct branch_desc_cb
{
139 const char *config_name
;
143 static int read_branch_desc_cb(const char *var
, const char *value
, void *cb
)
145 struct branch_desc_cb
*desc
= cb
;
146 if (strcmp(desc
->config_name
, var
))
148 free((char *)desc
->value
);
149 return git_config_string(&desc
->value
, var
, value
);
152 int read_branch_desc(struct strbuf
*buf
, const char *branch_name
)
154 struct branch_desc_cb cb
;
155 struct strbuf name
= STRBUF_INIT
;
156 strbuf_addf(&name
, "branch.%s.description", branch_name
);
157 cb
.config_name
= name
.buf
;
159 if (git_config(read_branch_desc_cb
, &cb
) < 0) {
160 strbuf_release(&name
);
164 strbuf_addstr(buf
, cb
.value
);
165 strbuf_release(&name
);
169 int validate_new_branchname(const char *name
, struct strbuf
*ref
,
170 int force
, int attr_only
)
172 if (strbuf_check_branch_ref(ref
, name
))
173 die("'%s' is not a valid branch name.", name
);
175 if (!ref_exists(ref
->buf
))
177 else if (!force
&& !attr_only
)
178 die("A branch named '%s' already exists.", ref
->buf
+ strlen("refs/heads/"));
182 unsigned char sha1
[20];
184 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
185 if (!is_bare_repository() && head
&& !strcmp(head
, ref
->buf
))
186 die("Cannot force update the current branch.");
191 void create_branch(const char *head
,
192 const char *name
, const char *start_name
,
193 int force
, int reflog
, enum branch_track track
)
195 struct ref_lock
*lock
= NULL
;
196 struct commit
*commit
;
197 unsigned char sha1
[20];
198 char *real_ref
, msg
[PATH_MAX
+ 20];
199 struct strbuf ref
= STRBUF_INIT
;
201 int dont_change_ref
= 0;
202 int explicit_tracking
= 0;
204 if (track
== BRANCH_TRACK_EXPLICIT
|| track
== BRANCH_TRACK_OVERRIDE
)
205 explicit_tracking
= 1;
207 if (validate_new_branchname(name
, &ref
, force
,
208 track
== BRANCH_TRACK_OVERRIDE
)) {
216 if (get_sha1(start_name
, sha1
))
217 die("Not a valid object name: '%s'.", start_name
);
219 switch (dwim_ref(start_name
, strlen(start_name
), sha1
, &real_ref
)) {
221 /* Not branching from any existing branch */
222 if (explicit_tracking
)
223 die("Cannot setup tracking information; starting point is not a branch.");
226 /* Unique completion -- good, only if it is a real branch */
227 if (prefixcmp(real_ref
, "refs/heads/") &&
228 prefixcmp(real_ref
, "refs/remotes/")) {
229 if (explicit_tracking
)
230 die("Cannot setup tracking information; starting point is not a branch.");
236 die("Ambiguous object name: '%s'.", start_name
);
240 if ((commit
= lookup_commit_reference(sha1
)) == NULL
)
241 die("Not a valid branch point: '%s'.", start_name
);
242 hashcpy(sha1
, commit
->object
.sha1
);
244 if (!dont_change_ref
) {
245 lock
= lock_any_ref_for_update(ref
.buf
, NULL
, 0);
247 die_errno("Failed to lock ref for update");
251 log_all_ref_updates
= 1;
254 snprintf(msg
, sizeof msg
, "branch: Reset to %s",
256 else if (!dont_change_ref
)
257 snprintf(msg
, sizeof msg
, "branch: Created from %s",
260 if (real_ref
&& track
)
261 setup_tracking(ref
.buf
+11, real_ref
, track
);
263 if (!dont_change_ref
)
264 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
265 die_errno("Failed to write ref");
267 strbuf_release(&ref
);
271 void remove_branch_state(void)
273 unlink(git_path("CHERRY_PICK_HEAD"));
274 unlink(git_path("MERGE_HEAD"));
275 unlink(git_path("MERGE_RR"));
276 unlink(git_path("MERGE_MSG"));
277 unlink(git_path("MERGE_MODE"));
278 unlink(git_path("SQUASH_MSG"));