@@ -154,10 +154,37 @@ static int branch_merged(int kind, const char *name,
154
154
return merged ;
155
155
}
156
156
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
+
157
184
static int delete_branches (int argc , const char * * argv , int force , int kinds ,
158
185
int quiet )
159
186
{
160
- struct commit * rev , * head_rev = NULL ;
187
+ struct commit * head_rev = NULL ;
161
188
unsigned char sha1 [20 ];
162
189
char * name = NULL ;
163
190
const char * fmt ;
@@ -187,6 +214,9 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
187
214
die (_ ("Couldn't look up commit object for HEAD" ));
188
215
}
189
216
for (i = 0 ; i < argc ; i ++ , strbuf_release (& bname )) {
217
+ const char * target ;
218
+ int flags = 0 ;
219
+
190
220
strbuf_branchname (& bname , argv [i ]);
191
221
if (kinds == REF_LOCAL_BRANCH && !strcmp (head , bname .buf )) {
192
222
error (_ ("Cannot delete the branch '%s' "
@@ -198,48 +228,41 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
198
228
free (name );
199
229
200
230
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 ))) {
202
234
error (remote_branch
203
235
? _ ("remote branch '%s' not found." )
204
236
: _ ("branch '%s' not found." ), bname .buf );
205
237
ret = 1 ;
206
238
continue ;
207
239
}
208
240
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 )) {
220
244
ret = 1 ;
221
245
continue ;
222
246
}
223
247
224
- if (delete_ref (name , sha1 , 0 )) {
248
+ if (delete_ref (name , sha1 , REF_NODEREF )) {
225
249
error (remote_branch
226
250
? _ ("Error deleting remote branch '%s'" )
227
251
: _ ("Error deleting branch '%s'" ),
228
252
bname .buf );
229
253
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 ));
242
264
}
265
+ delete_branch_config (bname .buf );
243
266
}
244
267
245
268
free (name );
0 commit comments