Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to filter by changes (-S flag) #4383

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

r-darwish
Copy link

@r-darwish r-darwish commented Mar 10, 2025

  • PR Description

This PR introduces the ability to filter logs by specific strings that appear in the patch itself. It uses Git's -S flag under the hood.

  • Please check if the PR fulfills these requirements
  • Cheatsheets are up-to-date (run go generate ./...)
  • Code has been formatted (see here)
  • Tests have been added/updated (see [here] - (https://github.com/jesseduffield/lazygit/blob/master/pkg/integration/README.md) for the integration test guide)
  • Text is internationalised (see here)
  • If a new UserConfig entry was added, make sure it can be hot-reloaded (see here) - irrelevant
  • Docs have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

@r-darwish r-darwish marked this pull request as ready for review March 10, 2025 06:56
@jesseduffield
Copy link
Owner

@r-darwish could you please explain what this PR intends to do in the PR description. 'changes' is ambiguous

@r-darwish r-darwish changed the title Add option to filter by changes Add option to filter by changes (-S flag) Mar 11, 2025
@r-darwish
Copy link
Author

@r-darwish could you please explain what this PR intends to do in the PR description. 'changes' is ambiguous

Done 🙂

Copy link
Owner

@jesseduffield jesseduffield left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking forward to having this in the codebase. Mostly we need to update the naming to be more aligned with git's own terminology i.e. 'pickaxe search', 'pickaxe string'.

@@ -46,6 +46,24 @@ type ModeStatus struct {
Reset func() error
}

func (self *ModeHelper) getFilter() string {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return two strings: one for the type of thing we're filtering by, and one for the value. That removes the ambiguity

self.c.Prompt(types.PromptOpts{
Title: self.c.Tr.EnterChanges,
HandleConfirm: func(response string) error {
return self.setFilteringChanges(strings.TrimSpace(response))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we shouldn't trim space now that we support filtering by patch content. Often you actually do care about trailing/leading space when doing that.

@@ -23,7 +23,7 @@ func NewReflogCommitLoader(common *common.Common, cmd oscommands.ICmdObjBuilder)

// GetReflogCommits only returns the new reflog commits since the given lastReflogCommit
// if none is passed (i.e. it's value is nil) then we get all the reflog commits
func (self *ReflogCommitLoader) GetReflogCommits(lastReflogCommit *models.Commit, filterPath string, filterAuthor string) ([]*models.Commit, bool, error) {
func (self *ReflogCommitLoader) GetReflogCommits(lastReflogCommit *models.Commit, filterPath string, filterAuthor string, filterChanges string) ([]*models.Commit, bool, error) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now has too many positional parameters: let's introduce a CommitFilters struct which contains Path, Author, and PickaxeString fields.

Seems Pickaxe is the official term (https://git-scm.com/book/en/v2/Git-Tools-Searching):

we can use the -S option (colloquially referred to as the Git “pickaxe” option) to tell Git to show us only those commits that changed the number of occurrences of that string.

EnterFileName: "Enter path:",
EnterAuthor: "Enter author:",
EnterChanges: "Enter changes:",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
EnterChanges: "Enter changes:",
EnterChanges: "Enter string:",

@@ -1637,8 +1639,10 @@ func EnglishTranslationSet() *TranslationSet {
ExitFilterMode: "Stop filtering",
FilterPathOption: "Enter path to filter by",
FilterAuthorOption: "Enter author to filter by",
FilterChangesOption: "Enter changes to filter by",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
FilterChangesOption: "Enter changes to filter by",
FilterChangesOption: "Enter patch string to filter by (pickaxe search)",

1. Start lazygit with the -f flag e.g. `lazygit -f my/path`
2. From within lazygit, press `<c-s>` and then enter the path of the file you want to filter by

## Filtering by changes
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Filtering by changes
## Filtering by patch content (pickaxe search)

return err
}

if self.c.Modes().Filtering.Active() {
if err := refresh(&model.FilteredReflogCommits, self.c.Modes().Filtering.GetPath(), self.c.Modes().Filtering.GetAuthor()); err != nil {
if err := refresh(&model.FilteredReflogCommits, self.c.Modes().Filtering.GetPath(), self.c.Modes().Filtering.GetAuthor(), self.c.Modes().Filtering.GetChanges()); err != nil {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even here I'd just go self.c.Modes().Filtering.GetFilters() and have that return a CommitFilters struct.

SetupConfig: func(config *config.AppConfig) {
},
SetupRepo: func(shell *Shell) {
shell.CreateNCommits(4)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to explicitly add files and associated commits, just to make this more readable. At the moment the reader has to look up the definition of CreateNCommits to see the relationship between the commit messages and their corresponding file changes. The shell.CreateNCommits is really a convenience function for when you just need multiple commits, but in this case we specifically want to be filtering based on the contents of those commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants