11using System . Diagnostics ;
2+ using System . IO ;
23
34namespace GitContentSearch
45{
@@ -7,23 +8,29 @@ public class GitHelper : IGitHelper
78 private readonly IProcessWrapper _processWrapper ;
89 private readonly string ? _workingDirectory ;
910 private readonly bool _follow ;
11+ private readonly TextWriter _logWriter ;
1012
1113 public GitHelper ( IProcessWrapper processWrapper )
14+ : this ( processWrapper , null , false , Console . Out )
1215 {
13- _processWrapper = processWrapper ;
1416 }
1517
1618 public GitHelper ( IProcessWrapper processWrapper , string ? workingDirectory )
17- : this ( processWrapper , workingDirectory , false )
19+ : this ( processWrapper , workingDirectory , false , Console . Out )
1820 {
19-
2021 }
2122
2223 public GitHelper ( IProcessWrapper processWrapper , string ? workingDirectory , bool follow )
24+ : this ( processWrapper , workingDirectory , follow , Console . Out )
25+ {
26+ }
27+
28+ public GitHelper ( IProcessWrapper processWrapper , string ? workingDirectory , bool follow , TextWriter logWriter )
2329 {
2430 _processWrapper = processWrapper ;
2531 _workingDirectory = workingDirectory ;
2632 _follow = follow ;
33+ _logWriter = logWriter ;
2734 }
2835
2936 public string GetCommitTime ( string commitHash )
@@ -81,7 +88,7 @@ public List<Commit> GetGitCommits(string earliest, string latest, string filePat
8188 var result = RunGitCommand ( "log --pretty=format:%H -n 1" ) ;
8289 if ( result == null || result . ExitCode != 0 )
8390 {
84- Console . WriteLine ( $ "Error retrieving git commits: { result ? . StandardError } ") ;
91+ _logWriter . WriteLine ( $ "Error retrieving git commits: { result ? . StandardError } ") ;
8592 return null ;
8693 }
8794
@@ -95,7 +102,7 @@ private List<Commit> GetCommits(string filePath)
95102 var result = RunGitCommand ( arguments ) ;
96103 if ( result == null || result . ExitCode != 0 )
97104 {
98- Console . WriteLine ( $ "Error retrieving git commits: { result ? . StandardError } ") ;
105+ _logWriter . WriteLine ( $ "Error retrieving git commits: { result ? . StandardError } ") ;
99106 return new List < Commit > ( ) ;
100107 }
101108
@@ -112,7 +119,7 @@ private List<Commit> GetCommitsWithFollow(string filePath)
112119 var result = RunGitCommand ( arguments ) ;
113120 if ( result . ExitCode != 0 )
114121 {
115- Console . WriteLine ( $ "Error retrieving git commits: { result ? . StandardError } ") ;
122+ _logWriter . WriteLine ( $ "Error retrieving git commits: { result ? . StandardError } ") ;
116123 return new List < Commit > ( ) ;
117124 }
118125
@@ -141,9 +148,9 @@ private List<Commit> GetCommitsWithFollow(string filePath)
141148 if ( oldPath != currentFilePath )
142149 {
143150 var commitTime = GetCommitTime ( currentCommitHash ) ;
144- Console . WriteLine ( $ "File renamed in commit { currentCommitHash } at { commitTime . Trim ( ) } :") ;
145- Console . WriteLine ( $ " From: { oldPath } ") ;
146- Console . WriteLine ( $ " To: { currentFilePath } ") ;
151+ _logWriter . WriteLine ( $ "File renamed in commit { currentCommitHash } at { commitTime . Trim ( ) } :") ;
152+ _logWriter . WriteLine ( $ " From: { oldPath } ") ;
153+ _logWriter . WriteLine ( $ " To: { currentFilePath } ") ;
147154 }
148155 previousFilePath = currentFilePath ;
149156 commits . Add ( new Commit ( currentCommitHash , currentFilePath ) ) ;
@@ -158,8 +165,8 @@ private List<Commit> GetCommitsWithFollow(string filePath)
158165 if ( previousFilePath != currentFilePath )
159166 {
160167 var commitTime = GetCommitTime ( currentCommitHash ) ;
161- Console . WriteLine ( $ "File path changed in commit { currentCommitHash } at { commitTime . Trim ( ) } :") ;
162- Console . WriteLine ( $ " New path: { currentFilePath } ") ;
168+ _logWriter . WriteLine ( $ "File path changed in commit { currentCommitHash } at { commitTime . Trim ( ) } :") ;
169+ _logWriter . WriteLine ( $ " New path: { currentFilePath } ") ;
163170 previousFilePath = currentFilePath ;
164171 }
165172 commits . Add ( new Commit ( currentCommitHash , currentFilePath ) ) ;
@@ -191,7 +198,7 @@ private List<Commit> FilterCommitsByRange(List<Commit> commits, string earliest,
191198 startIndex = commits . FindIndex ( c => c . CommitHash == latest ) ;
192199 if ( startIndex == - 1 )
193200 {
194- Console . WriteLine ( $ "Latest commit { latest } not found.") ;
201+ _logWriter . WriteLine ( $ "Latest commit { latest } not found.") ;
195202 return new List < Commit > ( ) ;
196203 }
197204 }
@@ -202,15 +209,15 @@ private List<Commit> FilterCommitsByRange(List<Commit> commits, string earliest,
202209 endIndex = commits . FindIndex ( c => c . CommitHash == earliest ) ;
203210 if ( endIndex == - 1 )
204211 {
205- Console . WriteLine ( $ "Earliest commit { earliest } not found.") ;
212+ _logWriter . WriteLine ( $ "Earliest commit { earliest } not found.") ;
206213 return new List < Commit > ( ) ;
207214 }
208215 }
209216
210217 // If the latest commit appears after the earliest commit in the list, the range is invalid
211218 if ( startIndex > endIndex )
212219 {
213- Console . WriteLine ( "Invalid commit range specified: latest commit is earlier than the earliest commit." ) ;
220+ _logWriter . WriteLine ( "Invalid commit range specified: latest commit is earlier than the earliest commit." ) ;
214221 return new List < Commit > ( ) ;
215222 }
216223
0 commit comments