@@ -9,7 +9,7 @@ namespace Open_Rails_Code_Bot.Git
9
9
{
10
10
public class Project
11
11
{
12
- string GitPath ;
12
+ readonly string GitPath ;
13
13
14
14
public Project ( string gitPath )
15
15
{
@@ -18,13 +18,11 @@ public Project(string gitPath)
18
18
19
19
public void Init ( string repository )
20
20
{
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 } ") ;
28
26
}
29
27
30
28
public void Fetch ( )
@@ -52,6 +50,16 @@ public void Clean()
52
50
RunCommand ( "clean --force -d -x" ) ;
53
51
}
54
52
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
+
55
63
public void Merge ( string reference )
56
64
{
57
65
RunCommand ( $ "merge --quiet --no-edit --no-ff -Xignore-space-change { reference } ") ;
@@ -129,45 +137,43 @@ public void SetBranchRef(string branch, string reference)
129
137
RunCommand ( $ "branch -f { branch } { reference } ") ;
130
138
}
131
139
132
- void RunCommand ( string command )
140
+ void RunCommand ( string arguments )
133
141
{
134
- foreach ( var line in GetCommandOutput ( command , true ) )
142
+ foreach ( var line in GetCommandOutput ( arguments , true ) )
135
143
{
136
144
}
137
145
}
138
146
139
- IEnumerable < string > GetCommandOutput ( string command , bool printOutput = false )
147
+ IEnumerable < string > GetCommandOutput ( string arguments , bool printOutput = false )
140
148
{
141
- var args = $ "--no-pager { command } ";
149
+ arguments = $ "--no-pager { arguments } ";
142
150
if ( printOutput )
143
- Console . WriteLine ( $ " > git { args } ") ;
151
+ Console . WriteLine ( $ " > git { arguments } ") ;
152
+ var lines = new List < string > ( ) ;
144
153
var git = new Process ( ) ;
145
154
git . StartInfo . WorkingDirectory = GitPath ;
146
155
git . StartInfo . FileName = "git" ;
147
- git . StartInfo . Arguments = args ;
156
+ git . StartInfo . Arguments = arguments ;
148
157
git . StartInfo . UseShellExecute = false ;
149
158
git . StartInfo . RedirectStandardOutput = true ;
150
159
git . StartInfo . RedirectStandardError = true ;
151
160
git . StartInfo . StandardOutputEncoding = Encoding . UTF8 ;
152
161
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 } ") ;
158
164
git . Start ( ) ;
165
+ git . BeginOutputReadLine ( ) ;
159
166
git . BeginErrorReadLine ( ) ;
160
- while ( ! git . StandardOutput . EndOfStream )
167
+ git . WaitForExit ( ) ;
168
+ foreach ( var line in lines )
161
169
{
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 ..] ;
166
173
}
167
- git . WaitForExit ( ) ;
168
174
if ( git . ExitCode != 0 )
169
175
{
170
- throw new ApplicationException ( $ "git { command } failed: { git . ExitCode } ") ;
176
+ throw new ApplicationException ( $ "git { arguments } failed: { git . ExitCode } ") ;
171
177
}
172
178
}
173
179
}
0 commit comments