1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-04-19 12:12:42 +02:00

Allow setting a default name when creating new branches (#3487)

- **PR Description**

I commonly prefix my branch names with my first initial and last name,
such as this one (`ecubit/branch-prefixes`). It can be a bit annoying to
type out.

This PR adds a config option to set a default value for the name in the
branch creation modal.

If there would have previously been a branch name autofilled (I do not
know all such cases), this change has no effect.

- **Please check if the PR fulfills these requirements**

* [x] Cheatsheets are up-to-date (run `go generate ./...`)
* [x] Code has been formatted (see
[here](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#code-formatting))
* [x] 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](https://github.com/jesseduffield/lazygit/blob/master/CONTRIBUTING.md#internationalisation))
- `CONTRIBUTING.md` says I may submit without doing localization (and I
am unable)
* [x] Docs (specifically `docs/Config.md`) have been updated if
necessary
* [x] You've read through your own file changes for silly mistakes etc
This commit is contained in:
Jesse Duffield 2024-07-06 21:16:31 +10:00 committed by GitHub
commit d3780fd57d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 63 additions and 0 deletions

View File

@ -323,6 +323,9 @@ git:
# Replace directive. E.g. for 'feature/AB-123' to start the commit message with 'AB-123 ' use "[$1] " # Replace directive. E.g. for 'feature/AB-123' to start the commit message with 'AB-123 ' use "[$1] "
replace: "" replace: ""
# See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix
branchPrefix: ""
# If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀 # If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀
# (This should really be under 'gui', not 'git') # (This should really be under 'gui', not 'git')
parseEmoji: false parseEmoji: false
@ -885,6 +888,21 @@ git:
replace: '[$1] ' replace: '[$1] '
``` ```
## Predefined branch name prefix
In situations where certain naming pattern is used for branches, this can be used to populate new branch creation with a static prefix.
Example:
Some branches:
- jsmith/AB-123
- cwilson/AB-125
```yaml
git:
branchPrefix: "firstlast/"
```
## Custom git log command ## Custom git log command
You can override the `git log` command that's used to render the log of the selected branch like so: You can override the `git log` command that's used to render the log of the selected branch like so:

View File

@ -236,6 +236,8 @@ type GitConfig struct {
CommitPrefix *CommitPrefixConfig `yaml:"commitPrefix"` CommitPrefix *CommitPrefixConfig `yaml:"commitPrefix"`
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix // See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix
CommitPrefixes map[string]CommitPrefixConfig `yaml:"commitPrefixes"` CommitPrefixes map[string]CommitPrefixConfig `yaml:"commitPrefixes"`
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix
BranchPrefix string `yaml:"branchPrefix"`
// If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀 // If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀
// (This should really be under 'gui', not 'git') // (This should really be under 'gui', not 'git')
ParseEmoji bool `yaml:"parseEmoji"` ParseEmoji bool `yaml:"parseEmoji"`
@ -750,6 +752,7 @@ func GetDefaultConfig() *UserConfig {
AllBranchesLogCmd: "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium", AllBranchesLogCmd: "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium",
DisableForcePushing: false, DisableForcePushing: false,
CommitPrefixes: map[string]CommitPrefixConfig(nil), CommitPrefixes: map[string]CommitPrefixConfig(nil),
BranchPrefix: "",
ParseEmoji: false, ParseEmoji: false,
TruncateCopiedCommitHashesTo: 12, TruncateCopiedCommitHashesTo: 12,
}, },

View File

@ -274,6 +274,10 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
}, },
) )
if suggestedBranchName == "" {
suggestedBranchName = self.c.UserConfig.Git.BranchPrefix
}
return self.c.Prompt(types.PromptOpts{ return self.c.Prompt(types.PromptOpts{
Title: message, Title: message,
InitialContent: suggestedBranchName, InitialContent: suggestedBranchName,

View File

@ -0,0 +1,33 @@
package commit
import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)
var NewBranchWithPrefix = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Creating a new branch from a commit with a default name",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(cfg *config.AppConfig) {
cfg.UserConfig.Git.BranchPrefix = "myprefix/"
},
SetupRepo: func(shell *Shell) {
shell.
EmptyCommit("commit 1")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("commit 1").IsSelected(),
).
SelectNextItem().
Press(keys.Universal.New).
Tap(func() {
branchName := "my-branch-name"
t.ExpectPopup().Prompt().Title(Contains("New branch name")).Type(branchName).Confirm()
t.Git().CurrentBranchName("myprefix/" + branchName)
})
},
})

View File

@ -92,6 +92,7 @@ var tests = []*components.IntegrationTest{
commit.History, commit.History,
commit.HistoryComplex, commit.HistoryComplex,
commit.NewBranch, commit.NewBranch,
commit.NewBranchWithPrefix,
commit.PasteCommitMessage, commit.PasteCommitMessage,
commit.PasteCommitMessageOverExisting, commit.PasteCommitMessageOverExisting,
commit.PreserveCommitMessage, commit.PreserveCommitMessage,

View File

@ -638,6 +638,10 @@
"type": "object", "type": "object",
"description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix" "description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix"
}, },
"branchPrefix": {
"type": "string",
"description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix"
},
"parseEmoji": { "parseEmoji": {
"type": "boolean", "type": "boolean",
"description": "If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀\n(This should really be under 'gui', not 'git')", "description": "If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀\n(This should really be under 'gui', not 'git')",