Add -v and --abbrev options to git-branch
[git/jnareb-git.git] / builtin-branch.c
blob69b7b55d86ffb70243d69a5b61de839bff8ccf60
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;
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 #define REF_UNKNOWN_TYPE 0x00
83 #define REF_LOCAL_BRANCH 0x01
84 #define REF_REMOTE_BRANCH 0x02
85 #define REF_TAG 0x04
87 struct ref_item {
88 char *name;
89 unsigned int kind;
90 unsigned char sha1[20];
93 struct ref_list {
94 int index, alloc, maxwidth;
95 struct ref_item *list;
96 int kinds;
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;
104 int len;
106 /* Detect kind */
107 if (!strncmp(refname, "refs/heads/", 11)) {
108 kind = REF_LOCAL_BRANCH;
109 refname += 11;
110 } else if (!strncmp(refname, "refs/remotes/", 13)) {
111 kind = REF_REMOTE_BRANCH;
112 refname += 13;
113 } else if (!strncmp(refname, "refs/tags/", 10)) {
114 kind = REF_TAG;
115 refname += 10;
118 /* Don't add types the caller doesn't want */
119 if ((kind & ref_list->kinds) == 0)
120 return 0;
122 /* Resize buffer */
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;
138 return 0;
141 static void free_ref_list(struct ref_list *ref_list)
143 int i;
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;
163 char subject[256];
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,
170 NULL, NULL, 0);
171 else
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)
179 int i;
180 char c;
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++) {
190 c = ' ';
191 if (ref_list.list[i].kind == REF_LOCAL_BRANCH &&
192 !strcmp(ref_list.list[i].name, head))
193 c = '*';
195 if (verbose) {
196 printf("%c %-*s", c, ref_list.maxwidth,
197 ref_list.list[i].name);
198 print_ref_info(ref_list.list[i].sha1, abbrev);
200 else
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)) {
220 if (!force)
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);
232 if (!lock)
233 die("Failed to lock ref for update: %s.", strerror(errno));
235 if (reflog) {
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;
248 int reflog = 0;
249 int kinds = REF_LOCAL_BRANCH;
250 int i;
252 git_config(git_default_config);
254 for (i = 1; i < argc; i++) {
255 const char *arg = argv[i];
257 if (arg[0] != '-')
258 break;
259 if (!strcmp(arg, "--")) {
260 i++;
261 break;
263 if (!strcmp(arg, "-d")) {
264 delete = 1;
265 continue;
267 if (!strcmp(arg, "-D")) {
268 delete = 1;
269 force_delete = 1;
270 continue;
272 if (!strcmp(arg, "-f")) {
273 force_create = 1;
274 continue;
276 if (!strcmp(arg, "-r")) {
277 kinds = REF_REMOTE_BRANCH;
278 continue;
280 if (!strcmp(arg, "-a")) {
281 kinds = REF_REMOTE_BRANCH | REF_LOCAL_BRANCH;
282 continue;
284 if (!strcmp(arg, "-l")) {
285 reflog = 1;
286 continue;
288 if (!strncmp(arg, "--abbrev=", 9)) {
289 abbrev = atoi(arg+9);
290 continue;
292 if (!strcmp(arg, "-v")) {
293 verbose = 1;
294 continue;
296 usage(builtin_branch_usage);
299 head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
300 if (!head)
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!");
304 head += 11;
306 if (delete)
307 delete_branches(argc - i, argv + i, force_delete);
308 else if (i == argc)
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);
314 else
315 usage(builtin_branch_usage);
317 return 0;