Merge branch 'jc/branch-remove-remote'
[git] / builtin-branch.c
1 /*
2  * Builtin "git branch"
3  *
4  * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5  * Based on git-branch.sh by Junio C Hamano.
6  */
7
8 #include "cache.h"
9 #include "color.h"
10 #include "refs.h"
11 #include "commit.h"
12 #include "builtin.h"
13
14 static const char builtin_branch_usage[] =
15   "git-branch [-r] (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | (-m | -M) [<oldbranch>] <newbranch> | [-r | -a] [-v [--abbrev=<length>]]";
16
17 #define REF_UNKNOWN_TYPE    0x00
18 #define REF_LOCAL_BRANCH    0x01
19 #define REF_REMOTE_BRANCH   0x02
20 #define REF_TAG             0x04
21
22 static const char *head;
23 static unsigned char head_sha1[20];
24
25 static int branch_use_color;
26 static char branch_colors[][COLOR_MAXLEN] = {
27         "\033[m",       /* reset */
28         "",             /* PLAIN (normal) */
29         "\033[31m",     /* REMOTE (red) */
30         "",             /* LOCAL (normal) */
31         "\033[32m",     /* CURRENT (green) */
32 };
33 enum color_branch {
34         COLOR_BRANCH_RESET = 0,
35         COLOR_BRANCH_PLAIN = 1,
36         COLOR_BRANCH_REMOTE = 2,
37         COLOR_BRANCH_LOCAL = 3,
38         COLOR_BRANCH_CURRENT = 4,
39 };
40
41 static int parse_branch_color_slot(const char *var, int ofs)
42 {
43         if (!strcasecmp(var+ofs, "plain"))
44                 return COLOR_BRANCH_PLAIN;
45         if (!strcasecmp(var+ofs, "reset"))
46                 return COLOR_BRANCH_RESET;
47         if (!strcasecmp(var+ofs, "remote"))
48                 return COLOR_BRANCH_REMOTE;
49         if (!strcasecmp(var+ofs, "local"))
50                 return COLOR_BRANCH_LOCAL;
51         if (!strcasecmp(var+ofs, "current"))
52                 return COLOR_BRANCH_CURRENT;
53         die("bad config variable '%s'", var);
54 }
55
56 int git_branch_config(const char *var, const char *value)
57 {
58         if (!strcmp(var, "color.branch")) {
59                 branch_use_color = git_config_colorbool(var, value);
60                 return 0;
61         }
62         if (!strncmp(var, "color.branch.", 13)) {
63                 int slot = parse_branch_color_slot(var, 13);
64                 color_parse(value, var, branch_colors[slot]);
65                 return 0;
66         }
67         return git_default_config(var, value);
68 }
69
70 const char *branch_get_color(enum color_branch ix)
71 {
72         if (branch_use_color)
73                 return branch_colors[ix];
74         return "";
75 }
76
77 static int in_merge_bases(const unsigned char *sha1,
78                           struct commit *rev1,
79                           struct commit *rev2)
80 {
81         struct commit_list *bases, *b;
82         int ret = 0;
83
84         bases = get_merge_bases(rev1, rev2, 1);
85         for (b = bases; b; b = b->next) {
86                 if (!hashcmp(sha1, b->item->object.sha1)) {
87                         ret = 1;
88                         break;
89                 }
90         }
91
92         free_commit_list(bases);
93         return ret;
94 }
95
96 static int delete_branches(int argc, const char **argv, int force, int kinds)
97 {
98         struct commit *rev, *head_rev = head_rev;
99         unsigned char sha1[20];
100         char *name = NULL;
101         const char *fmt, *remote;
102         int i;
103         int ret = 0;
104
105         switch (kinds) {
106         case REF_REMOTE_BRANCH:
107                 fmt = "refs/remotes/%s";
108                 remote = "remote ";
109                 force = 1;
110                 break;
111         case REF_LOCAL_BRANCH:
112                 fmt = "refs/heads/%s";
113                 remote = "";
114                 break;
115         default:
116                 die("cannot use -a with -d");
117         }
118
119         if (!force) {
120                 head_rev = lookup_commit_reference(head_sha1);
121                 if (!head_rev)
122                         die("Couldn't look up commit object for HEAD");
123         }
124         for (i = 0; i < argc; i++) {
125                 if (kinds == REF_LOCAL_BRANCH && !strcmp(head, argv[i])) {
126                         error("Cannot delete the branch '%s' "
127                                 "which you are currently on.", argv[i]);
128                         ret = 1;
129                         continue;
130                 }
131
132                 if (name)
133                         free(name);
134
135                 name = xstrdup(mkpath(fmt, argv[i]));
136                 if (!resolve_ref(name, sha1, 1, NULL)) {
137                         error("%sbranch '%s' not found.",
138                                         remote, argv[i]);
139                         ret = 1;
140                         continue;
141                 }
142
143                 rev = lookup_commit_reference(sha1);
144                 if (!rev) {
145                         error("Couldn't look up commit object for '%s'", name);
146                         ret = 1;
147                         continue;
148                 }
149
150                 /* This checks whether the merge bases of branch and
151                  * HEAD contains branch -- which means that the HEAD
152                  * contains everything in both.
153                  */
154
155                 if (!force &&
156                     !in_merge_bases(sha1, rev, head_rev)) {
157                         error("The branch '%s' is not a strict subset of "
158                                 "your current HEAD.\n"
159                                 "If you are sure you want to delete it, "
160                                 "run 'git branch -D %s'.", argv[i], argv[i]);
161                         ret = 1;
162                         continue;
163                 }
164
165                 if (delete_ref(name, sha1)) {
166                         error("Error deleting %sbranch '%s'", remote,
167                                argv[i]);
168                         ret = 1;
169                 } else
170                         printf("Deleted %sbranch %s.\n", remote, argv[i]);
171
172         }
173
174         if (name)
175                 free(name);
176
177         return(ret);
178 }
179
180 struct ref_item {
181         char *name;
182         unsigned int kind;
183         unsigned char sha1[20];
184 };
185
186 struct ref_list {
187         int index, alloc, maxwidth;
188         struct ref_item *list;
189         int kinds;
190 };
191
192 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
193 {
194         struct ref_list *ref_list = (struct ref_list*)(cb_data);
195         struct ref_item *newitem;
196         int kind = REF_UNKNOWN_TYPE;
197         int len;
198
199         /* Detect kind */
200         if (!strncmp(refname, "refs/heads/", 11)) {
201                 kind = REF_LOCAL_BRANCH;
202                 refname += 11;
203         } else if (!strncmp(refname, "refs/remotes/", 13)) {
204                 kind = REF_REMOTE_BRANCH;
205                 refname += 13;
206         } else if (!strncmp(refname, "refs/tags/", 10)) {
207                 kind = REF_TAG;
208                 refname += 10;
209         }
210
211         /* Don't add types the caller doesn't want */
212         if ((kind & ref_list->kinds) == 0)
213                 return 0;
214
215         /* Resize buffer */
216         if (ref_list->index >= ref_list->alloc) {
217                 ref_list->alloc = alloc_nr(ref_list->alloc);
218                 ref_list->list = xrealloc(ref_list->list,
219                                 ref_list->alloc * sizeof(struct ref_item));
220         }
221
222         /* Record the new item */
223         newitem = &(ref_list->list[ref_list->index++]);
224         newitem->name = xstrdup(refname);
225         newitem->kind = kind;
226         hashcpy(newitem->sha1, sha1);
227         len = strlen(newitem->name);
228         if (len > ref_list->maxwidth)
229                 ref_list->maxwidth = len;
230
231         return 0;
232 }
233
234 static void free_ref_list(struct ref_list *ref_list)
235 {
236         int i;
237
238         for (i = 0; i < ref_list->index; i++)
239                 free(ref_list->list[i].name);
240         free(ref_list->list);
241 }
242
243 static int ref_cmp(const void *r1, const void *r2)
244 {
245         struct ref_item *c1 = (struct ref_item *)(r1);
246         struct ref_item *c2 = (struct ref_item *)(r2);
247
248         if (c1->kind != c2->kind)
249                 return c1->kind - c2->kind;
250         return strcmp(c1->name, c2->name);
251 }
252
253 static void print_ref_info(const unsigned char *sha1, int abbrev)
254 {
255         struct commit *commit;
256         char subject[256];
257
258
259         commit = lookup_commit(sha1);
260         if (commit && !parse_commit(commit))
261                 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
262                                     subject, sizeof(subject), 0,
263                                     NULL, NULL, 0);
264         else
265                 strcpy(subject, " **** invalid ref ****");
266
267         printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
268 }
269
270 static void print_ref_list(int kinds, int verbose, int abbrev)
271 {
272         int i;
273         char c;
274         struct ref_list ref_list;
275         int color;
276
277         memset(&ref_list, 0, sizeof(ref_list));
278         ref_list.kinds = kinds;
279         for_each_ref(append_ref, &ref_list);
280
281         qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
282
283         for (i = 0; i < ref_list.index; i++) {
284                 switch( ref_list.list[i].kind ) {
285                         case REF_LOCAL_BRANCH:
286                                 color = COLOR_BRANCH_LOCAL;
287                                 break;
288                         case REF_REMOTE_BRANCH:
289                                 color = COLOR_BRANCH_REMOTE;
290                                 break;
291                         default:
292                                 color = COLOR_BRANCH_PLAIN;
293                                 break;
294                 }
295
296                 c = ' ';
297                 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
298                                 !strcmp(ref_list.list[i].name, head)) {
299                         c = '*';
300                         color = COLOR_BRANCH_CURRENT;
301                 }
302
303                 if (verbose) {
304                         printf("%c %s%-*s%s", c,
305                                         branch_get_color(color),
306                                         ref_list.maxwidth,
307                                         ref_list.list[i].name,
308                                         branch_get_color(COLOR_BRANCH_RESET));
309                         print_ref_info(ref_list.list[i].sha1, abbrev);
310                 }
311                 else
312                         printf("%c %s%s%s\n", c,
313                                         branch_get_color(color),
314                                         ref_list.list[i].name,
315                                         branch_get_color(COLOR_BRANCH_RESET));
316         }
317
318         free_ref_list(&ref_list);
319 }
320
321 static void create_branch(const char *name, const char *start,
322                           int force, int reflog)
323 {
324         struct ref_lock *lock;
325         struct commit *commit;
326         unsigned char sha1[20];
327         char ref[PATH_MAX], msg[PATH_MAX + 20];
328
329         snprintf(ref, sizeof ref, "refs/heads/%s", name);
330         if (check_ref_format(ref))
331                 die("'%s' is not a valid branch name.", name);
332
333         if (resolve_ref(ref, sha1, 1, NULL)) {
334                 if (!force)
335                         die("A branch named '%s' already exists.", name);
336                 else if (!strcmp(head, name))
337                         die("Cannot force update the current branch.");
338         }
339
340         if (get_sha1(start, sha1) ||
341             (commit = lookup_commit_reference(sha1)) == NULL)
342                 die("Not a valid branch point: '%s'.", start);
343         hashcpy(sha1, commit->object.sha1);
344
345         lock = lock_any_ref_for_update(ref, NULL);
346         if (!lock)
347                 die("Failed to lock ref for update: %s.", strerror(errno));
348
349         if (reflog) {
350                 log_all_ref_updates = 1;
351                 snprintf(msg, sizeof msg, "branch: Created from %s", start);
352         }
353
354         if (write_ref_sha1(lock, sha1, msg) < 0)
355                 die("Failed to write ref: %s.", strerror(errno));
356 }
357
358 static void rename_branch(const char *oldname, const char *newname, int force)
359 {
360         char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
361         unsigned char sha1[20];
362
363         if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
364                 die("Old branchname too long");
365
366         if (check_ref_format(oldref))
367                 die("Invalid branch name: %s", oldref);
368
369         if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
370                 die("New branchname too long");
371
372         if (check_ref_format(newref))
373                 die("Invalid branch name: %s", newref);
374
375         if (resolve_ref(newref, sha1, 1, NULL) && !force)
376                 die("A branch named '%s' already exists.", newname);
377
378         snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
379                  oldref, newref);
380
381         if (rename_ref(oldref, newref, logmsg))
382                 die("Branch rename failed");
383
384         if (!strcmp(oldname, head) && create_symref("HEAD", newref))
385                 die("Branch renamed to %s, but HEAD is not updated!", newname);
386 }
387
388 int cmd_branch(int argc, const char **argv, const char *prefix)
389 {
390         int delete = 0, force_delete = 0, force_create = 0;
391         int rename = 0, force_rename = 0;
392         int verbose = 0, abbrev = DEFAULT_ABBREV;
393         int reflog = 0;
394         int kinds = REF_LOCAL_BRANCH;
395         int i;
396
397         setup_ident();
398         git_config(git_branch_config);
399
400         for (i = 1; i < argc; i++) {
401                 const char *arg = argv[i];
402
403                 if (arg[0] != '-')
404                         break;
405                 if (!strcmp(arg, "--")) {
406                         i++;
407                         break;
408                 }
409                 if (!strcmp(arg, "-d")) {
410                         delete = 1;
411                         continue;
412                 }
413                 if (!strcmp(arg, "-D")) {
414                         delete = 1;
415                         force_delete = 1;
416                         continue;
417                 }
418                 if (!strcmp(arg, "-f")) {
419                         force_create = 1;
420                         continue;
421                 }
422                 if (!strcmp(arg, "-m")) {
423                         rename = 1;
424                         continue;
425                 }
426                 if (!strcmp(arg, "-M")) {
427                         rename = 1;
428                         force_rename = 1;
429                         continue;
430                 }
431                 if (!strcmp(arg, "-r")) {
432                         kinds = REF_REMOTE_BRANCH;
433                         continue;
434                 }
435                 if (!strcmp(arg, "-a")) {
436                         kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
437                         continue;
438                 }
439                 if (!strcmp(arg, "-l")) {
440                         reflog = 1;
441                         continue;
442                 }
443                 if (!strncmp(arg, "--abbrev=", 9)) {
444                         abbrev = atoi(arg+9);
445                         continue;
446                 }
447                 if (!strcmp(arg, "-v")) {
448                         verbose = 1;
449                         continue;
450                 }
451                 if (!strcmp(arg, "--color")) {
452                         branch_use_color = 1;
453                         continue;
454                 }
455                 if (!strcmp(arg, "--no-color")) {
456                         branch_use_color = 0;
457                         continue;
458                 }
459                 usage(builtin_branch_usage);
460         }
461
462         if ((delete && rename) || (delete && force_create) ||
463             (rename && force_create))
464                 usage(builtin_branch_usage);
465
466         head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
467         if (!head)
468                 die("Failed to resolve HEAD as a valid ref.");
469         if (strncmp(head, "refs/heads/", 11))
470                 die("HEAD not found below refs/heads!");
471         head += 11;
472
473         if (delete)
474                 return delete_branches(argc - i, argv + i, force_delete, kinds);
475         else if (i == argc)
476                 print_ref_list(kinds, verbose, abbrev);
477         else if (rename && (i == argc - 1))
478                 rename_branch(head, argv[i], force_rename);
479         else if (rename && (i == argc - 2))
480                 rename_branch(argv[i], argv[i + 1], force_rename);
481         else if (i == argc - 1)
482                 create_branch(argv[i], head, force_create, reflog);
483         else if (i == argc - 2)
484                 create_branch(argv[i], argv[i + 1], force_create, reflog);
485         else
486                 usage(builtin_branch_usage);
487
488         return 0;
489 }