Skip to content

Commit bfbd0d7

Browse files
committed
fix progress for linear search
1 parent e7f8161 commit bfbd0d7

File tree

1 file changed

+19
-29
lines changed

1 file changed

+19
-29
lines changed

GitContentSearch/GitContentSearcher.cs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,26 @@ public void SearchContent(string filePath, string searchString, string earliestC
7272
return;
7373
}
7474

75+
// Calculate total possible commits to search
76+
int totalPossibleSearches = commits.Count;
77+
int totalSearchesDone = 0;
78+
7579
// Search the most recent match first with FindLastMatchIndex
76-
int lastMatchIndex = FindLastMatchIndex(commits, filePath, searchString, 0);
80+
int lastMatchIndex = FindLastMatchIndex(commits, filePath, searchString, 0, ref totalSearchesDone, totalPossibleSearches);
7781

7882
// Pass lastMatchIndex to FindFirstMatchIndex to optimize the search range
79-
int firstMatchIndex = FindFirstMatchIndex(commits, filePath, searchString, lastMatchIndex);
83+
int firstMatchIndex = FindFirstMatchIndex(commits, filePath, searchString, lastMatchIndex, ref totalSearchesDone, totalPossibleSearches);
8084

8185
LogResults(firstMatchIndex, lastMatchIndex, commits, searchString);
8286

8387
_progress?.Report(1.0);
8488
}
8589

86-
private int FindFirstMatchIndex(List<Commit> commits, string filePath, string searchString, int lastMatchIndex)
90+
private int FindFirstMatchIndex(List<Commit> commits, string filePath, string searchString, int lastMatchIndex, ref int totalSearchesDone, int totalPossibleSearches)
8791
{
8892
int left = 0;
8993
int right = lastMatchIndex; // Use lastMatchIndex as the upper bound
9094
int? firstMatchIndex = null;
91-
// For binary search, we'll make approximately log2(n) comparisons
92-
int expectedSearches = (int)Math.Ceiling(Math.Log2(Math.Max(1, right - left + 1)));
93-
int searchesDone = 0;
9495

9596
while (left <= right)
9697
{
@@ -115,10 +116,9 @@ private int FindFirstMatchIndex(List<Commit> commits, string filePath, string se
115116
_logWriter.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");
116117
_logWriter.Flush();
117118

118-
searchesDone++;
119-
// Calculate progress between 62.5% and 100%
120-
double searchProgress = (double)searchesDone / expectedSearches;
121-
_currentProgress = 0.625 + (searchProgress * 0.375);
119+
totalSearchesDone++;
120+
// Calculate progress between 62.5% and 100% based on total possible searches
121+
_currentProgress = 0.625 + ((double)totalSearchesDone / totalPossibleSearches * 0.375);
122122
_progress?.Report(_currentProgress);
123123

124124
if (found)
@@ -137,14 +137,11 @@ private int FindFirstMatchIndex(List<Commit> commits, string filePath, string se
137137
return firstMatchIndex ?? -1;
138138
}
139139

140-
private int FindLastMatchIndex(List<Commit> commits, string filePath, string searchString, int searchStartIndex)
140+
private int FindLastMatchIndex(List<Commit> commits, string filePath, string searchString, int searchStartIndex, ref int totalSearchesDone, int totalPossibleSearches)
141141
{
142142
int left = searchStartIndex == -1 ? 0 : searchStartIndex;
143143
int right = commits.Count - 1;
144144
int? lastMatchIndex = null;
145-
// For binary search, we'll make approximately log2(n) comparisons
146-
int expectedSearches = (int)Math.Ceiling(Math.Log2(Math.Max(1, right - left + 1)));
147-
int searchesDone = 0;
148145

149146
while (left <= right)
150147
{
@@ -169,10 +166,9 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
169166
_logWriter.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");
170167
_logWriter.Flush();
171168

172-
searchesDone++;
173-
// Calculate progress between 25% and 62.5%
174-
double searchProgress = (double)searchesDone / expectedSearches;
175-
_currentProgress = 0.25 + (searchProgress * 0.375);
169+
totalSearchesDone++;
170+
// Calculate progress between 25% and 62.5% based on total possible searches
171+
_currentProgress = 0.25 + ((double)totalSearchesDone / totalPossibleSearches * 0.375);
176172
_progress?.Report(_currentProgress);
177173

178174
if (found)
@@ -185,13 +181,10 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
185181
// If not found and linear search is enabled, check remaining commits with linear search
186182
if (!_disableLinearSearch)
187183
{
188-
int? linearSearchResult = PerformLinearSearch(commits, filePath, searchString, mid + 1, right, reverse: true);
184+
int? linearSearchResult = PerformLinearSearch(commits, filePath, searchString, mid + 1, right, ref totalSearchesDone, totalPossibleSearches, reverse: true);
189185
if (linearSearchResult.HasValue)
190186
{
191187
lastMatchIndex = linearSearchResult;
192-
// Update progress to 62.5% since we're done with this phase
193-
_currentProgress = 0.625;
194-
_progress?.Report(_currentProgress);
195188
break;
196189
}
197190
}
@@ -205,13 +198,11 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
205198
return lastMatchIndex ?? -1;
206199
}
207200

208-
private int? PerformLinearSearch(List<Commit> commits, string filePath, string searchString, int left, int right, bool reverse = false)
201+
private int? PerformLinearSearch(List<Commit> commits, string filePath, string searchString, int left, int right, ref int totalSearchesDone, int totalPossibleSearches, bool reverse = false)
209202
{
210203
int step = reverse ? -1 : 1; // Use step to control direction of iteration
211204
int start = reverse ? right : left;
212205
int end = reverse ? left : right;
213-
int totalSearches = Math.Abs(end - start) + 1;
214-
int searchesDone = 0;
215206

216207
for (int i = start; reverse ? i >= end : i <= end; i += step)
217208
{
@@ -235,10 +226,9 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
235226
_logWriter.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");
236227
_logWriter.Flush();
237228

238-
searchesDone++;
239-
// Calculate progress for linear search portion
240-
double searchProgress = (double)searchesDone / totalSearches;
241-
_currentProgress = 0.25 + (searchProgress * 0.375);
229+
totalSearchesDone++;
230+
// Calculate progress based on total possible searches
231+
_currentProgress = 0.25 + ((double)totalSearchesDone / totalPossibleSearches * 0.375);
242232
_progress?.Report(_currentProgress);
243233

244234
_fileManager.DeleteTempFile(tempFileName); // Always clean up the temp file

0 commit comments

Comments
 (0)