4 * Copyright (c) 2006 Kristian Høgsberg <krh@redhat.com>
5 * Based on git-branch.sh by Junio C Hamano.
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,
24 struct commit_list *bases, *b;
27 bases = get_merge_bases(rev1, rev2, 1);
28 for (b = bases; b; b = b->next) {
29 if (!hashcmp(sha1, b->item->object.sha1)) {
35 free_commit_list(bases);
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];
47 head_rev = lookup_commit_reference(head_sha1);
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);
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.
69 !in_merge_bases(sha1, rev, head_rev)) {
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",
77 if (delete_ref(name, sha1))
78 printf("Error deleting branch '%s'\n", argv[i]);
80 printf("Deleted branch %s.\n", argv[i]);
86 #define REF_UNKNOWN_TYPE 0x00
87 #define REF_LOCAL_BRANCH 0x01
88 #define REF_REMOTE_BRANCH 0x02
94 unsigned char sha1[20];
98 int index, alloc, maxwidth;
99 struct ref_item *list;
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;
111 if (!strncmp(refname, "refs/heads/", 11)) {
112 kind = REF_LOCAL_BRANCH;
114 } else if (!strncmp(refname, "refs/remotes/", 13)) {
115 kind = REF_REMOTE_BRANCH;
117 } else if (!strncmp(refname, "refs/tags/", 10)) {
122 /* Don't add types the caller doesn't want */
123 if ((kind & ref_list->kinds) == 0)
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;
145 static void free_ref_list(struct ref_list *ref_list)
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;
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,
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)
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++) {
195 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
196 !strcmp(ref_list.list[i].name, head))
200 printf("%c %-*s", c, ref_list.maxwidth,
201 ref_list.list[i].name);
202 print_ref_info(ref_list.list[i].sha1, abbrev);
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)) {
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);
237 die("Failed to lock ref for update: %s.", strerror(errno));
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], logmsg[PATH_MAX*2 + 100];
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 snprintf(logmsg, sizeof(logmsg), "Branch: renamed %s to %s",
271 if (rename_ref(oldref, newref, logmsg))
272 die("Branch rename failed");
274 if (!strcmp(oldname, head) && create_symref("HEAD", newref))
275 die("Branch renamed to %s, but HEAD is not updated!", newname);
278 int cmd_branch(int argc, const char **argv, const char *prefix)
280 int delete = 0, force_delete = 0, force_create = 0;
281 int rename = 0, force_rename = 0;
282 int verbose = 0, abbrev = DEFAULT_ABBREV;
284 int kinds = REF_LOCAL_BRANCH;
288 git_config(git_default_config);
290 for (i = 1; i < argc; i++) {
291 const char *arg = argv[i];
295 if (!strcmp(arg, "--")) {
299 if (!strcmp(arg, "-d")) {
303 if (!strcmp(arg, "-D")) {
308 if (!strcmp(arg, "-f")) {
312 if (!strcmp(arg, "-m")) {
316 if (!strcmp(arg, "-M")) {
321 if (!strcmp(arg, "-r")) {
322 kinds = REF_REMOTE_BRANCH;
325 if (!strcmp(arg, "-a")) {
326 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
329 if (!strcmp(arg, "-l")) {
333 if (!strncmp(arg, "--abbrev=", 9)) {
334 abbrev = atoi(arg+9);
337 if (!strcmp(arg, "-v")) {
341 usage(builtin_branch_usage);
344 if ((delete && rename) || (delete && force_create) ||
345 (rename && force_create))
346 usage(builtin_branch_usage);
348 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
350 die("Failed to resolve HEAD as a valid ref.");
351 if (strncmp(head, "refs/heads/", 11))
352 die("HEAD not found below refs/heads!");
356 delete_branches(argc - i, argv + i, force_delete);
358 print_ref_list(kinds, verbose, abbrev);
359 else if (rename && (i == argc - 1))
360 rename_branch(head, argv[i], force_rename);
361 else if (rename && (i == argc - 2))
362 rename_branch(argv[i], argv[i + 1], force_rename);
363 else if (i == argc - 1)
364 create_branch(argv[i], head, force_create, reflog);
365 else if (i == argc - 2)
366 create_branch(argv[i], argv[i + 1], force_create, reflog);
368 usage(builtin_branch_usage);