Skip to content

Commit

Permalink
Allow force-tagging if tag exists
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanhaller committed Jul 31, 2023
1 parent 71d2fd3 commit d41a195
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 6 deletions.
18 changes: 15 additions & 3 deletions pkg/commands/git_commands/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,35 @@ func NewTagCommands(gitCommon *GitCommon) *TagCommands {
}
}

func (self *TagCommands) CreateLightweight(tagName string, ref string) error {
cmdArgs := NewGitCmd("tag").Arg("--", tagName).
func (self *TagCommands) CreateLightweight(tagName string, ref string, force bool) error {
cmdArgs := NewGitCmd("tag").
ArgIf(force, "--force").
Arg("--", tagName).
ArgIf(len(ref) > 0, ref).
ToArgv()

return self.cmd.New(cmdArgs).Run()
}

func (self *TagCommands) CreateAnnotated(tagName, ref, msg string) error {
func (self *TagCommands) CreateAnnotated(tagName, ref, msg string, force bool) error {
cmdArgs := NewGitCmd("tag").Arg(tagName).
ArgIf(force, "--force").
ArgIf(len(ref) > 0, ref).
Arg("-m", msg).
ToArgv()

return self.cmd.New(cmdArgs).Run()
}

func (self *TagCommands) HasTag(tagName string) bool {
cmdArgs := NewGitCmd("show-ref").
Arg("--tags", "--quiet", "--verify", "--").
Arg("refs/tags/" + tagName).
ToArgv()

return self.cmd.New(cmdArgs).Run() == nil
}

func (self *TagCommands) Delete(tagName string) error {
cmdArgs := NewGitCmd("tag").Arg("-d", tagName).
ToArgv()
Expand Down
29 changes: 26 additions & 3 deletions pkg/gui/controllers/helpers/tags_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/jesseduffield/gocui"
"github.com/jesseduffield/lazygit/pkg/gui/context"
"github.com/jesseduffield/lazygit/pkg/gui/types"
"github.com/jesseduffield/lazygit/pkg/utils"
)

type TagsHelper struct {
Expand All @@ -19,16 +20,16 @@ func NewTagsHelper(c *HelperCommon, commitsHelper *CommitsHelper) *TagsHelper {
}

func (self *TagsHelper) OpenCreateTagPrompt(ref string, onCreate func()) error {
onConfirm := func(tagName string, description string) error {
doCreateTag := func(tagName string, description string, force bool) error {
return self.c.WithWaitingStatus(self.c.Tr.CreatingTag, func(gocui.Task) error {
if description != "" {
self.c.LogAction(self.c.Tr.Actions.CreateAnnotatedTag)
if err := self.c.Git().Tag.CreateAnnotated(tagName, ref, description); err != nil {
if err := self.c.Git().Tag.CreateAnnotated(tagName, ref, description, force); err != nil {
return self.c.Error(err)
}
} else {
self.c.LogAction(self.c.Tr.Actions.CreateLightweightTag)
if err := self.c.Git().Tag.CreateLightweight(tagName, ref); err != nil {
if err := self.c.Git().Tag.CreateLightweight(tagName, ref, force); err != nil {
return self.c.Error(err)
}
}
Expand All @@ -41,6 +42,28 @@ func (self *TagsHelper) OpenCreateTagPrompt(ref string, onCreate func()) error {
})
}

onConfirm := func(tagName string, description string) error {
if self.c.Git().Tag.HasTag(tagName) {
prompt := utils.ResolvePlaceholderString(
self.c.Tr.ForceTagPrompt,
map[string]string{
"tagName": tagName,
"cancelKey": self.c.UserConfig.Keybinding.Universal.Return,
"confirmKey": self.c.UserConfig.Keybinding.Universal.Confirm,
},
)
return self.c.Confirm(types.ConfirmOpts{
Title: self.c.Tr.ForceTag,
Prompt: prompt,
HandleConfirm: func() error {
return doCreateTag(tagName, description, true)
},
})
} else {
return doCreateTag(tagName, description, false)
}
}

return self.commitsHelper.OpenCommitMessagePanel(
&OpenCommitMessagePanelOpts{
CommitIndex: context.NoCommitIndex,
Expand Down
4 changes: 4 additions & 0 deletions pkg/i18n/english.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,8 @@ type TranslationSet struct {
PushTag string
CreateTag string
CreatingTag string
ForceTag string
ForceTagPrompt string
FetchRemote string
FetchingRemoteStatus string
CheckoutCommit string
Expand Down Expand Up @@ -1102,6 +1104,8 @@ func EnglishTranslationSet() TranslationSet {
PushTag: "Push tag",
CreateTag: "Create tag",
CreatingTag: "Creating tag",
ForceTag: "Force Tag",
ForceTagPrompt: "The tag '{{.tagName}}' exists already. Press {{.cancelKey}} to cancel, or {{.confirmKey}} to overwrite.",
FetchRemote: "Fetch remote",
FetchingRemoteStatus: "Fetching remote",
CheckoutCommit: "Checkout commit",
Expand Down
47 changes: 47 additions & 0 deletions pkg/integration/tests/tag/force_tag_annotated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package tag

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

var ForceTagAnnotated = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Overwrite an annotated tag that already exists",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.EmptyCommit("first commit")
shell.CreateAnnotatedTag("new-tag", "message", "HEAD")
shell.EmptyCommit("second commit")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("second commit").IsSelected(),
Contains("new-tag").Contains("first commit"),
).
Press(keys.Commits.CreateTag).
Tap(func() {
t.ExpectPopup().CommitMessagePanel().
Title(Equals("Tag name")).
Type("new-tag").
SwitchToDescription().
Title(Equals("Tag description")).
Type("message").
SwitchToSummary().
Confirm()
}).
Tap(func() {
t.ExpectPopup().Confirmation().
Title(Equals("Force Tag")).
Content(Contains("The tag 'new-tag' exists already. Press <esc> to cancel, or <enter> to overwrite.")).
Confirm()
}).
Lines(
Contains("new-tag").Contains("second commit"),
DoesNotContain("new-tag").Contains("first commit"),
)
},
})
43 changes: 43 additions & 0 deletions pkg/integration/tests/tag/force_tag_lightweight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package tag

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

var ForceTagLightweight = NewIntegrationTest(NewIntegrationTestArgs{
Description: "Overwrite a lightweight tag that already exists",
ExtraCmdArgs: []string{},
Skip: false,
SetupConfig: func(config *config.AppConfig) {},
SetupRepo: func(shell *Shell) {
shell.EmptyCommit("first commit")
shell.CreateLightweightTag("new-tag", "HEAD")
shell.EmptyCommit("second commit")
},
Run: func(t *TestDriver, keys config.KeybindingConfig) {
t.Views().Commits().
Focus().
Lines(
Contains("second commit").IsSelected(),
Contains("new-tag").Contains("first commit"),
).
Press(keys.Commits.CreateTag).
Tap(func() {
t.ExpectPopup().CommitMessagePanel().
Title(Equals("Tag name")).
Type("new-tag").
Confirm()
}).
Tap(func() {
t.ExpectPopup().Confirmation().
Title(Equals("Force Tag")).
Content(Contains("The tag 'new-tag' exists already. Press <esc> to cancel, or <enter> to overwrite.")).
Confirm()
}).
Lines(
Contains("new-tag").Contains("second commit"),
DoesNotContain("new-tag").Contains("first commit"),
)
},
})
2 changes: 2 additions & 0 deletions pkg/integration/tests/test_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ var tests = []*components.IntegrationTest{
tag.CreateWhileCommitting,
tag.CrudAnnotated,
tag.CrudLightweight,
tag.ForceTagAnnotated,
tag.ForceTagLightweight,
tag.Reset,
ui.Accordion,
ui.DoublePopup,
Expand Down

0 comments on commit d41a195

Please sign in to comment.