Skip to content

Commit c9d52ff

Browse files
committed
fix excel search not working for some files
1 parent b7173c2 commit c9d52ff

File tree

8 files changed

+245
-138
lines changed

8 files changed

+245
-138
lines changed

GitContentSearch/FileSearcher.cs

Lines changed: 185 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,74 @@
1-
using NPOI.HSSF.UserModel; // For .xls
1+
using NPOI.HPSF;
2+
using NPOI.HSSF.UserModel; // For .xls
23
using NPOI.SS.UserModel;
34
using NPOI.XSSF.UserModel; // For .xlsx
45

56
namespace GitContentSearch
67
{
78
public class FileSearcher : IFileSearcher
8-
{
9-
public bool SearchInFile(string fileName, string searchString)
10-
{
11-
if (IsTextFile(fileName))
12-
{
13-
return SearchInTextFile(fileName, searchString);
14-
}
15-
else
16-
{
17-
return SearchInExcel(fileName, searchString);
18-
}
19-
}
9+
{
10+
public bool SearchInFile(string fileName, string searchString)
11+
{
12+
if (IsTextFile(fileName))
13+
{
14+
return SearchInTextFile(fileName, searchString);
15+
}
16+
else
17+
{
18+
return SearchInExcel(fileName, searchString);
19+
}
20+
}
2021

21-
public bool SearchInStream(Stream stream, string searchString, bool isBinary)
22-
{
23-
// Save the current position to restore it later
24-
long originalPosition = stream.Position;
25-
26-
try
27-
{
28-
if (isBinary)
29-
{
30-
stream.Position = 0;
31-
// For Excel files
32-
IWorkbook workbook;
33-
try
34-
{
35-
workbook = new XSSFWorkbook(stream); // Try XLSX format first
36-
}
37-
catch
38-
{
39-
stream.Position = 0;
40-
workbook = new HSSFWorkbook(stream); // Fall back to XLS format
41-
}
22+
public bool SearchInStream(Stream stream, string searchString, string extension)
23+
{
24+
if (IsTextFile(stream, extension))
25+
{
26+
return SearchInTextStream(stream, searchString);
27+
}
28+
else
29+
{
30+
return SearchInExcel(stream, searchString, extension);
31+
}
32+
}
4233

43-
using (workbook)
44-
{
45-
for (int i = 0; i < workbook.NumberOfSheets; i++)
46-
{
47-
var sheet = workbook.GetSheetAt(i);
48-
if (sheet == null) continue;
49-
50-
foreach (IRow row in sheet)
51-
{
52-
if (row == null) continue;
53-
54-
foreach (ICell cell in row)
55-
{
56-
if (cell == null) continue;
57-
58-
var cellValue = GetCellValueAsString(cell);
59-
if (!string.IsNullOrEmpty(cellValue) &&
60-
cellValue.Contains(searchString, StringComparison.OrdinalIgnoreCase))
61-
{
62-
return true;
63-
}
64-
}
65-
}
66-
}
67-
}
68-
return false;
69-
}
70-
else
71-
{
72-
// For text files
73-
stream.Position = 0;
74-
using var reader = new StreamReader(stream, leaveOpen: true);
75-
string? line;
76-
while ((line = reader.ReadLine()) != null)
77-
{
78-
if (line.Contains(searchString, StringComparison.OrdinalIgnoreCase))
79-
{
80-
return true;
81-
}
82-
}
83-
return false;
84-
}
85-
}
86-
finally
87-
{
88-
// Restore the original position
89-
try { stream.Position = originalPosition; } catch { }
90-
}
91-
}
34+
public bool SearchInTextStream(Stream stream, string searchString)
35+
{
36+
try
37+
{
38+
stream.Position = 0;
39+
using var reader = new StreamReader(stream);
40+
string line;
41+
while ((line = reader.ReadLine()) != null)
42+
{
43+
if (line.Contains(searchString, StringComparison.OrdinalIgnoreCase))
44+
{
45+
return true;
46+
}
47+
}
48+
}
49+
catch (Exception ex)
50+
{
51+
return false;
52+
}
53+
return false;
54+
}
9255

93-
public bool IsTextFile(string filePath)
56+
public bool IsTextFile(Stream stream, string extension)
57+
{
58+
if (IsTextStream(stream))
59+
{
60+
return true;
61+
}
62+
63+
return extension switch
64+
{
65+
".xls" or ".xlsx" => false,
66+
".txt" or ".cs" or ".json" or ".xml" or ".config" or ".md" or ".yml" or ".yaml" => true,
67+
_ => true // Default to text for unknown extensions
68+
};
69+
}
70+
71+
public bool IsTextFile(string filePath)
9472
{
9573
// For file paths that exist on disk
9674
if (File.Exists(filePath))
@@ -155,30 +133,129 @@ public string GetCellValueAsString(ICell cell)
155133
};
156134
}
157135

158-
public bool SearchInTextFile(string fileName, string searchString)
136+
public bool SearchInExcel(Stream stream, string searchString, string extension)
159137
{
160-
try
161-
{
162-
using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
163-
return SearchInStream(stream, searchString, false);
164-
}
165-
catch (Exception)
166-
{
167-
return false;
168-
}
169-
}
138+
try
139+
{
140+
stream.Position = 0;
141+
IWorkbook workbook;
170142

171-
public bool SearchInExcel(string fileName, string searchString)
172-
{
173-
try
174-
{
175-
using var stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
176-
return SearchInStream(stream, searchString, true);
177-
}
178-
catch (Exception)
179-
{
180-
return false;
181-
}
182-
}
183-
}
143+
if (extension == ".xls")
144+
{
145+
workbook = new HSSFWorkbook(stream);
146+
}
147+
else if (extension == ".xlsx")
148+
{
149+
workbook = new XSSFWorkbook(stream);
150+
}
151+
else
152+
{
153+
Console.WriteLine($"Unsupported file extension: {extension}");
154+
return false;
155+
}
156+
157+
for (int i = 0; i < workbook.NumberOfSheets; i++)
158+
{
159+
var sheet = workbook.GetSheetAt(i);
160+
if (sheet == null) continue;
161+
162+
foreach (IRow row in sheet)
163+
{
164+
if (row == null) continue;
165+
166+
foreach (ICell cell in row.Cells)
167+
{
168+
if (cell == null) continue;
169+
170+
string cellValue = GetCellValueAsString(cell);
171+
if (cellValue != null && cellValue.Contains(searchString, StringComparison.OrdinalIgnoreCase))
172+
{
173+
return true;
174+
}
175+
}
176+
}
177+
}
178+
}
179+
catch
180+
{
181+
return false;
182+
}
183+
184+
return false;
185+
}
186+
187+
public bool SearchInTextFile(string fileName, string searchString)
188+
{
189+
try
190+
{
191+
foreach (var line in File.ReadLines(fileName))
192+
{
193+
if (line.Contains(searchString, StringComparison.OrdinalIgnoreCase))
194+
{
195+
return true;
196+
}
197+
}
198+
}
199+
catch
200+
{
201+
return false;
202+
}
203+
204+
return false;
205+
}
206+
207+
public bool SearchInExcel(string fileName, string searchString)
208+
{
209+
try
210+
{
211+
using (var fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
212+
{
213+
IWorkbook workbook;
214+
string extension = Path.GetExtension(fileName).ToLower();
215+
216+
if (extension == ".xls")
217+
{
218+
workbook = new HSSFWorkbook(fileStream);
219+
}
220+
else if (extension == ".xlsx")
221+
{
222+
workbook = new XSSFWorkbook(fileStream);
223+
}
224+
else
225+
{
226+
Console.WriteLine($"Unsupported file extension: {extension}");
227+
return false;
228+
}
229+
230+
for (int i = 0; i < workbook.NumberOfSheets; i++)
231+
{
232+
var sheet = workbook.GetSheetAt(i);
233+
if (sheet == null) continue;
234+
235+
foreach (IRow row in sheet)
236+
{
237+
if (row == null) continue;
238+
239+
foreach (ICell cell in row.Cells)
240+
{
241+
if (cell == null) continue;
242+
243+
string cellValue = GetCellValueAsString(cell);
244+
if (cellValue != null && cellValue.Contains(searchString, StringComparison.OrdinalIgnoreCase))
245+
{
246+
return true;
247+
}
248+
}
249+
}
250+
}
251+
}
252+
}
253+
catch
254+
{
255+
return false;
256+
}
257+
258+
return false;
259+
}
260+
}
184261
}

GitContentSearch/GitContentSearch.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
<TargetFramework>net8.0</TargetFramework>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
8+
<Version>0.5.6.0</Version>
9+
<AssemblyVersion>0.5.6.0</AssemblyVersion>
10+
<FileVersion>0.5.6.0</FileVersion>
811
</PropertyGroup>
912

1013
<ItemGroup>

GitContentSearch/GitContentSearcher.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,7 @@ private bool FileExistsInCurrentCommit(string filePath, CancellationToken cancel
3737
return false;
3838
}
3939

40-
// Use LibGit2Sharp to check file existence
41-
var content = _gitHelper.GetFileContentAtCommit("HEAD", filePath, cancellationToken);
42-
return true;
40+
return _gitHelper.FileExistsAtCommit("HEAD", filePath, cancellationToken);
4341
}
4442
catch (Exception)
4543
{
@@ -235,9 +233,10 @@ private int FindFirstMatchIndex(List<Commit> commits, string filePath, string se
235233

236234
try
237235
{
238-
using var stream = _gitHelper.GetFileContentAtCommit(commit.CommitHash, commit.FilePath, cancellationToken);
239-
bool isBinary = !_fileSearcher.IsTextStream(stream);
240-
found = _fileSearcher.SearchInStream(stream, searchString, isBinary);
236+
using (var stream = _gitHelper.GetFileContentAtCommit(commit.CommitHash, commit.FilePath, cancellationToken))
237+
{
238+
found = _fileSearcher.SearchInStream(stream, searchString, Path.GetExtension(commit.FilePath));
239+
}
241240

242241
string commitTime = _gitHelper.GetCommitTime(commit.CommitHash);
243242
_logger.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");
@@ -283,9 +282,10 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
283282

284283
try
285284
{
286-
using var stream = _gitHelper.GetFileContentAtCommit(commit.CommitHash, commit.FilePath, cancellationToken);
287-
bool isBinary = !_fileSearcher.IsTextStream(stream);
288-
found = _fileSearcher.SearchInStream(stream, searchString, isBinary);
285+
using (var stream = _gitHelper.GetFileContentAtCommit(commit.CommitHash, commit.FilePath, cancellationToken))
286+
{
287+
found = _fileSearcher.SearchInStream(stream, searchString, Path.GetExtension(commit.FilePath));
288+
}
289289

290290
string commitTime = _gitHelper.GetCommitTime(commit.CommitHash);
291291
_logger.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");
@@ -338,9 +338,10 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
338338

339339
try
340340
{
341-
using var stream = _gitHelper.GetFileContentAtCommit(commit.CommitHash, commit.FilePath, cancellationToken);
342-
bool isBinary = !_fileSearcher.IsTextStream(stream);
343-
found = _fileSearcher.SearchInStream(stream, searchString, isBinary);
341+
using (var stream = _gitHelper.GetFileContentAtCommit(commit.CommitHash, commit.FilePath, cancellationToken))
342+
{
343+
found = _fileSearcher.SearchInStream(stream, searchString, Path.GetExtension(commit.FilePath));
344+
}
344345

345346
string commitTime = _gitHelper.GetCommitTime(commit.CommitHash);
346347
_logger.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");

0 commit comments

Comments
 (0)