@@ -129,45 +129,43 @@ public void SetBranchRef(string branch, string reference)
129
129
RunCommand ( $ "branch -f { branch } { reference } ") ;
130
130
}
131
131
132
- void RunCommand ( string command )
132
+ void RunCommand ( string arguments )
133
133
{
134
- foreach ( var line in GetCommandOutput ( command , true ) )
134
+ foreach ( var line in GetCommandOutput ( arguments , true ) )
135
135
{
136
136
}
137
137
}
138
138
139
- IEnumerable < string > GetCommandOutput ( string command , bool printOutput = false )
139
+ IEnumerable < string > GetCommandOutput ( string arguments , bool printOutput = false )
140
140
{
141
- var args = $ "--no-pager { command } ";
141
+ arguments = $ "--no-pager { arguments } ";
142
142
if ( printOutput )
143
- Console . WriteLine ( $ " > git { args } ") ;
143
+ Console . WriteLine ( $ " > git { arguments } ") ;
144
+ var lines = new List < string > ( ) ;
144
145
var git = new Process ( ) ;
145
146
git . StartInfo . WorkingDirectory = GitPath ;
146
147
git . StartInfo . FileName = "git" ;
147
- git . StartInfo . Arguments = args ;
148
+ git . StartInfo . Arguments = arguments ;
148
149
git . StartInfo . UseShellExecute = false ;
149
150
git . StartInfo . RedirectStandardOutput = true ;
150
151
git . StartInfo . RedirectStandardError = true ;
151
152
git . StartInfo . StandardOutputEncoding = Encoding . UTF8 ;
152
153
git . StartInfo . StandardErrorEncoding = Encoding . UTF8 ;
153
- git . ErrorDataReceived += ( sender , e ) =>
154
- {
155
- if ( e . Data ? . Length > 0 )
156
- Console . Error . WriteLine ( $ " ! { e . Data } ") ;
157
- } ;
154
+ git . OutputDataReceived += ( sender , e ) => lines . Add ( $ " < { e . Data } ") ;
155
+ git . ErrorDataReceived += ( sender , e ) => lines . Add ( $ " ! { e . Data } ") ;
158
156
git . Start ( ) ;
157
+ git . BeginOutputReadLine ( ) ;
159
158
git . BeginErrorReadLine ( ) ;
160
- while ( ! git . StandardOutput . EndOfStream )
159
+ git . WaitForExit ( ) ;
160
+ foreach ( var line in lines )
161
161
{
162
- if ( printOutput )
163
- Console . WriteLine ( $ " < { git . StandardOutput . ReadLine ( ) } ") ;
164
- else
165
- yield return git . StandardOutput . ReadLine ( ) ;
162
+ if ( printOutput && line . Length > 4 )
163
+ Console . WriteLine ( line ) ;
164
+ yield return line [ 4 ..] ;
166
165
}
167
- git . WaitForExit ( ) ;
168
166
if ( git . ExitCode != 0 )
169
167
{
170
- throw new ApplicationException ( $ "git { command } failed: { git . ExitCode } ") ;
168
+ throw new ApplicationException ( $ "git { arguments } failed: { git . ExitCode } ") ;
171
169
}
172
170
}
173
171
}
0 commit comments