Skip to content

Commit a9c29d0

Browse files
committed
add log-directory
1 parent 446f24f commit a9c29d0

5 files changed

Lines changed: 52 additions & 40 deletions

File tree

GitContentSearch.Tests/GitContentSearcherTests.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ public void SearchContent_ShouldLogFirstAndLastAppearance_Correctly()
2727
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit4")), It.IsAny<string>())).Returns(true);
2828
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit5")), It.IsAny<string>())).Returns(false);
2929

30-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager());
31-
3230
using (var stringWriter = new StringWriter())
3331
{
3432
// Act
35-
gitContentSearcher.SearchContent("dummy/path.txt", "search string", logWriter: stringWriter);
33+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
34+
gitContentSearcher.SearchContent("dummy/path.txt", "search string");
3635

3736
// Assert
3837
var logContent = stringWriter.ToString();
@@ -58,12 +57,11 @@ public void SearchContent_ShouldLogCorrectly_WhenStringNotFound()
5857
// Simulate no appearances of the string
5958
fileSearcherMock.Setup(f => f.SearchInFile(It.IsAny<string>(), It.IsAny<string>())).Returns(false);
6059

61-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager());
62-
6360
using (var stringWriter = new StringWriter())
6461
{
6562
// Act
66-
gitContentSearcher.SearchContent("dummy/path.txt", "search string", logWriter: stringWriter);
63+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
64+
gitContentSearcher.SearchContent("dummy/path.txt", "search string");
6765

6866
// Assert
6967
var logContent = stringWriter.ToString();
@@ -92,13 +90,11 @@ public void SearchContent_ShouldLogCorrectly_WhenStringAppearsInSingleCommit()
9290
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit4")), It.IsAny<string>())).Returns(false);
9391
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit5")), It.IsAny<string>())).Returns(false);
9492

95-
96-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager());
97-
9893
using (var stringWriter = new StringWriter())
9994
{
10095
// Act
101-
gitContentSearcher.SearchContent("dummy/path.txt", "search string", logWriter: stringWriter);
96+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
97+
gitContentSearcher.SearchContent("dummy/path.txt", "search string");
10298

10399
// Assert
104100
var logContent = stringWriter.ToString();
@@ -133,12 +129,11 @@ public void SearchContent_ShouldRestrictSearchToSpecifiedCommitRange()
133129
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit4")), It.IsAny<string>())).Returns(false);
134130
fileSearcherMock.Setup(f => f.SearchInFile(It.Is<string>(file => file.Contains("commit5")), It.IsAny<string>())).Returns(false);
135131

136-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager());
137-
138132
using (var stringWriter = new StringWriter())
139133
{
140134
// Act
141-
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit2", "commit3", stringWriter);
135+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
136+
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit2", "commit3");
142137

143138
// Assert
144139
var logContent = stringWriter.ToString();
@@ -170,12 +165,11 @@ public void SearchContent_ShouldLogError_WhenEarliestCommitIsMoreRecentThanLates
170165
gitHelperMock.Setup(g => g.GetGitCommits(It.IsAny<string>(), It.IsAny<string>())).Returns(allCommits);
171166
gitHelperMock.Setup(g => g.GetCommitTime(It.IsAny<string>())).Returns("2023-08-21 12:00:00");
172167

173-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager());
174-
175168
using (var stringWriter = new StringWriter())
176169
{
177170
// Act
178-
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit4", "commit3", stringWriter);
171+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
172+
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit4", "commit3");
179173

180174
// Assert
181175
var logContent = stringWriter.ToString();

GitContentSearch/GitContentSearcher.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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
{

GitContentSearch/Helpers/FileManager.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,16 @@ namespace GitContentSearch.Helpers
88
{
99
public class FileManager : IFileManager
1010
{
11+
private readonly string _directory = string.Empty;
12+
13+
public FileManager(string? directory = null)
14+
{
15+
_directory = directory ?? Directory.GetCurrentDirectory();
16+
}
17+
1118
public string GenerateTempFileName(string commit, string filePath)
1219
{
13-
return $"temp_{commit}{Path.GetExtension(filePath)}";
20+
return Path.Combine(_directory, $"temp_{commit}{Path.GetExtension(filePath)}");
1421
}
1522

1623
public void DeleteTempFile(string tempFileName)

GitContentSearch/Interfaces/IGitContentSearcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ namespace GitContentSearch
88
{
99
public interface IGitContentSearcher
1010
{
11-
void SearchContent(string filePath, string searchString, string earliestCommit = "", string latestCommit = "", TextWriter? logWriter = null);
11+
void SearchContent(string filePath, string searchString, string earliestCommit = "", string latestCommit = "");
1212
}
1313
}

GitContentSearch/Program.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ static void Main(string[] args)
1818
string earliestCommit = "";
1919
string latestCommit = "";
2020
string? workingDirectory = null;
21+
string? logDirectory = null;
2122

2223
// Parse optional arguments
2324
foreach (var arg in args.Skip(2))
@@ -34,12 +35,22 @@ static void Main(string[] args)
3435
{
3536
workingDirectory = arg.Replace("--working-directory=", "");
3637
}
38+
else if (arg.StartsWith("--log-directory="))
39+
{
40+
logDirectory = arg.Replace("--log-directory=", "");
41+
}
3742
}
3843

44+
var logWriter = new CompositeTextWriter(
45+
Console.Out,
46+
new StreamWriter(Path.Combine(logDirectory ?? Directory.GetCurrentDirectory(), "search_log.txt"), append: true)
47+
);
48+
3949
var processWrapper = new ProcessWrapper();
4050
var gitHelper = new GitHelper(processWrapper, workingDirectory);
4151
var fileSearcher = new FileSearcher();
42-
var gitContentSearcher = new GitContentSearcher(gitHelper, fileSearcher, new FileManager());
52+
var fileManager = new FileManager(logDirectory);
53+
var gitContentSearcher = new GitContentSearcher(gitHelper, fileSearcher, fileManager, logWriter);
4354
gitContentSearcher.SearchContent(filePath, searchString, earliestCommit, latestCommit);
4455
}
4556
}

0 commit comments

Comments
 (0)