Skip to content

Commit 53e9a29

Browse files
committed
use file filter for git commits
1 parent bc499c9 commit 53e9a29

File tree

5 files changed

+75
-46
lines changed

5 files changed

+75
-46
lines changed

GitContentSearch/GitContentSearcher.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public GitContentSearcher(IGitHelper gitHelper, IFileSearcher fileSearcher, IFil
2222

2323
public void SearchContent(string filePath, string searchString, string earliestCommit = "", string latestCommit = "")
2424
{
25-
var commits = _gitHelper.GetGitCommits(earliestCommit, latestCommit);
25+
var commits = _gitHelper.GetGitCommits(earliestCommit, latestCommit, filePath);
2626
commits = commits.Reverse().ToArray();
2727

2828
if (commits == null || commits.Length == 0)
@@ -40,7 +40,7 @@ public void SearchContent(string filePath, string searchString, string earliestC
4040
int firstMatchIndex = FindFirstMatchIndex(commits, filePath, searchString);
4141
int lastMatchIndex = FindLastMatchIndex(commits, filePath, searchString, firstMatchIndex);
4242

43-
LogResults(firstMatchIndex, lastMatchIndex, commits, searchString, _logWriter);
43+
LogResults(firstMatchIndex, lastMatchIndex, commits, searchString);
4444
}
4545

4646
private int FindFirstMatchIndex(string[] commits, string filePath, string searchString)
@@ -54,7 +54,7 @@ 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);
57+
string commitTime = GetCommitTime(commit);
5858

5959
bool gitShowSuccess = false;
6060
try
@@ -99,7 +99,7 @@ private int FindLastMatchIndex(string[] commits, string filePath, string searchS
9999
int mid = left + (right - left) / 2;
100100
string commit = commits[mid];
101101
string tempFileName = _fileManager.GenerateTempFileName(commit, filePath);
102-
string commitTime = GetCommitTime(commit, _logWriter);
102+
string commitTime = GetCommitTime(commit);
103103

104104
bool gitShowSuccess = false;
105105
try
@@ -133,33 +133,35 @@ private int FindLastMatchIndex(string[] commits, string filePath, string searchS
133133
return lastMatchIndex ?? -1;
134134
}
135135

136-
private string GetCommitTime(string commit, TextWriter logWriter)
136+
private string GetCommitTime(string commit)
137137
{
138138
try
139139
{
140140
return _gitHelper.GetCommitTime(commit);
141141
}
142142
catch (Exception ex)
143143
{
144-
logWriter.WriteLine($"Error retrieving commit time for {commit}: {ex.Message}");
144+
_logWriter.WriteLine($"Error retrieving commit time for {commit}: {ex.Message}");
145145
return "unknown time";
146146
}
147147
}
148148

149-
private void LogResults(int firstMatchIndex, int lastMatchIndex, string[] commits, string searchString, TextWriter logWriter)
149+
private void LogResults(int firstMatchIndex, int lastMatchIndex, string[] commits, string searchString)
150150
{
151151
if (firstMatchIndex == -1)
152152
{
153-
logWriter.WriteLine($"Search string \"{searchString}\" does not appear in any of the checked commits.");
153+
_logWriter.WriteLine($"Search string \"{searchString}\" does not appear in any of the checked commits.");
154154
}
155155
else
156156
{
157-
logWriter.WriteLine($"Search string \"{searchString}\" first appears in commit {commits[firstMatchIndex]}.");
157+
_logWriter.WriteLine($"Search string \"{searchString}\" first appears in commit {commits[firstMatchIndex]}.");
158158
if (lastMatchIndex != -1)
159159
{
160-
logWriter.WriteLine($"Search string \"{searchString}\" last appears in commit {commits[lastMatchIndex]}.");
160+
_logWriter.WriteLine($"Search string \"{searchString}\" last appears in commit {commits[lastMatchIndex]}.");
161161
}
162162
}
163+
164+
_logWriter.Flush();
163165
}
164166
}
165167
}

GitContentSearch/GitHelper.cs

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,7 @@ public string GetCommitTime(string commitHash)
4343

4444
public void RunGitShow(string commit, string filePath, string outputFile)
4545
{
46-
// Ensure the file path is properly formatted for Git
47-
if (filePath.StartsWith("/"))
48-
{
49-
filePath = filePath.Substring(1); // Remove the leading slash if it exists
50-
}
51-
52-
string quotedFilePath = $"\"{filePath}\"";
46+
string quotedFilePath = FormatFilePathForGit(filePath);
5347

5448
var startInfo = new ProcessStartInfo
5549
{
@@ -76,28 +70,51 @@ public void RunGitShow(string commit, string filePath, string outputFile)
7670

7771
public string[] GetGitCommits(string earliest, string latest)
7872
{
79-
var startInfo = new ProcessStartInfo
73+
return GetGitCommits(earliest, latest, string.Empty);
74+
}
75+
76+
public string[] GetGitCommits(string earliest, string latest, string filePath)
77+
{
78+
string[] GetCommits(string additionalArgs = "")
8079
{
81-
FileName = "git",
82-
Arguments = "log --pretty=format:%H",
83-
RedirectStandardOutput = true,
84-
RedirectStandardError = true,
85-
UseShellExecute = false,
86-
CreateNoWindow = true,
87-
WorkingDirectory = _workingDirectory // Set the working directory
88-
};
80+
var arguments = $"log --pretty=format:%H {additionalArgs}".Trim();
81+
var startInfo = new ProcessStartInfo
82+
{
83+
FileName = "git",
84+
Arguments = arguments,
85+
RedirectStandardOutput = true,
86+
RedirectStandardError = true,
87+
UseShellExecute = false,
88+
CreateNoWindow = true,
89+
WorkingDirectory = _workingDirectory
90+
};
91+
92+
var result = _processWrapper.Start(startInfo);
93+
if (result.ExitCode != 0)
94+
{
95+
Console.WriteLine($"Error retrieving git commits: {result.StandardError}");
96+
return Array.Empty<string>();
97+
}
8998

90-
var result = _processWrapper.Start(startInfo);
99+
return result.StandardOutput
100+
.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
101+
.ToArray();
102+
}
91103

92-
if (result.ExitCode != 0)
104+
var mostRecentCommit = GetCommits("-n 1").FirstOrDefault();
105+
var filteredCommits = string.IsNullOrEmpty(filePath) ? GetCommits() : GetCommits($"-- {FormatFilePathForGit(filePath)}");
106+
107+
if (mostRecentCommit != null && !filteredCommits.Contains(mostRecentCommit))
93108
{
94-
Console.WriteLine($"Error retrieving git commits: {result.StandardError}");
95-
return Array.Empty<string>();
109+
filteredCommits = new[] { mostRecentCommit }.Concat(filteredCommits).ToArray();
96110
}
97111

98-
var commits = result.StandardOutput.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
112+
return FilterCommitsByRange(filteredCommits, earliest, latest);
113+
}
99114

100-
return FilterCommitsByRange(commits, earliest, latest);
115+
private string FormatFilePathForGit(string filePath)
116+
{
117+
return filePath.StartsWith("/") ? $"\"{filePath.Substring(1)}\"" : $"\"{filePath}\"";
101118
}
102119

103120
private string[] FilterCommitsByRange(string[] commits, string earliest, string latest)

GitContentSearch/Helpers/CompositeTextWriter.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ public override void Write(char value)
1919
}
2020
}
2121

22-
public override void WriteLine(string value)
22+
public override void WriteLine(string? value)
2323
{
2424
foreach (var writer in _writers)
2525
{
26-
writer.WriteLine(value);
26+
if (value != null)
27+
{
28+
writer.WriteLine(value);
29+
}
2730
}
2831
}
2932

GitContentSearch/Interfaces/IGitHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ public interface IGitHelper
55
string GetCommitTime(string commitHash);
66
void RunGitShow(string commit, string filePath, string outputFile);
77
string[] GetGitCommits(string earliest, string latest);
8+
string[] GetGitCommits(string earliest, string latest, string filePath);
89
}
910
}

GitContentSearch/Program.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,26 @@ static void Main(string[] args)
5353
}
5454
}
5555

56-
Console.WriteLine("Starting GitContentSearch...");
57-
Console.WriteLine($"Logs and temporary files will be created in: {logAndTempFileDirectory}");
58-
59-
var logWriter = new CompositeTextWriter(
56+
using (var logWriter = new CompositeTextWriter(
6057
Console.Out,
61-
new StreamWriter(Path.Combine(logAndTempFileDirectory, "search_log.txt"), append: true)
62-
);
58+
new StreamWriter(Path.Combine(logAndTempFileDirectory, "search_log.txt"), append: true)))
59+
{
60+
logWriter.WriteLine(new string('=', 50));
61+
logWriter.WriteLine($"GitContentSearch started at {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
62+
logWriter.WriteLine($"Logs and temporary files will be created in: {logAndTempFileDirectory}");
63+
logWriter.WriteLine(new string('=', 50));
64+
65+
var processWrapper = new ProcessWrapper();
66+
var gitHelper = new GitHelper(processWrapper, workingDirectory);
67+
var fileSearcher = new FileSearcher();
68+
var fileManager = new FileManager(logAndTempFileDirectory);
69+
var gitContentSearcher = new GitContentSearcher(gitHelper, fileSearcher, fileManager, logWriter);
6370

64-
var processWrapper = new ProcessWrapper();
65-
var gitHelper = new GitHelper(processWrapper, workingDirectory);
66-
var fileSearcher = new FileSearcher();
67-
var fileManager = new FileManager(logAndTempFileDirectory);
68-
var gitContentSearcher = new GitContentSearcher(gitHelper, fileSearcher, fileManager, logWriter);
69-
gitContentSearcher.SearchContent(filePath, searchString, earliestCommit, latestCommit);
71+
gitContentSearcher.SearchContent(filePath, searchString, earliestCommit, latestCommit);
72+
73+
logWriter.WriteLine($"GitContentSearch completed at {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
74+
logWriter.WriteLine(new string('=', 50));
75+
}
7076
}
7177
}
7278
}

0 commit comments

Comments
 (0)