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>] | [-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;
42 unsigned char sha1[20];
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.
65 !in_merge_bases(sha1, rev, head_rev)) {
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",
73 if (delete_ref(name, sha1))
74 printf("Error deleting branch '%s'\n", argv[i]);
76 printf("Deleted branch %s.\n", argv[i]);
82 #define REF_UNKNOWN_TYPE 0x00
83 #define REF_LOCAL_BRANCH 0x01
84 #define REF_REMOTE_BRANCH 0x02
90 unsigned char sha1[20];
94 int index, alloc, maxwidth;
95 struct ref_item *list;
99 static int append_ref(const char *refname, const unsigned char *sha1, int flags, void *cb_data)
101 struct ref_list *ref_list = (struct ref_list*)(cb_data);
102 struct ref_item *newitem;
103 int kind = REF_UNKNOWN_TYPE;
107 if (!strncmp(refname, "refs/heads/", 11)) {
108 kind = REF_LOCAL_BRANCH;
110 } else if (!strncmp(refname, "refs/remotes/", 13)) {
111 kind = REF_REMOTE_BRANCH;
113 } else if (!strncmp(refname, "refs/tags/", 10)) {
118 /* Don't add types the caller doesn't want */
119 if ((kind & ref_list->kinds) == 0)
123 if (ref_list->index >= ref_list->alloc) {
124 ref_list->alloc = alloc_nr(ref_list->alloc);
125 ref_list->list = xrealloc(ref_list->list,
126 ref_list->alloc * sizeof(struct ref_item));
129 /* Record the new item */
130 newitem = &(ref_list->list[ref_list->index++]);
131 newitem->name = xstrdup(refname);
132 newitem->kind = kind;
133 hashcpy(newitem->sha1, sha1);
134 len = strlen(newitem->name);
135 if (len > ref_list->maxwidth)
136 ref_list->maxwidth = len;
141 static void free_ref_list(struct ref_list *ref_list)
145 for (i = 0; i < ref_list->index; i++)
146 free(ref_list->list[i].name);
147 free(ref_list->list);
150 static int ref_cmp(const void *r1, const void *r2)
152 struct ref_item *c1 = (struct ref_item *)(r1);
153 struct ref_item *c2 = (struct ref_item *)(r2);
155 if (c1->kind != c2->kind)
156 return c1->kind - c2->kind;
157 return strcmp(c1->name, c2->name);
160 static void print_ref_info(const unsigned char *sha1, int abbrev)
162 struct commit *commit;
166 commit = lookup_commit(sha1);
167 if (commit && !parse_commit(commit))
168 pretty_print_commit(CMIT_FMT_ONELINE, commit, ~0,
169 subject, sizeof(subject), 0,
172 strcpy(subject, " **** invalid ref ****");
174 printf(" %s %s\n", find_unique_abbrev(sha1, abbrev), subject);
177 static void print_ref_list(int kinds, int verbose, int abbrev)
181 struct ref_list ref_list;
183 memset(&ref_list, 0, sizeof(ref_list));
184 ref_list.kinds = kinds;
185 for_each_ref(append_ref, &ref_list);
187 qsort(ref_list.list, ref_list.index, sizeof(struct ref_item), ref_cmp);
189 for (i = 0; i < ref_list.index; i++) {
191 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
192 !strcmp(ref_list.list[i].name, head))
196 printf("%c %-*s", c, ref_list.maxwidth,
197 ref_list.list[i].name);
198 print_ref_info(ref_list.list[i].sha1, abbrev);
201 printf("%c %s\n", c, ref_list.list[i].name);
204 free_ref_list(&ref_list);
207 static void create_branch(const char *name, const char *start,
208 int force, int reflog)
210 struct ref_lock *lock;
211 struct commit *commit;
212 unsigned char sha1[20];
213 char ref[PATH_MAX], msg[PATH_MAX + 20];
215 snprintf(ref, sizeof ref, "refs/heads/%s", name);
216 if (check_ref_format(ref))
217 die("'%s' is not a valid branch name.", name);
219 if (resolve_ref(ref, sha1, 1, NULL)) {
221 die("A branch named '%s' already exists.", name);
222 else if (!strcmp(head, name))
223 die("Cannot force update the current branch.");
226 if (get_sha1(start, sha1) ||
227 (commit = lookup_commit_reference(sha1)) == NULL)
228 die("Not a valid branch point: '%s'.", start);
229 hashcpy(sha1, commit->object.sha1);
231 lock = lock_any_ref_for_update(ref, NULL);
233 die("Failed to lock ref for update: %s.", strerror(errno));
236 log_all_ref_updates = 1;
237 snprintf(msg, sizeof msg, "branch: Created from %s", start);
240 if (write_ref_sha1(lock, sha1, msg) < 0)
241 die("Failed to write ref: %s.", strerror(errno));
244 int cmd_branch(int argc, const char **argv, const char *prefix)
246 int delete = 0, force_delete = 0, force_create = 0;
247 int verbose = 0, abbrev = DEFAULT_ABBREV;
249 int kinds = REF_LOCAL_BRANCH;
252 git_config(git_default_config);
254 for (i = 1; i < argc; i++) {
255 const char *arg = argv[i];
259 if (!strcmp(arg, "--")) {
263 if (!strcmp(arg, "-d")) {
267 if (!strcmp(arg, "-D")) {
272 if (!strcmp(arg, "-f")) {
276 if (!strcmp(arg, "-r")) {
277 kinds = REF_REMOTE_BRANCH;
280 if (!strcmp(arg, "-a")) {
281 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
284 if (!strcmp(arg, "-l")) {
288 if (!strncmp(arg, "--abbrev=", 9)) {
289 abbrev = atoi(arg+9);
292 if (!strcmp(arg, "-v")) {
296 usage(builtin_branch_usage);
299 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
301 die("Failed to resolve HEAD as a valid ref.");
302 if (strncmp(head, "refs/heads/", 11))
303 die("HEAD not found below refs/heads!");
307 delete_branches(argc - i, argv + i, force_delete);
309 print_ref_list(kinds, verbose, abbrev);
310 else if (i == argc - 1)
311 create_branch(argv[i], head, force_create, reflog);
312 else if (i == argc - 2)
313 create_branch(argv[i], argv[i + 1], force_create, reflog);
315 usage(builtin_branch_usage);