-
Notifications
You must be signed in to change notification settings - Fork 235
Expand file tree
/
Copy pathclone.go
More file actions
177 lines (152 loc) · 4.73 KB
/
Copy pathclone.go
File metadata and controls
177 lines (152 loc) · 4.73 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// 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"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
)
// returns a list of all the git zoekt settings that have changed or are
// new. To do this it gets the current config, turns into into a map and diffs
// against the new settings map
func getZoektSettingsToUpdate(repoDest string, newSettings map[string]string, newSettingsKeys []string) ([]string, error) {
cmd := exec.Command("git", "-C", repoDest, "config", "--local", "--get-regexp", "zoekt")
outBuf := &bytes.Buffer{}
errBuf := &bytes.Buffer{}
cmd.Stdout = outBuf
cmd.Stderr = errBuf
if err := cmd.Run(); err != nil {
log.Printf("error getting settings\n")
return nil, err
}
// collect every current setting and put it into a map
oldSettings := make(map[string]string)
for _, cl := range bytes.Split(outBuf.Bytes(), []byte{'\n'}) {
if len(cl) == 0 {
continue
}
parts := bytes.SplitN(cl, []byte{' '}, 2)
if len(parts) != 2 {
return nil, fmt.Errorf("more parts than expected in git config key/v line")
}
oldSettings[string(parts[0])] = strings.TrimSpace(string(parts[1]))
}
// get the list of settings that have changed, or are new
var settingsToUpdate []string
for _, k := range newSettingsKeys {
oldVal, oldHasSetting := oldSettings[k]
if (!oldHasSetting && newSettings[k] != "") || oldVal != newSettings[k] {
settingsToUpdate = append(settingsToUpdate, k)
}
}
return settingsToUpdate, nil
}
// Updates the zoekt.* git config options after a repo is cloned.
// Once a repo is cloned, we can no longer use the --config flag to update all
// of it's zoekt.* settings at once. `git config` is limited to one option at once.
func updateZoektGitConfig(repoDest string, settings map[string]string) (bool, error) {
var keys []string
for k := range settings {
keys = append(keys, k)
}
sort.Strings(keys)
settingsToUpdate, err := getZoektSettingsToUpdate(repoDest, settings, keys)
if err != nil {
return false, err
}
if len(settingsToUpdate) == 0 {
return false, nil
}
for _, k := range settingsToUpdate {
if settings[k] != "" {
if err := exec.Command("git", "-C", repoDest, "config", k, settings[k]).Run(); err != nil {
return false, err
}
}
}
return true, nil
}
// 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 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
}
var keys []string
for k := range settings {
keys = append(keys, k)
}
sort.Strings(keys)
var config []string
for _, k := range keys {
if settings[k] != "" {
config = append(config, "--config", k+"="+settings[k])
}
}
cmd := exec.Command(
"git", "clone", "--bare", "--verbose", "--progress",
)
cmd.Args = append(cmd.Args, config...)
cmd.Args = append(cmd.Args, cloneURL, repoDest)
// Prevent prompting
cmd.Stdin = &bytes.Buffer{}
log.Println("running:", 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
}