forked from schollz/git
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (56 loc) · 1.47 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
)
func debug() {
f, e := os.OpenFile("/tmp/go-get-shallow-git.debug", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
defer f.Close()
if e != nil {
panic(e)
}
fmt.Fprintf(f, "$$$$ Running gitwrapper. IsGoGet: %t, Args: %s\n", isThisGoGet(), os.Args)
// fmt.Println(pid)
c := exec.Command("pstree", "-p", "--show-parent", strconv.Itoa(os.Getpid()))
c.Stdout = f
c.Stderr = f
if err := c.Run(); err != nil {
panic(err)
}
}
func isThisGoGet() bool {
ppid := os.Getppid()
cmdlineBytes, err := ioutil.ReadFile(`/proc/` + strconv.Itoa(ppid) + `/cmdline`)
if err != nil {
panic(err)
}
cmdlineArgs := strings.FieldsFunc(string(cmdlineBytes), func(r rune) bool { return r == '\u0000' })
return len(cmdlineArgs) > 1 && cmdlineArgs[0] == "go"
}
func main() {
os.Stderr.Write([]byte("WARN: Using patched git wrapper `" + os.Args[0] + "` for shallow 'go get'\n"))
args := os.Args
if isThisGoGet() {
if len(args) > 1 {
if args[1] == "pull" {
args = append(args[:2], append([]string{"--depth=1"}, args[2:]...)...)
} else if args[1] == "clone" {
args = append(args[:2], append([]string{"--depth=1", "--shallow-submodules", "--single-branch"}, args[2:]...)...)
}
}
}
args[0] = "/usr/bin/git"
cmd := exec.Command(args[0])
if len(args) > 0 {
cmd = exec.Command(args[0], args[1:]...)
}
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
os.Exit(1)
}
}