mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
Add author filtering to commit view
This commit introduces a new feature to the commit view, allowing users to filter commits based on the author's name or email address. Similar to the existing path filtering functionality, accessible through <c-s>, this feature allows users to filter the commit history by the currently selected commit's author if the commit view is focused, or by typing in the author's name or email address. This feature adds an entry to the filtering menu, to provide users with a familiar and intuitive experience
This commit is contained in:
committed by
Stefan Haller
parent
329b434915
commit
503422a72e
@ -13,6 +13,7 @@ type FilteringMenuAction struct {
|
||||
|
||||
func (self *FilteringMenuAction) Call() error {
|
||||
fileName := ""
|
||||
author := ""
|
||||
switch self.c.CurrentSideContext() {
|
||||
case self.c.Contexts().Files:
|
||||
node := self.c.Contexts().Files.GetSelected()
|
||||
@ -24,16 +25,36 @@ func (self *FilteringMenuAction) Call() error {
|
||||
if node != nil {
|
||||
fileName = node.GetPath()
|
||||
}
|
||||
case self.c.Contexts().LocalCommits:
|
||||
commit := self.c.Contexts().LocalCommits.GetSelected()
|
||||
if commit != nil {
|
||||
author = fmt.Sprintf("%s <%s>", commit.AuthorName, commit.AuthorEmail)
|
||||
}
|
||||
}
|
||||
|
||||
menuItems := []*types.MenuItem{}
|
||||
tooltip := ""
|
||||
if self.c.Modes().Filtering.Active() {
|
||||
tooltip = self.c.Tr.WillCancelExistingFilterTooltip
|
||||
}
|
||||
|
||||
if fileName != "" {
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: fmt.Sprintf("%s '%s'", self.c.Tr.FilterBy, fileName),
|
||||
OnPress: func() error {
|
||||
return self.setFiltering(fileName)
|
||||
return self.setFilteringPath(fileName)
|
||||
},
|
||||
Tooltip: tooltip,
|
||||
})
|
||||
}
|
||||
|
||||
if author != "" {
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: fmt.Sprintf("%s '%s'", self.c.Tr.FilterBy, author),
|
||||
OnPress: func() error {
|
||||
return self.setFilteringAuthor(author)
|
||||
},
|
||||
Tooltip: tooltip,
|
||||
})
|
||||
}
|
||||
|
||||
@ -44,10 +65,25 @@ func (self *FilteringMenuAction) Call() error {
|
||||
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetFilePathSuggestionsFunc(),
|
||||
Title: self.c.Tr.EnterFileName,
|
||||
HandleConfirm: func(response string) error {
|
||||
return self.setFiltering(strings.TrimSpace(response))
|
||||
return self.setFilteringPath(strings.TrimSpace(response))
|
||||
},
|
||||
})
|
||||
},
|
||||
Tooltip: tooltip,
|
||||
})
|
||||
|
||||
menuItems = append(menuItems, &types.MenuItem{
|
||||
Label: self.c.Tr.FilterAuthorOption,
|
||||
OnPress: func() error {
|
||||
return self.c.Prompt(types.PromptOpts{
|
||||
FindSuggestionsFunc: self.c.Helpers().Suggestions.GetAuthorsSuggestionsFunc(),
|
||||
Title: self.c.Tr.EnterAuthor,
|
||||
HandleConfirm: func(response string) error {
|
||||
return self.setFilteringAuthor(strings.TrimSpace(response))
|
||||
},
|
||||
})
|
||||
},
|
||||
Tooltip: tooltip,
|
||||
})
|
||||
|
||||
if self.c.Modes().Filtering.Active() {
|
||||
@ -60,9 +96,19 @@ func (self *FilteringMenuAction) Call() error {
|
||||
return self.c.Menu(types.CreateMenuOptions{Title: self.c.Tr.FilteringMenuTitle, Items: menuItems})
|
||||
}
|
||||
|
||||
func (self *FilteringMenuAction) setFiltering(path string) error {
|
||||
func (self *FilteringMenuAction) setFilteringPath(path string) error {
|
||||
self.c.Modes().Filtering.Reset()
|
||||
self.c.Modes().Filtering.SetPath(path)
|
||||
return self.setFiltering()
|
||||
}
|
||||
|
||||
func (self *FilteringMenuAction) setFilteringAuthor(author string) error {
|
||||
self.c.Modes().Filtering.Reset()
|
||||
self.c.Modes().Filtering.SetAuthor(author)
|
||||
return self.setFiltering()
|
||||
}
|
||||
|
||||
func (self *FilteringMenuAction) setFiltering() error {
|
||||
repoState := self.c.State().GetRepoState()
|
||||
if repoState.GetScreenMode() == types.SCREEN_NORMAL {
|
||||
repoState.SetScreenMode(types.SCREEN_HALF)
|
||||
|
@ -72,11 +72,12 @@ func (self *ModeHelper) Statuses() []ModeStatus {
|
||||
{
|
||||
IsActive: self.c.Modes().Filtering.Active,
|
||||
Description: func() string {
|
||||
filterContent := lo.Ternary(self.c.Modes().Filtering.GetPath() != "", self.c.Modes().Filtering.GetPath(), self.c.Modes().Filtering.GetAuthor())
|
||||
return self.withResetButton(
|
||||
fmt.Sprintf(
|
||||
"%s '%s'",
|
||||
self.c.Tr.FilteringBy,
|
||||
self.c.Modes().Filtering.GetPath(),
|
||||
filterContent,
|
||||
),
|
||||
style.FgRed,
|
||||
)
|
||||
|
@ -317,6 +317,7 @@ func (self *RefreshHelper) refreshCommitsWithLimit() error {
|
||||
git_commands.GetCommitsOptions{
|
||||
Limit: self.c.Contexts().LocalCommits.GetLimitCommits(),
|
||||
FilterPath: self.c.Modes().Filtering.GetPath(),
|
||||
FilterAuthor: self.c.Modes().Filtering.GetAuthor(),
|
||||
IncludeRebaseCommits: true,
|
||||
RefName: self.refForLog(),
|
||||
RefForPushedStatus: checkedOutBranchName,
|
||||
@ -342,6 +343,7 @@ func (self *RefreshHelper) refreshSubCommitsWithLimit() error {
|
||||
git_commands.GetCommitsOptions{
|
||||
Limit: self.c.Contexts().SubCommits.GetLimitCommits(),
|
||||
FilterPath: self.c.Modes().Filtering.GetPath(),
|
||||
FilterAuthor: self.c.Modes().Filtering.GetAuthor(),
|
||||
IncludeRebaseCommits: false,
|
||||
RefName: self.c.Contexts().SubCommits.GetRef().FullRefName(),
|
||||
RefToShowDivergenceFrom: self.c.Contexts().SubCommits.GetRefToShowDivergenceFrom(),
|
||||
@ -438,7 +440,7 @@ func (self *RefreshHelper) refreshBranches(refreshWorktrees bool, keepBranchSele
|
||||
// which allows us to order them correctly. So if we're filtering we'll just
|
||||
// manually load all the reflog commits here
|
||||
var err error
|
||||
reflogCommits, _, err = self.c.Git().Loaders.ReflogCommitLoader.GetReflogCommits(nil, "")
|
||||
reflogCommits, _, err = self.c.Git().Loaders.ReflogCommitLoader.GetReflogCommits(nil, "", "")
|
||||
if err != nil {
|
||||
self.c.Log.Error(err)
|
||||
}
|
||||
@ -597,9 +599,9 @@ func (self *RefreshHelper) refreshReflogCommits() error {
|
||||
lastReflogCommit = model.ReflogCommits[0]
|
||||
}
|
||||
|
||||
refresh := func(stateCommits *[]*models.Commit, filterPath string) error {
|
||||
refresh := func(stateCommits *[]*models.Commit, filterPath string, filterAuthor string) error {
|
||||
commits, onlyObtainedNewReflogCommits, err := self.c.Git().Loaders.ReflogCommitLoader.
|
||||
GetReflogCommits(lastReflogCommit, filterPath)
|
||||
GetReflogCommits(lastReflogCommit, filterPath, filterAuthor)
|
||||
if err != nil {
|
||||
return self.c.Error(err)
|
||||
}
|
||||
@ -612,12 +614,12 @@ func (self *RefreshHelper) refreshReflogCommits() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := refresh(&model.ReflogCommits, ""); err != nil {
|
||||
if err := refresh(&model.ReflogCommits, "", ""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if self.c.Modes().Filtering.Active() {
|
||||
if err := refresh(&model.FilteredReflogCommits, self.c.Modes().Filtering.GetPath()); err != nil {
|
||||
if err := refresh(&model.FilteredReflogCommits, self.c.Modes().Filtering.GetPath(), self.c.Modes().Filtering.GetAuthor()); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
|
@ -39,6 +39,7 @@ func (self *SubCommitsHelper) ViewSubCommits(opts ViewSubCommitsOpts) error {
|
||||
git_commands.GetCommitsOptions{
|
||||
Limit: true,
|
||||
FilterPath: self.c.Modes().Filtering.GetPath(),
|
||||
FilterAuthor: self.c.Modes().Filtering.GetAuthor(),
|
||||
IncludeRebaseCommits: false,
|
||||
RefName: opts.Ref.FullRefName(),
|
||||
RefForPushedStatus: opts.Ref.FullRefName(),
|
||||
|
Reference in New Issue
Block a user