mirror of
https://github.com/gojp/goreportcard.git
synced 2026-01-28 22:39:05 +08:00
remove git download functionality
This commit is contained in:
@@ -2,97 +2,11 @@ package download
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"log"
|
|
||||||
"net/url"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"golang.org/x/tools/go/vcs"
|
"golang.org/x/tools/go/vcs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Download takes a user-provided string that represents a remote
|
|
||||||
// Go repository, and attempts to download it in a way similar to go get.
|
|
||||||
// It is forgiving in terms of the exact path given: the path may have
|
|
||||||
// a scheme or username, which will be trimmed.
|
|
||||||
func Download(path, dest string) (root *vcs.RepoRoot, err error) {
|
|
||||||
return download(path, dest, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func download(path, dest string, firstAttempt bool) (root *vcs.RepoRoot, err error) {
|
|
||||||
vcs.ShowCmd = true
|
|
||||||
|
|
||||||
path, err = Clean(path)
|
|
||||||
if err != nil {
|
|
||||||
return root, err
|
|
||||||
}
|
|
||||||
|
|
||||||
root, err = vcs.RepoRootForImportPath(path, true)
|
|
||||||
if err != nil {
|
|
||||||
return root, err
|
|
||||||
}
|
|
||||||
|
|
||||||
localDirPath := filepath.Join(dest, root.Root, "..")
|
|
||||||
|
|
||||||
err = os.MkdirAll(localDirPath, 0777)
|
|
||||||
if err != nil {
|
|
||||||
return root, err
|
|
||||||
}
|
|
||||||
|
|
||||||
fullLocalPath := filepath.Join(dest, root.Root)
|
|
||||||
ex, err := exists(fullLocalPath)
|
|
||||||
if err != nil {
|
|
||||||
return root, err
|
|
||||||
}
|
|
||||||
if ex {
|
|
||||||
log.Println("Update", root.Repo)
|
|
||||||
err = root.VCS.Download(fullLocalPath)
|
|
||||||
if err != nil && firstAttempt {
|
|
||||||
// may have been rebased; we delete the directory, then try one more time:
|
|
||||||
log.Printf("Failed to download %q (%v), trying again...", root.Repo, err.Error())
|
|
||||||
err = os.RemoveAll(fullLocalPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Println("Failed to delete path:", fullLocalPath, err)
|
|
||||||
}
|
|
||||||
return download(path, dest, false)
|
|
||||||
} else if err != nil {
|
|
||||||
return root, err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
log.Println("Create", root.Repo)
|
|
||||||
|
|
||||||
if root.VCS.Name == "Git" {
|
|
||||||
root.VCS.CreateCmd = "clone --depth 1 {repo} {dir}"
|
|
||||||
root.VCS.TagSyncDefault = ""
|
|
||||||
}
|
|
||||||
var rootRepo = root.Repo
|
|
||||||
u, err := url.Parse(root.Repo)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("WARN: could not parse root.Repo: %v", err)
|
|
||||||
} else {
|
|
||||||
if u.Host == "github.com" {
|
|
||||||
u.User = url.UserPassword("gojp", "gojp")
|
|
||||||
rootRepo = u.String()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = root.VCS.Create(fullLocalPath, rootRepo)
|
|
||||||
if err != nil {
|
|
||||||
return root, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err = root.VCS.TagSync(fullLocalPath, "")
|
|
||||||
if err != nil && firstAttempt {
|
|
||||||
// may have been rebased; we delete the directory, then try one more time:
|
|
||||||
log.Printf("Failed to update %q (%v), trying again...", root.Repo, err.Error())
|
|
||||||
err = os.RemoveAll(fullLocalPath)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("Failed to delete directory %s", fullLocalPath)
|
|
||||||
}
|
|
||||||
return download(path, dest, false)
|
|
||||||
}
|
|
||||||
return root, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean trims any URL parts, like the scheme or username, that might be present
|
// Clean trims any URL parts, like the scheme or username, that might be present
|
||||||
// in a user-submitted URL
|
// in a user-submitted URL
|
||||||
func Clean(path string) (string, error) {
|
func Clean(path string) (string, error) {
|
||||||
@@ -129,16 +43,3 @@ func trimUsername(repo string) string {
|
|||||||
|
|
||||||
return repo
|
return repo
|
||||||
}
|
}
|
||||||
|
|
||||||
// exists returns whether the given file or directory exists or not
|
|
||||||
// from http://stackoverflow.com/a/10510783
|
|
||||||
func exists(path string) (bool, error) {
|
|
||||||
_, err := os.Stat(path)
|
|
||||||
if err == nil {
|
|
||||||
return true, nil
|
|
||||||
}
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
return false, nil
|
|
||||||
}
|
|
||||||
return true, err
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
package download
|
|
||||||
|
|
||||||
/*
|
|
||||||
var testDownloadDir = "test_downloads"
|
|
||||||
|
|
||||||
func TestRepoRootForImportPath(t *testing.T) {
|
|
||||||
cases := []struct {
|
|
||||||
giveURL string
|
|
||||||
wantPath string
|
|
||||||
wantVCS string
|
|
||||||
}{
|
|
||||||
{"github.com/gojp/goreportcard", "github.com/gojp/goreportcard", "git"},
|
|
||||||
{"https://github.com/boltdb/bolt", "github.com/boltdb/bolt", "git"},
|
|
||||||
{"https://bitbucket.org/rickb777/go-talk", "bitbucket.org/rickb777/go-talk", "hg"},
|
|
||||||
{"ssh://hg@bitbucket.org/rickb777/go-talk", "bitbucket.org/rickb777/go-talk", "hg"},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range cases {
|
|
||||||
root, err := Download(tt.giveURL, testDownloadDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Error calling Download(%q): %v", tt.giveURL, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if root.Root != tt.wantPath {
|
|
||||||
t.Errorf("Download(%q): root.Repo = %q, want %q", tt.giveURL, root.Repo, tt.wantPath)
|
|
||||||
}
|
|
||||||
|
|
||||||
wantPath := filepath.Join(testDownloadDir, tt.wantPath)
|
|
||||||
ex, _ := exists(wantPath)
|
|
||||||
if !ex {
|
|
||||||
t.Errorf("Download(%q): %q was not created", tt.giveURL, wantPath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// clean up the test
|
|
||||||
os.RemoveAll(testDownloadDir)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
@@ -92,19 +92,12 @@ func newChecksResp(db *badger.DB, repo string, forceRefresh bool) (checksResp, e
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch the repo and grade it
|
|
||||||
//repoRoot, err := download.Download(repo, "_repos/src")
|
|
||||||
//if err != nil {
|
|
||||||
// return checksResp{}, fmt.Errorf("could not clone repo: %v", err)
|
|
||||||
//}
|
|
||||||
|
|
||||||
ver, err := download.ProxyDownload(repo)
|
ver, err := download.ProxyDownload(repo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("ERROR:", err)
|
log.Println("ERROR:", err)
|
||||||
// return checksResp{}, fmt.Errorf("could not download repo: %v", err)
|
return checksResp{}, fmt.Errorf("could not download repo: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// repo = repoRoot.Root
|
|
||||||
checkResult, err := check.Run(dirName(repo))
|
checkResult, err := check.Run(dirName(repo))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return checksResp{}, err
|
return checksResp{}, err
|
||||||
|
|||||||
Reference in New Issue
Block a user