@@ -275,7 +275,7 @@ static void print_ref_item(struct ref_item *item, int maxwidth, int verbose,
275
275
}
276
276
}
277
277
278
- static void print_ref_list (int kinds , int verbose , int abbrev )
278
+ static void print_ref_list (int kinds , int detached , int verbose , int abbrev )
279
279
{
280
280
int i ;
281
281
struct ref_list ref_list ;
@@ -286,8 +286,20 @@ static void print_ref_list(int kinds, int verbose, int abbrev)
286
286
287
287
qsort (ref_list .list , ref_list .index , sizeof (struct ref_item ), ref_cmp );
288
288
289
+ detached = (detached && (kinds & REF_LOCAL_BRANCH ));
290
+ if (detached ) {
291
+ struct ref_item item ;
292
+ item .name = "(no branch)" ;
293
+ item .kind = REF_LOCAL_BRANCH ;
294
+ hashcpy (item .sha1 , head_sha1 );
295
+ if (strlen (item .name ) > ref_list .maxwidth )
296
+ ref_list .maxwidth = strlen (item .name );
297
+ print_ref_item (& item , ref_list .maxwidth , verbose , abbrev , 1 );
298
+ }
299
+
289
300
for (i = 0 ; i < ref_list .index ; i ++ ) {
290
- int current = (ref_list .list [i ].kind == REF_LOCAL_BRANCH ) &&
301
+ int current = !detached &&
302
+ (ref_list .list [i ].kind == REF_LOCAL_BRANCH ) &&
291
303
!strcmp (ref_list .list [i ].name , head );
292
304
print_ref_item (& ref_list .list [i ], ref_list .maxwidth , verbose ,
293
305
abbrev , current );
@@ -367,7 +379,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
367
379
{
368
380
int delete = 0 , force_delete = 0 , force_create = 0 ;
369
381
int rename = 0 , force_rename = 0 ;
370
- int verbose = 0 , abbrev = DEFAULT_ABBREV ;
382
+ int verbose = 0 , abbrev = DEFAULT_ABBREV , detached = 0 ;
371
383
int reflog = 0 ;
372
384
int kinds = REF_LOCAL_BRANCH ;
373
385
int i ;
@@ -444,14 +456,19 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
444
456
head = xstrdup (resolve_ref ("HEAD" , head_sha1 , 0 , NULL ));
445
457
if (!head )
446
458
die ("Failed to resolve HEAD as a valid ref." );
447
- if (strncmp (head , "refs/heads/" , 11 ))
448
- die ("HEAD not found below refs/heads!" );
449
- head += 11 ;
459
+ if (!strcmp (head , "HEAD" )) {
460
+ detached = 1 ;
461
+ }
462
+ else {
463
+ if (strncmp (head , "refs/heads/" , 11 ))
464
+ die ("HEAD not found below refs/heads!" );
465
+ head += 11 ;
466
+ }
450
467
451
468
if (delete )
452
469
return delete_branches (argc - i , argv + i , force_delete , kinds );
453
470
else if (i == argc )
454
- print_ref_list (kinds , verbose , abbrev );
471
+ print_ref_list (kinds , detached , verbose , abbrev );
455
472
else if (rename && (i == argc - 1 ))
456
473
rename_branch (head , argv [i ], force_rename );
457
474
else if (rename && (i == argc - 2 ))
0 commit comments