diff --git a/.golangci.yml b/.golangci.yml index 6f791d2..04cda8c 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -9,7 +9,6 @@ run: - ".*Mock.*" - ".*_mock.*" - ".*_generated.*" - go: "1.18" output: sort-results: true @@ -17,21 +16,18 @@ output: linters: disable-all: true enable: - - deadcode - errcheck - gosimple - govet - staticcheck - typecheck - unused - - varcheck - errname - errorlint - execinquery - goerr113 - gofumpt - gosec - - ifshort - importas - makezero - nilnil @@ -66,9 +62,4 @@ linters-settings: - unreachable - unusedresult staticcheck: - go: "1.18" checks: ["all", "-SA1019"] - -issues: - new: true - fix: true diff --git a/internal/cli/action.go b/internal/cli/action.go index 18c2d24..7b9380f 100644 --- a/internal/cli/action.go +++ b/internal/cli/action.go @@ -19,18 +19,20 @@ const ( type action struct { flags struct { - scopes map[string]struct{} - output string - from string - to string - version string - repository string - filename string - filetype string - verbose bool - dryRun bool - interactive bool - autoCommit bool + scopes map[string]struct{} + output string + from string + to string + version string + repository string + filename string + filetype string + verbose bool + dryRun bool + interactive bool + autoGitCommit bool + autoGitTag bool + autoGitPush bool } } @@ -75,7 +77,9 @@ func (a *action) getFlags(c *cli.Context) { a.flags.dryRun = c.Bool(flagDryRunName) a.flags.interactive = c.Bool(flagInteractiveName) - a.flags.autoCommit = c.Bool(flagAutoCommitName) + a.flags.autoGitCommit = c.Bool(flagAutoGitCommitName) + a.flags.autoGitTag = c.Bool(flagAutoGitTagName) + a.flags.autoGitPush = c.Bool(flagAutoGitPushName) a.log("Flags %+v\n", a.flags) } diff --git a/internal/cli/action_generate.go b/internal/cli/action_generate.go index 404cfcc..6125626 100644 --- a/internal/cli/action_generate.go +++ b/internal/cli/action_generate.go @@ -70,24 +70,8 @@ func (a *action) RunGenerate(c *cli.Context) error { return err } - if a.flags.autoCommit { - commitMsg := fmt.Sprintf(autoCommitMessageTemplate, version) - - // TODO: disable until https://github.com/go-git/go-git/issues/180 is fixed - // if err := repo.Commit(commitMsg, finalOutput); err != nil { - // return err - // } - cmdOutput, err := exec.CommandContext(c.Context, "git", "add", finalOutput).CombinedOutput() - if err != nil { - return err - } - a.log("Git add output:\n%s", cmdOutput) - - cmdOutput, err = exec.CommandContext(c.Context, "git", "commit", "-m", commitMsg).CombinedOutput() - if err != nil { - return err - } - a.log("Git commit output:\n%s", cmdOutput) + if err := a.doGit(finalOutput, version); err != nil { + return err } return nil @@ -210,3 +194,53 @@ func (a *action) generateRSTChangelog(output, version string, commits []conventi return nil } + +func (a *action) doGit(finalOutput, version string) error { + if !a.flags.autoGitCommit { + return nil + } + + // TODO: disable until https://github.com/go-git/go-git/issues/180 is fixed + // if err := repo.Commit(commitMsg, finalOutput); err != nil { + // return err + // } + cmdOutput, err := exec.Command("git", "add", finalOutput).CombinedOutput() + if err != nil { + return err + } + a.log("Git add output:\n%s", cmdOutput) + + commitMsg := fmt.Sprintf(autoCommitMessageTemplate, version) + + cmdOutput, err = exec.Command("git", "commit", "-m", commitMsg).CombinedOutput() + if err != nil { + return err + } + a.log("Git commit output:\n%s", cmdOutput) + + if a.flags.autoGitTag { + cmdOutput, err = exec.Command("git", "tag", version).CombinedOutput() + if err != nil { + return err + } + a.log("Git tag output:\n%s", cmdOutput) + } + + if a.flags.autoGitPush { + cmdOutput, err = exec.Command("git", "push").CombinedOutput() + if err != nil { + return err + } + a.log("Git push output:\n%s", cmdOutput) + + if a.flags.autoGitTag { + cmdOutput, err = exec.Command("git", "push", "origin", version).CombinedOutput() + if err != nil { + return err + } + a.log("Git push tag output:\n%s", cmdOutput) + } + } + + return nil +} diff --git a/internal/cli/app.go b/internal/cli/app.go index 3033df2..e82acc9 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -47,8 +47,14 @@ const ( flagInteractiveName = "interactive" flagInteractiveUsage = "interactive mode" - flagAutoCommitName = "auto-commit" - flagAutoCommitUsage = "enable auto commit after generating changelog" + flagAutoGitCommitName = "auto-commit" + flagAutoGitCommitUsage = "enable auto git commit after generating changelog" + + flagAutoGitTagName = "auto-tag" + flagAutoGitTagUsage = "enable auto git tag after generating changelog, only works if auto-commit is enabled" + + flagAutoGitPushName = "auto-push" + flagAutoGitPushUsage = "enable auto git push after generating changelog, only works if auto-commit is enabled, if auto-tag is enabled will auto git push tag too" ) var ( @@ -120,8 +126,8 @@ func NewApp() *App { Aliases: flagInteractiveAliases, }, &cli.BoolFlag{ - Name: flagAutoCommitName, - Usage: flagAutoCommitUsage, + Name: flagAutoGitCommitName, + Usage: flagAutoGitCommitUsage, }, }, Action: a.RunGenerate,