@@ -154,10 +154,37 @@ static int branch_merged(int kind, const char *name,
154154 return merged ;
155155}
156156
157+ static int check_branch_commit (const char * branchname , const char * refname ,
158+ unsigned char * sha1 , struct commit * head_rev ,
159+ int kinds , int force )
160+ {
161+ struct commit * rev = lookup_commit_reference (sha1 );
162+ if (!rev ) {
163+ error (_ ("Couldn't look up commit object for '%s'" ), refname );
164+ return -1 ;
165+ }
166+ if (!force && !branch_merged (kinds , branchname , rev , head_rev )) {
167+ error (_ ("The branch '%s' is not fully merged.\n"
168+ "If you are sure you want to delete it, "
169+ "run 'git branch -D %s'." ), branchname , branchname );
170+ return -1 ;
171+ }
172+ return 0 ;
173+ }
174+
175+ static void delete_branch_config (const char * branchname )
176+ {
177+ struct strbuf buf = STRBUF_INIT ;
178+ strbuf_addf (& buf , "branch.%s" , branchname );
179+ if (git_config_rename_section (buf .buf , NULL ) < 0 )
180+ warning (_ ("Update of config-file failed" ));
181+ strbuf_release (& buf );
182+ }
183+
157184static int delete_branches (int argc , const char * * argv , int force , int kinds ,
158185 int quiet )
159186{
160- struct commit * rev , * head_rev = NULL ;
187+ struct commit * head_rev = NULL ;
161188 unsigned char sha1 [20 ];
162189 char * name = NULL ;
163190 const char * fmt ;
@@ -187,6 +214,9 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
187214 die (_ ("Couldn't look up commit object for HEAD" ));
188215 }
189216 for (i = 0 ; i < argc ; i ++ , strbuf_release (& bname )) {
217+ const char * target ;
218+ int flags = 0 ;
219+
190220 strbuf_branchname (& bname , argv [i ]);
191221 if (kinds == REF_LOCAL_BRANCH && !strcmp (head , bname .buf )) {
192222 error (_ ("Cannot delete the branch '%s' "
@@ -198,48 +228,41 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
198228 free (name );
199229
200230 name = mkpathdup (fmt , bname .buf );
201- if (read_ref (name , sha1 )) {
231+ target = resolve_ref_unsafe (name , sha1 , 0 , & flags );
232+ if (!target ||
233+ (!(flags & REF_ISSYMREF ) && is_null_sha1 (sha1 ))) {
202234 error (remote_branch
203235 ? _ ("remote branch '%s' not found." )
204236 : _ ("branch '%s' not found." ), bname .buf );
205237 ret = 1 ;
206238 continue ;
207239 }
208240
209- rev = lookup_commit_reference (sha1 );
210- if (!rev ) {
211- error (_ ("Couldn't look up commit object for '%s'" ), name );
212- ret = 1 ;
213- continue ;
214- }
215-
216- if (!force && !branch_merged (kinds , bname .buf , rev , head_rev )) {
217- error (_ ("The branch '%s' is not fully merged.\n"
218- "If you are sure you want to delete it, "
219- "run 'git branch -D %s'." ), bname .buf , bname .buf );
241+ if (!(flags & REF_ISSYMREF ) &&
242+ check_branch_commit (bname .buf , name , sha1 , head_rev , kinds ,
243+ force )) {
220244 ret = 1 ;
221245 continue ;
222246 }
223247
224- if (delete_ref (name , sha1 , 0 )) {
248+ if (delete_ref (name , sha1 , REF_NODEREF )) {
225249 error (remote_branch
226250 ? _ ("Error deleting remote branch '%s'" )
227251 : _ ("Error deleting branch '%s'" ),
228252 bname .buf );
229253 ret = 1 ;
230- } else {
231- struct strbuf buf = STRBUF_INIT ;
232- if (!quiet )
233- printf (remote_branch
234- ? _ ("Deleted remote branch %s (was %s).\n" )
235- : _ ("Deleted branch %s (was %s).\n" ),
236- bname .buf ,
237- find_unique_abbrev (sha1 , DEFAULT_ABBREV ));
238- strbuf_addf (& buf , "branch.%s" , bname .buf );
239- if (git_config_rename_section (buf .buf , NULL ) < 0 )
240- warning (_ ("Update of config-file failed" ));
241- strbuf_release (& buf );
254+ continue ;
255+ }
256+ if (!quiet ) {
257+ printf (remote_branch
258+ ? _ ("Deleted remote branch %s (was %s).\n" )
259+ : _ ("Deleted branch %s (was %s).\n" ),
260+ bname .buf ,
261+ (flags & REF_ISSYMREF )
262+ ? target
263+ : find_unique_abbrev (sha1 , DEFAULT_ABBREV ));
242264 }
265+ delete_branch_config (bname .buf );
243266 }
244267
245268 free (name );
0 commit comments