Skip to content

Commit 9e2c50e

Browse files
Martin Dindofferclaude
andcommitted
feature: add fulltext commit search across SHA/author/committer/message (#985)
Git cannot OR --grep/--author/--committer in a single invocation, so the new `All` method (now the default) runs the field queries separately, plus an exact-SHA lookup, and merges the deduplicated results sorted by commit time. Adds an internal-only `ByCommitter` query mode to power it. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 7149a8d commit 9e2c50e

5 files changed

Lines changed: 75 additions & 16 deletions

File tree

‎src/Commands/QueryCommits.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public QueryCommits(string repo, string filter, Models.CommitSearchMethod method
2828
{
2929
builder.Append("-i --author=").Append(filter.Quoted());
3030
}
31+
else if (method == Models.CommitSearchMethod.ByCommitter)
32+
{
33+
builder.Append("-i --committer=").Append(filter.Quoted());
34+
}
3135
else if (method == Models.CommitSearchMethod.ByMessage)
3236
{
3337
var words = filter.Split([' ', '\t', '\r'], StringSplitOptions.RemoveEmptyEntries);

‎src/Models/Commit.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@
44

55
namespace SourceGit.Models
66
{
7+
// Bound by index to the search-method ComboBox in Views/Repository.axaml, so
8+
// UI-selectable methods must stay first and in dropdown order. ByCommitter is
9+
// internal-only (used by the fulltext `All` search).
710
public enum CommitSearchMethod
811
{
9-
BySHA = 0,
12+
All = 0,
13+
BySHA,
1014
ByAuthor,
1115
ByMessage,
1216
ByPath,
1317
ByContent,
18+
ByCommitter,
1419
}
1520

1621
public class Commit : ObservableObject

‎src/Resources/Locales/en_US.axaml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@
821821
<x:String x:Key="Text.Repository.Remotes.Add" xml:space="preserve">Add Remote</x:String>
822822
<x:String x:Key="Text.Repository.Resolve" xml:space="preserve">RESOLVE</x:String>
823823
<x:String x:Key="Text.Repository.Search" xml:space="preserve">Search Commit</x:String>
824+
<x:String x:Key="Text.Repository.Search.ByAll" xml:space="preserve">SHA/Author/Message</x:String>
824825
<x:String x:Key="Text.Repository.Search.ByAuthor" xml:space="preserve">Author</x:String>
825826
<x:String x:Key="Text.Repository.Search.ByContent" xml:space="preserve">Content</x:String>
826827
<x:String x:Key="Text.Repository.Search.ByMessage" xml:space="preserve">Message</x:String>

‎src/ViewModels/SearchCommitContext.cs‎

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -129,22 +129,21 @@ public void StartSearch()
129129
result.Add(commit);
130130
}
131131
}
132-
else if (_onlySearchCurrentBranch)
133-
{
134-
result = await new Commands.QueryCommits(repoPath, _filter, method, true)
135-
.GetResultAsync()
136-
.ConfigureAwait(false);
137-
138-
foreach (var c in result)
139-
c.IsMerged = true;
140-
}
141132
else
142133
{
143-
result = await new Commands.QueryCommits(repoPath, _filter, method, false)
144-
.GetResultAsync()
145-
.ConfigureAwait(false);
134+
if (method == Models.CommitSearchMethod.All)
135+
result = await SearchAllFieldsAsync(repoPath).ConfigureAwait(false);
136+
else
137+
result = await new Commands.QueryCommits(repoPath, _filter, method, _onlySearchCurrentBranch)
138+
.GetResultAsync()
139+
.ConfigureAwait(false);
146140

147-
if (result.Count > 0)
141+
if (_onlySearchCurrentBranch)
142+
{
143+
foreach (var c in result)
144+
c.IsMerged = true;
145+
}
146+
else if (result.Count > 0)
148147
{
149148
var set = await new Commands.QueryCurrentBranchCommitHashes(repoPath, result[^1].CommitterTime)
150149
.GetResultAsync()
@@ -171,6 +170,55 @@ public void StartSearch()
171170
}, token);
172171
}
173172

173+
private async Task<List<Models.Commit>> SearchAllFieldsAsync(string repoPath)
174+
{
175+
var result = new List<Models.Commit>();
176+
var seen = new HashSet<string>();
177+
178+
var isCommitSHA = await new Commands.IsCommitSHA(repoPath, _filter)
179+
.GetResultAsync()
180+
.ConfigureAwait(false);
181+
182+
if (isCommitSHA)
183+
{
184+
var commit = await new Commands.QuerySingleCommit(repoPath, _filter)
185+
.GetResultAsync()
186+
.ConfigureAwait(false);
187+
188+
var matchesScope = commit != null;
189+
if (matchesScope && _onlySearchCurrentBranch)
190+
matchesScope = await new Commands.IsAncestor(repoPath, commit.SHA, "HEAD")
191+
.GetResultAsync()
192+
.ConfigureAwait(false);
193+
194+
if (matchesScope && seen.Add(commit.SHA))
195+
result.Add(commit);
196+
}
197+
198+
var fields = new[]
199+
{
200+
Models.CommitSearchMethod.ByMessage,
201+
Models.CommitSearchMethod.ByAuthor,
202+
Models.CommitSearchMethod.ByCommitter,
203+
};
204+
205+
foreach (var field in fields)
206+
{
207+
var matched = await new Commands.QueryCommits(repoPath, _filter, field, _onlySearchCurrentBranch)
208+
.GetResultAsync()
209+
.ConfigureAwait(false);
210+
211+
foreach (var c in matched)
212+
{
213+
if (seen.Add(c.SHA))
214+
result.Add(c);
215+
}
216+
}
217+
218+
result.Sort((l, r) => r.CommitterTime.CompareTo(l.CommitterTime));
219+
return result;
220+
}
221+
174222
public void EndSearch()
175223
{
176224
if (_cancellation is { IsCancellationRequested: false })
@@ -287,7 +335,7 @@ private void UpdateSuggestions()
287335

288336
private Repository _repo = null;
289337
private CancellationTokenSource _cancellation = null;
290-
private int _method = (int)Models.CommitSearchMethod.ByMessage;
338+
private int _method = (int)Models.CommitSearchMethod.All;
291339
private string _filter = string.Empty;
292340
private bool _onlySearchCurrentBranch = false;
293341
private bool _isQuerying = false;

‎src/Views/Repository.axaml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,7 @@
617617
BorderThickness="0"
618618
SelectedIndex="{Binding SearchCommitContext.Method, Mode=TwoWay}">
619619
<ComboBox.Items>
620+
<TextBlock Text="{DynamicResource Text.Repository.Search.ByAll}"/>
620621
<TextBlock Text="{DynamicResource Text.Repository.Search.BySHA}"/>
621622
<TextBlock Text="{DynamicResource Text.Repository.Search.ByAuthor}"/>
622623
<TextBlock Text="{DynamicResource Text.Repository.Search.ByMessage}"/>
@@ -628,7 +629,7 @@
628629
<CheckBox Height="24"
629630
Margin="4,0,0,0"
630631
IsChecked="{Binding SearchCommitContext.OnlySearchCurrentBranch, Mode=TwoWay}"
631-
IsVisible="{Binding SearchCommitContext.Method, Converter={x:Static c:IntConverters.IsGreaterThanZero}}">
632+
IsVisible="{Binding SearchCommitContext.Method, Converter={x:Static c:IntConverters.IsNotOne}}">
632633
<TextBlock Text="{DynamicResource Text.Repository.Search.InCurrentBranch}"/>
633634
</CheckBox>
634635
</StackPanel>

0 commit comments

Comments
 (0)