github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/fetch_repo/vcs.go (about) 1 /* Copyright 2019 The Bazel Authors. All rights reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 */ 15 16 package main 17 18 import ( 19 "fmt" 20 21 "golang.org/x/tools/go/vcs" 22 ) 23 24 func fetchRepo(dest, remote, cmd, importpath, rev string) error { 25 root, err := getRepoRoot(remote, cmd, importpath) 26 if err != nil { 27 return err 28 } 29 return root.VCS.CreateAtRev(dest, root.Repo, rev) 30 } 31 32 func getRepoRoot(remote, cmd, importpath string) (*vcs.RepoRoot, error) { 33 if (cmd == "") != (remote == "") { 34 return nil, fmt.Errorf("--remote should be used with the --vcs flag. If this is an import path, use --importpath instead.") 35 } 36 37 if cmd != "" && remote != "" { 38 v := vcs.ByCmd(cmd) 39 if v == nil { 40 return nil, fmt.Errorf("invalid VCS type: %s", cmd) 41 } 42 return &vcs.RepoRoot{ 43 VCS: v, 44 Repo: remote, 45 Root: importpath, 46 }, nil 47 } 48 49 // User did not give us complete information for VCS / Remote. 50 // Try to figure out the information from the import path. 51 verbose := false 52 r, err := repoRootForImportPath(importpath, verbose) 53 if err != nil { 54 return nil, err 55 } 56 if importpath != r.Root { 57 return nil, fmt.Errorf("not a root of a repository: %s", importpath) 58 } 59 return r, nil 60 }