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

feat(custom command): support multiple contexts within one command #3784

Conversation

yam-liu
Copy link
Contributor

@yam-liu yam-liu commented Jul 27, 2024

  • PR Description

For some custom commands, they can be used in multiple contexts. But for now, if we want to do this, we should copy and paste the same config times and times with just a different context.

Related issue: #3759

This PR changes the context of customCommand to contexts.

There are at least 3 solutions for this:
1: make a breaking change to context completely.
2: keep the context and add a new contexts key, only one of them can be configured at the same command. contexts has a higher priority if both are present.
3: keep the context key but support both string and array types.

The current implementation is 1. Maybe 3 is better? Any advice is appreciated.

  • 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 for the integration test guide)
  • Text is internationalised (see here)
  • Docs have been updated if necessary
  • You've read through your own file changes for silly mistakes etc

@stefanhaller
Copy link
Collaborator

Nice, thanks for working on this.

A fourth solution could be to keep the config as a string, and simply support a comma-separated list of contexts, e.g.

  - key: X
    context: commits,branches

I think I would be in favor of this, I find this pretty intuitive, it's simple to implement, and it's not a breaking change.

@yam-liu yam-liu force-pushed the feature/custom_command_multiple_contexts branch from fb80753 to c5d846b Compare July 28, 2024 09:44
@yam-liu
Copy link
Contributor Author

yam-liu commented Jul 28, 2024

@stefanhaller Great advice, I have done the modification. Docs will be updated later.

@yam-liu yam-liu force-pushed the feature/custom_command_multiple_contexts branch 2 times, most recently from fa6e683 to 2034ece Compare July 29, 2024 12:51
@yam-liu yam-liu changed the title feat(custom command)!: support multiple contexts within one command feat(custom command): support multiple contexts within one command Jul 29, 2024
@yam-liu yam-liu marked this pull request as ready for review July 29, 2024 12:53
Copy link
Collaborator

@stefanhaller stefanhaller left a comment

Choose a reason for hiding this comment

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

Great work, we're almost there. There's one bug and a minor style thing, see below.

Apart from that, it would be great if we could add an integration test for the case where there's more than one context (in addition to a test for "global" as suggested below). It should be easy to do given the existing examples that we already have.

if customCommand.Context == "global" {
return "", nil
return []string{}, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a bug, it breaks custom commands for the "global" context. You need to change this to return []string{""} to fix that.

Unfortunately we don't have a test that catches this; it would be great to add one. We have several existing tests for custom commands, you could pick one of those as an example to follow, e.g. pkg/integration/tests/custom_commands/show_output_in_panel.go.

Description: description,
})
}
return bindings, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

Matter of taste, probably, but I'd write this block as

	return lo.Map(viewNames, func(viewName string, _ int) *types.Binding {
		return &types.Binding{
			ViewName:    viewName,
			Key:         keybindings.GetKey(customCommand.Key),
			Modifier:    gocui.ModNone,
			Handler:     handler,
			Description: description,
		}
	}), nil

@stefanhaller stefanhaller added the enhancement New feature or request label Jul 30, 2024
@yam-liu yam-liu force-pushed the feature/custom_command_multiple_contexts branch from 2034ece to 969eb87 Compare August 1, 2024 05:54
@stefanhaller
Copy link
Collaborator

Very nice, good work on the tests. Just one small suggestion for one of the tests below.

Now the only complaint I have left is the commit history. My suggestion for structuring the commits would be:

  • a commit that adds the test for the global context
  • all the other changes (the implementation, the other test, and the docs) as a single, second commit.

Also, I find your use of "feat" in the commit messages questionable. I'm not a fan of conventional commits myself, I'd rather omit that entirely, but if you do want to use it, please do it consistently. A commit that adds a test shouldn't be labelled with "feat".

@yam-liu yam-liu force-pushed the feature/custom_command_multiple_contexts branch 2 times, most recently from 938375a to 9720514 Compare August 1, 2024 18:42
@yam-liu
Copy link
Contributor Author

yam-liu commented Aug 1, 2024

@stefanhaller Done, I followed the commit message convention/style of this repo.

@stefanhaller stefanhaller force-pushed the feature/custom_command_multiple_contexts branch from 9720514 to 542030f Compare August 2, 2024 09:56
Copy link
Collaborator

@stefanhaller stefanhaller left a comment

Choose a reason for hiding this comment

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

Nice, thanks. I took the liberty of making the remaining commit history changes that I suggested myself.

Comment on lines 47 to 48
t.Shell().DeleteFile("myfile")
t.GlobalPress(keys.Files.RefreshFiles)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would drop these two lines; they are unnecessary because no file was actually created in this case.

@@ -52,7 +52,7 @@ func (self *KeybindingCreator) call(customCommand config.CustomCommand, handler

func (self *KeybindingCreator) getViewNamesAndContexts(customCommand config.CustomCommand) ([]string, error) {
if customCommand.Context == "global" {
return []string{}, nil
return []string{""}, nil
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd squash this change into the commit that implements the feature.

@stefanhaller stefanhaller merged commit 74fe069 into jesseduffield:master Aug 2, 2024
15 checks passed
tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Sep 23, 2024
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [jesseduffield/lazygit](https://github.com/jesseduffield/lazygit) | minor | `v0.43.1` -> `v0.44.1` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>jesseduffield/lazygit (jesseduffield/lazygit)</summary>

### [`v0.44.1`](https://github.com/jesseduffield/lazygit/releases/tag/v0.44.1)

[Compare Source](jesseduffield/lazygit@v0.44.0...v0.44.1)

#### What's Changed

##### Enhancements 🔥

-   With stacked branches, create fixup commit at the end of the branch it belongs to by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3892
-   Add options for disabling switching to the Files panel after popping or applying a stash by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3913

##### Fixes 🔧

-   Fix crash when viewing the divergence of a branch which is up to date with its upstream by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3918

##### Performance improvements 📊

-   Improve performance with large numbers of untracked or modified files by [@&#8203;parthokunda](https://github.com/parthokunda) in jesseduffield/lazygit#3919

##### I18n 🌎

-   Update language files from Crowdin by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3898

#### New Contributors

-   [@&#8203;parthokunda](https://github.com/parthokunda) made their first contribution in jesseduffield/lazygit#3919

**Full Changelog**: jesseduffield/lazygit@v0.44.0...v0.44.1

### [`v0.44.0`](https://github.com/jesseduffield/lazygit/releases/tag/v0.44.0)

[Compare Source](jesseduffield/lazygit@v0.43.1...v0.44.0)

#### What's Changed

Lots of great changes in this release. Thanks to everybody who contributed!

##### Enhancements 🔥

-   Per-repo config files (and reloading of edited config files) by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3787
    -   In addition to the global config file you can now create repo-specific config files in `<repo>/.git/lazygit.yml`. Settings in these files override settings in the global config file. In addition, files called `.lazygit.yml` in any of the parent directories of a repo will also be loaded; this can be useful if you have settings that you want to apply to a group of repositories.
    -   We now also automatically apply (most) config changes without the need to restart lazygit
-   Easily view diff across range of commits by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3869
    -   If you select a range of commits, we now show the diff across the range (inclusive). This makes it easy to see the total changes across a number of commits. Likewise, if you press enter when a range of commits are selected, we will show the changed files for the range.

https://github.com/user-attachments/assets/6646c78b-5770-41c1-93b9-5442d32404de

-   Support hyperlinks from pagers by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3825
    -   If you're using delta as a pager (which I highly recommend trying), you can now click on line numbers to go to that line in your editor by using the following config:
    ```yaml
    git:
      paging:
        colorArg: always
        pager: delta --paging=never --line-numbers --hyperlinks --hyperlinks-file-link-format="lazygit-edit://{path}:{line}"
    ```

https://github.com/user-attachments/assets/75fef6c4-d437-4595-ab00-b8990215cfed

-   Switch to Files panel after popping/applying a stash by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3888
    -   This is a nice quality of life improvement. You generally want to go straight to the files panel after you pop or apply from the stash
-   Ask to auto-stage unstaged files when continuing a rebase after resolving conflicts by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3879
    -   Another quality of life improvement: often you resolve some conflicts, then make another couple changes, then in lazygit you say to continue and you get an error saying there are unstaged changes. Now instead of showing an error, lazygit asks if you want to stage those changes and continue.
-   Offer autostash option when creating new branch by [@&#8203;brandondong](https://github.com/brandondong) in jesseduffield/lazygit#3871
    -   Another great quality of life improvement
-   Allow using shell aliases in interactive custom commands by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3793
-   Switch tabs with panel jump keys by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3794
    -   If you've already been using the number keys (1-5) for jumping to specific side panels, you'll be pleased to know that you can now also use those keys for jumping to tabs within a side panel. E.g. to go to the reflog panel, you can now press 4 to jump to the commits panel, then 4 again to go to the reflog tab.
-   Rename "Custom Command" to "Shell Command" by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3800
    -   There are two ways of invoking a 'custom' command in Lazygit: first by pre-defining a command in your config, and second by pressing ':' and typing in the command directly. We referred to both of these as 'custom commands' which was confusing. We now refer to the second approach as invoking a 'shell command'.
-   Improve template placeholders for custom commands by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3809
    -   Now you can use `SelectedCommit` to refer to the selected commit regardless of which commits panel you're in (local commits, reflog, etc)
    -   Likewise, you can use `SelectedPath` whether you're in the files panel or the commit-files panel.
-   feat(custom command): support multiple contexts within one command by [@&#8203;yam-liu](https://github.com/yam-liu) in jesseduffield/lazygit#3784
    -   You can now use a comma-separated list of contexts for which a custom command can be invoked. For example:
    ```yaml
    customCommands:
      - description: 'Add empty commit'
        key: 'E'
        context: 'commits,files'
    ```
-   Improve mouse support for commit message panel by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3836
-   Make auto-staging resolved conflicts optional by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3870
    -   If you set `git.autoStageResolvedConflicts` to false in your config, lazygit will no longer auto-stage files in which you've resolved merge conflicts.
-   Allow using `<`/`>` and `,`/`.` in sticky range select mode in patch explorer by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3837
-   Add Zed editor support to editorPreset by [@&#8203;susl](https://github.com/susl) in jesseduffield/lazygit#3886

##### Fixes 🔧

-   Allow GPG reword for last commit by [@&#8203;Neko-Box-Coder](https://github.com/Neko-Box-Coder) in jesseduffield/lazygit#3815
-   Don't exit app when GetRepoPaths call fails during startup by [@&#8203;ppoum](https://github.com/ppoum) in jesseduffield/lazygit#3779
-   Fix lack of icon when extension isn't lowercase by [@&#8203;hasecilu](https://github.com/hasecilu) in jesseduffield/lazygit#3810
-   Fix redraw bug (stale content) in commits view by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3783
-   Fix line coloring when using the delta pager by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3820
-   Fix pressing escape after clicking in diff view by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3828
-   Fix fast-forward issue caused by a conflicting tag name [@&#8203;Neko-Box-Coder](https://github.com/Neko-Box-Coder) in jesseduffield/lazygit#3807
-   Fix range select -> stage failure when deleted file is already staged by [@&#8203;brandondong](https://github.com/brandondong) in jesseduffield/lazygit#3631
-   Scroll views up if needed to show all their content by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3839
-   Fix crash when filtering commits by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3838
-   Fix cancelled autostash resulting in stuck inline status by [@&#8203;brandondong](https://github.com/brandondong) in jesseduffield/lazygit#3860
-   Don't allow opening a menu while the search or filter prompt is open by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3878

##### Maintenance ⚙️

-   Reapply "Check for fixup commits on CI" by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3745
-   Some cleanups for APIs related to contexts by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3808
-   Improve fixup commits script by [@&#8203;jesseduffield](https://github.com/jesseduffield) in jesseduffield/lazygit#3853
-   Fix linter warnings by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3854
-   Add codespell support (config, workflow to detect/not fix) and make it fix few typos by [@&#8203;yarikoptic](https://github.com/yarikoptic) in jesseduffield/lazygit#3751
-   Get rid of a lot of error return values by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3890
-   Add a readme file for the JSON files in pkg/i18n/translations by [@&#8203;stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3781

#### New Contributors

-   [@&#8203;yam-liu](https://github.com/yam-liu) made their first contribution in jesseduffield/lazygit#3784
-   [@&#8203;ppoum](https://github.com/ppoum) made their first contribution in jesseduffield/lazygit#3779
-   [@&#8203;Neko-Box-Coder](https://github.com/Neko-Box-Coder) made their first contribution in jesseduffield/lazygit#3815
-   [@&#8203;yarikoptic](https://github.com/yarikoptic) made their first contribution in jesseduffield/lazygit#3751
-   [@&#8203;susl](https://github.com/susl) made their first contribution in jesseduffield/lazygit#3886

**Full Changelog**: jesseduffield/lazygit@v0.43.1...v0.44.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever MR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40NDAuNyIsInVwZGF0ZWRJblZlciI6IjM3LjQ0MC43IiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJSZW5vdmF0ZSBCb3QiXX0=-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants