Skip to content

Commit b7173c2

Browse files
committed
fix follow not working
1 parent 8264403 commit b7173c2

File tree

3 files changed

+44
-21
lines changed

3 files changed

+44
-21
lines changed

GitContentSearch/GitContentSearcher.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,13 @@ public void SearchContentByDate(string filePath, string searchString, DateTime?
185185
_logger.WriteLine(""); // Empty line
186186
}
187187

188-
// Get commits for the specified date range
188+
// Get commits for the specified date range, following renames
189189
var commits = _gitHelper.GetGitCommitsByDate(startDate, endDate, filePath, cancellationToken);
190190
commits.Reverse(); // Reverse to get chronological order
191191

192192
if (commits.Count == 0)
193193
{
194+
_logger.WriteLine("No commits found containing the specified file within the given date range.");
194195
_progress?.Report(1.0);
195196
return;
196197
}

GitContentSearch/GitHelper.cs

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -143,15 +143,19 @@ private List<Commit> GetCommitsWithFollow(string filePath, CancellationToken can
143143
return new List<Commit>();
144144
}
145145

146-
var commitLines = result.StandardOutput
147-
.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
148-
.ToArray();
146+
return ParseGitLogWithNameStatus(result.StandardOutput, filePath, cancellationToken);
147+
}
148+
149+
private List<Commit> ParseGitLogWithNameStatus(string gitLogOutput, string originalFilePath, CancellationToken cancellationToken)
150+
{
151+
var commitLines = gitLogOutput.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
152+
.ToArray();
149153

150154
var commits = new List<Commit>();
151155
var processedCommits = new HashSet<string>(); // Track processed commits to avoid duplicates
152156
string? currentCommitHash = null;
153157
string? currentFilePath = null;
154-
string? previousFilePath = filePath;
158+
string? previousFilePath = originalFilePath;
155159

156160
foreach (var line in commitLines)
157161
{
@@ -371,26 +375,43 @@ public List<Commit> GetGitCommitsByDate(DateTime? startDate, DateTime? endDate,
371375
var filePathArg = string.IsNullOrEmpty(filePath) ? string.Empty : $"-- {FormatFilePathForGit(filePath)}";
372376
var startDateArg = startDate.HasValue ? $"--since=\"{startDate.Value:yyyy-MM-dd}\"" : string.Empty;
373377
var endDateArg = endDate.HasValue ? $"--until=\"{endDate.Value:yyyy-MM-dd}\"" : string.Empty;
374-
var arguments = $"log --pretty=format:%H {startDateArg} {endDateArg} {filePathArg}".Trim();
375378

376-
var result = RunGitCommand(arguments, null, cancellationToken);
377-
if (result == null || result.ExitCode != 0)
379+
if (_follow && !string.IsNullOrEmpty(filePath))
378380
{
379-
_logger?.WriteLine($"Error retrieving git commits: {result?.StandardError}");
380-
return new List<Commit>();
381+
// When following file renames, we need to use --name-status to track the file path changes
382+
var arguments = $"log --name-status --pretty=format:%H --follow {startDateArg} {endDateArg} {filePathArg}".Trim();
383+
var result = RunGitCommand(arguments, null, cancellationToken);
384+
if (result == null || result.ExitCode != 0)
385+
{
386+
_logger?.WriteLine($"Error retrieving git commits: {result?.StandardError}");
387+
return new List<Commit>();
388+
}
389+
390+
return ParseGitLogWithNameStatus(result.StandardOutput, filePath, cancellationToken);
381391
}
392+
else
393+
{
394+
// When not following renames, use the simpler log command
395+
var arguments = $"log --pretty=format:%H {startDateArg} {endDateArg} {filePathArg}".Trim();
396+
var result = RunGitCommand(arguments, null, cancellationToken);
397+
if (result == null || result.ExitCode != 0)
398+
{
399+
_logger?.WriteLine($"Error retrieving git commits: {result?.StandardError}");
400+
return new List<Commit>();
401+
}
382402

383-
var commits = result.StandardOutput
384-
.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
385-
.Select(x => new Commit(x, filePath))
386-
.ToList();
403+
var commits = result.StandardOutput
404+
.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries)
405+
.Select(x => new Commit(x, filePath))
406+
.ToList();
387407

388-
if (!commits.Any())
389-
{
390-
_logger?.WriteLine($"No commits found between {startDate?.ToString("yyyy-MM-dd") ?? "repository start"} and {endDate?.ToString("yyyy-MM-dd") ?? "repository end"}");
391-
}
408+
if (!commits.Any())
409+
{
410+
_logger?.WriteLine($"No commits found between {startDate?.ToString("yyyy-MM-dd") ?? "repository start"} and {endDate?.ToString("yyyy-MM-dd") ?? "repository end"}");
411+
}
392412

393-
return commits;
413+
return commits;
414+
}
394415
}
395416

396417
private void EnsureRepositoryInitialized()

GitContentSearch/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ static void Main(string[] args)
145145
var gitContentSearcher = new GitContentSearcher(gitHelper, fileSearcher, fileManager, logger);
146146

147147
// If both commit-based and date-based options are provided, prioritize commit-based
148-
if (!string.IsNullOrEmpty(earliestCommit) || !string.IsNullOrEmpty(latestCommit))
148+
if (!startDate.HasValue && !endDate.HasValue)
149149
{
150-
if (startDate.HasValue || endDate.HasValue)
150+
if (!string.IsNullOrEmpty(earliestCommit) || !string.IsNullOrEmpty(latestCommit))
151151
{
152152
logger.WriteLine("Warning: Both commit-based and date-based options provided. Using commit-based options.");
153153
}
154+
154155
gitContentSearcher.SearchContent(filePath, searchString, earliestCommit, latestCommit);
155156
}
156157
else

0 commit comments

Comments
 (0)