-
-
Notifications
You must be signed in to change notification settings - Fork 661
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new tool fetch_repo, a fetcher of go external repositories (#80)
- Loading branch information
Showing
2 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
load("//go:def.bzl", "go_binary") | ||
|
||
go_binary( | ||
name = "fetch_repo", | ||
srcs = ["main.go"], | ||
deps = ["@org_golang_x_tools//:go/vcs"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Command fetch_repo is similar to "go get -d" but it works even if the given | ||
// repository path is not a buildable Go package and it checks out a specific | ||
// revision rather than the latest revision. | ||
// | ||
// The difference between fetch_repo and "git clone" or {new_,}git_repository is | ||
// that fetch_repo recognizes import redirection of Go and it supports other | ||
// version control systems than git. | ||
// | ||
// These differences help us to manage external Go repositories in the manner of | ||
// Bazel. | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"log" | ||
|
||
"golang.org/x/tools/go/vcs" | ||
) | ||
|
||
var ( | ||
remote = flag.String("remote", "", "Go importpath to the repository fetch") | ||
rev = flag.String("rev", "", "target revision") | ||
dest = flag.String("dest", "", "destination directory") | ||
) | ||
|
||
func run() error { | ||
r, err := vcs.RepoRootForImportPath(*remote, true) | ||
if err != nil { | ||
return err | ||
} | ||
if *remote != r.Root { | ||
return fmt.Errorf("not a root of a repository: %s", *remote) | ||
} | ||
return r.VCS.CreateAtRev(*dest, r.Repo, *rev) | ||
} | ||
|
||
func main() { | ||
flag.Parse() | ||
|
||
if err := run(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} |