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