-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.go
84 lines (73 loc) · 1.85 KB
/
git.go
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
// Copyright 2020 Manlio Perillo. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"io/ioutil"
"os"
"os/exec"
"os/user"
"path/filepath"
"text/template"
)
const (
gitTemplate = `#!/bin/sh
{{ .GitPath }} -c url.ssh://{{ .User }}@localhost:{{ .Path }}.insteadof=https://{{ .ModPath }} "$@"
`
)
type gitContext struct {
GitPath string
User string
ModPath string
Path string
}
// tempExecutable returns a temporary executable file with the given base name,
// with an 0o744 permission.
func tempExecutable(name string) (*os.File, error) {
// Create the file in a temporary directory, in order to ensure that the
// base name is set correctly.
dirpath, err := ioutil.TempDir("", "gomod-pack")
if err != nil {
return nil, err
}
path := filepath.Join(dirpath, name)
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o744)
if err != nil {
return nil, err
}
return f, nil
}
// InstallGit installs a custom temporary git script that will setup an URL
// rewrite in order to allow modpath to be resolved to the local filesystem
// path via an SSH connection to localhost.
func InstallGit(modpath, path string) (string, error) {
// Find the current OS user and git command path.
user, err := user.Current()
if err != nil {
return "", err
}
gitpath, err := exec.LookPath("git")
if err != nil {
return "", err
}
// Create the custom git script.
f, err := tempExecutable("git")
if err != nil {
return "", err
}
defer f.Close()
t := template.Must(template.New("git").Parse(gitTemplate))
ctx := gitContext{
GitPath: gitpath,
User: user.Username,
ModPath: modpath,
Path: path,
}
if err := t.Execute(f, ctx); err != nil {
return "", err
}
if err := f.Sync(); err != nil {
return "", err
}
return f.Name(), nil
}