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 struct strbuf key
= STRBUF_INIT
;
53 int rebasing
= should_setup_rebase(origin
);
55 strbuf_addf(&key
, "branch.%s.remote", local
);
56 git_config_set(key
.buf
, origin
? origin
: ".");
59 strbuf_addf(&key
, "branch.%s.merge", local
);
60 git_config_set(key
.buf
, remote
);
64 strbuf_addf(&key
, "branch.%s.rebase", local
);
65 git_config_set(key
.buf
, "true");
68 if (flag
& BRANCH_CONFIG_VERBOSE
) {
71 strbuf_addstr(&key
, origin
? "remote" : "local");
73 /* Are we tracking a proper "branch"? */
74 if (!prefixcmp(remote
, "refs/heads/")) {
75 strbuf_addf(&key
, " branch %s", remote
+ 11);
77 strbuf_addf(&key
, " from %s", origin
);
80 strbuf_addf(&key
, " ref %s", remote
);
81 printf("Branch %s set up to track %s%s.\n",
83 rebasing
? " by rebasing" : "");
89 * This is called when new_ref is branched off of orig_ref, and tries
90 * to infer the settings for branch.<new_ref>.{remote,merge} from the
93 static int setup_tracking(const char *new_ref
, const char *orig_ref
,
94 enum branch_track track
)
96 struct tracking tracking
;
98 if (strlen(new_ref
) > 1024 - 7 - 7 - 1)
99 return error("Tracking not set up: name too long: %s",
102 memset(&tracking
, 0, sizeof(tracking
));
103 tracking
.spec
.dst
= (char *)orig_ref
;
104 if (for_each_remote(find_tracked_branch
, &tracking
))
107 if (!tracking
.matches
)
109 case BRANCH_TRACK_ALWAYS
:
110 case BRANCH_TRACK_EXPLICIT
:
111 case BRANCH_TRACK_OVERRIDE
:
117 if (tracking
.matches
> 1)
118 return error("Not tracking: ambiguous information for ref %s",
121 install_branch_config(BRANCH_CONFIG_VERBOSE
, new_ref
, tracking
.remote
,
122 tracking
.src
? tracking
.src
: orig_ref
);
128 void create_branch(const char *head
,
129 const char *name
, const char *start_name
,
130 int force
, int reflog
, enum branch_track track
)
132 struct ref_lock
*lock
= NULL
;
133 struct commit
*commit
;
134 unsigned char sha1
[20];
135 char *real_ref
, msg
[PATH_MAX
+ 20];
136 struct strbuf ref
= STRBUF_INIT
;
138 int dont_change_ref
= 0;
139 int explicit_tracking
= 0;
141 if (track
== BRANCH_TRACK_EXPLICIT
|| track
== BRANCH_TRACK_OVERRIDE
)
142 explicit_tracking
= 1;
144 if (strbuf_check_branch_ref(&ref
, name
))
145 die("'%s' is not a valid branch name.", name
);
147 if (resolve_ref(ref
.buf
, sha1
, 1, NULL
)) {
148 if (!force
&& track
== BRANCH_TRACK_OVERRIDE
)
151 die("A branch named '%s' already exists.", name
);
152 else if (!is_bare_repository() && !strcmp(head
, name
))
153 die("Cannot force update the current branch.");
158 if (get_sha1(start_name
, sha1
))
159 die("Not a valid object name: '%s'.", start_name
);
161 switch (dwim_ref(start_name
, strlen(start_name
), sha1
, &real_ref
)) {
163 /* Not branching from any existing branch */
164 if (explicit_tracking
)
165 die("Cannot setup tracking information; starting point is not a branch.");
168 /* Unique completion -- good, only if it is a real ref */
169 if (explicit_tracking
&& !strcmp(real_ref
, "HEAD"))
170 die("Cannot setup tracking information; starting point is not a branch.");
173 die("Ambiguous object name: '%s'.", start_name
);
177 if ((commit
= lookup_commit_reference(sha1
)) == NULL
)
178 die("Not a valid branch point: '%s'.", start_name
);
179 hashcpy(sha1
, commit
->object
.sha1
);
181 if (!dont_change_ref
) {
182 lock
= lock_any_ref_for_update(ref
.buf
, NULL
, 0);
184 die_errno("Failed to lock ref for update");
188 log_all_ref_updates
= 1;
191 snprintf(msg
, sizeof msg
, "branch: Reset from %s",
193 else if (!dont_change_ref
)
194 snprintf(msg
, sizeof msg
, "branch: Created from %s",
197 if (real_ref
&& track
)
198 setup_tracking(name
, real_ref
, track
);
200 if (!dont_change_ref
)
201 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
202 die_errno("Failed to write ref");
204 strbuf_release(&ref
);
208 void remove_branch_state(void)
210 unlink(git_path("MERGE_HEAD"));
211 unlink(git_path("MERGE_RR"));
212 unlink(git_path("MERGE_MSG"));
213 unlink(git_path("MERGE_MODE"));
214 unlink(git_path("SQUASH_MSG"));