forked from git-lfs/git-lfs
/
lfs.go
171 lines (138 loc) · 3.8 KB
/
lfs.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
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
package lfs
import (
"fmt"
"github.com/github/git-lfs/git"
"github.com/rubyist/tracerx"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
)
const Version = "0.5.0"
var (
LargeSizeThreshold = 5 * 1024 * 1024
TempDir = filepath.Join(os.TempDir(), "git-lfs")
UserAgent string
LocalWorkingDir string
LocalGitDir string
LocalMediaDir string
LocalLogDir string
checkedTempDir string
)
func TempFile(prefix string) (*os.File, error) {
if checkedTempDir != TempDir {
if err := os.MkdirAll(TempDir, 0774); err != nil {
return nil, err
}
checkedTempDir = TempDir
}
return ioutil.TempFile(TempDir, prefix)
}
func ResetTempDir() error {
checkedTempDir = ""
return os.RemoveAll(TempDir)
}
func LocalMediaPath(sha string) (string, error) {
path := filepath.Join(LocalMediaDir, sha[0:2], sha[2:4])
if err := os.MkdirAll(path, 0744); err != nil {
return "", fmt.Errorf("Error trying to create local media directory in '%s': %s", path, err)
}
return filepath.Join(path, sha), nil
}
func Environ() []string {
osEnviron := os.Environ()
env := make([]string, 4, len(osEnviron)+4)
env[0] = fmt.Sprintf("LocalWorkingDir=%s", LocalWorkingDir)
env[1] = fmt.Sprintf("LocalGitDir=%s", LocalGitDir)
env[2] = fmt.Sprintf("LocalMediaDir=%s", LocalMediaDir)
env[3] = fmt.Sprintf("TempDir=%s", TempDir)
for _, e := range osEnviron {
if !strings.Contains(e, "GIT_") {
continue
}
env = append(env, e)
}
return env
}
func InRepo() bool {
return LocalWorkingDir != ""
}
func init() {
var err error
tracerx.DefaultKey = "GIT"
tracerx.Prefix = "trace git-lfs: "
LocalWorkingDir, LocalGitDir, err = resolveGitDir()
if err == nil {
LocalMediaDir = filepath.Join(LocalGitDir, "lfs", "objects")
LocalLogDir = filepath.Join(LocalMediaDir, "logs")
TempDir = filepath.Join(LocalGitDir, "lfs", "tmp")
if err := os.MkdirAll(LocalMediaDir, 0744); err != nil {
panic(fmt.Errorf("Error trying to create objects directory in '%s': %s", LocalMediaDir, err))
}
if err := os.MkdirAll(LocalLogDir, 0744); err != nil {
panic(fmt.Errorf("Error trying to create log directory in '%s': %s", LocalLogDir, err))
}
if err := os.MkdirAll(TempDir, 0744); err != nil {
panic(fmt.Errorf("Error trying to create temp directory in '%s': %s", TempDir, err))
}
}
gitVersion, err := git.Config.Version()
if err != nil {
gitVersion = "unknown"
}
UserAgent = fmt.Sprintf("git-lfs/%s (GitHub; %s %s; git %s; go %s)", Version,
runtime.GOOS,
runtime.GOARCH,
strings.Replace(gitVersion, "git version ", "", 1),
strings.Replace(runtime.Version(), "go", "", 1))
}
func resolveGitDir() (string, string, error) {
wd, err := os.Getwd()
if err != nil {
return "", "", err
}
return recursiveResolveGitDir(wd)
}
func recursiveResolveGitDir(dir string) (string, string, error) {
var cleanDir = filepath.Clean(dir)
if cleanDir[len(cleanDir)-1] == os.PathSeparator {
return "", "", fmt.Errorf("Git repository not found")
}
if filepath.Base(dir) == gitExt {
return filepath.Dir(dir), dir, nil
}
gitDir := filepath.Join(dir, gitExt)
if info, err := os.Stat(gitDir); err == nil {
if info.IsDir() {
return dir, gitDir, nil
} else {
return processDotGitFile(gitDir)
}
}
return recursiveResolveGitDir(filepath.Dir(dir))
}
func processDotGitFile(file string) (string, string, error) {
f, err := os.Open(file)
defer f.Close()
if err != nil {
return "", "", err
}
data := make([]byte, 512)
n, err := f.Read(data)
if err != nil {
return "", "", err
}
contents := string(data[0:n])
wd, _ := os.Getwd()
if strings.HasPrefix(contents, gitPtrPrefix) {
dir := strings.TrimSpace(strings.Split(contents, gitPtrPrefix)[1])
absDir, _ := filepath.Abs(dir)
return wd, absDir, nil
}
return wd, "", nil
}
const (
gitExt = ".git"
gitPtrPrefix = "gitdir: "
)