forked from sourcegraph/zoekt
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclone.go
More file actions
114 lines (100 loc) · 3.08 KB
/
Copy pathclone.go
File metadata and controls
114 lines (100 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gitindex
import (
"bytes"
"fmt"
"log"
"net/url"
"os"
"os/exec"
"path/filepath"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
)
// redactURLCredentials removes any basic-auth userinfo from a URL-shaped
// string so that credentials are never written to logs. Non-URL arguments
// are returned unchanged.
func redactURLCredentials(s string) string {
u, err := url.Parse(s)
if err != nil || u.Scheme == "" || u.User == nil {
return s
}
u.User = url.User("redacted")
return u.String()
}
// redactURLCredentialsInArgs returns a copy of args with any URL-valued
// arguments having their userinfo redacted.
func redactURLCredentialsInArgs(args []string) []string {
out := make([]string, len(args))
for i, a := range args {
out[i] = redactURLCredentials(a)
}
return out
}
// CloneRepo clones one repository, adding the given config
// settings. It returns the bare repo directory. The `name` argument
// determines where the repo is stored relative to `destDir`. Returns
// the directory of the repository.
func CloneRepo(destDir, name, cloneURL string, settings map[string]string) (string, error) {
parent := filepath.Join(destDir, filepath.Dir(name))
if err := os.MkdirAll(parent, 0o755); err != nil {
return "", err
}
repoDest := filepath.Join(parent, filepath.Base(name)+".git")
if _, err := os.Lstat(repoDest); err == nil {
// Repository exists, ensure zoekt settings are in sync.
hadUpdate, err := updateZoektGitConfig(repoDest, settings)
if err != nil {
return "", fmt.Errorf("failed to update repository settings: %w", err)
}
if hadUpdate {
return repoDest, nil
}
return "", nil
}
cmd := exec.Command(
"git", "clone", "--bare", "--verbose", "--progress",
)
cmd.Args = append(cmd.Args, cloneConfigArgs(settings)...)
cmd.Args = append(cmd.Args, cloneURL, repoDest)
// Prevent prompting
cmd.Stdin = &bytes.Buffer{}
log.Println("running:", redactURLCredentialsInArgs(cmd.Args))
if err := cmd.Run(); err != nil {
return "", err
}
if err := setFetch(repoDest, "origin", "+refs/heads/*:refs/heads/*"); err != nil {
log.Printf("addFetch: %v", err)
}
return repoDest, nil
}
func setFetch(repoDir, remote, refspec string) error {
repo, err := git.PlainOpen(repoDir)
if err != nil {
return err
}
cfg, err := repo.Config()
if err != nil {
return err
}
rm := cfg.Remotes[remote]
if rm != nil {
rm.Fetch = []config.RefSpec{config.RefSpec(refspec)}
}
if err := repo.Storer.SetConfig(cfg); err != nil {
return err
}
return nil
}