Skip to content

Commit 4b0953f

Browse files
committed
simplify FetchTags
1 parent a6d5e57 commit 4b0953f

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

internal/gitsemver/gitter.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type Gitter interface {
3434
GetBranchesFromTag(repo, tag string) (branches []string, err error)
3535
// GetBuild returns the number of commits in the currently checked out branch as a string, or an empty string
3636
GetBuild(repo string) (string, error)
37-
// FetchTags calls "git fetch --tags"
37+
// FetchTags calls "git fetch --tags". Uses the "--unshallow" option if needed.
3838
FetchTags(repo string) error
3939
// CreateTag creates a new lightweight tag. Does nothing if tag is empty.
4040
CreateTag(repo, tag string) error
@@ -350,10 +350,14 @@ func (dg DefaultGitter) GetBuild(repo string) (buildnum string, err error) {
350350
}
351351

352352
func (dg DefaultGitter) FetchTags(repo string) (err error) {
353-
// If this is a shallow clone, attempt to unshallow as part of fetching tags.
354-
// Ignore the expected error when the repository is already complete.
355-
_, _ = dg.Exec("-C", repo, "fetch", "--unshallow", "--tags")
356-
_, err = dg.Exec("-C", repo, "fetch", "--tags") /* #nosec G204 */
353+
var b []byte
354+
if b, err = dg.Exec("-C", repo, "rev-parse", "--is-shallow-repository"); err == nil {
355+
args := []string{"-C", repo, "fetch", "--tags", "--unshallow"}
356+
if strings.TrimSpace(string(b)) != "true" {
357+
args = args[:len(args)-1]
358+
}
359+
_, err = dg.Exec(args...) /* #nosec G204 */
360+
}
357361
return
358362
}
359363

0 commit comments

Comments
 (0)