From 3a5b0d0e50ebc0743a306dd68e4b98a46cca9b60 Mon Sep 17 00:00:00 2001 From: Elliot Cubit Date: Tue, 9 Apr 2024 15:41:26 -0400 Subject: [PATCH] Allow setting a default name when creating new branches --- docs/Config.md | 15 +++++++ pkg/config/user_config.go | 3 ++ pkg/gui/controllers/helpers/refs_helper.go | 4 ++ .../tests/commit/new_branch_with_prefix.go | 41 +++++++++++++++++++ pkg/integration/tests/test_list.go | 1 + schema/config.json | 4 ++ 6 files changed, 68 insertions(+) create mode 100644 pkg/integration/tests/commit/new_branch_with_prefix.go diff --git a/docs/Config.md b/docs/Config.md index d85637429c3..293e43fae90 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -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: diff --git a/pkg/config/user_config.go b/pkg/config/user_config.go index c225944613a..5df4ce0b0b5 100644 --- a/pkg/config/user_config.go +++ b/pkg/config/user_config.go @@ -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"` @@ -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, }, diff --git a/pkg/gui/controllers/helpers/refs_helper.go b/pkg/gui/controllers/helpers/refs_helper.go index d837d826647..9081acd13af 100644 --- a/pkg/gui/controllers/helpers/refs_helper.go +++ b/pkg/gui/controllers/helpers/refs_helper.go @@ -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, 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 1c94e3bbd6a..91ec7e50f1f 100644 --- a/pkg/integration/tests/test_list.go +++ b/pkg/integration/tests/test_list.go @@ -83,6 +83,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 05a2db1517f..136e569f232 100644 --- a/schema/config.json +++ b/schema/config.json @@ -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')"