]>
Commit | Line | Data |
---|---|---|
e496c003 DB |
1 | #include "cache.h" |
2 | #include "branch.h" | |
3 | #include "refs.h" | |
4 | #include "remote.h" | |
5 | #include "commit.h" | |
6 | ||
7 | struct tracking { | |
8 | struct refspec spec; | |
9 | char *src; | |
10 | const char *remote; | |
11 | int matches; | |
12 | }; | |
13 | ||
14 | static int find_tracked_branch(struct remote *remote, void *priv) | |
15 | { | |
16 | struct tracking *tracking = priv; | |
17 | ||
18 | if (!remote_find_tracking(remote, &tracking->spec)) { | |
19 | if (++tracking->matches == 1) { | |
20 | tracking->src = tracking->spec.src; | |
21 | tracking->remote = remote->name; | |
22 | } else { | |
23 | free(tracking->spec.src); | |
24 | if (tracking->src) { | |
25 | free(tracking->src); | |
26 | tracking->src = NULL; | |
27 | } | |
28 | } | |
29 | tracking->spec.src = NULL; | |
30 | } | |
31 | ||
32 | return 0; | |
33 | } | |
34 | ||
a9f2c136 | 35 | static int should_setup_rebase(const char *origin) |
c998ae9b DS |
36 | { |
37 | switch (autorebase) { | |
38 | case AUTOREBASE_NEVER: | |
39 | return 0; | |
40 | case AUTOREBASE_LOCAL: | |
a9f2c136 | 41 | return origin == NULL; |
c998ae9b | 42 | case AUTOREBASE_REMOTE: |
a9f2c136 | 43 | return origin != NULL; |
c998ae9b DS |
44 | case AUTOREBASE_ALWAYS: |
45 | return 1; | |
46 | } | |
47 | return 0; | |
48 | } | |
49 | ||
a9f2c136 JH |
50 | void install_branch_config(int flag, const char *local, const char *origin, const char *remote) |
51 | { | |
52 | struct strbuf key = STRBUF_INIT; | |
53 | int rebasing = should_setup_rebase(origin); | |
54 | ||
55 | strbuf_addf(&key, "branch.%s.remote", local); | |
56 | git_config_set(key.buf, origin ? origin : "."); | |
57 | ||
58 | strbuf_reset(&key); | |
59 | strbuf_addf(&key, "branch.%s.merge", local); | |
60 | git_config_set(key.buf, remote); | |
61 | ||
62 | if (rebasing) { | |
63 | strbuf_reset(&key); | |
64 | strbuf_addf(&key, "branch.%s.rebase", local); | |
65 | git_config_set(key.buf, "true"); | |
66 | } | |
67 | ||
72f60083 JH |
68 | if (flag & BRANCH_CONFIG_VERBOSE) { |
69 | strbuf_reset(&key); | |
70 | ||
71 | strbuf_addstr(&key, origin ? "remote" : "local"); | |
72 | ||
73 | /* Are we tracking a proper "branch"? */ | |
74 | if (!prefixcmp(remote, "refs/heads/")) { | |
75 | strbuf_addf(&key, " branch %s", remote + 11); | |
76 | if (origin) | |
77 | strbuf_addf(&key, " from %s", origin); | |
78 | } | |
79 | else | |
80 | strbuf_addf(&key, " ref %s", remote); | |
81 | printf("Branch %s set up to track %s%s.\n", | |
82 | local, key.buf, | |
83 | rebasing ? " by rebasing" : ""); | |
84 | } | |
a9f2c136 JH |
85 | strbuf_release(&key); |
86 | } | |
87 | ||
e496c003 DB |
88 | /* |
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 | |
91 | * config. | |
92 | */ | |
9ed36cfa JS |
93 | static int setup_tracking(const char *new_ref, const char *orig_ref, |
94 | enum branch_track track) | |
e496c003 | 95 | { |
e496c003 DB |
96 | struct tracking tracking; |
97 | ||
98 | if (strlen(new_ref) > 1024 - 7 - 7 - 1) | |
99 | return error("Tracking not set up: name too long: %s", | |
100 | new_ref); | |
101 | ||
102 | memset(&tracking, 0, sizeof(tracking)); | |
103 | tracking.spec.dst = (char *)orig_ref; | |
9ed36cfa | 104 | if (for_each_remote(find_tracked_branch, &tracking)) |
e496c003 DB |
105 | return 1; |
106 | ||
9ed36cfa JS |
107 | if (!tracking.matches) |
108 | switch (track) { | |
109 | case BRANCH_TRACK_ALWAYS: | |
110 | case BRANCH_TRACK_EXPLICIT: | |
111 | break; | |
112 | default: | |
113 | return 1; | |
114 | } | |
115 | ||
e496c003 DB |
116 | if (tracking.matches > 1) |
117 | return error("Not tracking: ambiguous information for ref %s", | |
118 | orig_ref); | |
119 | ||
a9f2c136 JH |
120 | install_branch_config(BRANCH_CONFIG_VERBOSE, new_ref, tracking.remote, |
121 | tracking.src ? tracking.src : orig_ref); | |
e496c003 | 122 | |
a9f2c136 | 123 | free(tracking.src); |
e496c003 DB |
124 | return 0; |
125 | } | |
126 | ||
127 | void create_branch(const char *head, | |
128 | const char *name, const char *start_name, | |
9ed36cfa | 129 | int force, int reflog, enum branch_track track) |
e496c003 DB |
130 | { |
131 | struct ref_lock *lock; | |
132 | struct commit *commit; | |
133 | unsigned char sha1[20]; | |
8415d5c7 JH |
134 | char *real_ref, msg[PATH_MAX + 20]; |
135 | struct strbuf ref = STRBUF_INIT; | |
e496c003 DB |
136 | int forcing = 0; |
137 | ||
a2fab531 | 138 | if (strbuf_check_branch_ref(&ref, name)) |
e496c003 DB |
139 | die("'%s' is not a valid branch name.", name); |
140 | ||
8415d5c7 | 141 | if (resolve_ref(ref.buf, sha1, 1, NULL)) { |
e496c003 DB |
142 | if (!force) |
143 | die("A branch named '%s' already exists.", name); | |
144 | else if (!is_bare_repository() && !strcmp(head, name)) | |
145 | die("Cannot force update the current branch."); | |
146 | forcing = 1; | |
147 | } | |
148 | ||
149 | real_ref = NULL; | |
150 | if (get_sha1(start_name, sha1)) | |
151 | die("Not a valid object name: '%s'.", start_name); | |
152 | ||
153 | switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) { | |
154 | case 0: | |
155 | /* Not branching from any existing branch */ | |
9ed36cfa JS |
156 | if (track == BRANCH_TRACK_EXPLICIT) |
157 | die("Cannot setup tracking information; starting point is not a branch."); | |
e496c003 DB |
158 | break; |
159 | case 1: | |
51a94af8 JH |
160 | /* Unique completion -- good, only if it is a real ref */ |
161 | if (track == BRANCH_TRACK_EXPLICIT && !strcmp(real_ref, "HEAD")) | |
162 | die("Cannot setup tracking information; starting point is not a branch."); | |
e496c003 DB |
163 | break; |
164 | default: | |
165 | die("Ambiguous object name: '%s'.", start_name); | |
166 | break; | |
167 | } | |
168 | ||
169 | if ((commit = lookup_commit_reference(sha1)) == NULL) | |
170 | die("Not a valid branch point: '%s'.", start_name); | |
171 | hashcpy(sha1, commit->object.sha1); | |
172 | ||
8415d5c7 | 173 | lock = lock_any_ref_for_update(ref.buf, NULL, 0); |
e496c003 | 174 | if (!lock) |
d824cbba | 175 | die_errno("Failed to lock ref for update"); |
e496c003 DB |
176 | |
177 | if (reflog) | |
178 | log_all_ref_updates = 1; | |
179 | ||
180 | if (forcing) | |
181 | snprintf(msg, sizeof msg, "branch: Reset from %s", | |
182 | start_name); | |
183 | else | |
184 | snprintf(msg, sizeof msg, "branch: Created from %s", | |
185 | start_name); | |
186 | ||
e496c003 | 187 | if (real_ref && track) |
9ed36cfa | 188 | setup_tracking(name, real_ref, track); |
e496c003 DB |
189 | |
190 | if (write_ref_sha1(lock, sha1, msg) < 0) | |
d824cbba | 191 | die_errno("Failed to write ref"); |
e496c003 | 192 | |
8415d5c7 | 193 | strbuf_release(&ref); |
9ed36cfa | 194 | free(real_ref); |
e496c003 | 195 | } |
c369e7b8 DB |
196 | |
197 | void remove_branch_state(void) | |
198 | { | |
199 | unlink(git_path("MERGE_HEAD")); | |
0af0ac7e | 200 | unlink(git_path("MERGE_RR")); |
c369e7b8 | 201 | unlink(git_path("MERGE_MSG")); |
cf10f9fd | 202 | unlink(git_path("MERGE_MODE")); |
c369e7b8 DB |
203 | unlink(git_path("SQUASH_MSG")); |
204 | } |