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
= remote
+ 11;
54 int remote_is_branch
= !prefixcmp(remote
, "refs/heads/");
55 struct strbuf key
= STRBUF_INIT
;
56 int rebasing
= should_setup_rebase(origin
);
59 && !strcmp(local
, shortname
)
61 warning("Not setting branch %s as its own upstream.",
66 strbuf_addf(&key
, "branch.%s.remote", local
);
67 git_config_set(key
.buf
, origin
? origin
: ".");
70 strbuf_addf(&key
, "branch.%s.merge", local
);
71 git_config_set(key
.buf
, remote
);
75 strbuf_addf(&key
, "branch.%s.rebase", local
);
76 git_config_set(key
.buf
, "true");
79 if (flag
& BRANCH_CONFIG_VERBOSE
) {
82 strbuf_addstr(&key
, origin
? "remote" : "local");
84 /* Are we tracking a proper "branch"? */
85 if (remote_is_branch
) {
86 strbuf_addf(&key
, " branch %s", shortname
);
88 strbuf_addf(&key
, " from %s", origin
);
91 strbuf_addf(&key
, " ref %s", remote
);
92 printf("Branch %s set up to track %s%s.\n",
94 rebasing
? " by rebasing" : "");
100 * This is called when new_ref is branched off of orig_ref, and tries
101 * to infer the settings for branch.<new_ref>.{remote,merge} from the
104 static int setup_tracking(const char *new_ref
, const char *orig_ref
,
105 enum branch_track track
)
107 struct tracking tracking
;
109 if (strlen(new_ref
) > 1024 - 7 - 7 - 1)
110 return error("Tracking not set up: name too long: %s",
113 memset(&tracking
, 0, sizeof(tracking
));
114 tracking
.spec
.dst
= (char *)orig_ref
;
115 if (for_each_remote(find_tracked_branch
, &tracking
))
118 if (!tracking
.matches
)
120 case BRANCH_TRACK_ALWAYS
:
121 case BRANCH_TRACK_EXPLICIT
:
122 case BRANCH_TRACK_OVERRIDE
:
128 if (tracking
.matches
> 1)
129 return error("Not tracking: ambiguous information for ref %s",
132 install_branch_config(BRANCH_CONFIG_VERBOSE
, new_ref
, tracking
.remote
,
133 tracking
.src
? tracking
.src
: orig_ref
);
139 int validate_new_branchname(const char *name
, struct strbuf
*ref
,
140 int force
, int attr_only
)
142 if (strbuf_check_branch_ref(ref
, name
))
143 die("'%s' is not a valid branch name.", name
);
145 if (!ref_exists(ref
->buf
))
147 else if (!force
&& !attr_only
)
148 die("A branch named '%s' already exists.", ref
->buf
+ strlen("refs/heads/"));
152 unsigned char sha1
[20];
154 head
= resolve_ref("HEAD", sha1
, 0, NULL
);
155 if (!is_bare_repository() && head
&& !strcmp(head
, ref
->buf
))
156 die("Cannot force update the current branch.");
161 void create_branch(const char *head
,
162 const char *name
, const char *start_name
,
163 int force
, int reflog
, int clobber_head
,
164 enum branch_track track
)
166 struct ref_lock
*lock
= NULL
;
167 struct commit
*commit
;
168 unsigned char sha1
[20];
169 char *real_ref
, msg
[PATH_MAX
+ 20];
170 struct strbuf ref
= STRBUF_INIT
;
172 int dont_change_ref
= 0;
173 int explicit_tracking
= 0;
175 if (track
== BRANCH_TRACK_EXPLICIT
|| track
== BRANCH_TRACK_OVERRIDE
)
176 explicit_tracking
= 1;
178 if (validate_new_branchname(name
, &ref
, force
,
179 track
== BRANCH_TRACK_OVERRIDE
||
188 if (get_sha1(start_name
, sha1
))
189 die("Not a valid object name: '%s'.", start_name
);
191 switch (dwim_ref(start_name
, strlen(start_name
), sha1
, &real_ref
)) {
193 /* Not branching from any existing branch */
194 if (explicit_tracking
)
195 die("Cannot setup tracking information; starting point is not a branch.");
198 /* Unique completion -- good, only if it is a real branch */
199 if (prefixcmp(real_ref
, "refs/heads/") &&
200 prefixcmp(real_ref
, "refs/remotes/")) {
201 if (explicit_tracking
)
202 die("Cannot setup tracking information; starting point is not a branch.");
208 die("Ambiguous object name: '%s'.", start_name
);
212 if ((commit
= lookup_commit_reference(sha1
)) == NULL
)
213 die("Not a valid branch point: '%s'.", start_name
);
214 hashcpy(sha1
, commit
->object
.sha1
);
216 if (!dont_change_ref
) {
217 lock
= lock_any_ref_for_update(ref
.buf
, NULL
, 0);
219 die_errno("Failed to lock ref for update");
223 log_all_ref_updates
= 1;
226 snprintf(msg
, sizeof msg
, "branch: Reset to %s",
228 else if (!dont_change_ref
)
229 snprintf(msg
, sizeof msg
, "branch: Created from %s",
232 if (real_ref
&& track
)
233 setup_tracking(ref
.buf
+11, real_ref
, track
);
235 if (!dont_change_ref
)
236 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
237 die_errno("Failed to write ref");
239 strbuf_release(&ref
);
243 void remove_branch_state(void)
245 unlink(git_path("CHERRY_PICK_HEAD"));
246 unlink(git_path("REVERT_HEAD"));
247 unlink(git_path("MERGE_HEAD"));
248 unlink(git_path("MERGE_RR"));
249 unlink(git_path("MERGE_MSG"));
250 unlink(git_path("MERGE_MODE"));
251 unlink(git_path("SQUASH_MSG"));
252 remove_sequencer_state(0);