11using System . Collections . Generic ;
2+ using System . Text ;
23using System . Text . RegularExpressions ;
34
45namespace SourceGit . Commands
@@ -9,6 +10,8 @@ public partial class QuerySubmodules : Command
910 private static partial Regex REG_FORMAT1 ( ) ;
1011 [ GeneratedRegex ( @"^[\-\+ ][0-9a-f]+\s(.*)$" ) ]
1112 private static partial Regex REG_FORMAT2 ( ) ;
13+ [ GeneratedRegex ( @"^\s?[\w\?]{1,4}\s+(.+)$" ) ]
14+ private static partial Regex REG_FORMAT_STATUS ( ) ;
1215
1316 public QuerySubmodules ( string repo )
1417 {
@@ -17,28 +20,59 @@ public QuerySubmodules(string repo)
1720 Args = "submodule status" ;
1821 }
1922
20- public List < string > Result ( )
23+ public List < Models . Submodule > Result ( )
2124 {
22- Exec ( ) ;
23- return _submodules ;
24- }
25+ var submodules = new List < Models . Submodule > ( ) ;
26+ var rs = ReadToEnd ( ) ;
27+ if ( ! rs . IsSuccess )
28+ return submodules ;
2529
26- protected override void OnReadline ( string line )
27- {
28- var match = REG_FORMAT1 ( ) . Match ( line ) ;
29- if ( match . Success )
30+ var builder = new StringBuilder ( ) ;
31+ var lines = rs . StdOut . Split ( '\n ' , System . StringSplitOptions . RemoveEmptyEntries ) ;
32+ foreach ( var line in lines )
3033 {
31- _submodules . Add ( match . Groups [ 1 ] . Value ) ;
32- return ;
34+ var match = REG_FORMAT1 ( ) . Match ( line ) ;
35+ if ( match . Success )
36+ {
37+ var path = match . Groups [ 1 ] . Value ;
38+ builder . Append ( $ "\" { path } \" ") ;
39+ submodules . Add ( new Models . Submodule ( ) { Path = path } ) ;
40+ continue ;
41+ }
42+
43+ match = REG_FORMAT2 ( ) . Match ( line ) ;
44+ if ( match . Success )
45+ {
46+ var path = match . Groups [ 1 ] . Value ;
47+ builder . Append ( $ "\" { path } \" ") ;
48+ submodules . Add ( new Models . Submodule ( ) { Path = path } ) ;
49+ }
3350 }
3451
35- match = REG_FORMAT2 ( ) . Match ( line ) ;
36- if ( match . Success )
52+ if ( submodules . Count > 0 )
3753 {
38- _submodules . Add ( match . Groups [ 1 ] . Value ) ;
54+ Args = $ "status -uno --porcelain -- { builder } ";
55+ rs = ReadToEnd ( ) ;
56+ if ( ! rs . IsSuccess )
57+ return submodules ;
58+
59+ var dirty = new HashSet < string > ( ) ;
60+ lines = rs . StdOut . Split ( '\n ' , System . StringSplitOptions . RemoveEmptyEntries ) ;
61+ foreach ( var line in lines )
62+ {
63+ var match = REG_FORMAT_STATUS ( ) . Match ( line ) ;
64+ if ( match . Success )
65+ {
66+ var path = match . Groups [ 1 ] . Value ;
67+ dirty . Add ( path ) ;
68+ }
69+ }
70+
71+ foreach ( var submodule in submodules )
72+ submodule . IsDirty = dirty . Contains ( submodule . Path ) ;
3973 }
40- }
4174
42- private readonly List < string > _submodules = new List < string > ( ) ;
75+ return submodules ;
76+ }
4377 }
4478}
0 commit comments