-
Notifications
You must be signed in to change notification settings - Fork 2k
/
currentstore.go
86 lines (69 loc) · 1.63 KB
/
currentstore.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
package localstorage
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/git-lfs/git-lfs/config"
"github.com/git-lfs/git-lfs/errors"
)
const (
tempDirPerms = 0755
localMediaDirPerms = 0755
localLogDirPerms = 0755
)
var (
objects *LocalStorage
notInRepoErr = errors.New("not in a repository")
TempDir = filepath.Join(os.TempDir(), "git-lfs")
checkedTempDir string
)
func Objects() *LocalStorage {
return objects
}
func InitStorage() error {
if len(config.LocalGitStorageDir) == 0 || len(config.LocalGitDir) == 0 {
return notInRepoErr
}
cfg := config.Config.StorageConfig()
TempDir = filepath.Join(cfg.LfsStorageDir, "tmp") // temp files per worktree
objs, err := NewStorage(
filepath.Join(cfg.LfsStorageDir, "objects"),
filepath.Join(TempDir, "objects"),
)
if err != nil {
return errors.Wrap(err, "init LocalStorage")
}
objects = objs
config.LocalLogDir = filepath.Join(objs.RootDir, "logs")
if err := os.MkdirAll(config.LocalLogDir, localLogDirPerms); err != nil {
return errors.Wrap(err, "create log dir")
}
return nil
}
func InitStorageOrFail() {
if err := InitStorage(); err != nil {
if err == notInRepoErr {
return
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
}
func ResolveDirs() {
config.ResolveGitBasicDirs()
InitStorageOrFail()
}
func TempFile(prefix string) (*os.File, error) {
if checkedTempDir != TempDir {
if err := os.MkdirAll(TempDir, tempDirPerms); err != nil {
return nil, err
}
checkedTempDir = TempDir
}
return ioutil.TempFile(TempDir, prefix)
}
func ResetTempDir() error {
checkedTempDir = ""
return os.RemoveAll(TempDir)
}