Documentation: Correct alternates documentation, document http-alternates
[git/dkf.git] / builtin-branch.c
blob368b68ec91f3d72d1f5e1d1dea503334207f864d
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]";
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 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)
88 if (ref_index >= ref_alloc) {
89 ref_alloc = alloc_nr(ref_alloc);
90 ref_list = xrealloc(ref_list, ref_alloc * sizeof(char *));
93 ref_list[ref_index++] = xstrdup(refname);
95 return 0;
98 static int ref_cmp(const void *r1, const void *r2)
100 return strcmp(*(char **)r1, *(char **)r2);
103 static void print_ref_list(int remote_only)
105 int i;
106 char c;
108 if (remote_only)
109 for_each_remote_ref(append_ref, NULL);
110 else
111 for_each_branch_ref(append_ref, NULL);
113 qsort(ref_list, ref_index, sizeof(char *), ref_cmp);
115 for (i = 0; i < ref_index; i++) {
116 c = ' ';
117 if (!strcmp(ref_list[i], head))
118 c = '*';
120 printf("%c %s\n", c, ref_list[i]);
124 static void create_branch(const char *name, const char *start,
125 int force, int reflog)
127 struct ref_lock *lock;
128 struct commit *commit;
129 unsigned char sha1[20];
130 char ref[PATH_MAX], msg[PATH_MAX + 20];
132 snprintf(ref, sizeof ref, "refs/heads/%s", name);
133 if (check_ref_format(ref))
134 die("'%s' is not a valid branch name.", name);
136 if (resolve_ref(ref, sha1, 1, NULL)) {
137 if (!force)
138 die("A branch named '%s' already exists.", name);
139 else if (!strcmp(head, name))
140 die("Cannot force update the current branch.");
143 if (get_sha1(start, sha1) ||
144 (commit = lookup_commit_reference(sha1)) == NULL)
145 die("Not a valid branch point: '%s'.", start);
146 hashcpy(sha1, commit->object.sha1);
148 lock = lock_any_ref_for_update(ref, NULL);
149 if (!lock)
150 die("Failed to lock ref for update: %s.", strerror(errno));
152 if (reflog) {
153 log_all_ref_updates = 1;
154 snprintf(msg, sizeof msg, "branch: Created from %s", start);
157 if (write_ref_sha1(lock, sha1, msg) < 0)
158 die("Failed to write ref: %s.", strerror(errno));
161 int cmd_branch(int argc, const char **argv, const char *prefix)
163 int delete = 0, force_delete = 0, force_create = 0, remote_only = 0;
164 int reflog = 0;
165 int i;
167 git_config(git_default_config);
169 for (i = 1; i < argc; i++) {
170 const char *arg = argv[i];
172 if (arg[0] != '-')
173 break;
174 if (!strcmp(arg, "--")) {
175 i++;
176 break;
178 if (!strcmp(arg, "-d")) {
179 delete = 1;
180 continue;
182 if (!strcmp(arg, "-D")) {
183 delete = 1;
184 force_delete = 1;
185 continue;
187 if (!strcmp(arg, "-f")) {
188 force_create = 1;
189 continue;
191 if (!strcmp(arg, "-r")) {
192 remote_only = 1;
193 continue;
195 if (!strcmp(arg, "-l")) {
196 reflog = 1;
197 continue;
199 usage(builtin_branch_usage);
202 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
203 if (!head)
204 die("Failed to resolve HEAD as a valid ref.");
205 if (strncmp(head, "refs/heads/", 11))
206 die("HEAD not found below refs/heads!");
207 head += 11;
209 if (delete)
210 delete_branches(argc - i, argv + i, force_delete);
211 else if (i == argc)
212 print_ref_list(remote_only);
213 else if (i == argc - 1)
214 create_branch(argv[i], head, force_create, reflog);
215 else if (i == argc - 2)
216 create_branch(argv[i], argv[i + 1], force_create, reflog);
217 else
218 usage(builtin_branch_usage);
220 return 0;