Support bash completion of refs/remote.
[git/gitweb-caching.git] / builtin-branch.c
blob3d5cb0e4b2de8abf3a9f1af8fadb9c1bc5393967
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] [-v] [--abbrev=<length>] ";
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 = head_rev;
42 unsigned char sha1[20];
43 char *name;
44 int i;
46 if (!force) {
47 head_rev = lookup_commit_reference(head_sha1);
48 if (!head_rev)
49 die("Couldn't look up commit object for HEAD");
51 for (i = 0; i < argc; i++) {
52 if (!strcmp(head, argv[i]))
53 die("Cannot delete the branch you are currently on.");
55 name = xstrdup(mkpath("refs/heads/%s", argv[i]));
56 if (!resolve_ref(name, sha1, 1, NULL))
57 die("Branch '%s' not found.", argv[i]);
59 rev = lookup_commit_reference(sha1);
60 if (!rev)
61 die("Couldn't look up commit object for '%s'", name);
63 /* This checks whether the merge bases of branch and
64 * HEAD contains branch -- which means that the HEAD
65 * contains everything in both.
68 if (!force &&
69 !in_merge_bases(sha1, rev, head_rev)) {
70 fprintf(stderr,
71 "The branch '%s' is not a strict subset of your current HEAD.\n"
72 "If you are sure you want to delete it, run 'git branch -D %s'.\n",
73 argv[i], argv[i]);
74 exit(1);
77 if (delete_ref(name, sha1))
78 printf("Error deleting branch '%s'\n", argv[i]);
79 else
80 printf("Deleted branch %s.\n", argv[i]);
82 free(name);
86 #define REF_UNKNOWN_TYPE 0x00
87 #define REF_LOCAL_BRANCH 0x01
88 #define REF_REMOTE_BRANCH 0x02
89 #define REF_TAG 0x04
91 struct ref_item {
92 char *name;
93 unsigned int kind;
94 unsigned char sha1[20];
97 struct ref_list {
98 int index, alloc, maxwidth;
99 struct ref_item *list;
100 int kinds;
103 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
105 struct ref_list *ref_list = (struct ref_list*)(cb_data);
106 struct ref_item *newitem;
107 int kind = REF_UNKNOWN_TYPE;
108 int len;
110 /* Detect kind */
111 if (!strncmp(refname, "refs/heads/", 11)) {
112 kind = REF_LOCAL_BRANCH;
113 refname += 11;
114 } else if (!strncmp(refname, "refs/remotes/", 13)) {
115 kind = REF_REMOTE_BRANCH;
116 refname += 13;
117 } else if (!strncmp(refname, "refs/tags/", 10)) {
118 kind = REF_TAG;
119 refname += 10;
122 /* Don't add types the caller doesn't want */
123 if ((kind & ref_list->kinds) == 0)
124 return 0;
126 /* Resize buffer */
127 if (ref_list->index >= ref_list->alloc) {
128 ref_list->alloc = alloc_nr(ref_list->alloc);
129 ref_list->list = xrealloc(ref_list->list,
130 ref_list->alloc * sizeof(struct ref_item));
133 /* Record the new item */
134 newitem = &(ref_list->list[ref_list->index++]);
135 newitem->name = xstrdup(refname);
136 newitem->kind = kind;
137 hashcpy(newitem->sha1, sha1);
138 len = strlen(newitem->name);
139 if (len > ref_list->maxwidth)
140 ref_list->maxwidth = len;
142 return 0;
145 static void free_ref_list(struct ref_list *ref_list)
147 int i;
149 for (i = 0; i < ref_list->index; i++)
150 free(ref_list->list[i].name);
151 free(ref_list->list);
154 static int ref_cmp(const void *r1, const void *r2)
156 struct ref_item *c1 = (struct ref_item *)(r1);
157 struct ref_item *c2 = (struct ref_item *)(r2);
159 if (c1->kind != c2->kind)
160 return c1->kind - c2->kind;
161 return strcmp(c1->name, c2->name);
164 static void print_ref_info(const unsigned char *sha1, int abbrev)
166 struct commit *commit;
167 char subject[256];
170 commit = lookup_commit(sha1);
171 if (commit && !parse_commit(commit))
172 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
173 subject, sizeof(subject), 0,
174 NULL, NULL, 0);
175 else
176 strcpy(subject, " **** invalid ref ****");
178 printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
181 static void print_ref_list(int kinds, int verbose, int abbrev)
183 int i;
184 char c;
185 struct ref_list ref_list;
187 memset(&ref_list, 0, sizeof(ref_list));
188 ref_list.kinds = kinds;
189 for_each_ref(append_ref, &ref_list);
191 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
193 for (i = 0; i < ref_list.index; i++) {
194 c = ' ';
195 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
196 !strcmp(ref_list.list[i].name, head))
197 c = '*';
199 if (verbose) {
200 printf("%c %-*s", c, ref_list.maxwidth,
201 ref_list.list[i].name);
202 print_ref_info(ref_list.list[i].sha1, abbrev);
204 else
205 printf("%c %s\n", c, ref_list.list[i].name);
208 free_ref_list(&ref_list);
211 static void create_branch(const char *name, const char *start,
212 int force, int reflog)
214 struct ref_lock *lock;
215 struct commit *commit;
216 unsigned char sha1[20];
217 char ref[PATH_MAX], msg[PATH_MAX + 20];
219 snprintf(ref, sizeof ref, "refs/heads/%s", name);
220 if (check_ref_format(ref))
221 die("'%s' is not a valid branch name.", name);
223 if (resolve_ref(ref, sha1, 1, NULL)) {
224 if (!force)
225 die("A branch named '%s' already exists.", name);
226 else if (!strcmp(head, name))
227 die("Cannot force update the current branch.");
230 if (get_sha1(start, sha1) ||
231 (commit = lookup_commit_reference(sha1)) == NULL)
232 die("Not a valid branch point: '%s'.", start);
233 hashcpy(sha1, commit->object.sha1);
235 lock = lock_any_ref_for_update(ref, NULL);
236 if (!lock)
237 die("Failed to lock ref for update: %s.", strerror(errno));
239 if (reflog) {
240 log_all_ref_updates = 1;
241 snprintf(msg, sizeof msg, "branch: Created from %s", start);
244 if (write_ref_sha1(lock, sha1, msg) < 0)
245 die("Failed to write ref: %s.", strerror(errno));
248 int cmd_branch(int argc, const char **argv, const char *prefix)
250 int delete = 0, force_delete = 0, force_create = 0;
251 int verbose = 0, abbrev = DEFAULT_ABBREV;
252 int reflog = 0;
253 int kinds = REF_LOCAL_BRANCH;
254 int i;
256 git_config(git_default_config);
258 for (i = 1; i < argc; i++) {
259 const char *arg = argv[i];
261 if (arg[0] != '-')
262 break;
263 if (!strcmp(arg, "--")) {
264 i++;
265 break;
267 if (!strcmp(arg, "-d")) {
268 delete = 1;
269 continue;
271 if (!strcmp(arg, "-D")) {
272 delete = 1;
273 force_delete = 1;
274 continue;
276 if (!strcmp(arg, "-f")) {
277 force_create = 1;
278 continue;
280 if (!strcmp(arg, "-r")) {
281 kinds = REF_REMOTE_BRANCH;
282 continue;
284 if (!strcmp(arg, "-a")) {
285 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
286 continue;
288 if (!strcmp(arg, "-l")) {
289 reflog = 1;
290 continue;
292 if (!strncmp(arg, "--abbrev=", 9)) {
293 abbrev = atoi(arg+9);
294 continue;
296 if (!strcmp(arg, "-v")) {
297 verbose = 1;
298 continue;
300 usage(builtin_branch_usage);
303 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
304 if (!head)
305 die("Failed to resolve HEAD as a valid ref.");
306 if (strncmp(head, "refs/heads/", 11))
307 die("HEAD not found below refs/heads!");
308 head += 11;
310 if (delete)
311 delete_branches(argc - i, argv + i, force_delete);
312 else if (i == argc)
313 print_ref_list(kinds, verbose, abbrev);
314 else if (i == argc - 1)
315 create_branch(argv[i], head, force_create, reflog);
316 else if (i == argc - 2)
317 create_branch(argv[i], argv[i + 1], force_create, reflog);
318 else
319 usage(builtin_branch_usage);
321 return 0;