Skip to content

Commit 1b1db11

Browse files
committed
zip: add ErrUnrecognizedVCS error, allowing fallback behavior
Add ErrUnrecognizedVCS, which allows calling functions (such as gorelease) to fallback: usually to CreateFromDir. Updates golang/go#37413 Change-Id: I846f72b1ce22bfc699e8cd83b28ea4529e73d6e9 Reviewed-on: https://go-review.googlesource.com/c/mod/+/345730 Trust: Jean de Klerk <[email protected]> Run-TryBot: Jean de Klerk <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 4029241 commit 1b1db11

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

zip/zip.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -571,41 +571,54 @@ func CreateFromDir(w io.Writer, m module.Version, dir string) (err error) {
571571
// CreateFromVCS creates a module zip file for module m from the contents of a
572572
// VCS repository stored locally. The zip content is written to w.
573573
//
574-
// repo must be an absolute path to the base of the repository, such as
575-
// "/Users/some-user/my-repo".
574+
// repoRoot must be an absolute path to the base of the repository, such as
575+
// "/Users/some-user/some-repo".
576576
//
577577
// revision is the revision of the repository to create the zip from. Examples
578578
// include HEAD or SHA sums for git repositories.
579579
//
580580
// subdir must be the relative path from the base of the repository, such as
581581
// "sub/dir". To create a zip from the base of the repository, pass an empty
582582
// string.
583-
func CreateFromVCS(w io.Writer, m module.Version, repo, revision, subdir string) (err error) {
583+
//
584+
// If CreateFromVCS returns ErrUnrecognizedVCS, consider falling back to
585+
// CreateFromDir.
586+
func CreateFromVCS(w io.Writer, m module.Version, repoRoot, revision, subdir string) (err error) {
584587
defer func() {
585588
if zerr, ok := err.(*zipError); ok {
586-
zerr.path = repo
589+
zerr.path = repoRoot
587590
} else if err != nil {
588-
err = &zipError{verb: "create zip from version control system", path: repo, err: err}
591+
err = &zipError{verb: "create zip from version control system", path: repoRoot, err: err}
589592
}
590593
}()
591594

592595
var filesToCreate []File
593596

594597
switch {
595-
case isGitRepo(repo):
596-
files, err := filesInGitRepo(repo, revision, subdir)
598+
case isGitRepo(repoRoot):
599+
files, err := filesInGitRepo(repoRoot, revision, subdir)
597600
if err != nil {
598601
return err
599602
}
600603

601604
filesToCreate = files
602605
default:
603-
return fmt.Errorf("%q does not use a recognised version control system", repo)
606+
return &UnrecognizedVCSError{RepoRoot: repoRoot}
604607
}
605608

606609
return Create(w, m, filesToCreate)
607610
}
608611

612+
// UnrecognizedVCSError indicates that no recognized version control system was
613+
// found in the given directory.
614+
type UnrecognizedVCSError struct {
615+
RepoRoot string
616+
}
617+
618+
func (e *UnrecognizedVCSError) Error() string {
619+
return fmt.Sprintf("could not find a recognized version control system at %q", e.RepoRoot)
620+
}
621+
609622
// filterGitIgnored filters out any files that are git ignored in the directory.
610623
func filesInGitRepo(dir, rev, subdir string) ([]File, error) {
611624
stderr := bytes.Buffer{}

zip/zip_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"bytes"
1010
"crypto/sha256"
1111
"encoding/hex"
12+
"errors"
1213
"fmt"
1314
"io"
1415
"io/ioutil"
@@ -1669,8 +1670,15 @@ var A = 5
16691670

16701671
m := module.Version{Path: "example.com/foo/bar", Version: "v0.0.1"}
16711672

1672-
if err := modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", ""); err == nil {
1673-
t.Error("CreateFromVCS: expected error, got nil")
1673+
err = modzip.CreateFromVCS(tmpZip, m, tmpDir, "HEAD", "")
1674+
if err == nil {
1675+
t.Fatal("CreateFromVCS: expected error, got nil")
1676+
}
1677+
var gotErr *modzip.UnrecognizedVCSError
1678+
if !errors.As(err, &gotErr) {
1679+
t.Errorf("CreateFromVCS: returned error does not unwrap to modzip.ErrUnrecognisedVCS, but expected it to. returned error: %v", err)
1680+
} else if gotErr.RepoRoot != tmpDir {
1681+
t.Errorf("CreateFromVCS: returned error has RepoRoot %q, but want %q. returned error: %v", gotErr.RepoRoot, tmpDir, err)
16741682
}
16751683
}
16761684

0 commit comments

Comments
 (0)