Fix issues

This commit is contained in:
2025-12-24 11:39:34 +03:00
parent dc05c68ee2
commit 56848c73fd
3 changed files with 101 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
package main
import (
"fmt"
"log"
"os"
"strings"
@@ -8,6 +9,7 @@ import (
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
)
@@ -27,6 +29,22 @@ func normalizePath(p string) string {
return p
}
func pushRefSpec(headRef *plumbing.Reference) (config.RefSpec, error) {
if headRef == nil || !headRef.Name().IsBranch() {
return "", fmt.Errorf("cannot determine branch to push from HEAD")
}
branch := headRef.Name().String()
return config.RefSpec(branch + ":" + branch), nil
}
func signatureFromEnv() *object.Signature {
return &object.Signature{
Name: getenvDefault("GIT_USERNAME", "bumpversion"),
Email: getenvDefault("GIT_EMAIL", "bumpversion@deploy"),
When: time.Now().UTC(),
}
}
// gitCommit выполняет коммит с внесёнными изменениями
func gitCommit(bc *BumpConfig, newVersion string, configPath string) {
// Открываем локальный репозиторий (предполагается, что он существует в папке ".")
@@ -90,11 +108,7 @@ func gitCommit(bc *BumpConfig, newVersion string, configPath string) {
"{new_version}", newVersion,
).Replace(bc.Message)
author := &object.Signature{
Name: getenvDefault("GIT_USERNAME", "bumpversion"),
Email: getenvDefault("GIT_EMAIL", "bumpversion@deploy"),
When: time.Now().UTC(),
}
author := signatureFromEnv()
hash, err := worktree.Commit(commitMsg, &git.CommitOptions{
Author: author,
@@ -132,11 +146,7 @@ func gitTag(bc *BumpConfig, newVersion string) {
commitMsg = strings.ReplaceAll(commitMsg, "{new_version}", newVersion)
tagName := strings.ReplaceAll(bc.TagName, "{new_version}", newVersion)
_, err = repo.CreateTag(tagName, headRef.Hash(), &git.CreateTagOptions{
Tagger: &object.Signature{
Name: os.Getenv("GIT_USERNAME"),
Email: os.Getenv("GIT_EMAIL"),
When: time.Now(),
},
Tagger: signatureFromEnv(),
Message: commitMsg,
})
if err != nil {
@@ -154,12 +164,22 @@ func gitPush(bc *BumpConfig, newVersion string) {
tagName := strings.ReplaceAll(bc.TagName, "{new_version}", newVersion)
headRef, err := repo.Head()
if err != nil {
log.Fatalf("HEAD open error: %v", err)
}
branchSpec, err := pushRefSpec(headRef)
if err != nil {
log.Fatalf("Push branch detection error: %v", err)
}
// (Опционально) Выполняем push на удаленный репозиторий
tagSpec := config.RefSpec("refs/tags/" + tagName + ":refs/tags/" + tagName)
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
"refs/heads/master:refs/heads/master",
branchSpec,
tagSpec,
},
})