diff --git a/docs/Config.md b/docs/Config.md index cf78a3113fe..823b0a1dd52 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -555,6 +555,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: diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index 20a404b16ab..f45118d7fd2 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -218,6 +218,8 @@ type GitConfig struct { DisableForcePushing bool `yaml:"disableForcePushing"` // 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-message-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"` @@ -715,6 +717,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, }, diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go index 6ddff118c5a..b531a709ac5 100644 --- a/pkg/gui/controllers/helpers/refs_helper.go +++ b/pkg/gui/controllers/helpers/refs_helper.go @@ -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{ Title: message, InitialContent: suggestedBranchName, diff --git a/pkg/integration/tests/commit/new_branch_with_prefix.go b/pkg/integration/tests/commit/new_branch_with_prefix.go new file mode 100644 index 00000000000..e8656689e3c --- /dev/null +++ b/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"), + ) + }, +}) diff --git a/pkg/integration/tests/test_list.go b/pkg/integration/tests/test_list.go index 8f62ca7f5ee..3f51a5c984d 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -81,6 +81,7 @@ var tests = []*components.IntegrationTest{ commit.History, commit.HistoryComplex, commit.NewBranch, + commit.NewBranchWithPrefix, commit.PreserveCommitMessage, commit.ResetAuthor, commit.Revert, diff --git a/schema/config.json b/schema/config.json index 21860c39ceb..f1914ed4663 100644 --- a/schema/config.json +++ b/schema/config.json @@ -550,6 +550,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-message-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')"