Skip to content

Commit

Permalink
Allow setting a default name when creating new branches
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotcubit committed May 7, 2024
1 parent 618fe53 commit 3a5b0d0
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/Config.md
Expand Up @@ -566,6 +566,21 @@ git:
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

You can override the `git log` command that's used to render the log of the selected branch like so:
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/user_config.go
Expand Up @@ -225,6 +225,8 @@ type GitConfig struct {
CommitPrefix *CommitPrefixConfig `yaml:"commitPrefix"`
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-commit-message-prefix
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 🚀
// (This should really be under 'gui', not 'git')
ParseEmoji bool `yaml:"parseEmoji"`
Expand Down Expand Up @@ -724,6 +726,7 @@ func GetDefaultConfig() *UserConfig {
AllBranchesLogCmd: "git log --graph --all --color=always --abbrev-commit --decorate --date=relative --pretty=medium",
DisableForcePushing: false,
CommitPrefixes: map[string]CommitPrefixConfig(nil),
BranchPrefix: "",
ParseEmoji: false,
TruncateCopiedCommitHashesTo: 12,
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/gui/controllers/helpers/refs_helper.go
Expand Up @@ -272,6 +272,10 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
},
)

if suggestedBranchName == "" {
suggestedBranchName = self.c.UserConfig.Git.BranchPrefix
}

return self.c.Prompt(types.PromptOpts{
Title: message,
InitialContent: suggestedBranchName,
Expand Down
41 changes: 41 additions & 0 deletions pkg/integration/tests/commit/new_branch_with_prefix.go
@@ -0,0 +1,41 @@
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").
EmptyCommit("commit 2").
EmptyCommit("commit 3")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("commit 3").IsSelected(),
Contains("commit 2"),
Contains("commit 1"),
).
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)
}).
Lines(
Contains("commit 2"),
Contains("commit 1"),
)
},
})
1 change: 1 addition & 0 deletions pkg/integration/tests/test_list.go
Expand Up @@ -83,6 +83,7 @@ var tests = []*components.IntegrationTest{
commit.History,
commit.HistoryComplex,
commit.NewBranch,
commit.NewBranchWithPrefix,
commit.PreserveCommitMessage,
commit.ResetAuthor,
commit.Revert,
Expand Down
4 changes: 4 additions & 0 deletions schema/config.json
Expand Up @@ -588,6 +588,10 @@
"type": "object",
"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": {
"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')"
Expand Down

0 comments on commit 3a5b0d0

Please sign in to comment.