rename_ref: use lstat(2) when testing for symlink
[git/jrn.git] / builtin-branch.c
blob153682601e2f77514edab0e6ba9d3077cd3114e7
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>] | (-m | -M) [<oldbranch>] <newbranch> | [-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 static void rename_branch(const char *oldname, const char *newname, int force)
250 char oldref[PATH_MAX], newref[PATH_MAX];
251 unsigned char sha1[20];
253 if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
254 die("Old branchname too long");
256 if (check_ref_format(oldref))
257 die("Invalid branch name: %s", oldref);
259 if (snprintf(newref, sizeof(newref), "refs/heads/%s", newname) > sizeof(newref))
260 die("New branchname too long");
262 if (check_ref_format(newref))
263 die("Invalid branch name: %s", newref);
265 if (resolve_ref(newref, sha1, 1, NULL) && !force)
266 die("A branch named '%s' already exists.", newname);
268 if (rename_ref(oldref, newref))
269 die("Branch rename failed");
271 if (!strcmp(oldname, head) && create_symref("HEAD", newref))
272 die("Branch renamed to %s, but HEAD is not updated!", newname);
275 int cmd_branch(int argc, const char **argv, const char *prefix)
277 int delete = 0, force_delete = 0, force_create = 0;
278 int rename = 0, force_rename = 0;
279 int verbose = 0, abbrev = DEFAULT_ABBREV;
280 int reflog = 0;
281 int kinds = REF_LOCAL_BRANCH;
282 int i;
284 git_config(git_default_config);
286 for (i = 1; i < argc; i++) {
287 const char *arg = argv[i];
289 if (arg[0] != '-')
290 break;
291 if (!strcmp(arg, "--")) {
292 i++;
293 break;
295 if (!strcmp(arg, "-d")) {
296 delete = 1;
297 continue;
299 if (!strcmp(arg, "-D")) {
300 delete = 1;
301 force_delete = 1;
302 continue;
304 if (!strcmp(arg, "-f")) {
305 force_create = 1;
306 continue;
308 if (!strcmp(arg, "-m")) {
309 rename = 1;
310 continue;
312 if (!strcmp(arg, "-M")) {
313 rename = 1;
314 force_rename = 1;
315 continue;
317 if (!strcmp(arg, "-r")) {
318 kinds = REF_REMOTE_BRANCH;
319 continue;
321 if (!strcmp(arg, "-a")) {
322 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
323 continue;
325 if (!strcmp(arg, "-l")) {
326 reflog = 1;
327 continue;
329 if (!strncmp(arg, "--abbrev=", 9)) {
330 abbrev = atoi(arg+9);
331 continue;
333 if (!strcmp(arg, "-v")) {
334 verbose = 1;
335 continue;
337 usage(builtin_branch_usage);
340 if ((delete && rename) || (delete && force_create) ||
341 (rename && force_create))
342 usage(builtin_branch_usage);
344 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
345 if (!head)
346 die("Failed to resolve HEAD as a valid ref.");
347 if (strncmp(head, "refs/heads/", 11))
348 die("HEAD not found below refs/heads!");
349 head += 11;
351 if (delete)
352 delete_branches(argc - i, argv + i, force_delete);
353 else if (i == argc)
354 print_ref_list(kinds, verbose, abbrev);
355 else if (rename && (i == argc - 1))
356 rename_branch(head, argv[i], force_rename);
357 else if (rename && (i == argc - 2))
358 rename_branch(argv[i], argv[i + 1], force_rename);
359 else if (i == argc - 1)
360 create_branch(argv[i], head, force_create, reflog);
361 else if (i == argc - 2)
362 create_branch(argv[i], argv[i + 1], force_create, reflog);
363 else
364 usage(builtin_branch_usage);
366 return 0;