@@ -7,43 +7,43 @@ public class GitContentSearcher : IGitContentSearcher
77 private readonly IGitHelper _gitHelper ;
88 private readonly IFileSearcher _fileSearcher ;
99 private readonly IFileManager _fileManager ;
10+ private readonly TextWriter _logWriter ;
1011
11- public GitContentSearcher ( IGitHelper gitHelper , IFileSearcher fileSearcher , IFileManager fileManager )
12+ public GitContentSearcher ( IGitHelper gitHelper , IFileSearcher fileSearcher , IFileManager fileManager , TextWriter ? logWriter = null )
1213 {
1314 _gitHelper = gitHelper ;
1415 _fileSearcher = fileSearcher ;
1516 _fileManager = fileManager ;
16- }
17-
18- public void SearchContent ( string filePath , string searchString , string earliestCommit = "" , string latestCommit = "" , TextWriter ? logWriter = null )
19- {
20- logWriter ??= new CompositeTextWriter (
17+ _logWriter = logWriter ?? new CompositeTextWriter (
2118 Console . Out ,
2219 new StreamWriter ( "search_log.txt" , append : true )
2320 ) ;
21+ }
2422
23+ public void SearchContent ( string filePath , string searchString , string earliestCommit = "" , string latestCommit = "" )
24+ {
2525 var commits = _gitHelper . GetGitCommits ( earliestCommit , latestCommit ) ;
2626 commits = commits . Reverse ( ) . ToArray ( ) ;
2727
2828 if ( commits == null || commits . Length == 0 )
2929 {
30- logWriter . WriteLine ( "No commits found in the specified range." ) ;
30+ _logWriter . WriteLine ( "No commits found in the specified range." ) ;
3131 return ;
3232 }
3333
3434 if ( Array . IndexOf ( commits , earliestCommit ) > Array . IndexOf ( commits , latestCommit ) )
3535 {
36- logWriter . WriteLine ( "Error: The earliest commit is more recent than the latest commit." ) ;
36+ _logWriter . WriteLine ( "Error: The earliest commit is more recent than the latest commit." ) ;
3737 return ;
3838 }
3939
40- int firstMatchIndex = FindFirstMatchIndex ( commits , filePath , searchString , logWriter ) ;
41- int lastMatchIndex = FindLastMatchIndex ( commits , filePath , searchString , logWriter , firstMatchIndex ) ;
40+ int firstMatchIndex = FindFirstMatchIndex ( commits , filePath , searchString ) ;
41+ int lastMatchIndex = FindLastMatchIndex ( commits , filePath , searchString , firstMatchIndex ) ;
4242
43- LogResults ( firstMatchIndex , lastMatchIndex , commits , searchString , logWriter ) ;
43+ LogResults ( firstMatchIndex , lastMatchIndex , commits , searchString , _logWriter ) ;
4444 }
4545
46- private int FindFirstMatchIndex ( string [ ] commits , string filePath , string searchString , TextWriter logWriter )
46+ private int FindFirstMatchIndex ( string [ ] commits , string filePath , string searchString )
4747 {
4848 int left = 0 ;
4949 int right = commits . Length - 1 ;
@@ -61,16 +61,16 @@ private int FindFirstMatchIndex(string[] commits, string filePath, string search
6161 }
6262 catch ( Exception ex )
6363 {
64- logWriter . WriteLine ( $ "Error retrieving file at commit { commit } : { ex . Message } ") ;
64+ _logWriter . WriteLine ( $ "Error retrieving file at commit { commit } : { ex . Message } ") ;
6565 right = mid - 1 ;
6666 continue ;
6767 }
6868
6969 bool found = _fileSearcher . SearchInFile ( tempFileName , searchString ) ;
70- string commitTime = GetCommitTime ( commit , logWriter ) ;
70+ string commitTime = GetCommitTime ( commit , _logWriter ) ;
7171
72- logWriter . WriteLine ( $ "Checked commit: { commit } at { commitTime } , found: { found } ") ;
73- logWriter . Flush ( ) ;
72+ _logWriter . WriteLine ( $ "Checked commit: { commit } at { commitTime } , found: { found } ") ;
73+ _logWriter . Flush ( ) ;
7474
7575 if ( found )
7676 {
@@ -88,7 +88,7 @@ private int FindFirstMatchIndex(string[] commits, string filePath, string search
8888 return firstMatchIndex ?? - 1 ;
8989 }
9090
91- private int FindLastMatchIndex ( string [ ] commits , string filePath , string searchString , TextWriter logWriter , int searchStartIndex )
91+ private int FindLastMatchIndex ( string [ ] commits , string filePath , string searchString , int searchStartIndex )
9292 {
9393 int left = searchStartIndex == - 1 ? 0 : searchStartIndex ;
9494 int right = commits . Length - 1 ;
@@ -106,16 +106,16 @@ private int FindLastMatchIndex(string[] commits, string filePath, string searchS
106106 }
107107 catch ( Exception ex )
108108 {
109- logWriter . WriteLine ( $ "Error retrieving file at commit { commit } : { ex . Message } ") ;
109+ _logWriter . WriteLine ( $ "Error retrieving file at commit { commit } : { ex . Message } ") ;
110110 right = mid - 1 ;
111111 continue ;
112112 }
113113
114114 bool found = _fileSearcher . SearchInFile ( tempFileName , searchString ) ;
115- string commitTime = GetCommitTime ( commit , logWriter ) ;
115+ string commitTime = GetCommitTime ( commit , _logWriter ) ;
116116
117- logWriter . WriteLine ( $ "Checked commit: { commit } at { commitTime } , found: { found } ") ;
118- logWriter . Flush ( ) ;
117+ _logWriter . WriteLine ( $ "Checked commit: { commit } at { commitTime } , found: { found } ") ;
118+ _logWriter . Flush ( ) ;
119119
120120 if ( found )
121121 {
0 commit comments