1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using System . Text ;
34using System . Text . RegularExpressions ;
45
56namespace SourceGit . Commands
67{
78 public partial class QuerySubmodules : Command
89 {
9- [ GeneratedRegex ( @"^[U\-\+ ][0-9a-f]+\s(.*)\s\(.*\)$" ) ]
10- private static partial Regex REG_FORMAT1 ( ) ;
11- [ GeneratedRegex ( @"^[U\-\+ ][0-9a-f]+\s(.*)$" ) ]
12- private static partial Regex REG_FORMAT2 ( ) ;
13- [ GeneratedRegex ( @"^\s?[\w\?]{1,4}\s+(.+)$" ) ]
10+ [ GeneratedRegex ( @"^([U\-\+ ])([0-9a-f]+)\s(.*?)(\s\(.*\))?$" ) ]
1411 private static partial Regex REG_FORMAT_STATUS ( ) ;
12+ [ GeneratedRegex ( @"^\s?[\w\?]{1,4}\s+(.+)$" ) ]
13+ private static partial Regex REG_FORMAT_DIRTY ( ) ;
1514
1615 public QuerySubmodules ( string repo )
1716 {
@@ -25,49 +24,65 @@ public QuerySubmodules(string repo)
2524 var submodules = new List < Models . Submodule > ( ) ;
2625 var rs = ReadToEnd ( ) ;
2726
28- var builder = new StringBuilder ( ) ;
29- var lines = rs . StdOut . Split ( [ ' \r ' , ' \n ' ] , System . StringSplitOptions . RemoveEmptyEntries ) ;
27+ var lines = rs . StdOut . Split ( [ ' \r ' , ' \n ' ] , StringSplitOptions . RemoveEmptyEntries ) ;
28+ var needCheckLocalChanges = new Dictionary < string , Models . Submodule > ( ) ;
3029 foreach ( var line in lines )
3130 {
32- var match = REG_FORMAT1 ( ) . Match ( line ) ;
31+ var match = REG_FORMAT_STATUS ( ) . Match ( line ) ;
3332 if ( match . Success )
3433 {
35- var path = match . Groups [ 1 ] . Value ;
36- builder . Append ( $ "\" { path } \" ") ;
37- submodules . Add ( new Models . Submodule ( ) { Path = path } ) ;
38- continue ;
39- }
34+ var stat = match . Groups [ 1 ] . Value ;
35+ var sha = match . Groups [ 2 ] . Value ;
36+ var path = match . Groups [ 3 ] . Value ;
4037
41- match = REG_FORMAT2 ( ) . Match ( line ) ;
42- if ( match . Success )
43- {
44- var path = match . Groups [ 1 ] . Value ;
45- builder . Append ( $ "\" { path } \" ") ;
46- submodules . Add ( new Models . Submodule ( ) { Path = path } ) ;
38+ var module = new Models . Submodule ( ) { Path = path , SHA = sha } ;
39+ switch ( stat [ 0 ] )
40+ {
41+ case '-' :
42+ module . Status = Models . SubmoduleStatus . NotInited ;
43+ break ;
44+ case '+' :
45+ module . Status = Models . SubmoduleStatus . RevisionChanged ;
46+ break ;
47+ case 'U' :
48+ module . Status = Models . SubmoduleStatus . Unmerged ;
49+ break ;
50+ default :
51+ module . Status = Models . SubmoduleStatus . Normal ;
52+ needCheckLocalChanges . Add ( path , module ) ;
53+ break ;
54+ }
55+
56+ submodules . Add ( module ) ;
4757 }
4858 }
4959
50- if ( submodules . Count > 0 )
60+ if ( needCheckLocalChanges . Count > 0 )
5161 {
62+ var builder = new StringBuilder ( ) ;
63+ foreach ( var kv in needCheckLocalChanges )
64+ {
65+ builder . Append ( '"' ) ;
66+ builder . Append ( kv . Key ) ;
67+ builder . Append ( "\" " ) ;
68+ }
69+
5270 Args = $ "--no-optional-locks status -uno --porcelain -- { builder } ";
5371 rs = ReadToEnd ( ) ;
5472 if ( ! rs . IsSuccess )
5573 return submodules ;
5674
57- var dirty = new HashSet < string > ( ) ;
58- lines = rs . StdOut . Split ( [ '\r ' , '\n ' ] , System . StringSplitOptions . RemoveEmptyEntries ) ;
75+ lines = rs . StdOut . Split ( [ '\r ' , '\n ' ] , StringSplitOptions . RemoveEmptyEntries ) ;
5976 foreach ( var line in lines )
6077 {
61- var match = REG_FORMAT_STATUS ( ) . Match ( line ) ;
78+ var match = REG_FORMAT_DIRTY ( ) . Match ( line ) ;
6279 if ( match . Success )
6380 {
6481 var path = match . Groups [ 1 ] . Value ;
65- dirty . Add ( path ) ;
82+ if ( needCheckLocalChanges . TryGetValue ( path , out var m ) )
83+ m . Status = Models . SubmoduleStatus . Modified ;
6684 }
6785 }
68-
69- foreach ( var submodule in submodules )
70- submodule . IsDirty = dirty . Contains ( submodule . Path ) ;
7186 }
7287
7388 return submodules ;
0 commit comments