Refactor remaining local store state into localstorage pkg

This commit is contained in:
Steve Streeting 2016-05-17 10:28:42 +01:00
parent 3298d9bbb3
commit e14f7b8029
7 changed files with 95 additions and 59 deletions

@ -8,7 +8,7 @@ import (
"github.com/github/git-lfs/config"
"github.com/github/git-lfs/git"
"github.com/github/git-lfs/lfs"
"github.com/github/git-lfs/localstorage"
"github.com/github/git-lfs/tools"
"github.com/github/git-lfs/vendor/_nuts/github.com/spf13/cobra"
)
@ -60,7 +60,7 @@ func cloneCommand(cmd *cobra.Command, args []string) {
defer os.Chdir(cwd)
// Also need to derive dirs now
lfs.ResolveDirs()
localstorage.ResolveDirs()
requireInRepo()
// Now just call pull with default args

@ -2,7 +2,6 @@ package lfs
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
@ -13,66 +12,39 @@ import (
"github.com/github/git-lfs/vendor/_nuts/github.com/rubyist/tracerx"
)
const (
tempDirPerms = 0755
localMediaDirPerms = 0755
localLogDirPerms = 0755
)
var (
LargeSizeThreshold = 5 * 1024 * 1024
objects *localstorage.LocalStorage
LocalMediaDir string // root of lfs objects
LocalObjectTempDir string // where temporarily downloading objects are stored
TempDir = filepath.Join(os.TempDir(), "git-lfs")
checkedTempDir string
)
func ResolveDirs() {
config.ResolveGitBasicDirs()
TempDir = filepath.Join(config.LocalGitDir, "lfs", "tmp") // temp files per worktree
objs, err := localstorage.New(
filepath.Join(config.LocalGitStorageDir, "lfs", "objects"),
filepath.Join(TempDir, "objects"),
)
if err != nil {
panic(fmt.Sprintf("Error trying to init LocalStorage: %s", err))
// LocalMediaDir returns the root of lfs objects
func LocalMediaDir() string {
if localstorage.Objects() != nil {
return localstorage.Objects().RootDir
}
return ""
}
objects = objs
LocalMediaDir = objs.RootDir
LocalObjectTempDir = objs.TempDir
config.LocalLogDir = filepath.Join(objs.RootDir, "logs")
if err := os.MkdirAll(config.LocalLogDir, localLogDirPerms); err != nil {
panic(fmt.Errorf("Error trying to create log directory in '%s': %s", config.LocalLogDir, err))
func LocalObjectTempDir() string {
if localstorage.Objects() != nil {
return localstorage.Objects().TempDir
}
return ""
}
func TempDir() string {
return localstorage.TempDir
}
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)
return localstorage.TempFile(prefix)
}
func LocalMediaPath(oid string) (string, error) {
return objects.BuildObjectPath(oid)
return localstorage.Objects().BuildObjectPath(oid)
}
func LocalMediaPathReadOnly(oid string) string {
return objects.ObjectPath(oid)
return localstorage.Objects().ObjectPath(oid)
}
func LocalReferencePath(sha string) string {
@ -83,7 +55,7 @@ func LocalReferencePath(sha string) string {
}
func ObjectExistsOfSize(oid string, size int64) bool {
path := objects.ObjectPath(oid)
path := localstorage.Objects().ObjectPath(oid)
return tools.FileExistsOfSize(path, size)
}
@ -94,9 +66,9 @@ func Environ() []string {
fmt.Sprintf("LocalWorkingDir=%s", config.LocalWorkingDir),
fmt.Sprintf("LocalGitDir=%s", config.LocalGitDir),
fmt.Sprintf("LocalGitStorageDir=%s", config.LocalGitStorageDir),
fmt.Sprintf("LocalMediaDir=%s", LocalMediaDir),
fmt.Sprintf("LocalMediaDir=%s", LocalMediaDir()),
fmt.Sprintf("LocalReferenceDir=%s", config.LocalReferenceDir),
fmt.Sprintf("TempDir=%s", TempDir),
fmt.Sprintf("TempDir=%s", TempDir()),
fmt.Sprintf("ConcurrentTransfers=%d", config.Config.ConcurrentTransfers()),
fmt.Sprintf("BatchTransfer=%v", config.Config.BatchTransfer()),
fmt.Sprintf("SkipDownloadErrors=%v", config.Config.SkipDownloadErrors()),
@ -135,21 +107,21 @@ func InRepo() bool {
}
func ClearTempObjects() error {
if objects == nil {
if localstorage.Objects() == nil {
return nil
}
return objects.ClearTempObjects()
return localstorage.Objects().ClearTempObjects()
}
func ScanObjectsChan() <-chan localstorage.Object {
return objects.ScanObjectsChan()
return localstorage.Objects().ScanObjectsChan()
}
func init() {
tracerx.DefaultKey = "GIT"
tracerx.Prefix = "trace git-lfs: "
ResolveDirs()
localstorage.ResolveDirs()
}
const (
@ -159,7 +131,7 @@ const (
// only used in tests
func AllObjects() []localstorage.Object {
return objects.AllObjects()
return localstorage.Objects().AllObjects()
}
func LinkOrCopyFromReference(oid string, size int64) error {

@ -161,7 +161,7 @@ func downloadFile(writer io.Writer, ptr *Pointer, workingfile, mediafile string,
// external Git LFS tools.
func bufferDownloadedFile(filename string, reader io.Reader, size int64, cb progress.CopyCallback) error {
oid := filepath.Base(filename)
f, err := ioutil.TempFile(LocalObjectTempDir, oid+"-")
f, err := ioutil.TempFile(LocalObjectTempDir(), oid+"-")
if err != nil {
return fmt.Errorf("cannot create temp file: %v", err)
}

@ -264,7 +264,7 @@ func IsWindows() bool {
}
func CopyFileContents(src string, dst string) error {
tmp, err := ioutil.TempFile(TempDir, filepath.Base(dst))
tmp, err := ioutil.TempFile(TempDir(), filepath.Base(dst))
if err != nil {
return err
}

@ -0,0 +1,63 @@
package localstorage
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/github/git-lfs/config"
)
const (
tempDirPerms = 0755
localMediaDirPerms = 0755
localLogDirPerms = 0755
)
var (
objects *LocalStorage
TempDir = filepath.Join(os.TempDir(), "git-lfs")
checkedTempDir string
)
func Objects() *LocalStorage {
return objects
}
func ResolveDirs() {
config.ResolveGitBasicDirs()
TempDir = filepath.Join(config.LocalGitDir, "lfs", "tmp") // temp files per worktree
objs, err := NewStorage(
filepath.Join(config.LocalGitStorageDir, "lfs", "objects"),
filepath.Join(TempDir, "objects"),
)
if err != nil {
panic(fmt.Sprintf("Error trying to init LocalStorage: %s", err))
}
objects = objs
config.LocalLogDir = filepath.Join(objs.RootDir, "logs")
if err := os.MkdirAll(config.LocalLogDir, localLogDirPerms); err != nil {
panic(fmt.Errorf("Error trying to create log directory in '%s': %s", config.LocalLogDir, err))
}
}
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)
}

@ -28,7 +28,7 @@ type Object struct {
Size int64
}
func New(storageDir, tempDir string) (*LocalStorage, error) {
func NewStorage(storageDir, tempDir string) (*LocalStorage, error) {
if err := os.MkdirAll(storageDir, dirPerms); err != nil {
return nil, err
}

@ -21,6 +21,7 @@ import (
"github.com/github/git-lfs/git"
"github.com/github/git-lfs/lfs"
"github.com/github/git-lfs/localstorage"
)
type RepoType int
@ -74,7 +75,7 @@ func (r *Repo) Pushd() {
r.callback.Fatalf("Can't chdir %v", err)
}
r.popDir = oldwd
lfs.ResolveDirs()
localstorage.ResolveDirs()
}
func (r *Repo) Popd() {