Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ffe282105 | |||
| 0dc6d0747d |
@@ -33,6 +33,7 @@ func newRootCmd() *cobra.Command {
|
|||||||
var (
|
var (
|
||||||
dryRun bool
|
dryRun bool
|
||||||
noPush bool
|
noPush bool
|
||||||
|
noRelease bool
|
||||||
noCommit bool
|
noCommit bool
|
||||||
tagOnly bool
|
tagOnly bool
|
||||||
branchOverride string
|
branchOverride string
|
||||||
@@ -62,6 +63,7 @@ func newRootCmd() *cobra.Command {
|
|||||||
patternSet: patternSet,
|
patternSet: patternSet,
|
||||||
dryRun: dryRun,
|
dryRun: dryRun,
|
||||||
noPush: noPush,
|
noPush: noPush,
|
||||||
|
noRelease: noRelease,
|
||||||
noCommit: noCommit,
|
noCommit: noCommit,
|
||||||
tagOnly: tagOnly,
|
tagOnly: tagOnly,
|
||||||
})
|
})
|
||||||
@@ -70,6 +72,7 @@ func newRootCmd() *cobra.Command {
|
|||||||
|
|
||||||
root.Flags().BoolVar(&dryRun, "dry-run", false, "print next version without making changes")
|
root.Flags().BoolVar(&dryRun, "dry-run", false, "print next version without making changes")
|
||||||
root.Flags().BoolVar(&noPush, "no-push", false, "create commit and tag locally without pushing or creating a GitLab release")
|
root.Flags().BoolVar(&noPush, "no-push", false, "create commit and tag locally without pushing or creating a GitLab release")
|
||||||
|
root.Flags().BoolVar(&noRelease, "no-release", false, "push commit and tag but skip creating the GitLab release")
|
||||||
root.Flags().BoolVar(&noCommit, "no-commit", false, "update pom.xml but do not commit, tag, or push")
|
root.Flags().BoolVar(&noCommit, "no-commit", false, "update pom.xml but do not commit, tag, or push")
|
||||||
root.Flags().BoolVar(&tagOnly, "tag-only", false, "tag HEAD without updating pom.xml (assumes version was already committed)")
|
root.Flags().BoolVar(&tagOnly, "tag-only", false, "tag HEAD without updating pom.xml (assumes version was already committed)")
|
||||||
root.Flags().StringVar(&branchOverride, "branch", "", "override branch name detection (required in detached HEAD)")
|
root.Flags().StringVar(&branchOverride, "branch", "", "override branch name detection (required in detached HEAD)")
|
||||||
@@ -101,6 +104,7 @@ type options struct {
|
|||||||
patternSet bool
|
patternSet bool
|
||||||
dryRun bool
|
dryRun bool
|
||||||
noPush bool
|
noPush bool
|
||||||
|
noRelease bool
|
||||||
noCommit bool
|
noCommit bool
|
||||||
tagOnly bool
|
tagOnly bool
|
||||||
}
|
}
|
||||||
@@ -264,6 +268,10 @@ func run(o options) error {
|
|||||||
fmt.Fprintln(os.Stderr, "info: pushed")
|
fmt.Fprintln(os.Stderr, "info: pushed")
|
||||||
|
|
||||||
// --- GitLab release ---
|
// --- GitLab release ---
|
||||||
|
if o.noRelease {
|
||||||
|
fmt.Printf("released %s\n", nextTag)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
if cfg.GitLab.URL == "" || cfg.GitLab.Project == "" {
|
if cfg.GitLab.URL == "" || cfg.GitLab.Project == "" {
|
||||||
fmt.Fprintln(os.Stderr, "warning: GitLab URL or project not configured — skipping release creation")
|
fmt.Fprintln(os.Stderr, "warning: GitLab URL or project not configured — skipping release creation")
|
||||||
fmt.Printf("released %s\n", nextTag)
|
fmt.Printf("released %s\n", nextTag)
|
||||||
|
|||||||
@@ -460,6 +460,25 @@ func TestRunSkipGitLab(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRunNoRelease(t *testing.T) {
|
||||||
|
_, dir := setupRepoWithRemote(t)
|
||||||
|
addFile(t, dir, "x.go", "// fix")
|
||||||
|
repo, _ := gogit.PlainOpen(dir)
|
||||||
|
w, _ := repo.Worktree()
|
||||||
|
w.Add("x.go")
|
||||||
|
w.Commit("fix: patch something", &gogit.CommitOptions{Author: testSig()})
|
||||||
|
|
||||||
|
// --no-release skips GitLab release even when credentials are configured
|
||||||
|
t.Setenv("CI_SERVER_URL", "https://gitlab.example.com")
|
||||||
|
t.Setenv("CI_PROJECT_ID", "42")
|
||||||
|
t.Setenv("GITLAB_TOKEN", "test-token")
|
||||||
|
|
||||||
|
err := execCmd(t, "--no-release", "--branch", "release/1.2", "--repo", dir)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("--no-release: unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRunMissingToken(t *testing.T) {
|
func TestRunMissingToken(t *testing.T) {
|
||||||
_, dir := setupRepoWithRemote(t)
|
_, dir := setupRepoWithRemote(t)
|
||||||
addFile(t, dir, "x.go", "// fix")
|
addFile(t, dir, "x.go", "// fix")
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ package gitutil
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -231,9 +233,17 @@ func CreateTag(repo *gogit.Repository, tagName string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Push pushes the given branch and tag to the "origin" remote.
|
// Push pushes the given branch and tag to the "origin" remote.
|
||||||
// If token is non-empty, HTTPS basic auth (oauth2/token) is used.
|
// When token is non-empty, go-git is used with HTTPS basic auth (oauth2/token) — suitable for CI.
|
||||||
// Passing an empty token lets go-git use the system credential helper or SSH agent.
|
// When token is empty, the system git binary is invoked so that credential helpers,
|
||||||
|
// SSH agents, and netrc are all available as they would be for a regular git push.
|
||||||
func Push(repo *gogit.Repository, branchName, tagName, token string) error {
|
func Push(repo *gogit.Repository, branchName, tagName, token string) error {
|
||||||
|
if token != "" {
|
||||||
|
return pushWithGoGit(repo, branchName, tagName, token)
|
||||||
|
}
|
||||||
|
return pushWithCLI(repo, branchName, tagName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func pushWithGoGit(repo *gogit.Repository, branchName, tagName, token string) error {
|
||||||
remote, err := repo.Remote("origin")
|
remote, err := repo.Remote("origin")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("remote origin not found: %w", err)
|
return fmt.Errorf("remote origin not found: %w", err)
|
||||||
@@ -244,13 +254,10 @@ func Push(repo *gogit.Repository, branchName, tagName, token string) error {
|
|||||||
gitconfig.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", branchName, branchName)),
|
gitconfig.RefSpec(fmt.Sprintf("refs/heads/%s:refs/heads/%s", branchName, branchName)),
|
||||||
gitconfig.RefSpec(fmt.Sprintf("refs/tags/%s:refs/tags/%s", tagName, tagName)),
|
gitconfig.RefSpec(fmt.Sprintf("refs/tags/%s:refs/tags/%s", tagName, tagName)),
|
||||||
},
|
},
|
||||||
}
|
Auth: &githttp.BasicAuth{
|
||||||
|
|
||||||
if token != "" {
|
|
||||||
opts.Auth = &githttp.BasicAuth{
|
|
||||||
Username: "oauth2",
|
Username: "oauth2",
|
||||||
Password: token,
|
Password: token,
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := remote.Push(opts); err != nil && !errors.Is(err, gogit.NoErrAlreadyUpToDate) {
|
if err := remote.Push(opts); err != nil && !errors.Is(err, gogit.NoErrAlreadyUpToDate) {
|
||||||
@@ -259,6 +266,24 @@ func Push(repo *gogit.Repository, branchName, tagName, token string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func pushWithCLI(repo *gogit.Repository, branchName, tagName string) error {
|
||||||
|
wt, err := repo.Worktree()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("get worktree: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd := exec.Command("git", "-C", wt.Filesystem.Root(), "push", "origin",
|
||||||
|
fmt.Sprintf("HEAD:refs/heads/%s", branchName),
|
||||||
|
fmt.Sprintf("refs/tags/%s:refs/tags/%s", tagName, tagName),
|
||||||
|
)
|
||||||
|
cmd.Stdout = os.Stderr // git push status goes to stderr conventionally
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
if err := cmd.Run(); err != nil {
|
||||||
|
return fmt.Errorf("git push: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// resolveTagToCommit follows tag objects until it reaches a commit.
|
// resolveTagToCommit follows tag objects until it reaches a commit.
|
||||||
// Handles both lightweight tags (ref → commit) and annotated tags (ref → tag object → … → commit).
|
// Handles both lightweight tags (ref → commit) and annotated tags (ref → tag object → … → commit).
|
||||||
func resolveTagToCommit(repo *gogit.Repository, ref *plumbing.Reference) (plumbing.Hash, error) {
|
func resolveTagToCommit(repo *gogit.Repository, ref *plumbing.Reference) (plumbing.Hash, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user