]>
Commit | Line | Data |
---|---|---|
c31820c2 LH |
1 | /* |
2 | * Builtin "git branch" | |
3 | * | |
d972cce0 | 4 | * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com> |
c31820c2 LH |
5 | * Based on git-branch.sh by Junio C Hamano. |
6 | */ | |
7 | ||
8 | #include "cache.h" | |
85023577 | 9 | #include "color.h" |
c31820c2 LH |
10 | #include "refs.h" |
11 | #include "commit.h" | |
12 | #include "builtin.h" | |
6f084a56 | 13 | #include "remote.h" |
a8dfd5ea PH |
14 | #include "parse-options.h" |
15 | ||
16 | static const char * const builtin_branch_usage[] = { | |
17 | "git-branch [options] [-r | -a]", | |
18 | "git-branch [options] [-l] [-f] <branchname> [<start-point>]", | |
19 | "git-branch [options] [-r] (-d | -D) <branchname>", | |
20 | "git-branch [options] (-m | -M) [<oldbranch>] <newbranch>", | |
21 | NULL | |
22 | }; | |
c31820c2 | 23 | |
f3d985c3 JH |
24 | #define REF_UNKNOWN_TYPE 0x00 |
25 | #define REF_LOCAL_BRANCH 0x01 | |
26 | #define REF_REMOTE_BRANCH 0x02 | |
27 | #define REF_TAG 0x04 | |
c31820c2 LH |
28 | |
29 | static const char *head; | |
30 | static unsigned char head_sha1[20]; | |
31 | ||
6f084a56 | 32 | static int branch_track = 1; |
0746d19a | 33 | |
6b2f2d98 | 34 | static int branch_use_color = -1; |
a1158cae AP |
35 | static char branch_colors[][COLOR_MAXLEN] = { |
36 | "\033[m", /* reset */ | |
37 | "", /* PLAIN (normal) */ | |
38 | "\033[31m", /* REMOTE (red) */ | |
753f96a4 JH |
39 | "", /* LOCAL (normal) */ |
40 | "\033[32m", /* CURRENT (green) */ | |
a1158cae AP |
41 | }; |
42 | enum color_branch { | |
43 | COLOR_BRANCH_RESET = 0, | |
44 | COLOR_BRANCH_PLAIN = 1, | |
45 | COLOR_BRANCH_REMOTE = 2, | |
46 | COLOR_BRANCH_LOCAL = 3, | |
47 | COLOR_BRANCH_CURRENT = 4, | |
48 | }; | |
49 | ||
50 | static int parse_branch_color_slot(const char *var, int ofs) | |
51 | { | |
52 | if (!strcasecmp(var+ofs, "plain")) | |
53 | return COLOR_BRANCH_PLAIN; | |
54 | if (!strcasecmp(var+ofs, "reset")) | |
55 | return COLOR_BRANCH_RESET; | |
56 | if (!strcasecmp(var+ofs, "remote")) | |
57 | return COLOR_BRANCH_REMOTE; | |
58 | if (!strcasecmp(var+ofs, "local")) | |
59 | return COLOR_BRANCH_LOCAL; | |
60 | if (!strcasecmp(var+ofs, "current")) | |
61 | return COLOR_BRANCH_CURRENT; | |
62 | die("bad config variable '%s'", var); | |
63 | } | |
64 | ||
52fae7de | 65 | static int git_branch_config(const char *var, const char *value) |
a1158cae AP |
66 | { |
67 | if (!strcmp(var, "color.branch")) { | |
0f6f5a40 | 68 | branch_use_color = git_config_colorbool(var, value, -1); |
a1158cae AP |
69 | return 0; |
70 | } | |
cc44c765 | 71 | if (!prefixcmp(var, "color.branch.")) { |
a1158cae | 72 | int slot = parse_branch_color_slot(var, 13); |
5768c98a JH |
73 | if (!value) |
74 | return config_error_nonbool(var); | |
a1158cae AP |
75 | color_parse(value, var, branch_colors[slot]); |
76 | return 0; | |
77 | } | |
5768c98a JH |
78 | if (!strcmp(var, "branch.autosetupmerge")) { |
79 | branch_track = git_config_bool(var, value); | |
80 | return 0; | |
81 | } | |
6b2f2d98 | 82 | return git_color_default_config(var, value); |
a1158cae AP |
83 | } |
84 | ||
52fae7de | 85 | static const char *branch_get_color(enum color_branch ix) |
a1158cae | 86 | { |
6b2f2d98 | 87 | if (branch_use_color > 0) |
a1158cae AP |
88 | return branch_colors[ix]; |
89 | return ""; | |
90 | } | |
91 | ||
b8e9a00d | 92 | static int delete_branches(int argc, const char **argv, int force, int kinds) |
c31820c2 | 93 | { |
67affd51 | 94 | struct commit *rev, *head_rev = head_rev; |
c31820c2 | 95 | unsigned char sha1[20]; |
b8e9a00d | 96 | char *name = NULL; |
f3d985c3 | 97 | const char *fmt, *remote; |
f1eccbab | 98 | char section[PATH_MAX]; |
c31820c2 | 99 | int i; |
b8e9a00d | 100 | int ret = 0; |
c31820c2 | 101 | |
f3d985c3 JH |
102 | switch (kinds) { |
103 | case REF_REMOTE_BRANCH: | |
104 | fmt = "refs/remotes/%s"; | |
105 | remote = "remote "; | |
106 | force = 1; | |
107 | break; | |
108 | case REF_LOCAL_BRANCH: | |
109 | fmt = "refs/heads/%s"; | |
110 | remote = ""; | |
111 | break; | |
112 | default: | |
113 | die("cannot use -a with -d"); | |
114 | } | |
c31820c2 | 115 | |
67affd51 JH |
116 | if (!force) { |
117 | head_rev = lookup_commit_reference(head_sha1); | |
118 | if (!head_rev) | |
119 | die("Couldn't look up commit object for HEAD"); | |
120 | } | |
c31820c2 | 121 | for (i = 0; i < argc; i++) { |
b8e9a00d QT |
122 | if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) { |
123 | error("Cannot delete the branch '%s' " | |
124 | "which you are currently on.", argv[i]); | |
125 | ret = 1; | |
126 | continue; | |
127 | } | |
128 | ||
129 | if (name) | |
130 | free(name); | |
c31820c2 | 131 | |
f3d985c3 | 132 | name = xstrdup(mkpath(fmt, argv[i])); |
b8e9a00d QT |
133 | if (!resolve_ref(name, sha1, 1, NULL)) { |
134 | error("%sbranch '%s' not found.", | |
135 | remote, argv[i]); | |
136 | ret = 1; | |
137 | continue; | |
138 | } | |
c31820c2 LH |
139 | |
140 | rev = lookup_commit_reference(sha1); | |
b8e9a00d QT |
141 | if (!rev) { |
142 | error("Couldn't look up commit object for '%s'", name); | |
143 | ret = 1; | |
144 | continue; | |
145 | } | |
c31820c2 LH |
146 | |
147 | /* This checks whether the merge bases of branch and | |
148 | * HEAD contains branch -- which means that the HEAD | |
149 | * contains everything in both. | |
150 | */ | |
151 | ||
152 | if (!force && | |
03840fc3 | 153 | !in_merge_bases(rev, &head_rev, 1)) { |
00ae8289 | 154 | error("The branch '%s' is not an ancestor of " |
b8e9a00d QT |
155 | "your current HEAD.\n" |
156 | "If you are sure you want to delete it, " | |
157 | "run 'git branch -D %s'.", argv[i], argv[i]); | |
158 | ret = 1; | |
159 | continue; | |
c31820c2 LH |
160 | } |
161 | ||
b8e9a00d QT |
162 | if (delete_ref(name, sha1)) { |
163 | error("Error deleting %sbranch '%s'", remote, | |
f3d985c3 | 164 | argv[i]); |
b8e9a00d | 165 | ret = 1; |
f1eccbab | 166 | } else { |
f3d985c3 | 167 | printf("Deleted %sbranch %s.\n", remote, argv[i]); |
f1eccbab GP |
168 | snprintf(section, sizeof(section), "branch.%s", |
169 | argv[i]); | |
170 | if (git_config_rename_section(section, NULL) < 0) | |
171 | warning("Update of config-file failed"); | |
172 | } | |
c31820c2 | 173 | } |
c31820c2 | 174 | |
b8e9a00d QT |
175 | if (name) |
176 | free(name); | |
177 | ||
178 | return(ret); | |
c31820c2 | 179 | } |
c31820c2 | 180 | |
bfcc9214 AP |
181 | struct ref_item { |
182 | char *name; | |
183 | unsigned int kind; | |
75e6e213 | 184 | unsigned char sha1[20]; |
bfcc9214 AP |
185 | }; |
186 | ||
187 | struct ref_list { | |
75e6e213 | 188 | int index, alloc, maxwidth; |
bfcc9214 | 189 | struct ref_item *list; |
694a5775 | 190 | struct commit_list *with_commit; |
bfcc9214 AP |
191 | int kinds; |
192 | }; | |
193 | ||
694a5775 JH |
194 | static int has_commit(const unsigned char *sha1, struct commit_list *with_commit) |
195 | { | |
196 | struct commit *commit; | |
197 | ||
198 | if (!with_commit) | |
199 | return 1; | |
200 | commit = lookup_commit_reference_gently(sha1, 1); | |
201 | if (!commit) | |
202 | return 0; | |
203 | while (with_commit) { | |
204 | struct commit *other; | |
205 | ||
206 | other = with_commit->item; | |
207 | with_commit = with_commit->next; | |
208 | if (in_merge_bases(other, &commit, 1)) | |
209 | return 1; | |
210 | } | |
211 | return 0; | |
212 | } | |
213 | ||
bfcc9214 | 214 | static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data) |
c31820c2 | 215 | { |
bfcc9214 AP |
216 | struct ref_list *ref_list = (struct ref_list*)(cb_data); |
217 | struct ref_item *newitem; | |
218 | int kind = REF_UNKNOWN_TYPE; | |
75e6e213 | 219 | int len; |
bfcc9214 AP |
220 | |
221 | /* Detect kind */ | |
cc44c765 | 222 | if (!prefixcmp(refname, "refs/heads/")) { |
bfcc9214 AP |
223 | kind = REF_LOCAL_BRANCH; |
224 | refname += 11; | |
cc44c765 | 225 | } else if (!prefixcmp(refname, "refs/remotes/")) { |
bfcc9214 AP |
226 | kind = REF_REMOTE_BRANCH; |
227 | refname += 13; | |
cc44c765 | 228 | } else if (!prefixcmp(refname, "refs/tags/")) { |
bfcc9214 AP |
229 | kind = REF_TAG; |
230 | refname += 10; | |
231 | } | |
232 | ||
694a5775 JH |
233 | /* Filter with with_commit if specified */ |
234 | if (!has_commit(sha1, ref_list->with_commit)) | |
235 | return 0; | |
236 | ||
bfcc9214 AP |
237 | /* Don't add types the caller doesn't want */ |
238 | if ((kind & ref_list->kinds) == 0) | |
239 | return 0; | |
240 | ||
241 | /* Resize buffer */ | |
242 | if (ref_list->index >= ref_list->alloc) { | |
243 | ref_list->alloc = alloc_nr(ref_list->alloc); | |
244 | ref_list->list = xrealloc(ref_list->list, | |
245 | ref_list->alloc * sizeof(struct ref_item)); | |
c31820c2 LH |
246 | } |
247 | ||
bfcc9214 AP |
248 | /* Record the new item */ |
249 | newitem = &(ref_list->list[ref_list->index++]); | |
250 | newitem->name = xstrdup(refname); | |
251 | newitem->kind = kind; | |
75e6e213 LH |
252 | hashcpy(newitem->sha1, sha1); |
253 | len = strlen(newitem->name); | |
254 | if (len > ref_list->maxwidth) | |
255 | ref_list->maxwidth = len; | |
c31820c2 LH |
256 | |
257 | return 0; | |
258 | } | |
259 | ||
bfcc9214 AP |
260 | static void free_ref_list(struct ref_list *ref_list) |
261 | { | |
262 | int i; | |
263 | ||
264 | for (i = 0; i < ref_list->index; i++) | |
265 | free(ref_list->list[i].name); | |
266 | free(ref_list->list); | |
267 | } | |
268 | ||
c31820c2 LH |
269 | static int ref_cmp(const void *r1, const void *r2) |
270 | { | |
bfcc9214 AP |
271 | struct ref_item *c1 = (struct ref_item *)(r1); |
272 | struct ref_item *c2 = (struct ref_item *)(r2); | |
273 | ||
274 | if (c1->kind != c2->kind) | |
275 | return c1->kind - c2->kind; | |
276 | return strcmp(c1->name, c2->name); | |
c31820c2 LH |
277 | } |
278 | ||
af0e4ac0 LH |
279 | static void print_ref_item(struct ref_item *item, int maxwidth, int verbose, |
280 | int abbrev, int current) | |
75e6e213 | 281 | { |
af0e4ac0 LH |
282 | char c; |
283 | int color; | |
75e6e213 | 284 | struct commit *commit; |
75e6e213 | 285 | |
af0e4ac0 LH |
286 | switch (item->kind) { |
287 | case REF_LOCAL_BRANCH: | |
288 | color = COLOR_BRANCH_LOCAL; | |
289 | break; | |
290 | case REF_REMOTE_BRANCH: | |
291 | color = COLOR_BRANCH_REMOTE; | |
292 | break; | |
293 | default: | |
294 | color = COLOR_BRANCH_PLAIN; | |
295 | break; | |
296 | } | |
75e6e213 | 297 | |
af0e4ac0 LH |
298 | c = ' '; |
299 | if (current) { | |
300 | c = '*'; | |
301 | color = COLOR_BRANCH_CURRENT; | |
302 | } | |
75e6e213 | 303 | |
af0e4ac0 | 304 | if (verbose) { |
674d1727 | 305 | struct strbuf subject; |
80583c0e JH |
306 | const char *sub = " **** invalid ref ****"; |
307 | ||
674d1727 PH |
308 | strbuf_init(&subject, 0); |
309 | ||
af0e4ac0 | 310 | commit = lookup_commit(item->sha1); |
80583c0e | 311 | if (commit && !parse_commit(commit)) { |
674d1727 | 312 | pretty_print_commit(CMIT_FMT_ONELINE, commit, |
4593fb84 | 313 | &subject, 0, NULL, NULL, 0, 0); |
674d1727 | 314 | sub = subject.buf; |
80583c0e | 315 | } |
af0e4ac0 LH |
316 | printf("%c %s%-*s%s %s %s\n", c, branch_get_color(color), |
317 | maxwidth, item->name, | |
318 | branch_get_color(COLOR_BRANCH_RESET), | |
80583c0e | 319 | find_unique_abbrev(item->sha1, abbrev), sub); |
674d1727 | 320 | strbuf_release(&subject); |
af0e4ac0 LH |
321 | } else { |
322 | printf("%c %s%s%s\n", c, branch_get_color(color), item->name, | |
323 | branch_get_color(COLOR_BRANCH_RESET)); | |
324 | } | |
75e6e213 LH |
325 | } |
326 | ||
694a5775 | 327 | static void print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit) |
c31820c2 LH |
328 | { |
329 | int i; | |
bfcc9214 | 330 | struct ref_list ref_list; |
c31820c2 | 331 | |
bfcc9214 AP |
332 | memset(&ref_list, 0, sizeof(ref_list)); |
333 | ref_list.kinds = kinds; | |
694a5775 | 334 | ref_list.with_commit = with_commit; |
bfcc9214 | 335 | for_each_ref(append_ref, &ref_list); |
c31820c2 | 336 | |
bfcc9214 | 337 | qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp); |
c31820c2 | 338 | |
0016a482 | 339 | detached = (detached && (kinds & REF_LOCAL_BRANCH)); |
694a5775 | 340 | if (detached && has_commit(head_sha1, with_commit)) { |
0016a482 | 341 | struct ref_item item; |
3a55602e | 342 | item.name = xstrdup("(no branch)"); |
0016a482 LH |
343 | item.kind = REF_LOCAL_BRANCH; |
344 | hashcpy(item.sha1, head_sha1); | |
345 | if (strlen(item.name) > ref_list.maxwidth) | |
346 | ref_list.maxwidth = strlen(item.name); | |
347 | print_ref_item(&item, ref_list.maxwidth, verbose, abbrev, 1); | |
3a55602e | 348 | free(item.name); |
0016a482 LH |
349 | } |
350 | ||
bfcc9214 | 351 | for (i = 0; i < ref_list.index; i++) { |
0016a482 LH |
352 | int current = !detached && |
353 | (ref_list.list[i].kind == REF_LOCAL_BRANCH) && | |
af0e4ac0 LH |
354 | !strcmp(ref_list.list[i].name, head); |
355 | print_ref_item(&ref_list.list[i], ref_list.maxwidth, verbose, | |
356 | abbrev, current); | |
c31820c2 | 357 | } |
bfcc9214 AP |
358 | |
359 | free_ref_list(&ref_list); | |
c31820c2 LH |
360 | } |
361 | ||
6f084a56 JS |
362 | struct tracking { |
363 | struct refspec spec; | |
364 | char *src; | |
365 | const char *remote; | |
366 | int matches; | |
367 | }; | |
0746d19a | 368 | |
6f084a56 | 369 | static int find_tracked_branch(struct remote *remote, void *priv) |
0746d19a | 370 | { |
6f084a56 | 371 | struct tracking *tracking = priv; |
0746d19a | 372 | |
6f084a56 JS |
373 | if (!remote_find_tracking(remote, &tracking->spec)) { |
374 | if (++tracking->matches == 1) { | |
375 | tracking->src = tracking->spec.src; | |
376 | tracking->remote = remote->name; | |
377 | } else { | |
378 | free(tracking->spec.src); | |
379 | if (tracking->src) { | |
380 | free(tracking->src); | |
381 | tracking->src = NULL; | |
382 | } | |
383 | } | |
384 | tracking->spec.src = NULL; | |
0746d19a PB |
385 | } |
386 | ||
0746d19a PB |
387 | return 0; |
388 | } | |
389 | ||
6f084a56 JS |
390 | |
391 | /* | |
392 | * This is called when new_ref is branched off of orig_ref, and tries | |
393 | * to infer the settings for branch.<new_ref>.{remote,merge} from the | |
394 | * config. | |
395 | */ | |
396 | static int setup_tracking(const char *new_ref, const char *orig_ref) | |
0746d19a PB |
397 | { |
398 | char key[1024]; | |
6f084a56 | 399 | struct tracking tracking; |
9debc324 | 400 | |
6f084a56 JS |
401 | if (strlen(new_ref) > 1024 - 7 - 7 - 1) |
402 | return error("Tracking not set up: name too long: %s", | |
403 | new_ref); | |
0746d19a | 404 | |
6f084a56 JS |
405 | memset(&tracking, 0, sizeof(tracking)); |
406 | tracking.spec.dst = (char *)orig_ref; | |
407 | if (for_each_remote(find_tracked_branch, &tracking) || | |
408 | !tracking.matches) | |
409 | return 1; | |
410 | ||
411 | if (tracking.matches > 1) | |
412 | return error("Not tracking: ambiguous information for ref %s", | |
413 | orig_ref); | |
414 | ||
415 | if (tracking.matches == 1) { | |
416 | sprintf(key, "branch.%s.remote", new_ref); | |
417 | git_config_set(key, tracking.remote ? tracking.remote : "."); | |
418 | sprintf(key, "branch.%s.merge", new_ref); | |
419 | git_config_set(key, tracking.src); | |
420 | free(tracking.src); | |
0746d19a | 421 | printf("Branch %s set up to track remote branch %s.\n", |
6f084a56 | 422 | new_ref, orig_ref); |
0746d19a PB |
423 | } |
424 | ||
6f084a56 | 425 | return 0; |
0746d19a PB |
426 | } |
427 | ||
c847f537 | 428 | static void create_branch(const char *name, const char *start_name, |
0746d19a | 429 | int force, int reflog, int track) |
c31820c2 LH |
430 | { |
431 | struct ref_lock *lock; | |
432 | struct commit *commit; | |
433 | unsigned char sha1[20]; | |
0746d19a | 434 | char *real_ref, ref[PATH_MAX], msg[PATH_MAX + 20]; |
5f856dd4 | 435 | int forcing = 0; |
c31820c2 LH |
436 | |
437 | snprintf(ref, sizeof ref, "refs/heads/%s", name); | |
438 | if (check_ref_format(ref)) | |
439 | die("'%s' is not a valid branch name.", name); | |
440 | ||
441 | if (resolve_ref(ref, sha1, 1, NULL)) { | |
442 | if (!force) | |
443 | die("A branch named '%s' already exists.", name); | |
11a6ddb2 | 444 | else if (!is_bare_repository() && !strcmp(head, name)) |
c31820c2 | 445 | die("Cannot force update the current branch."); |
5f856dd4 | 446 | forcing = 1; |
c31820c2 LH |
447 | } |
448 | ||
45994a1e JH |
449 | real_ref = NULL; |
450 | if (get_sha1(start_name, sha1)) | |
451 | die("Not a valid object name: '%s'.", start_name); | |
452 | ||
453 | switch (dwim_ref(start_name, strlen(start_name), sha1, &real_ref)) { | |
454 | case 0: | |
455 | /* Not branching from any existing branch */ | |
0746d19a | 456 | real_ref = NULL; |
45994a1e JH |
457 | break; |
458 | case 1: | |
459 | /* Unique completion -- good */ | |
460 | break; | |
461 | default: | |
0746d19a | 462 | die("Ambiguous object name: '%s'.", start_name); |
45994a1e JH |
463 | break; |
464 | } | |
c847f537 JH |
465 | |
466 | if ((commit = lookup_commit_reference(sha1)) == NULL) | |
467 | die("Not a valid branch point: '%s'.", start_name); | |
c31820c2 LH |
468 | hashcpy(sha1, commit->object.sha1); |
469 | ||
68db31cc | 470 | lock = lock_any_ref_for_update(ref, NULL, 0); |
c31820c2 LH |
471 | if (!lock) |
472 | die("Failed to lock ref for update: %s.", strerror(errno)); | |
473 | ||
5f856dd4 | 474 | if (reflog) |
c31820c2 | 475 | log_all_ref_updates = 1; |
5f856dd4 JH |
476 | |
477 | if (forcing) | |
478 | snprintf(msg, sizeof msg, "branch: Reset from %s", | |
479 | start_name); | |
480 | else | |
c847f537 JH |
481 | snprintf(msg, sizeof msg, "branch: Created from %s", |
482 | start_name); | |
c31820c2 | 483 | |
0746d19a PB |
484 | /* When branching off a remote branch, set up so that git-pull |
485 | automatically merges from there. So far, this is only done for | |
486 | remotes registered via .git/config. */ | |
6f084a56 JS |
487 | if (real_ref && track) |
488 | setup_tracking(name, real_ref); | |
0746d19a | 489 | |
c31820c2 LH |
490 | if (write_ref_sha1(lock, sha1, msg) < 0) |
491 | die("Failed to write ref: %s.", strerror(errno)); | |
0746d19a PB |
492 | |
493 | if (real_ref) | |
494 | free(real_ref); | |
c31820c2 LH |
495 | } |
496 | ||
c976d415 LH |
497 | static void rename_branch(const char *oldname, const char *newname, int force) |
498 | { | |
678d0f4c | 499 | char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100]; |
c976d415 | 500 | unsigned char sha1[20]; |
19eba151 | 501 | char oldsection[PATH_MAX], newsection[PATH_MAX]; |
c976d415 | 502 | |
c847f537 | 503 | if (!oldname) |
3dff5379 | 504 | die("cannot rename the current branch while not on any."); |
c847f537 | 505 | |
c976d415 LH |
506 | if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref)) |
507 | die("Old branchname too long"); | |
508 | ||
509 | if (check_ref_format(oldref)) | |
510 | die("Invalid branch name: %s", oldref); | |
511 | ||
512 | if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref)) | |
513 | die("New branchname too long"); | |
514 | ||
515 | if (check_ref_format(newref)) | |
516 | die("Invalid branch name: %s", newref); | |
517 | ||
518 | if (resolve_ref(newref, sha1, 1, NULL) && !force) | |
519 | die("A branch named '%s' already exists.", newname); | |
520 | ||
678d0f4c LH |
521 | snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s", |
522 | oldref, newref); | |
523 | ||
524 | if (rename_ref(oldref, newref, logmsg)) | |
c976d415 LH |
525 | die("Branch rename failed"); |
526 | ||
8b5157e4 NP |
527 | /* no need to pass logmsg here as HEAD didn't really move */ |
528 | if (!strcmp(oldname, head) && create_symref("HEAD", newref, NULL)) | |
c976d415 | 529 | die("Branch renamed to %s, but HEAD is not updated!", newname); |
19eba151 LH |
530 | |
531 | snprintf(oldsection, sizeof(oldsection), "branch.%s", oldref + 11); | |
532 | snprintf(newsection, sizeof(newsection), "branch.%s", newref + 11); | |
533 | if (git_config_rename_section(oldsection, newsection) < 0) | |
534 | die("Branch is renamed, but update of config-file failed"); | |
c976d415 LH |
535 | } |
536 | ||
694a5775 JH |
537 | static int opt_parse_with_commit(const struct option *opt, const char *arg, int unset) |
538 | { | |
539 | unsigned char sha1[20]; | |
540 | struct commit *commit; | |
541 | ||
542 | if (!arg) | |
543 | return -1; | |
544 | if (get_sha1(arg, sha1)) | |
545 | die("malformed object name %s", arg); | |
546 | commit = lookup_commit_reference(sha1); | |
547 | if (!commit) | |
548 | die("no such commit %s", arg); | |
549 | commit_list_insert(commit, opt->value); | |
550 | return 0; | |
551 | } | |
552 | ||
c31820c2 LH |
553 | int cmd_branch(int argc, const char **argv, const char *prefix) |
554 | { | |
d11d44fa | 555 | int delete = 0, rename = 0, force_create = 0; |
0016a482 | 556 | int verbose = 0, abbrev = DEFAULT_ABBREV, detached = 0; |
0746d19a | 557 | int reflog = 0, track; |
d11d44fa | 558 | int kinds = REF_LOCAL_BRANCH; |
694a5775 | 559 | struct commit_list *with_commit = NULL; |
a8dfd5ea PH |
560 | |
561 | struct option options[] = { | |
562 | OPT_GROUP("Generic options"), | |
563 | OPT__VERBOSE(&verbose), | |
564 | OPT_BOOLEAN( 0 , "track", &track, "set up tracking mode (see git-pull(1))"), | |
565 | OPT_BOOLEAN( 0 , "color", &branch_use_color, "use colored output"), | |
d11d44fa PH |
566 | OPT_SET_INT('r', NULL, &kinds, "act on remote-tracking branches", |
567 | REF_REMOTE_BRANCH), | |
694a5775 JH |
568 | OPT_CALLBACK(0, "contains", &with_commit, "commit", |
569 | "print only branches that contain the commit", | |
570 | opt_parse_with_commit), | |
571 | { | |
572 | OPTION_CALLBACK, 0, "with", &with_commit, "commit", | |
573 | "print only branches that contain the commit", | |
574 | PARSE_OPT_HIDDEN, opt_parse_with_commit, | |
575 | }, | |
a8dfd5ea PH |
576 | OPT__ABBREV(&abbrev), |
577 | ||
578 | OPT_GROUP("Specific git-branch actions:"), | |
d11d44fa PH |
579 | OPT_SET_INT('a', NULL, &kinds, "list both remote-tracking and local branches", |
580 | REF_REMOTE_BRANCH | REF_LOCAL_BRANCH), | |
581 | OPT_BIT('d', NULL, &delete, "delete fully merged branch", 1), | |
582 | OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2), | |
583 | OPT_BIT('m', NULL, &rename, "move/rename a branch and its reflog", 1), | |
584 | OPT_BIT('M', NULL, &rename, "move/rename a branch, even if target exists", 2), | |
585 | OPT_BOOLEAN('l', NULL, &reflog, "create the branch's reflog"), | |
586 | OPT_BOOLEAN('f', NULL, &force_create, "force creation (when already exists)"), | |
a8dfd5ea PH |
587 | OPT_END(), |
588 | }; | |
c31820c2 | 589 | |
a1158cae | 590 | git_config(git_branch_config); |
6b2f2d98 MK |
591 | |
592 | if (branch_use_color == -1) | |
593 | branch_use_color = git_use_color_default; | |
594 | ||
4017761f | 595 | track = branch_track; |
a8dfd5ea | 596 | argc = parse_options(argc, argv, options, builtin_branch_usage, 0); |
d11d44fa | 597 | if (!!delete + !!rename + !!force_create > 1) |
a8dfd5ea | 598 | usage_with_options(builtin_branch_usage, options); |
c976d415 | 599 | |
078f8380 | 600 | head = resolve_ref("HEAD", head_sha1, 0, NULL); |
c31820c2 LH |
601 | if (!head) |
602 | die("Failed to resolve HEAD as a valid ref."); | |
078f8380 | 603 | head = xstrdup(head); |
0016a482 LH |
604 | if (!strcmp(head, "HEAD")) { |
605 | detached = 1; | |
a8dfd5ea | 606 | } else { |
cc44c765 | 607 | if (prefixcmp(head, "refs/heads/")) |
0016a482 LH |
608 | die("HEAD not found below refs/heads!"); |
609 | head += 11; | |
610 | } | |
c31820c2 LH |
611 | |
612 | if (delete) | |
d11d44fa | 613 | return delete_branches(argc, argv, delete > 1, kinds); |
a8dfd5ea | 614 | else if (argc == 0) |
694a5775 | 615 | print_ref_list(kinds, detached, verbose, abbrev, with_commit); |
a8dfd5ea | 616 | else if (rename && (argc == 1)) |
d11d44fa | 617 | rename_branch(head, argv[0], rename > 1); |
a8dfd5ea | 618 | else if (rename && (argc == 2)) |
d11d44fa | 619 | rename_branch(argv[0], argv[1], rename > 1); |
a8dfd5ea PH |
620 | else if (argc <= 2) |
621 | create_branch(argv[0], (argc == 2) ? argv[1] : head, | |
45994a1e | 622 | force_create, reflog, track); |
c31820c2 | 623 | else |
a8dfd5ea | 624 | usage_with_options(builtin_branch_usage, options); |
c31820c2 LH |
625 | |
626 | return 0; | |
627 | } |