localstorage: exit stage left

This commit is contained in:
rick olson 2017-10-25 13:23:12 -06:00
parent 3191b2cb46
commit b61a4bcb63
11 changed files with 9 additions and 194 deletions

@ -7,7 +7,6 @@ import (
"path/filepath"
"strings"
"github.com/git-lfs/git-lfs/localstorage"
"github.com/git-lfs/git-lfs/subprocess"
"github.com/git-lfs/git-lfs/git"
@ -71,8 +70,6 @@ func cloneCommand(cmd *cobra.Command, args []string) {
// Make sure we pop back to dir we started in at the end
defer os.Chdir(cwd)
// Also need to derive dirs now
localstorage.ResolveDirs(cfg)
requireInRepo()
// Now just call pull with default args

@ -4,7 +4,6 @@ import (
"os"
"github.com/git-lfs/git-lfs/lfs"
"github.com/git-lfs/git-lfs/localstorage"
"github.com/spf13/cobra"
)
@ -25,7 +24,6 @@ func installCommand(cmd *cobra.Command, args []string) {
}
if !skipRepoInstall && (localInstall || cfg.InRepo()) {
localstorage.InitStorageOrFail(cfg)
installHooksCommand(cmd, args)
}
@ -78,8 +76,6 @@ func init() {
cmd.Flags().BoolVarP(&skipSmudgeInstall, "skip-smudge", "s", false, "Skip automatic downloading of objects on clone or pull.")
cmd.Flags().BoolVarP(&skipRepoInstall, "skip-repo", "", false, "Skip repo setup, just install global filters.")
cmd.Flags().BoolVarP(&manualInstall, "manual", "m", false, "Print instructions for manual install.")
cmd.AddCommand(NewCommand("hooks", installHooksCommand))
cmd.PreRun = setupLocalStorage
})
}

@ -9,7 +9,6 @@ import (
"github.com/git-lfs/git-lfs/git/githistory"
"github.com/git-lfs/git-lfs/git/githistory/log"
"github.com/git-lfs/git-lfs/git/odb"
"github.com/git-lfs/git-lfs/localstorage"
"github.com/spf13/cobra"
)
@ -254,17 +253,6 @@ func init() {
cmd.PersistentFlags().StringSliceVar(&migrateExcludeRefs, "exclude-ref", nil, "An explicit list of refs to exclude")
cmd.PersistentFlags().BoolVar(&migrateEverything, "everything", false, "Migrate all local references")
cmd.PersistentPreRun = func(_ *cobra.Command, args []string) {
// Initialize local storage before running any child
// subcommands, since migrations require lfs.TempDir to
// be initialized within ".git/lfs/objects".
//
// When lfs.TempDir is initialized to "/tmp",
// hard-linking can fail when another filesystem is
// mounted at "/tmp" (such as tmpfs).
localstorage.InitStorageOrFail(cfg)
}
cmd.AddCommand(importCmd, info)
})
}

@ -10,7 +10,6 @@ import (
"github.com/git-lfs/git-lfs/fs"
"github.com/git-lfs/git-lfs/git"
"github.com/git-lfs/git-lfs/lfs"
"github.com/git-lfs/git-lfs/localstorage"
"github.com/git-lfs/git-lfs/progress"
"github.com/git-lfs/git-lfs/tools"
"github.com/git-lfs/git-lfs/tools/humanize"
@ -54,7 +53,7 @@ type PruneProgress struct {
type PruneProgressChan chan PruneProgress
func prune(fetchPruneConfig lfs.FetchPruneConfig, verifyRemote, dryRun, verbose bool) {
localObjects := make([]localstorage.Object, 0, 100)
localObjects := make([]fs.Object, 0, 100)
retainedObjects := tools.NewStringSetWithCapacity(100)
var reachableObjects tools.StringSet
var taskwait sync.WaitGroup
@ -295,14 +294,11 @@ func pruneDeleteFiles(prunableObjects []string) {
}
// Background task, must call waitg.Done() once at end
func pruneTaskGetLocalObjects(outLocalObjects *[]localstorage.Object, progChan PruneProgressChan, waitg *sync.WaitGroup) {
func pruneTaskGetLocalObjects(outLocalObjects *[]fs.Object, progChan PruneProgressChan, waitg *sync.WaitGroup) {
defer waitg.Done()
cfg.EachLFSObject(func(obj fs.Object) error {
*outLocalObjects = append(*outLocalObjects, localstorage.Object{
Oid: obj.Oid,
Size: obj.Size,
})
*outLocalObjects = append(*outLocalObjects, obj)
progChan <- PruneProgress{PruneProgressTypeLocal, 1}
return nil
})

@ -1,7 +1,6 @@
package commands
import (
"github.com/git-lfs/git-lfs/localstorage"
"github.com/spf13/cobra"
)
@ -12,7 +11,6 @@ func uninstallCommand(cmd *cobra.Command, args []string) {
}
if localInstall || cfg.InRepo() {
localstorage.InitStorageOrFail(cfg)
uninstallHooksCommand(cmd, args)
}
@ -33,6 +31,5 @@ func init() {
cmd.Flags().BoolVarP(&localInstall, "local", "l", false, "Set the Git LFS config for the local Git repository only.")
cmd.Flags().BoolVarP(&systemInstall, "system", "", false, "Set the Git LFS config in system-wide scope.")
cmd.AddCommand(NewCommand("hooks", uninstallHooksCommand))
cmd.PreRun = setupLocalStorage
})
}

@ -92,6 +92,7 @@ func closeAPIClient() error {
func newLockClient(remote string) *locking.Client {
lockClient, err := locking.NewClient(remote, getAPIClient())
if err == nil {
os.MkdirAll(cfg.LFSStorageDir(), 0755)
err = lockClient.SetupFileCache(cfg.LFSStorageDir())
}

@ -10,8 +10,6 @@ import (
"time"
"github.com/git-lfs/git-lfs/config"
"github.com/git-lfs/git-lfs/lfsapi"
"github.com/git-lfs/git-lfs/localstorage"
"github.com/spf13/cobra"
)
@ -26,7 +24,7 @@ var (
// Each command will initialize the local storage ('.git/lfs') directory when
// run, unless the PreRun hook is set to nil.
func NewCommand(name string, runFn func(*cobra.Command, []string)) *cobra.Command {
return &cobra.Command{Use: name, Run: runFn, PreRun: resolveLocalStorage}
return &cobra.Command{Use: name, Run: runFn, PreRun: setupHTTPLogger}
}
// RegisterCommand creates a direct 'git-lfs' subcommand, given a command name,
@ -79,18 +77,6 @@ func gitlfsCommand(cmd *cobra.Command, args []string) {
cmd.Usage()
}
// resolveLocalStorage implements the `func(*cobra.Command, []string)` signature
// necessary to wire it up via `cobra.Command.PreRun`. When run, this function
// will resolve the localstorage directories.
func resolveLocalStorage(cmd *cobra.Command, args []string) {
localstorage.ResolveDirs(cfg)
setupHTTPLogger(getAPIClient())
}
func setupLocalStorage(cmd *cobra.Command, args []string) {
setupHTTPLogger(getAPIClient())
}
func helpCommand(cmd *cobra.Command, args []string) {
if len(args) == 0 {
printHelp("git-lfs")
@ -112,8 +98,8 @@ func printHelp(commandName string) {
}
}
func setupHTTPLogger(c *lfsapi.Client) {
if c == nil || len(os.Getenv("GIT_LOG_STATS")) < 1 {
func setupHTTPLogger(cmd *cobra.Command, args []string) {
if len(os.Getenv("GIT_LOG_STATS")) < 1 {
return
}
@ -128,6 +114,6 @@ func setupHTTPLogger(c *lfsapi.Client) {
if err != nil {
fmt.Fprintf(os.Stderr, "Error logging http stats: %s\n", err)
} else {
c.LogHTTPStats(file)
getAPIClient().LogHTTPStats(file)
}
}

@ -1,62 +0,0 @@
package localstorage
import (
"fmt"
"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 InitStorage(cfg *config.Configuration) error {
if len(cfg.LocalGitStorageDir()) == 0 || len(cfg.LocalGitDir()) == 0 {
return notInRepoErr
}
storCfg := NewConfig(cfg)
TempDir = filepath.Join(storCfg.LfsStorageDir, "tmp") // temp files per worktree
objs, err := NewStorage(
filepath.Join(storCfg.LfsStorageDir, "objects"),
filepath.Join(TempDir, "objects"),
)
if err != nil {
return errors.Wrap(err, "init LocalStorage")
}
objects = objs
if err := os.MkdirAll(cfg.LocalLogDir(), localLogDirPerms); err != nil {
return errors.Wrap(err, "create log dir")
}
return nil
}
func InitStorageOrFail(cfg *config.Configuration) {
if err := InitStorage(cfg); err != nil {
if err == notInRepoErr {
return
}
fmt.Fprintf(os.Stderr, "ERROR: %s\n", err)
os.Exit(1)
}
}
func ResolveDirs(cfg *config.Configuration) {
InitStorageOrFail(cfg)
}

@ -1,81 +0,0 @@
// Package localstorage handles LFS content stored locally
// NOTE: Subject to change, do not rely on this package from outside git-lfs source
package localstorage
import (
"fmt"
"os"
"path/filepath"
"regexp"
"github.com/git-lfs/git-lfs/config"
)
const (
chanBufSize = 100
)
var (
oidRE = regexp.MustCompile(`\A[[:alnum:]]{64}`)
dirPerms os.FileMode = 0755
)
// LocalStorage manages the locally stored LFS objects for a repository.
type LocalStorage struct {
RootDir string
TempDir string
}
// Object represents a locally stored LFS object.
type Object struct {
Oid string
Size int64
}
func NewStorage(storageDir, tempDir string) (*LocalStorage, error) {
if err := os.MkdirAll(storageDir, dirPerms); err != nil {
return nil, err
}
if err := os.MkdirAll(tempDir, dirPerms); err != nil {
return nil, err
}
return &LocalStorage{storageDir, tempDir}, nil
}
func (s *LocalStorage) ObjectPath(oid string) string {
return filepath.Join(localObjectDir(s, oid), oid)
}
func (s *LocalStorage) BuildObjectPath(oid string) (string, error) {
dir := localObjectDir(s, oid)
if err := os.MkdirAll(dir, dirPerms); err != nil {
return "", fmt.Errorf("Error trying to create local storage directory in %q: %s", dir, err)
}
return filepath.Join(dir, oid), nil
}
// Storage configuration
type Configuration struct {
LfsStorageDir string
}
func NewConfig(cfg *config.Configuration) (c Configuration) {
dir, _ := cfg.Git.Get("lfs.storage")
if len(dir) == 0 {
dir = "lfs"
}
if filepath.IsAbs(dir) {
c.LfsStorageDir = dir
} else {
c.LfsStorageDir = filepath.Join(cfg.LocalGitStorageDir(), dir)
}
return
}
func localObjectDir(s *LocalStorage, oid string) string {
return filepath.Join(s.RootDir, oid[0:2], oid[2:4])
}

@ -152,7 +152,7 @@ begin_test "config: ignoring unsafe lfsconfig keys"
# Insert an 'unsafe' key into this repository's '.lfsconfig'.
git config --file=.lfsconfig core.askpass unsafe
git lfs status 2>&1 | tee status.log
git lfs env 2>&1 | tee status.log
grep "WARNING: These unsafe lfsconfig keys were ignored:" status.log
grep " core.askpass" status.log

@ -25,7 +25,6 @@ import (
"github.com/git-lfs/git-lfs/fs"
"github.com/git-lfs/git-lfs/git"
"github.com/git-lfs/git-lfs/lfs"
"github.com/git-lfs/git-lfs/localstorage"
)
type RepoType int
@ -89,7 +88,6 @@ func (r *Repo) Pushd() {
r.callback.Fatalf("Can't chdir %v", err)
}
r.popDir = oldwd
localstorage.ResolveDirs(r.cfg)
}
func (r *Repo) Popd() {
@ -256,7 +254,6 @@ func (infile *FileInput) writeLFSPointer(repo *Repo, inputData io.Reader) (*lfs.
// this only created the temp file, move to final location
tmpfile := cleaned.Filename
storageOnce.Do(func() { localstorage.ResolveDirs(repo.cfg) })
mediafile, err := repo.fs.ObjectPath(cleaned.Oid)
if err != nil {
return nil, errors.Wrap(err, "local media path")