@@ -9,7 +9,7 @@ namespace Open_Rails_Code_Bot.Git
99{
1010 public class Project
1111 {
12- string GitPath ;
12+ readonly string GitPath ;
1313
1414 public Project ( string gitPath )
1515 {
@@ -18,13 +18,11 @@ public Project(string gitPath)
1818
1919 public void Init ( string repository )
2020 {
21- if ( ! Directory . Exists ( GitPath ) )
22- {
23- Directory . CreateDirectory ( GitPath ) ;
24- RunCommand ( $ "init") ;
25- RunCommand ( $ "remote add origin { repository } ") ;
26- RunCommand ( $ "config remote.origin.fetch +refs/*:refs/*") ;
27- }
21+ if ( ! Directory . Exists ( GitPath ) ) Directory . CreateDirectory ( GitPath ) ;
22+ if ( ! File . Exists ( Path . Join ( GitPath , ".git" , "config" ) ) ) RunCommand ( $ "init") ;
23+
24+ RunCommand ( $ "config remove-section remote.origin") ;
25+ RunCommand ( $ "remote add origin --mirror=fetch { repository } ") ;
2826 }
2927
3028 public void Fetch ( )
@@ -52,6 +50,16 @@ public void Clean()
5250 RunCommand ( "clean --force -d -x" ) ;
5351 }
5452
53+ public void DiffStat ( string reference1 , string reference2 )
54+ {
55+ foreach ( var line in GetCommandOutput ( $ "diff --numstat { reference1 } ...{ reference2 } ") )
56+ {
57+ var parts = line . Split ( '\t ' ) ;
58+ if ( parts . Length == 3 && int . TryParse ( parts [ 0 ] , out var added ) && int . TryParse ( parts [ 1 ] , out var deleted ) )
59+ Console . WriteLine ( " {2} {0:+#,##0} {1:-#,##0}" , added , deleted , parts [ 2 ] ) ;
60+ }
61+ }
62+
5563 public void Merge ( string reference )
5664 {
5765 RunCommand ( $ "merge --quiet --no-edit --no-ff -Xignore-space-change { reference } ") ;
@@ -129,45 +137,43 @@ public void SetBranchRef(string branch, string reference)
129137 RunCommand ( $ "branch -f { branch } { reference } ") ;
130138 }
131139
132- void RunCommand ( string command )
140+ void RunCommand ( string arguments )
133141 {
134- foreach ( var line in GetCommandOutput ( command , true ) )
142+ foreach ( var line in GetCommandOutput ( arguments , true ) )
135143 {
136144 }
137145 }
138146
139- IEnumerable < string > GetCommandOutput ( string command , bool printOutput = false )
147+ IEnumerable < string > GetCommandOutput ( string arguments , bool printOutput = false )
140148 {
141- var args = $ "--no-pager { command } ";
149+ arguments = $ "--no-pager { arguments } ";
142150 if ( printOutput )
143- Console . WriteLine ( $ " > git { args } ") ;
151+ Console . WriteLine ( $ " > git { arguments } ") ;
152+ var lines = new List < string > ( ) ;
144153 var git = new Process ( ) ;
145154 git . StartInfo . WorkingDirectory = GitPath ;
146155 git . StartInfo . FileName = "git" ;
147- git . StartInfo . Arguments = args ;
156+ git . StartInfo . Arguments = arguments ;
148157 git . StartInfo . UseShellExecute = false ;
149158 git . StartInfo . RedirectStandardOutput = true ;
150159 git . StartInfo . RedirectStandardError = true ;
151160 git . StartInfo . StandardOutputEncoding = Encoding . UTF8 ;
152161 git . StartInfo . StandardErrorEncoding = Encoding . UTF8 ;
153- git . ErrorDataReceived += ( sender , e ) =>
154- {
155- if ( e . Data ? . Length > 0 )
156- Console . Error . WriteLine ( $ " ! { e . Data } ") ;
157- } ;
162+ git . OutputDataReceived += ( sender , e ) => lines . Add ( $ " < { e . Data } ") ;
163+ git . ErrorDataReceived += ( sender , e ) => lines . Add ( $ " ! { e . Data } ") ;
158164 git . Start ( ) ;
165+ git . BeginOutputReadLine ( ) ;
159166 git . BeginErrorReadLine ( ) ;
160- while ( ! git . StandardOutput . EndOfStream )
167+ git . WaitForExit ( ) ;
168+ foreach ( var line in lines )
161169 {
162- if ( printOutput )
163- Console . WriteLine ( $ " < { git . StandardOutput . ReadLine ( ) } ") ;
164- else
165- yield return git . StandardOutput . ReadLine ( ) ;
170+ if ( printOutput && line . Length > 4 )
171+ Console . WriteLine ( line ) ;
172+ yield return line [ 4 ..] ;
166173 }
167- git . WaitForExit ( ) ;
168174 if ( git . ExitCode != 0 )
169175 {
170- throw new ApplicationException ( $ "git { command } failed: { git . ExitCode } ") ;
176+ throw new ApplicationException ( $ "git { arguments } failed: { git . ExitCode } ") ;
171177 }
172178 }
173179 }
0 commit comments