Skip to content

Commit a295104

Browse files
committed
fix binary search logic when file not found in commit
1 parent c247c11 commit a295104

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

GitContentSearch/GitContentSearcher.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,20 @@ private int FindFirstMatchIndex(string[] commits, string filePath, string search
5454
int mid = left + (right - left) / 2;
5555
string commit = commits[mid];
5656
string tempFileName = _fileManager.GenerateTempFileName(commit, filePath);
57+
string commitTime = GetCommitTime(commit, _logWriter);
5758

59+
bool gitShowSuccess = false;
5860
try
5961
{
6062
_gitHelper.RunGitShow(commit, filePath, tempFileName);
63+
gitShowSuccess = true;
6164
}
6265
catch (Exception ex)
6366
{
6467
_logWriter.WriteLine($"Error retrieving file at commit {commit}: {ex.Message}");
65-
right = mid - 1;
66-
_fileManager.DeleteTempFile(tempFileName);
67-
continue;
6868
}
6969

70-
bool found = _fileSearcher.SearchInFile(tempFileName, searchString);
71-
string commitTime = GetCommitTime(commit, _logWriter);
70+
bool found = gitShowSuccess && _fileSearcher.SearchInFile(tempFileName, searchString);
7271

7372
_logWriter.WriteLine($"Checked commit: {commit} at {commitTime}, found: {found}");
7473
_logWriter.Flush();
@@ -100,20 +99,20 @@ private int FindLastMatchIndex(string[] commits, string filePath, string searchS
10099
int mid = left + (right - left) / 2;
101100
string commit = commits[mid];
102101
string tempFileName = _fileManager.GenerateTempFileName(commit, filePath);
102+
string commitTime = GetCommitTime(commit, _logWriter);
103103

104+
bool gitShowSuccess = false;
104105
try
105106
{
106107
_gitHelper.RunGitShow(commit, filePath, tempFileName);
108+
gitShowSuccess = true;
107109
}
108110
catch (Exception ex)
109111
{
110112
_logWriter.WriteLine($"Error retrieving file at commit {commit}: {ex.Message}");
111-
right = mid - 1;
112-
continue;
113113
}
114114

115-
bool found = _fileSearcher.SearchInFile(tempFileName, searchString);
116-
string commitTime = GetCommitTime(commit, _logWriter);
115+
bool found = gitShowSuccess && _fileSearcher.SearchInFile(tempFileName, searchString);
117116

118117
_logWriter.WriteLine($"Checked commit: {commit} at {commitTime}, found: {found}");
119118
_logWriter.Flush();

0 commit comments

Comments
 (0)