-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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 runCommand
function to Go template syntax + add support for templates in git branchPrefix
setting
#4438
base: master
Are you sure you want to change the base?
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. |
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\"" }}/' ```
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. |
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: