-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathgit_instrument.go
135 lines (111 loc) · 2.9 KB
/
git_instrument.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
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
package main
import (
"fmt"
"log"
"net/url"
"os"
"strings"
"github.com/pkg/errors"
)
// InstrumentContextT ...
type InstrumentContextT struct {
OldHTTPProxy string
oldHTTPProxyAuthMethod string
UseMirror bool
WorkDir string
Global bool
ShouldReset bool
MirroredURL string
OriginalURL string
RemoteName string
}
// InstrumentContext ...
type InstrumentContext = *InstrumentContextT
var instrumentContext = &InstrumentContextT{}
// GithubInstrument ...GithubInstrument
func GithubInstrument(cmdline CommandLine, config Config) {
ictx := instrumentContext
if cmdline.UseProxy {
ictx.UseMirror = false
} else {
ictx.UseMirror = true
}
if cmdline.IsGitClone {
ictx.Global = true
ictx.WorkDir = cmdline.GitCloneDir
} else {
ictx.Global = false
ictx.WorkDir = cmdline.GitDir
}
if Debug {
log.Printf("[fgit] 修改前: %s\n", JSONPretty(ictx))
}
defer ResetGithubRemote()
if ictx.UseMirror {
if Debug {
log.Println("[fgit] 设置镜像")
}
ictx.OriginalURL = cmdline.GitURLText
ictx.MirroredURL = GeneratedMirroredURL(ictx.OriginalURL, config.Mirror)
ictx.RemoteName = cmdline.GitRemoteName
if cmdline.IsGitClone {
cmdline.Args[cmdline.ArgIndexOfGitURLText] = ictx.MirroredURL
} else {
SetGitRemoteURL(ictx.WorkDir, ictx.RemoteName, ictx.MirroredURL)
}
} else {
if Debug {
log.Println("[fgit] 设置代理")
}
ictx.OldHTTPProxy, ictx.oldHTTPProxyAuthMethod = ConfigGitHTTPProxy(ictx.WorkDir, ictx.Global, config.Proxy)
}
ictx.ShouldReset = true
if Debug {
log.Printf("[fgit] 修改后: %s\n", JSONPretty(ictx))
}
oldGit(cmdline, true, false)
}
// ResetGithubRemote ...
func ResetGithubRemote() {
ictx := instrumentContext
if Debug {
log.Printf("[fgit] 重置修改: %s\n", JSONPretty(ictx))
}
if ictx.ShouldReset {
if ictx.UseMirror {
if Debug {
log.Println("[fgit] 重置镜像")
}
SetGitRemoteURL(ictx.WorkDir, ictx.RemoteName, ictx.OriginalURL)
} else {
if Debug {
log.Println("[fgit] 重置代理")
}
SetGitHTTPProxy(ictx.WorkDir, ictx.Global, ictx.OldHTTPProxy, ictx.oldHTTPProxyAuthMethod)
}
}
}
// GeneratedMirroredURL ...
func GeneratedMirroredURL(gitURLText string, mirrorURLText string) string {
var err error
var gitURL *url.URL
if gitURL, err = url.Parse(gitURLText); err != nil {
panic(errors.Wrapf(err, "无法解析URL: %s", gitURLText))
}
gitURLPath := gitURL.Path
slashBeforeProj := strings.LastIndex(gitURLPath, "/")
if slashBeforeProj <= 0 || slashBeforeProj == len(gitURLPath)-1 {
panic(fmt.Sprintf("无法解析URL: %s", gitURLText))
}
gitProject := gitURLPath[slashBeforeProj+1:]
gitOrg := gitURLPath[1:slashBeforeProj]
return os.Expand(mirrorURLText, func(varName string) string {
if varName == "org" {
return gitOrg
}
if varName == "project" {
return gitProject
}
return ""
})
}