From 5b64795015c7f320c5a9839f9f8e54f7a1ee9e23 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 22 Dec 2025 12:24:03 -0300 Subject: [PATCH] git: fix --tags --- git.go | 55 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/git.go b/git.go index 36ccf68..081ef36 100644 --- a/git.go +++ b/git.go @@ -532,34 +532,37 @@ aside from those, there is also: log("- setting HEAD to branch %s\n", color.CyanString(remoteBranch)) } - // add all refs/tags - output, err := exec.Command("git", "show-ref", "--tags").Output() - if err != nil { - return fmt.Errorf("failed to get local tags: %s", err) - } else { - lines := strings.Split(strings.TrimSpace(string(output)), "\n") - for _, line := range lines { - line = strings.TrimSpace(line) - if line == "" { - continue - } - parts := strings.Fields(line) - if len(parts) != 2 { - continue - } - commitHash := parts[0] - ref := parts[1] - - tagName := strings.TrimPrefix(ref, "refs/tags/") - - if !c.Bool("force") { - // if --force is not passed then we can't overwrite tags - if existingHash, exists := state.Tags[tagName]; exists && existingHash != commitHash { - return fmt.Errorf("tag %s that is already published pointing to %s, call with --force to overwrite", tagName, existingHash) + if c.Bool("tags") { + // add all refs/tags + output, err := exec.Command("git", "show-ref", "--tags").Output() + if err != nil && err.Error() != "exit status 1" { + // exit status 1 is returned when there are no tags, which should be ok for us + return fmt.Errorf("failed to get local tags: %s", err) + } else { + lines := strings.Split(strings.TrimSpace(string(output)), "\n") + for _, line := range lines { + line = strings.TrimSpace(line) + if line == "" { + continue } + parts := strings.Fields(line) + if len(parts) != 2 { + continue + } + commitHash := parts[0] + ref := parts[1] + + tagName := strings.TrimPrefix(ref, "refs/tags/") + + if !c.Bool("force") { + // if --force is not passed then we can't overwrite tags + if existingHash, exists := state.Tags[tagName]; exists && existingHash != commitHash { + return fmt.Errorf("tag %s that is already published pointing to %s, call with --force to overwrite", tagName, existingHash) + } + } + state.Tags[tagName] = commitHash + log("- setting tag %s to commit %s\n", color.CyanString(tagName), color.CyanString(commitHash)) } - state.Tags[tagName] = commitHash - log("- setting tag %s to commit %s\n", color.CyanString(tagName), color.CyanString(commitHash)) } }