@@ -6,11 +6,8 @@ namespace SourceGit.Commands
66{
77 public partial class QueryRepositoryStatus : Command
88 {
9- [ GeneratedRegex ( @"ahead\s(\d+)" ) ]
10- private static partial Regex REG_AHEAD ( ) ;
11-
12- [ GeneratedRegex ( @"behind\s(\d+)" ) ]
13- private static partial Regex REG_BEHIND ( ) ;
9+ [ GeneratedRegex ( @"\+(\d+) \-(\d+)" ) ]
10+ private static partial Regex REG_BRANCH_AB ( ) ;
1411
1512 public QueryRepositoryStatus ( string repo )
1613 {
@@ -20,23 +17,27 @@ public QueryRepositoryStatus(string repo)
2017
2118 public async Task < Models . RepositoryStatus > GetResultAsync ( )
2219 {
23- Args = "branch -l -v --format= \" %(refname:short)%00%(HEAD)%00%(upstream:track,nobracket) \" " ;
20+ Args = "status --porcelain=v2 -b " ;
2421 var rs = await ReadToEndAsync ( ) . ConfigureAwait ( false ) ;
2522 if ( ! rs . IsSuccess )
2623 return null ;
2724
2825 var status = new Models . RepositoryStatus ( ) ;
2926 var lines = rs . StdOut . Split ( [ '\r ' , '\n ' ] , StringSplitOptions . RemoveEmptyEntries ) ;
30- foreach ( var line in lines )
31- {
32- var parts = line . Split ( '\0 ' ) ;
33- if ( parts . Length != 3 || ! parts [ 1 ] . Equals ( "*" , StringComparison . Ordinal ) )
34- continue ;
27+ var count = lines . Length ;
28+ if ( count < 2 )
29+ return null ;
3530
36- status . CurrentBranch = parts [ 0 ] ;
37- if ( ! string . IsNullOrEmpty ( parts [ 2 ] ) )
38- ParseTrackStatus ( status , parts [ 2 ] ) ;
39- }
31+ var sha1 = lines [ 0 ] . Substring ( 13 ) . Trim ( ) ; // Remove "# branch.oid " prefix
32+ var head = lines [ 1 ] . Substring ( 14 ) . Trim ( ) ; // Remove "# branch.head " prefix
33+
34+ if ( head . Equals ( "(detached)" , StringComparison . Ordinal ) )
35+ status . CurrentBranch = sha1 . Length > 10 ? $ "({ sha1 . Substring ( 0 , 10 ) } )" : "-" ;
36+ else
37+ status . CurrentBranch = head ;
38+
39+ if ( count == 4 && lines [ 3 ] . StartsWith ( "# branch.ab " , StringComparison . Ordinal ) )
40+ ParseTrackStatus ( status , lines [ 3 ] . Substring ( 12 ) . Trim ( ) ) ;
4041
4142 status . LocalChanges = await new CountLocalChanges ( WorkingDirectory , true ) { RaiseError = false }
4243 . GetResultAsync ( )
@@ -47,13 +48,12 @@ public QueryRepositoryStatus(string repo)
4748
4849 private void ParseTrackStatus ( Models . RepositoryStatus status , string input )
4950 {
50- var aheadMatch = REG_AHEAD ( ) . Match ( input ) ;
51- if ( aheadMatch . Success )
52- status . Ahead = int . Parse ( aheadMatch . Groups [ 1 ] . Value ) ;
53-
54- var behindMatch = REG_BEHIND ( ) . Match ( input ) ;
55- if ( behindMatch . Success )
56- status . Behind = int . Parse ( behindMatch . Groups [ 1 ] . Value ) ;
51+ var match = REG_BRANCH_AB ( ) . Match ( input ) ;
52+ if ( match . Success )
53+ {
54+ status . Ahead = int . Parse ( match . Groups [ 1 ] . Value ) ;
55+ status . Behind = int . Parse ( match . Groups [ 2 ] . Value ) ;
56+ }
5757 }
5858 }
5959}
0 commit comments