Code

Merge branch 'rs/cherry'
[git.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  */
8 #include "cache.h"
9 #include "refs.h"
10 #include "commit.h"
11 #include "builtin.h"
13 static const char builtin_branch_usage[] =
14 "git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | [-r]";
17 static const char *head;
18 static unsigned char head_sha1[20];
20 static int in_merge_bases(const unsigned char *sha1,
21                           struct commit *rev1,
22                           struct commit *rev2)
23 {
24         struct commit_list *bases, *b;
25         int ret = 0;
27         bases = get_merge_bases(rev1, rev2, 1);
28         for (b = bases; b; b = b->next) {
29                 if (!hashcmp(sha1, b->item->object.sha1)) {
30                         ret = 1;
31                         break;
32                 }
33         }
35         free_commit_list(bases);
36         return ret;
37 }
39 static void delete_branches(int argc, const char **argv, int force)
40 {
41         struct commit *rev, *head_rev;
42         unsigned char sha1[20];
43         char *name;
44         int i;
46         head_rev = lookup_commit_reference(head_sha1);
47         for (i = 0; i < argc; i++) {
48                 if (!strcmp(head, argv[i]))
49                         die("Cannot delete the branch you are currently on.");
51                 name = xstrdup(mkpath("refs/heads/%s", argv[i]));
52                 if (!resolve_ref(name, sha1, 1, NULL))
53                         die("Branch '%s' not found.", argv[i]);
55                 rev = lookup_commit_reference(sha1);
56                 if (!rev || !head_rev)
57                         die("Couldn't look up commit objects.");
59                 /* This checks whether the merge bases of branch and
60                  * HEAD contains branch -- which means that the HEAD
61                  * contains everything in both.
62                  */
64                 if (!force &&
65                     !in_merge_bases(sha1, rev, head_rev)) {
66                         fprintf(stderr,
67                                 "The branch '%s' is not a strict subset of your current HEAD.\n"
68                                 "If you are sure you want to delete it, run 'git branch -D %s'.\n",
69                                 argv[i], argv[i]);
70                         exit(1);
71                 }
73                 if (delete_ref(name, sha1))
74                         printf("Error deleting branch '%s'\n", argv[i]);
75                 else
76                         printf("Deleted branch %s.\n", argv[i]);
78                 free(name);
79         }
80 }
82 static int ref_index, ref_alloc;
83 static char **ref_list;
85 static int append_ref(const char *refname, const unsigned char *sha1, int flags,
86                 void *cb_data)
87 {
88         if (ref_index >= ref_alloc) {
89                 ref_alloc = alloc_nr(ref_alloc);
90                 ref_list = xrealloc(ref_list, ref_alloc * sizeof(char *));
91         }
93         ref_list[ref_index++] = xstrdup(refname);
95         return 0;
96 }
98 static int ref_cmp(const void *r1, const void *r2)
99 {
100         return strcmp(*(char **)r1, *(char **)r2);
103 static void print_ref_list(int remote_only)
105         int i;
107         if (remote_only)
108                 for_each_remote_ref(append_ref, NULL);
109         else
110                 for_each_branch_ref(append_ref, NULL);
112         qsort(ref_list, ref_index, sizeof(char *), ref_cmp);
114         for (i = 0; i < ref_index; i++) {
115                 if (!strcmp(ref_list[i], head))
116                         printf("* %s\n", ref_list[i]);
117                 else
118                         printf("  %s\n", ref_list[i]);
119         }
122 static void create_branch(const char *name, const char *start,
123                           int force, int reflog)
125         struct ref_lock *lock;
126         struct commit *commit;
127         unsigned char sha1[20];
128         char ref[PATH_MAX], msg[PATH_MAX + 20];
130         snprintf(ref, sizeof ref, "refs/heads/%s", name);
131         if (check_ref_format(ref))
132                 die("'%s' is not a valid branch name.", name);
134         if (resolve_ref(ref, sha1, 1, NULL)) {
135                 if (!force)
136                         die("A branch named '%s' already exists.", name);
137                 else if (!strcmp(head, name))
138                         die("Cannot force update the current branch.");
139         }
141         if (get_sha1(start, sha1) ||
142             (commit = lookup_commit_reference(sha1)) == NULL)
143                 die("Not a valid branch point: '%s'.", start);
144         hashcpy(sha1, commit->object.sha1);
146         lock = lock_any_ref_for_update(ref, NULL);
147         if (!lock)
148                 die("Failed to lock ref for update: %s.", strerror(errno));
150         if (reflog) {
151                 log_all_ref_updates = 1;
152                 snprintf(msg, sizeof msg, "branch: Created from %s", start);
153         }
155         if (write_ref_sha1(lock, sha1, msg) < 0)
156                 die("Failed to write ref: %s.", strerror(errno));
159 int cmd_branch(int argc, const char **argv, const char *prefix)
161         int delete = 0, force_delete = 0, force_create = 0, remote_only = 0;
162         int reflog = 0;
163         int i;
165         git_config(git_default_config);
167         for (i = 1; i < argc; i++) {
168                 const char *arg = argv[i];
170                 if (arg[0] != '-')
171                         break;
172                 if (!strcmp(arg, "--")) {
173                         i++;
174                         break;
175                 }
176                 if (!strcmp(arg, "-d")) {
177                         delete = 1;
178                         continue;
179                 }
180                 if (!strcmp(arg, "-D")) {
181                         delete = 1;
182                         force_delete = 1;
183                         continue;
184                 }
185                 if (!strcmp(arg, "-f")) {
186                         force_create = 1;
187                         continue;
188                 }
189                 if (!strcmp(arg, "-r")) {
190                         remote_only = 1;
191                         continue;
192                 }
193                 if (!strcmp(arg, "-l")) {
194                         reflog = 1;
195                         continue;
196                 }
197                 usage(builtin_branch_usage);
198         }
200         head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
201         if (!head)
202                 die("Failed to resolve HEAD as a valid ref.");
203         if (strncmp(head, "refs/heads/", 11))
204                 die("HEAD not found below refs/heads!");
205         head += 11;
207         if (delete)
208                 delete_branches(argc - i, argv + i, force_delete);
209         else if (i == argc)
210                 print_ref_list(remote_only);
211         else if (i == argc - 1)
212                 create_branch(argv[i], head, force_create, reflog);
213         else if (i == argc - 2)
214                 create_branch(argv[i], argv[i + 1], force_create, reflog);
215         else
216                 usage(builtin_branch_usage);
218         return 0;