-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add runCommand
function to Go template syntax + add support for templates in git branchPrefix
setting
#4438
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
Conversation
@jesseduffield I know you're probably busy, but do you have any advice on where to add the tests? Then I can finish the PR. Thanks for this amazing program, I love it! |
Happy to step in for Jesse, but I don't understand what you're trying to do. 😅 I understand the example with the What I don't understand is the branchPrefix example. We don't expand templates in that context, or do we? Are you planning on adding that; if so, how? Your exported formatDate function is very specific to custom commands. |
That's a good suggestion. What about
You're right. My idea was to work on adding support for that next, in a similar way. Before this PR, I had this custom command to create a branch (using git town) - context: "files, localBranches, commits, subCommits"
key: "a"
description: "Git Town Hack"
prompts:
- type: "input"
title: "Branch name (ruudk/yyyy/m will be added automatically)."
key: "BranchName"
command: 'git town hack "ruudk/$(date +"%Y/%-m")/{{.Form.BranchName}}"'
stream: true As you can see, the title now has to say something like Even better, to use it in initialValue so that you can modify it, for situations where you don't want the prefix. With the current PR, you can use the formatted dates in the |
Based on: - context: "files, localBranches, commits, subCommits"
key: "a"
description: "Git Town Append"
prompts:
- type: "input"
title: 'Branch name to append from "{{.CheckedOutBranch.Name}}"'
key: "BranchName"
initialValue: 'ruudk/{{ formatCurrentDate "2006/1" }}/'
command: 'git town append {{.Form.BranchName | quote}}'
stream: true
loadingText: "Appending" |
formatCurrentDate
function to Go template syntax + add support for templates in git branchPrefix
setting
Ok, now I understand better what you are trying to do. I do have to wonder if it would be better to expose a general |
That would be more flexible for sure! Do you think it's hard to do? I can give it a try if you steer me in the right direction |
I don't think it would be hard at all; the only tricky thing is to decide how to handle errors. And maybe whether it's a problem if the command returns multi-line output; we could consider replacing line feeds with spaces (like echo does), or at least trim trailing newlines. Anyway, something like this in your "runCommand": func(command string) string {
output, err := self.c.Git().Custom.RunWithOutput(command)
if err != nil {
return err.Error() // is this the best way to handle errors?
}
return output |
It's working 🎉 branchPrefix: 'ruudk/{{ runCommand "date +\"%Y/%-m\"" }}/'
initialValue: 'ruudk/{{ runCommand "date +\"%Y/%-m\"" }}/' |
Nice! Now to your question about tests and documentation. It's true that the quote function has neither tests nor documentation. I'm not sure it needs tests, but adding a bit of documentation would be nice; this could simply go somewhere in As for tests: yes, it would be good to have some for your new function, and I think integration tests would be best. For custom commands you could use Also, can you please squash the commits, the commit history is not reviewable like this. |
Thanks for the guidance @stefanhaller, I'll follow up later with the changes. |
76b40ae
to
0e32c5e
Compare
@stefanhaller I applied your feedback and added the docs and tests. Could you approve the workflow so we can see if it works correctly? Thanks! |
formatCurrentDate
function to Go template syntax + add support for templates in git branchPrefix
settingrunCommand
function to Go template syntax + add support for templates in git branchPrefix
setting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice; I pushed a few fixups, I sometimes find this easier than explaining what I would like to see changed. (In one case I did both now though. 😄)
Feel free to squash them if you agree.
The final thing to do would be to update the PR description, it is out of date with what we are doing here now. This is important because it will end up in the commit message of the merge commit.
@stefanhaller Thank you! I updated the PR description and fixed up all the commits. |
567bbb2
to
34c9166
Compare
@stefanhaller You did a rebase? Let's see if it's still green. |
I only fixed the subject of the first commit, and the description of one of the tests. Shouldn't have changed anything. |
I'll wait with merging until @jesseduffield had a chance to look at the approach we're taking here (just in case), and also to see if the folks over in #4183 have anything to say about it. They were trying to solve the same problem there, but with a different approach. |
@jesseduffield I'm curious to hear your opinion about this. Are there things that I should change? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -329,7 +330,15 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest | |||
) | |||
|
|||
if suggestedBranchName == "" { | |||
suggestedBranchName = self.c.UserConfig().Git.BranchPrefix | |||
var err error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need to be declared separately?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think so. Without this you'd have to say :=
below, but that makes suggestedBranchName
a new variable in that scope, so that doesn't work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yes, makes sense
This makes it possible to use date and time in initial values like this: ```yaml initialValue: 'ruudk/{{ runCommand "date +\"%Y/%-m\"" }}/' ``` I want to use this to configure my BranchPrefix like this: ```yaml git: branchPrefix: 'ruudk/{{ runCommand "date +\"%Y/%-m\"" }}/' ```
34c9166
to
1282048
Compare
This MR contains the following updates: | Package | Update | Change | |---|---|---| | [jesseduffield/lazygit](https://github.com/jesseduffield/lazygit) | minor | `v0.48.0` -> `v0.50.0` | 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.50.0`](https://github.com/jesseduffield/lazygit/releases/tag/v0.50.0) [Compare Source](jesseduffield/lazygit@v0.49.0...v0.50.0) <!-- Release notes generated using configuration in .github/release.yml at v0.50.0 --> #### What's Changed ##### Enhancements 🔥 - Continue/abort a conflicted cherry-pick or revert by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4441 - Show todo items for pending cherry-picks and reverts by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4442 - Use "git cherry-pick" for implementing copy/paste of commits by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4443 - Allow reverting a range of commits by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4444 - Section headers for rebase todos and actual commits by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4463 - Focus the main view by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4429 - Auto-forward main branches after fetching by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4493 - Add new command "Move commits to new branch" by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#3876 - Strip the '+' and '-' characters when copying parts of a diff to the clipboard by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4519 - Reduce memory consumption of graph (pipe sets) by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4498 ##### Fixes 🔧 - Fix truncation of branches when scrolling branches panel to the left by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4483 - Fix nvim-remote commands for fish shell by [@​SavingFrame](https://github.com/SavingFrame) in jesseduffield/lazygit#4506 - Disallow creating custom patches when the diff context size is 0 by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4522 - Remove double space between rebase todo and author columns by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4520 ##### Maintenance ⚙️ - Allow closing issues via github actions by [@​jesseduffield](https://github.com/jesseduffield) in jesseduffield/lazygit#4515 ##### Docs 📖 - Add Debian installation instructions alongside Ubuntu by [@​jmkim](https://github.com/jmkim) in jesseduffield/lazygit#4501 - fix wording of random tip by [@​dawedawe](https://github.com/dawedawe) in jesseduffield/lazygit#4488 #### New Contributors - [@​jmkim](https://github.com/jmkim) made their first contribution in jesseduffield/lazygit#4501 - [@​SavingFrame](https://github.com/SavingFrame) made their first contribution in jesseduffield/lazygit#4506 - [@​dawedawe](https://github.com/dawedawe) made their first contribution in jesseduffield/lazygit#4488 **Full Changelog**: jesseduffield/lazygit@v0.49.0...v0.50.0 ### [`v0.49.0`](https://github.com/jesseduffield/lazygit/releases/tag/v0.49.0) [Compare Source](jesseduffield/lazygit@v0.48.0...v0.49.0) <!-- Release notes generated using configuration in .github/release.yml at v0.49.0 --> #### What's Changed ##### Enhancements 🔥 - Support fish when running shell command by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4350 - Add acme editor preset by [@​rakoo](https://github.com/rakoo) in jesseduffield/lazygit#4360 - Support home and end as alternatives to '<' and '>' by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4396 - Drop the git config cache when getting focus by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4376 - Add a "Content of selected file" entry to the copy menu for commit files by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4341 - Add root node in file tree by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4346 - \[FEAT] Add Recursive Bulk Initialize and Update for Submodules by [@​cesarandr](https://github.com/cesarandr) in jesseduffield/lazygit#4259 - Commit without pre-commit hooks action is independent on prefix by [@​kschweiger](https://github.com/kschweiger) in jesseduffield/lazygit#4374 - Let users define custom icons and color for files on the config file by [@​hasecilu](https://github.com/hasecilu) in jesseduffield/lazygit#4395 - Add "Absolute path" item to the file view's copy menu by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4410 - Allow range drop stashes by [@​gaogao-qwq](https://github.com/gaogao-qwq) in jesseduffield/lazygit#4451 - More navigation keybindings for confirmation panel by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4404 - Resolve non-inline merge conflicts by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4431 - Add `runCommand` function to Go template syntax + add support for templates in git `branchPrefix` setting by [@​ruudk](https://github.com/ruudk) in jesseduffield/lazygit#4438 - Show "(hooks disabled)" in title bar of commit message editor by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4467 - Add a command to select all commits of the current branch by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4448 ##### Fixes 🔧 - Use a waiting status for rewording a non-head commit by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4343 - Fix layout of options view for non-english languages by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4359 - Fix changing language while lazygit is running by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4361 - URL encode gitlab brackets to make consistent with branch names by [@​ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4336 - Fix commitPrefix setting with empty pattern by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4381 - Commit only tracked files in tracked filter view by [@​parthokunda](https://github.com/parthokunda) in jesseduffield/lazygit#4386 - Revert [#​4313](jesseduffield/lazygit#4313) (Skip post-checkout hook when discarding changes) by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4407 - Enhance support for GPG signed tags by [@​ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4394 - Fix checking out a file from a range selection of commits by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4423 - Fix discarding submodule changes in nested folders by [@​brandondong](https://github.com/brandondong) in jesseduffield/lazygit#4317 - Better support for shell aliases by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4385 - Fix hyperlinks in last line of confirmation popups by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4454 - Fix wrong main view content after pressing `e` in a stack of branches by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4450 - fix: update vscode color to logo color by [@​PeterCardenas](https://github.com/PeterCardenas) in jesseduffield/lazygit#4459 - Fix crash with drag selecting and staging by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4480 - Escape special characters in filenames when git-ignoring files by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4475 ##### Maintenance ⚙️ - Fix linter warnings by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4352 - Fix release schedule again by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4364 - Update to go 1.24 by [@​radsaq](https://github.com/radsaq) in jesseduffield/lazygit#4377 - Add an integration test for a config with a negative refspec by [@​radsaq](https://github.com/radsaq) in jesseduffield/lazygit#4382 - Specify a go release minor version by [@​radsaq](https://github.com/radsaq) in jesseduffield/lazygit#4393 - Fix flaky integration test by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4432 - Some code cleanup by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4449 - Bump the minimum required git version to 2.22 by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4439 - Bump go-git, and get rid of github.com/imdario/mergo by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4460 - Skip date check when release worfklow is manually invoked by [@​jesseduffield](https://github.com/jesseduffield) in jesseduffield/lazygit#4484 ##### Docs 📖 - Include empty arrays and maps in config docs by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4413 - Filter out deprecated user config fields from generated Config.md by [@​karimkhaleel](https://github.com/karimkhaleel) in jesseduffield/lazygit#4416 - Corrected interactive rebase keybind example in README.md by [@​NewtonChutney](https://github.com/NewtonChutney) in jesseduffield/lazygit#4426 - Update kidpix link in README to active url by [@​ChrisMcD1](https://github.com/ChrisMcD1) in jesseduffield/lazygit#4425 ##### I18n 🌎 - Update translation files from Crowdin by [@​stefanhaller](https://github.com/stefanhaller) in jesseduffield/lazygit#4473 #### New Contributors - [@​rakoo](https://github.com/rakoo) made their first contribution in jesseduffield/lazygit#4360 - [@​radsaq](https://github.com/radsaq) made their first contribution in jesseduffield/lazygit#4377 - [@​cesarandr](https://github.com/cesarandr) made their first contribution in jesseduffield/lazygit#4259 - [@​kschweiger](https://github.com/kschweiger) made their first contribution in jesseduffield/lazygit#4374 - [@​NewtonChutney](https://github.com/NewtonChutney) made their first contribution in jesseduffield/lazygit#4426 - [@​gaogao-qwq](https://github.com/gaogao-qwq) made their first contribution in jesseduffield/lazygit#4451 - [@​ruudk](https://github.com/ruudk) made their first contribution in jesseduffield/lazygit#4438 **Full Changelog**: jesseduffield/lazygit@v0.48.0...v0.49.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:eyJjcmVhdGVkSW5WZXIiOiI0MC4xMS4yIiwidXBkYXRlZEluVmVyIjoiNDAuMTEuMiIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90Il19-->
go generate ./...
)This makes it possible to use date and time in initial values like this:
I want to use this to configure my BranchPrefix like this: