Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating a branch with a default name #3598

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions docs/Config.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ git:
# Replace directive. E.g. for 'feature/AB-123' to start the commit message with 'AB-123 ' use "[$1] "
replace: ""

# See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix
branchPrefix: ""

# Config for showing the log in the commits view
log:
# One of: 'date-order' | 'author-date-order' | 'topo-order' | 'default'
Expand Down Expand Up @@ -801,6 +804,30 @@ 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/"
```

For repository-specific prefixes, you can map them with `branchPrefixes`. If you have both `branchPrefix` defined and an entry in `branchPrefixes` for the current repo, the `branchPrefix` entry is given higher precedence. Repository folder names must be an exact match.

```yaml
git:
branchPrefixes:
my_project: > # This is repository folder name
"lastfirst/"
```

## 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
6 changes: 6 additions & 0 deletions pkg/config/user_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ 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"`
// See https://github.com/jesseduffield/lazygit/blob/master/docs/Config.md#predefined-branch-name-prefix
BranchPrefixes map[string]string `yaml:"branchPrefixes"`
// 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 @@ -727,6 +731,8 @@ 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: "",
BranchPrefixes: map[string]string(nil),
ParseEmoji: false,
TruncateCopiedCommitHashesTo: 12,
},
Expand Down
13 changes: 13 additions & 0 deletions pkg/gui/controllers/helpers/refs_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ func (self *RefsHelper) NewBranch(from string, fromFormattedName string, suggest
},
)

if suggestedBranchName == "" {
suggestedBranchName = self.branchPrefixConfigForRepo()
}

return self.c.Prompt(types.PromptOpts{
Title: message,
InitialContent: suggestedBranchName,
Expand Down Expand Up @@ -318,3 +322,12 @@ func (self *RefsHelper) ParseRemoteBranchName(fullBranchName string) (string, st

return remoteName, branchName, true
}

func (self *RefsHelper) branchPrefixConfigForRepo() string {
cfg, ok := self.c.UserConfig.Git.BranchPrefixes[self.c.Git().RepoPaths.RepoName()]
if ok {
return cfg
}

return self.c.UserConfig.Git.BranchPrefix
}
41 changes: 41 additions & 0 deletions pkg/integration/tests/commit/new_branch_with_prefix.go
Original file line number Diff line number Diff line change
@@ -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"),
)
},
})
42 changes: 42 additions & 0 deletions pkg/integration/tests/commit/new_branch_with_prefixes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package commit

import (
"github.com/jesseduffield/lazygit/pkg/config"
. "github.com/jesseduffield/lazygit/pkg/integration/components"
)

var NewBranchWithPrefixes = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Creating a new branch from a commit with a default name for a specific repo",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(cfg *config.AppConfig) {
cfg.UserConfig.Git.BranchPrefix = "myprefix/"
cfg.UserConfig.Git.BranchPrefixes = map[string]string{"repo": "ourprefix/"}
},
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("ourprefix/" + branchName)
}).
Lines(
Contains("commit 2"),
Contains("commit 1"),
)
},
})
2 changes: 2 additions & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ var tests = []*components.IntegrationTest{
commit.History,
commit.HistoryComplex,
commit.NewBranch,
commit.NewBranchWithPrefix,
commit.NewBranchWithPrefixes,
commit.PreserveCommitMessage,
commit.ResetAuthor,
commit.Revert,
Expand Down
11 changes: 11 additions & 0 deletions schema/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,17 @@
"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"
},
"branchPrefixes": {
"additionalProperties": {
"type": "string"
},
"type": "object",
"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