gitweb: Replace SPC with   also in tag comment
[git/dscho.git] / builtin-branch.c
blob22e3285a4ccf42f1f5a679eb209925bb03419d0f
1 /*
2 * Builtin "git branch"
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] | [-a]";
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)
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;
35 free_commit_list(bases);
36 return ret;
39 static void delete_branches(int argc, const char **argv, int force)
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.
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);
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);
82 #define REF_UNKNOWN_TYPE 0x00
83 #define REF_LOCAL_BRANCH 0x01
84 #define REF_REMOTE_BRANCH 0x02
85 #define REF_TAG 0x04
87 struct ref_item {
88 char *name;
89 unsigned int kind;
92 struct ref_list {
93 int index, alloc;
94 struct ref_item *list;
95 int kinds;
98 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
100 struct ref_list *ref_list = (struct ref_list*)(cb_data);
101 struct ref_item *newitem;
102 int kind = REF_UNKNOWN_TYPE;
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;
116 /* Don't add types the caller doesn't want */
117 if ((kind & ref_list->kinds) == 0)
118 return 0;
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));
127 /* Record the new item */
128 newitem = &(ref_list->list[ref_list->index++]);
129 newitem->name = xstrdup(refname);
130 newitem->kind = kind;
132 return 0;
135 static void free_ref_list(struct ref_list *ref_list)
137 int i;
139 for (i = 0; i < ref_list->index; i++)
140 free(ref_list->list[i].name);
141 free(ref_list->list);
144 static int ref_cmp(const void *r1, const void *r2)
146 struct ref_item *c1 = (struct ref_item *)(r1);
147 struct ref_item *c2 = (struct ref_item *)(r2);
149 if (c1->kind != c2->kind)
150 return c1->kind - c2->kind;
151 return strcmp(c1->name, c2->name);
154 static void print_ref_list(int kinds)
156 int i;
157 char c;
158 struct ref_list ref_list;
160 memset(&ref_list, 0, sizeof(ref_list));
161 ref_list.kinds = kinds;
162 for_each_ref(append_ref, &ref_list);
164 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
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 = '*';
172 printf("%c %s\n", c, ref_list.list[i].name);
175 free_ref_list(&ref_list);
178 static void create_branch(const char *name, const char *start,
179 int force, int reflog)
181 struct ref_lock *lock;
182 struct commit *commit;
183 unsigned char sha1[20];
184 char ref[PATH_MAX], msg[PATH_MAX + 20];
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);
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.");
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);
202 lock = lock_any_ref_for_update(ref, NULL);
203 if (!lock)
204 die("Failed to lock ref for update: %s.", strerror(errno));
206 if (reflog) {
207 log_all_ref_updates = 1;
208 snprintf(msg, sizeof msg, "branch: Created from %s", start);
211 if (write_ref_sha1(lock, sha1, msg) < 0)
212 die("Failed to write ref: %s.", strerror(errno));
215 int cmd_branch(int argc, const char **argv, const char *prefix)
217 int delete = 0, force_delete = 0, force_create = 0;
218 int reflog = 0;
219 int kinds = REF_LOCAL_BRANCH;
220 int i;
222 git_config(git_default_config);
224 for (i = 1; i < argc; i++) {
225 const char *arg = argv[i];
227 if (arg[0] != '-')
228 break;
229 if (!strcmp(arg, "--")) {
230 i++;
231 break;
233 if (!strcmp(arg, "-d")) {
234 delete = 1;
235 continue;
237 if (!strcmp(arg, "-D")) {
238 delete = 1;
239 force_delete = 1;
240 continue;
242 if (!strcmp(arg, "-f")) {
243 force_create = 1;
244 continue;
246 if (!strcmp(arg, "-r")) {
247 kinds = REF_REMOTE_BRANCH;
248 continue;
250 if (!strcmp(arg, "-a")) {
251 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
252 continue;
254 if (!strcmp(arg, "-l")) {
255 reflog = 1;
256 continue;
258 usage(builtin_branch_usage);
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;
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);
279 return 0;