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 "refs.h"
10#include "commit.h"
11#include "builtin.h"
12
13static const char builtin_branch_usage[] =
14"git-branch (-d | -D) <branchname> | [-l] [-f] <branchname> [<start-point>] | [-r] | [-a]";
15
16
17static const char *head;
18static unsigned char head_sha1[20];
19
20static 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;
26
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 }
34
35 free_commit_list(bases);
36 return ret;
37}
38
39static 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;
45
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.");
50
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]);
54
55 rev = lookup_commit_reference(sha1);
56 if (!rev || !head_rev)
57 die("Couldn't look up commit objects.");
58
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 */
63
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 }
72
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]);
77
78 free(name);
79 }
80}
81
82#define REF_UNKNOWN_TYPE 0x00
83#define REF_LOCAL_BRANCH 0x01
84#define REF_REMOTE_BRANCH 0x02
85#define REF_TAG 0x04
86
87struct ref_item {
88 char *name;
89 unsigned int kind;
90};
91
92struct ref_list {
93 int index, alloc;
94 struct ref_item *list;
95 int kinds;
96};
97
98static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
99{
100 struct ref_list *ref_list = (struct ref_list*)(cb_data);
101 struct ref_item *newitem;
102 int kind = REF_UNKNOWN_TYPE;
103
104 /* Detect kind */
105 if (!strncmp(refname, "refs/heads/", 11)) {
106 kind = REF_LOCAL_BRANCH;
107 refname += 11;
108 } else if (!strncmp(refname, "refs/remotes/", 13)) {
109 kind = REF_REMOTE_BRANCH;
110 refname += 13;
111 } else if (!strncmp(refname, "refs/tags/", 10)) {
112 kind = REF_TAG;
113 refname += 10;
114 }
115
116 /* Don't add types the caller doesn't want */
117 if ((kind & ref_list->kinds) == 0)
118 return 0;
119
120 /* Resize buffer */
121 if (ref_list->index >= ref_list->alloc) {
122 ref_list->alloc = alloc_nr(ref_list->alloc);
123 ref_list->list = xrealloc(ref_list->list,
124 ref_list->alloc * sizeof(struct ref_item));
125 }
126
127 /* Record the new item */
128 newitem = &(ref_list->list[ref_list->index++]);
129 newitem->name = xstrdup(refname);
130 newitem->kind = kind;
131
132 return 0;
133}
134
135static void free_ref_list(struct ref_list *ref_list)
136{
137 int i;
138
139 for (i = 0; i < ref_list->index; i++)
140 free(ref_list->list[i].name);
141 free(ref_list->list);
142}
143
144static int ref_cmp(const void *r1, const void *r2)
145{
146 struct ref_item *c1 = (struct ref_item *)(r1);
147 struct ref_item *c2 = (struct ref_item *)(r2);
148
149 if (c1->kind != c2->kind)
150 return c1->kind - c2->kind;
151 return strcmp(c1->name, c2->name);
152}
153
154static void print_ref_list(int kinds)
155{
156 int i;
157 char c;
158 struct ref_list ref_list;
159
160 memset(&ref_list, 0, sizeof(ref_list));
161 ref_list.kinds = kinds;
162 for_each_ref(append_ref, &ref_list);
163
164 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
165
166 for (i = 0; i < ref_list.index; i++) {
167 c = ' ';
168 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
169 !strcmp(ref_list.list[i].name, head))
170 c = '*';
171
172 printf("%c %s\n", c, ref_list.list[i].name);
173 }
174
175 free_ref_list(&ref_list);
176}
177
178static void create_branch(const char *name, const char *start,
179 int force, int reflog)
180{
181 struct ref_lock *lock;
182 struct commit *commit;
183 unsigned char sha1[20];
184 char ref[PATH_MAX], msg[PATH_MAX + 20];
185
186 snprintf(ref, sizeof ref, "refs/heads/%s", name);
187 if (check_ref_format(ref))
188 die("'%s' is not a valid branch name.", name);
189
190 if (resolve_ref(ref, sha1, 1, NULL)) {
191 if (!force)
192 die("A branch named '%s' already exists.", name);
193 else if (!strcmp(head, name))
194 die("Cannot force update the current branch.");
195 }
196
197 if (get_sha1(start, sha1) ||
198 (commit = lookup_commit_reference(sha1)) == NULL)
199 die("Not a valid branch point: '%s'.", start);
200 hashcpy(sha1, commit->object.sha1);
201
202 lock = lock_any_ref_for_update(ref, NULL);
203 if (!lock)
204 die("Failed to lock ref for update: %s.", strerror(errno));
205
206 if (reflog) {
207 log_all_ref_updates = 1;
208 snprintf(msg, sizeof msg, "branch: Created from %s", start);
209 }
210
211 if (write_ref_sha1(lock, sha1, msg) < 0)
212 die("Failed to write ref: %s.", strerror(errno));
213}
214
215int cmd_branch(int argc, const char **argv, const char *prefix)
216{
217 int delete = 0, force_delete = 0, force_create = 0;
218 int reflog = 0;
219 int kinds = REF_LOCAL_BRANCH;
220 int i;
221
222 git_config(git_default_config);
223
224 for (i = 1; i < argc; i++) {
225 const char *arg = argv[i];
226
227 if (arg[0] != '-')
228 break;
229 if (!strcmp(arg, "--")) {
230 i++;
231 break;
232 }
233 if (!strcmp(arg, "-d")) {
234 delete = 1;
235 continue;
236 }
237 if (!strcmp(arg, "-D")) {
238 delete = 1;
239 force_delete = 1;
240 continue;
241 }
242 if (!strcmp(arg, "-f")) {
243 force_create = 1;
244 continue;
245 }
246 if (!strcmp(arg, "-r")) {
247 kinds = REF_REMOTE_BRANCH;
248 continue;
249 }
250 if (!strcmp(arg, "-a")) {
251 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
252 continue;
253 }
254 if (!strcmp(arg, "-l")) {
255 reflog = 1;
256 continue;
257 }
258 usage(builtin_branch_usage);
259 }
260
261 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
262 if (!head)
263 die("Failed to resolve HEAD as a valid ref.");
264 if (strncmp(head, "refs/heads/", 11))
265 die("HEAD not found below refs/heads!");
266 head += 11;
267
268 if (delete)
269 delete_branches(argc - i, argv + i, force_delete);
270 else if (i == argc)
271 print_ref_list(kinds);
272 else if (i == argc - 1)
273 create_branch(argv[i], head, force_create, reflog);
274 else if (i == argc - 2)
275 create_branch(argv[i], argv[i + 1], force_create, reflog);
276 else
277 usage(builtin_branch_usage);
278
279 return 0;
280}