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 struct tracking
*tracking
)
38 case AUTOREBASE_NEVER
:
40 case AUTOREBASE_LOCAL
:
41 return tracking
->remote
== NULL
;
42 case AUTOREBASE_REMOTE
:
43 return tracking
->remote
!= NULL
;
44 case AUTOREBASE_ALWAYS
:
51 * This is called when new_ref is branched off of orig_ref, and tries
52 * to infer the settings for branch.<new_ref>.{remote,merge} from the
55 static int setup_tracking(const char *new_ref
, const char *orig_ref
,
56 enum branch_track track
)
59 struct tracking tracking
;
61 if (strlen(new_ref
) > 1024 - 7 - 7 - 1)
62 return error("Tracking not set up: name too long: %s",
65 memset(&tracking
, 0, sizeof(tracking
));
66 tracking
.spec
.dst
= (char *)orig_ref
;
67 if (for_each_remote(find_tracked_branch
, &tracking
))
70 if (!tracking
.matches
)
72 case BRANCH_TRACK_ALWAYS
:
73 case BRANCH_TRACK_EXPLICIT
:
79 if (tracking
.matches
> 1)
80 return error("Not tracking: ambiguous information for ref %s",
83 sprintf(key
, "branch.%s.remote", new_ref
);
84 git_config_set(key
, tracking
.remote
? tracking
.remote
: ".");
85 sprintf(key
, "branch.%s.merge", new_ref
);
86 git_config_set(key
, tracking
.src
? tracking
.src
: orig_ref
);
87 printf("Branch %s set up to track %s branch %s.\n", new_ref
,
88 tracking
.remote
? "remote" : "local", orig_ref
);
89 if (should_setup_rebase(&tracking
)) {
90 sprintf(key
, "branch.%s.rebase", new_ref
);
91 git_config_set(key
, "true");
92 printf("This branch will rebase on pull.\n");
99 void create_branch(const char *head
,
100 const char *name
, const char *start_name
,
101 int force
, int reflog
, enum branch_track track
)
103 struct ref_lock
*lock
;
104 struct commit
*commit
;
105 unsigned char sha1
[20];
106 char *real_ref
, ref
[PATH_MAX
], msg
[PATH_MAX
+ 20];
109 snprintf(ref
, sizeof ref
, "refs/heads/%s", name
);
110 if (check_ref_format(ref
))
111 die("'%s' is not a valid branch name.", name
);
113 if (resolve_ref(ref
, sha1
, 1, NULL
)) {
115 die("A branch named '%s' already exists.", name
);
116 else if (!is_bare_repository() && !strcmp(head
, name
))
117 die("Cannot force update the current branch.");
122 if (get_sha1(start_name
, sha1
))
123 die("Not a valid object name: '%s'.", start_name
);
125 switch (dwim_ref(start_name
, strlen(start_name
), sha1
, &real_ref
)) {
127 /* Not branching from any existing branch */
128 if (track
== BRANCH_TRACK_EXPLICIT
)
129 die("Cannot setup tracking information; starting point is not a branch.");
132 /* Unique completion -- good, only if it is a real ref */
133 if (track
== BRANCH_TRACK_EXPLICIT
&& !strcmp(real_ref
, "HEAD"))
134 die("Cannot setup tracking information; starting point is not a branch.");
137 die("Ambiguous object name: '%s'.", start_name
);
141 if ((commit
= lookup_commit_reference(sha1
)) == NULL
)
142 die("Not a valid branch point: '%s'.", start_name
);
143 hashcpy(sha1
, commit
->object
.sha1
);
145 lock
= lock_any_ref_for_update(ref
, NULL
, 0);
147 die("Failed to lock ref for update: %s.", strerror(errno
));
150 log_all_ref_updates
= 1;
153 snprintf(msg
, sizeof msg
, "branch: Reset from %s",
156 snprintf(msg
, sizeof msg
, "branch: Created from %s",
159 if (real_ref
&& track
)
160 setup_tracking(name
, real_ref
, track
);
162 if (write_ref_sha1(lock
, sha1
, msg
) < 0)
163 die("Failed to write ref: %s.", strerror(errno
));
168 void remove_branch_state(void)
170 unlink(git_path("MERGE_HEAD"));
171 unlink(git_path("MERGE_RR"));
172 unlink(git_path("MERGE_MSG"));
173 unlink(git_path("SQUASH_MSG"));