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]";
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 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
,
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
);
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
)
109 for_each_remote_ref(append_ref
, NULL
);
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
++) {
117 if (!strcmp(ref_list
[i
], head
))
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
)) {
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
);
150 die("Failed to lock ref for update: %s.", strerror(errno
));
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;
167 git_config(git_default_config
);
169 for (i
= 1; i
< argc
; i
++) {
170 const char *arg
= argv
[i
];
174 if (!strcmp(arg
, "--")) {
178 if (!strcmp(arg
, "-d")) {
182 if (!strcmp(arg
, "-D")) {
187 if (!strcmp(arg
, "-f")) {
191 if (!strcmp(arg
, "-r")) {
195 if (!strcmp(arg
, "-l")) {
199 usage(builtin_branch_usage
);
202 head
= xstrdup(resolve_ref("HEAD", head_sha1
, 0, NULL
));
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!");
210 delete_branches(argc
- i
, argv
+ i
, force_delete
);
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
);
218 usage(builtin_branch_usage
);