fork of https://github.com/sourcegraph/zoekt
1// Copyright 2016 Google Inc. 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// This binary fetches all repos of a user or organization and clones
16// them. It is strongly recommended to get a personal API token from
17// https://github.com/settings/tokens, save the token in a file, and
18// point the --token option to it.
19package main
20
21import (
22 "flag"
23 "fmt"
24 "log"
25 "net/url"
26 "os"
27 "path/filepath"
28 "strconv"
29 "strings"
30
31 "github.com/sourcegraph/zoekt/internal/gitindex"
32)
33
34func main() {
35 dest := flag.String("dest", "", "destination directory")
36 nameFlag := flag.String("name", "", "name of repository")
37 repoIDFlag := flag.Uint("repoid", 0, "id of repository")
38 flag.Parse()
39
40 if *dest == "" {
41 log.Fatal("must set --dest")
42 }
43 if len(flag.Args()) == 0 {
44 log.Fatal("must provide URL")
45 }
46 u, err := url.Parse(flag.Arg(0))
47 if err != nil {
48 log.Fatalf("url.Parse: %v", err)
49 }
50
51 name := *nameFlag
52 if name == "" {
53 name = filepath.Join(u.Host, u.Path)
54 name = strings.TrimSuffix(name, ".git")
55 }
56
57 destDir := filepath.Dir(filepath.Join(*dest, name))
58 if err := os.MkdirAll(destDir, 0o755); err != nil {
59 log.Fatal(err)
60 }
61
62 config := map[string]string{
63 "zoekt.name": name,
64 }
65
66 repoID := *repoIDFlag
67 if repoID != 0 {
68 config["zoekt.repoid"] = strconv.FormatUint(uint64(repoID), 10)
69 }
70
71 destRepo, err := gitindex.CloneRepo(destDir, filepath.Base(name), u.String(), config)
72 if err != nil {
73 log.Fatalf("CloneRepo: %v", err)
74 }
75 if destRepo != "" {
76 fmt.Println(destRepo)
77 }
78}