Skip to content

Commit 541e482

Browse files
committed
fix missing file path change logs in UI
1 parent d4055f8 commit 541e482

3 files changed

Lines changed: 33 additions & 24 deletions

File tree

GitContentSearch.UI/ViewModels/MainWindowViewModel.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -287,23 +287,21 @@ private async Task StartSearchAsync()
287287
try
288288
{
289289
var processWrapper = new ProcessWrapper();
290-
_gitHelper = new GitHelper(processWrapper, WorkingDirectory, FollowHistory);
291-
_fileSearcher = new FileSearcher();
292-
293290
string logAndTempFileDirectory = LogDirectory;
294291
if (string.IsNullOrEmpty(logAndTempFileDirectory))
295292
{
296293
logAndTempFileDirectory = Path.Combine(Path.GetTempPath(), "GitContentSearch");
297294
Directory.CreateDirectory(logAndTempFileDirectory);
298295
}
299296

300-
_fileManager = new FileManager(logAndTempFileDirectory);
301-
302297
var uiTextWriter = new UiTextWriter(LogOutput);
303298
var logFile = Path.Combine(logAndTempFileDirectory, "search_log.txt");
304299
var fileWriter = new StreamWriter(logFile, append: true);
300+
var writer = new CompositeTextWriter(uiTextWriter, fileWriter);
305301

306-
using var writer = new CompositeTextWriter(uiTextWriter, fileWriter);
302+
_gitHelper = new GitHelper(processWrapper, WorkingDirectory, FollowHistory, writer);
303+
_fileSearcher = new FileSearcher();
304+
_fileManager = new FileManager(logAndTempFileDirectory);
307305

308306
writer.WriteLine(new string('=', 50));
309307
writer.WriteLine($"GitContentSearch started at {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
@@ -323,6 +321,10 @@ await Task.Run(() => gitContentSearcher.SearchContent(
323321

324322
writer.WriteLine($"GitContentSearch completed at {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
325323
writer.WriteLine(new string('=', 50));
324+
325+
// Ensure we flush and dispose the writer
326+
writer.Flush();
327+
fileWriter.Dispose();
326328
}
327329
catch (Exception ex)
328330
{

GitContentSearch/GitContentSearcher.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ public class GitContentSearcher : IGitContentSearcher
1212

1313
public GitContentSearcher(IGitHelper gitHelper, IFileSearcher fileSearcher, IFileManager fileManager, bool disableLinearSearch, TextWriter? logWriter = null)
1414
{
15-
_gitHelper = gitHelper;
16-
_fileSearcher = fileSearcher;
17-
_fileManager = fileManager;
18-
_disableLinearSearch = disableLinearSearch;
1915
_logWriter = logWriter ?? new CompositeTextWriter(
2016
Console.Out,
2117
new StreamWriter("search_log.txt", append: true)
2218
);
19+
_gitHelper = gitHelper;
20+
_fileSearcher = fileSearcher;
21+
_fileManager = fileManager;
22+
_disableLinearSearch = disableLinearSearch;
2323
}
2424

2525
private bool FileExistsInCurrentCommit(string filePath)

GitContentSearch/GitHelper.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics;
2+
using System.IO;
23

34
namespace GitContentSearch
45
{
@@ -7,23 +8,29 @@ public class GitHelper : IGitHelper
78
private readonly IProcessWrapper _processWrapper;
89
private readonly string? _workingDirectory;
910
private readonly bool _follow;
11+
private readonly TextWriter _logWriter;
1012

1113
public GitHelper(IProcessWrapper processWrapper)
14+
: this(processWrapper, null, false, Console.Out)
1215
{
13-
_processWrapper = processWrapper;
1416
}
1517

1618
public GitHelper(IProcessWrapper processWrapper, string? workingDirectory)
17-
: this(processWrapper, workingDirectory, false)
19+
: this(processWrapper, workingDirectory, false, Console.Out)
1820
{
19-
2021
}
2122

2223
public GitHelper(IProcessWrapper processWrapper, string? workingDirectory, bool follow)
24+
: this(processWrapper, workingDirectory, follow, Console.Out)
25+
{
26+
}
27+
28+
public GitHelper(IProcessWrapper processWrapper, string? workingDirectory, bool follow, TextWriter logWriter)
2329
{
2430
_processWrapper = processWrapper;
2531
_workingDirectory = workingDirectory;
2632
_follow = follow;
33+
_logWriter = logWriter;
2734
}
2835

2936
public string GetCommitTime(string commitHash)
@@ -81,7 +88,7 @@ public List<Commit> GetGitCommits(string earliest, string latest, string filePat
8188
var result = RunGitCommand("log --pretty=format:%H -n 1");
8289
if (result == null || result.ExitCode != 0)
8390
{
84-
Console.WriteLine($"Error retrieving git commits: {result?.StandardError}");
91+
_logWriter.WriteLine($"Error retrieving git commits: {result?.StandardError}");
8592
return null;
8693
}
8794

@@ -95,7 +102,7 @@ private List<Commit> GetCommits(string filePath)
95102
var result = RunGitCommand(arguments);
96103
if (result == null || result.ExitCode != 0)
97104
{
98-
Console.WriteLine($"Error retrieving git commits: {result?.StandardError}");
105+
_logWriter.WriteLine($"Error retrieving git commits: {result?.StandardError}");
99106
return new List<Commit>();
100107
}
101108

@@ -112,7 +119,7 @@ private List<Commit> GetCommitsWithFollow(string filePath)
112119
var result = RunGitCommand(arguments);
113120
if (result.ExitCode != 0)
114121
{
115-
Console.WriteLine($"Error retrieving git commits: {result?.StandardError}");
122+
_logWriter.WriteLine($"Error retrieving git commits: {result?.StandardError}");
116123
return new List<Commit>();
117124
}
118125

@@ -141,9 +148,9 @@ private List<Commit> GetCommitsWithFollow(string filePath)
141148
if (oldPath != currentFilePath)
142149
{
143150
var commitTime = GetCommitTime(currentCommitHash);
144-
Console.WriteLine($"File renamed in commit {currentCommitHash} at {commitTime.Trim()}:");
145-
Console.WriteLine($" From: {oldPath}");
146-
Console.WriteLine($" To: {currentFilePath}");
151+
_logWriter.WriteLine($"File renamed in commit {currentCommitHash} at {commitTime.Trim()}:");
152+
_logWriter.WriteLine($" From: {oldPath}");
153+
_logWriter.WriteLine($" To: {currentFilePath}");
147154
}
148155
previousFilePath = currentFilePath;
149156
commits.Add(new Commit(currentCommitHash, currentFilePath));
@@ -158,8 +165,8 @@ private List<Commit> GetCommitsWithFollow(string filePath)
158165
if (previousFilePath != currentFilePath)
159166
{
160167
var commitTime = GetCommitTime(currentCommitHash);
161-
Console.WriteLine($"File path changed in commit {currentCommitHash} at {commitTime.Trim()}:");
162-
Console.WriteLine($" New path: {currentFilePath}");
168+
_logWriter.WriteLine($"File path changed in commit {currentCommitHash} at {commitTime.Trim()}:");
169+
_logWriter.WriteLine($" New path: {currentFilePath}");
163170
previousFilePath = currentFilePath;
164171
}
165172
commits.Add(new Commit(currentCommitHash, currentFilePath));
@@ -191,7 +198,7 @@ private List<Commit> FilterCommitsByRange(List<Commit> commits, string earliest,
191198
startIndex = commits.FindIndex(c => c.CommitHash == latest);
192199
if (startIndex == -1)
193200
{
194-
Console.WriteLine($"Latest commit {latest} not found.");
201+
_logWriter.WriteLine($"Latest commit {latest} not found.");
195202
return new List<Commit>();
196203
}
197204
}
@@ -202,15 +209,15 @@ private List<Commit> FilterCommitsByRange(List<Commit> commits, string earliest,
202209
endIndex = commits.FindIndex(c => c.CommitHash == earliest);
203210
if (endIndex == -1)
204211
{
205-
Console.WriteLine($"Earliest commit {earliest} not found.");
212+
_logWriter.WriteLine($"Earliest commit {earliest} not found.");
206213
return new List<Commit>();
207214
}
208215
}
209216

210217
// If the latest commit appears after the earliest commit in the list, the range is invalid
211218
if (startIndex > endIndex)
212219
{
213-
Console.WriteLine("Invalid commit range specified: latest commit is earlier than the earliest commit.");
220+
_logWriter.WriteLine("Invalid commit range specified: latest commit is earlier than the earliest commit.");
214221
return new List<Commit>();
215222
}
216223

0 commit comments

Comments
 (0)