Skip to content

Commit 8e336d4

Browse files
committed
add linear search
1 parent 27f78bd commit 8e336d4

3 files changed

Lines changed: 63 additions & 7 deletions

File tree

GitContentSearch.Tests/GitContentSearcherTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void SearchContent_ShouldLogFirstAndLastAppearance_Correctly()
3030
using (var stringWriter = new StringWriter())
3131
{
3232
// Act
33-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
33+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), disableLinearSearch: true, logWriter: stringWriter);
3434
gitContentSearcher.SearchContent("dummy/path.txt", "search string");
3535

3636
// Assert
@@ -60,7 +60,7 @@ public void SearchContent_ShouldLogCorrectly_WhenStringNotFound()
6060
using (var stringWriter = new StringWriter())
6161
{
6262
// Act
63-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
63+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), disableLinearSearch: true, logWriter: stringWriter);
6464
gitContentSearcher.SearchContent("dummy/path.txt", "search string");
6565

6666
// Assert
@@ -93,7 +93,7 @@ public void SearchContent_ShouldLogCorrectly_WhenStringAppearsInSingleCommit()
9393
using (var stringWriter = new StringWriter())
9494
{
9595
// Act
96-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
96+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), disableLinearSearch: true, logWriter: stringWriter);
9797
gitContentSearcher.SearchContent("dummy/path.txt", "search string");
9898

9999
// Assert
@@ -132,7 +132,7 @@ public void SearchContent_ShouldRestrictSearchToSpecifiedCommitRange()
132132
using (var stringWriter = new StringWriter())
133133
{
134134
// Act
135-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
135+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), disableLinearSearch: true, logWriter: stringWriter);
136136
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit2", "commit3");
137137

138138
// Assert
@@ -168,7 +168,7 @@ public void SearchContent_ShouldLogError_WhenEarliestCommitIsMoreRecentThanLates
168168
using (var stringWriter = new StringWriter())
169169
{
170170
// Act
171-
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), logWriter: stringWriter);
171+
var gitContentSearcher = new GitContentSearcher(gitHelperMock.Object, fileSearcherMock.Object, new FileManager(), disableLinearSearch: true, logWriter: stringWriter);
172172
gitContentSearcher.SearchContent("dummy/path.txt", "search string", "commit4", "commit3");
173173

174174
// Assert

GitContentSearch/GitContentSearcher.cs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ public class GitContentSearcher : IGitContentSearcher
88
private readonly IFileSearcher _fileSearcher;
99
private readonly IFileManager _fileManager;
1010
private readonly TextWriter _logWriter;
11+
private readonly bool _disableLinearSearch;
1112

12-
public GitContentSearcher(IGitHelper gitHelper, IFileSearcher fileSearcher, IFileManager fileManager, TextWriter? logWriter = null)
13+
public GitContentSearcher(IGitHelper gitHelper, IFileSearcher fileSearcher, IFileManager fileManager, bool disableLinearSearch, TextWriter? logWriter = null)
1314
{
1415
_gitHelper = gitHelper;
1516
_fileSearcher = fileSearcher;
1617
_fileManager = fileManager;
18+
_disableLinearSearch = disableLinearSearch;
1719
_logWriter = logWriter ?? new CompositeTextWriter(
1820
Console.Out,
1921
new StreamWriter("search_log.txt", append: true)
@@ -79,6 +81,17 @@ private int FindFirstMatchIndex(string[] commits, string filePath, string search
7981
}
8082
else
8183
{
84+
if (!_disableLinearSearch)
85+
{
86+
// Use the linear search helper
87+
int? linearSearchResult = PerformLinearSearch(commits, filePath, searchString, left, mid - 1);
88+
if (linearSearchResult.HasValue)
89+
{
90+
firstMatchIndex = linearSearchResult;
91+
break;
92+
}
93+
}
94+
8295
left = mid + 1;
8396
}
8497

@@ -133,6 +146,44 @@ private int FindLastMatchIndex(string[] commits, string filePath, string searchS
133146
return lastMatchIndex ?? -1;
134147
}
135148

149+
private int? PerformLinearSearch(string[] commits, string filePath, string searchString, int left, int right)
150+
{
151+
int? matchIndex = null;
152+
153+
for (int i = left; i <= right; i++)
154+
{
155+
string commit = commits[i];
156+
string tempFileName = _fileManager.GenerateTempFileName(commit, filePath);
157+
string commitTime = GetCommitTime(commit);
158+
159+
bool gitShowSuccess = false;
160+
try
161+
{
162+
_gitHelper.RunGitShow(commit, filePath, tempFileName);
163+
gitShowSuccess = true;
164+
}
165+
catch (Exception ex)
166+
{
167+
_logWriter.WriteLine($"Error retrieving file at commit {commit}: {ex.Message}");
168+
}
169+
170+
bool found = gitShowSuccess && _fileSearcher.SearchInFile(tempFileName, searchString);
171+
172+
_logWriter.WriteLine($"Linear search commit: {commit} at {commitTime}, found: {found}");
173+
_logWriter.Flush();
174+
175+
if (found)
176+
{
177+
matchIndex = i; // Update matchIndex when a match is found
178+
break; // Stop search on first match (can be modified for last match)
179+
}
180+
181+
_fileManager.DeleteTempFile(tempFileName);
182+
}
183+
184+
return matchIndex;
185+
}
186+
136187
private string GetCommitTime(string commit)
137188
{
138189
try

GitContentSearch/Program.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ static void Main(string[] args)
1717
string searchString = args[1];
1818
string earliestCommit = "";
1919
string latestCommit = "";
20+
bool disableLinearSearch = false;
2021
string? workingDirectory = null;
2122
string? logDirectory = null;
2223

@@ -39,6 +40,10 @@ static void Main(string[] args)
3940
{
4041
logDirectory = arg.Replace("--log-directory=", "");
4142
}
43+
else if (arg == "--disable-linear-search")
44+
{
45+
disableLinearSearch = true;
46+
}
4247
}
4348

4449
if (string.IsNullOrEmpty(workingDirectory))
@@ -72,7 +77,7 @@ static void Main(string[] args)
7277
var gitHelper = new GitHelper(processWrapper, workingDirectory);
7378
var fileSearcher = new FileSearcher();
7479
var fileManager = new FileManager(logAndTempFileDirectory);
75-
var gitContentSearcher = new GitContentSearcher(gitHelper, fileSearcher, fileManager, logWriter);
80+
var gitContentSearcher = new GitContentSearcher(gitHelper, fileSearcher, fileManager, disableLinearSearch, logWriter);
7681

7782
gitContentSearcher.SearchContent(filePath, searchString, earliestCommit, latestCommit);
7883

0 commit comments

Comments
 (0)