From 6cf75af0afbec83a269b313d9253bb32b758737d Mon Sep 17 00:00:00 2001 From: Kristijan Husak Date: Wed, 15 Apr 2020 17:27:42 +0200 Subject: Add option to set predefined commit message prefix. Fixes #760. --- docs/Config.md | 16 ++++++++++++++++ pkg/gui/files_panel.go | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/docs/Config.md b/docs/Config.md index 3d9caba36..4becd364f 100644 --- a/docs/Config.md +++ b/docs/Config.md @@ -269,3 +269,19 @@ Where: - `gitDomain` stands for the domain used by git itself (i.e. the one present on clone URLs), e.g. `git.work.com` - `provider` is one of `github`, `bitbucket` or `gitlab` - `webDomain` is the URL where your git service exposes a web interface and APIs, e.g. `gitservice.work.com` + +## Predefined commit message prefix +In situations where certain naming pattern is used for branches and commits, pattern can be used to populate +commit message with prefix that is parsed from the branch name. + +Example: +* Branch name: feature/AB-123 +* Commit message: [AB-123] Adding feature + +```yaml + git: + commitPrefixes: + my_project: # This is repository folder name + pattern: "^\\w+\\/(\\w+-\\w+)" + replace: "[$1] " +``` diff --git a/pkg/gui/files_panel.go b/pkg/gui/files_panel.go index 635ca69dd..8129b02a4 100644 --- a/pkg/gui/files_panel.go +++ b/pkg/gui/files_panel.go @@ -8,11 +8,13 @@ import ( // "strings" "fmt" + "regexp" "strings" "github.com/jesseduffield/gocui" "github.com/jesseduffield/lazygit/pkg/commands" "github.com/jesseduffield/lazygit/pkg/gui/presentation" + "github.com/jesseduffield/lazygit/pkg/utils" ) // list panel functions @@ -286,6 +288,17 @@ func (gui *Gui) handleCommitPress(g *gocui.Gui, filesView *gocui.View) error { return gui.createErrorPanel(gui.Tr.SLocalize("NoStagedFilesToCommit")) } commitMessageView := gui.getCommitMessageView() + prefixPattern := gui.Config.GetUserConfig().GetString("git.commitPrefixes." + utils.GetCurrentRepoName() + ".pattern") + prefixReplace := gui.Config.GetUserConfig().GetString("git.commitPrefixes." + utils.GetCurrentRepoName() + ".replace") + if len(prefixPattern) > 0 && len(prefixReplace) > 0 { + rgx := regexp.MustCompile(prefixPattern) + prefix := rgx.ReplaceAllString(gui.getCheckedOutBranch().Name, prefixReplace) + gui.renderString(g, "commitMessage", prefix) + if err := commitMessageView.SetCursor(len(prefix), 0); err != nil { + return err + } + } + g.Update(func(g *gocui.Gui) error { if _, err := g.SetViewOnTop("commitMessage"); err != nil { return err -- cgit v1.2.3