Skip to content

Commit d3780fd

Browse files
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
2 parents 436240b + 5959f7b commit d3780fd

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

docs/Config.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,9 @@ git:
323323
# Replace directive. E.g. for 'feature/AB-123' to start the commit message with 'AB-123 ' use "[$1] "
324324
replace: ""
325325

326+
# See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix
327+
branchPrefix: ""
328+
326329
# If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀
327330
# (This should really be under 'gui', not 'git')
328331
parseEmoji: false
@@ -885,6 +888,21 @@ git:
885888
replace: '[$1] '
886889
```
887890

891+
## Predefined branch name prefix
892+
893+
In situations where certain naming pattern is used for branches, this can be used to populate new branch creation with a static prefix.
894+
895+
Example:
896+
897+
Some branches:
898+
- jsmith/AB-123
899+
- cwilson/AB-125
900+
901+
```yaml
902+
git:
903+
branchPrefix: "firstlast/"
904+
```
905+
888906
## Custom git log command
889907

890908
You can override the `git log` command that's used to render the log of the selected branch like so:

pkg/config/user_config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,8 @@ type GitConfig struct {
236236
CommitPrefix *CommitPrefixConfig `yaml:"commitPrefix"`
237237
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix
238238
CommitPrefixes map[string]CommitPrefixConfig `yaml:"commitPrefixes"`
239+
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix
240+
BranchPrefix string `yaml:"branchPrefix"`
239241
// If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀
240242
// (This should really be under 'gui', not 'git')
241243
ParseEmoji bool `yaml:"parseEmoji"`
@@ -750,6 +752,7 @@ func GetDefaultConfig() *UserConfig {
750752
AllBranchesLogCmd: "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium",
751753
DisableForcePushing: false,
752754
CommitPrefixes: map[string]CommitPrefixConfig(nil),
755+
BranchPrefix: "",
753756
ParseEmoji: false,
754757
TruncateCopiedCommitHashesTo: 12,
755758
},

pkg/gui/controllers/helpers/refs_helper.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
274274
},
275275
)
276276

277+
if suggestedBranchName == "" {
278+
suggestedBranchName = self.c.UserConfig.Git.BranchPrefix
279+
}
280+
277281
return self.c.Prompt(types.PromptOpts{
278282
Title: message,
279283
InitialContent: suggestedBranchName,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package commit
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/config"
5+
. "github.com/jesseduffield/lazygit/pkg/integration/components"
6+
)
7+
8+
var NewBranchWithPrefix = NewIntegrationTest(NewIntegrationTestArgs{
9+
Description: "Creating a new branch from a commit with a default name",
10+
ExtraCmdArgs: []string{},
11+
Skip: false,
12+
SetupConfig: func(cfg *config.AppConfig) {
13+
cfg.UserConfig.Git.BranchPrefix = "myprefix/"
14+
},
15+
SetupRepo: func(shell *Shell) {
16+
shell.
17+
EmptyCommit("commit 1")
18+
},
19+
Run: func(t *TestDriver, keys config.KeybindingConfig) {
20+
t.Views().Commits().
21+
Focus().
22+
Lines(
23+
Contains("commit 1").IsSelected(),
24+
).
25+
SelectNextItem().
26+
Press(keys.Universal.New).
27+
Tap(func() {
28+
branchName := "my-branch-name"
29+
t.ExpectPopup().Prompt().Title(Contains("New branch name")).Type(branchName).Confirm()
30+
t.Git().CurrentBranchName("myprefix/" + branchName)
31+
})
32+
},
33+
})

pkg/integration/tests/test_list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ var tests = []*components.IntegrationTest{
9292
commit.History,
9393
commit.HistoryComplex,
9494
commit.NewBranch,
95+
commit.NewBranchWithPrefix,
9596
commit.PasteCommitMessage,
9697
commit.PasteCommitMessageOverExisting,
9798
commit.PreserveCommitMessage,

schema/config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@
638638
"type": "object",
639639
"description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix"
640640
},
641+
"branchPrefix": {
642+
"type": "string",
643+
"description": "See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix"
644+
},
641645
"parseEmoji": {
642646
"type": "boolean",
643647
"description": "If true, parse emoji strings in commit messages e.g. render :rocket: as 🚀\n(This should really be under 'gui', not 'git')",

0 commit comments

Comments
 (0)