git-lfs/lfs/lfs.go

172 lines
3.8 KiB
Go
Raw Normal View History

2015-03-19 19:30:55 +00:00
package lfs
import (
"fmt"
2015-03-19 19:30:55 +00:00
"github.com/github/git-lfs/git"
2014-10-04 16:10:02 +00:00
"github.com/rubyist/tracerx"
2013-09-27 15:19:54 +00:00
"io/ioutil"
"os"
"path/filepath"
"runtime"
"strings"
)
2015-03-27 18:13:03 +00:00
const Version = "0.5.0.pre1"
2013-09-27 15:19:54 +00:00
var (
LargeSizeThreshold = 5 * 1024 * 1024
2015-03-19 19:30:55 +00:00
TempDir = filepath.Join(os.TempDir(), "git-lfs")
UserAgent string
2014-02-01 20:08:53 +00:00
LocalWorkingDir string
LocalGitDir string
2013-09-27 15:19:54 +00:00
LocalMediaDir string
2013-12-05 21:31:59 +00:00
LocalLogDir string
checkedTempDir string
2013-09-27 15:19:54 +00:00
)
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)
2013-09-27 15:19:54 +00:00
}
func ResetTempDir() error {
checkedTempDir = ""
return os.RemoveAll(TempDir)
}
2014-06-05 19:05:02 +00:00
func LocalMediaPath(sha string) (string, error) {
2013-09-27 15:19:54 +00:00
path := filepath.Join(LocalMediaDir, sha[0:2], sha[2:4])
if err := os.MkdirAll(path, 0744); err != nil {
2014-06-05 19:05:02 +00:00
return "", fmt.Errorf("Error trying to create local media directory in '%s': %s", path, err)
2013-09-27 15:19:54 +00:00
}
2014-06-05 19:05:02 +00:00
return filepath.Join(path, sha), nil
2013-09-27 15:19:54 +00:00
}
func Environ() []string {
osEnviron := os.Environ()
2014-02-01 20:08:53 +00:00
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
}
2014-02-01 20:11:33 +00:00
env = append(env, e)
}
return env
}
2014-06-03 21:05:58 +00:00
func InRepo() bool {
return LocalWorkingDir != ""
}
2013-09-27 15:19:54 +00:00
func init() {
var err error
2014-10-04 16:10:02 +00:00
tracerx.DefaultKey = "GIT"
2015-03-19 19:30:55 +00:00
tracerx.Prefix = "trace git-lfs: "
2014-10-04 16:10:02 +00:00
LocalWorkingDir, LocalGitDir, err = resolveGitDir()
if err == nil {
2015-03-19 19:30:55 +00:00
LocalMediaDir = filepath.Join(LocalGitDir, "lfs", "objects")
LocalLogDir = filepath.Join(LocalMediaDir, "logs")
2015-03-19 19:30:55 +00:00
TempDir = filepath.Join(LocalGitDir, "lfs", "tmp")
2015-01-30 20:04:52 +00:00
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 {
2014-06-05 18:48:23 +00:00
panic(fmt.Errorf("Error trying to create temp directory in '%s': %s", TempDir, err))
}
2014-09-18 18:38:10 +00:00
2013-09-27 15:19:54 +00:00
}
gitVersion, err := git.Config.Version()
if err != nil {
gitVersion = "unknown"
}
2015-03-19 19:30:55 +00:00
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))
2013-10-04 17:09:03 +00:00
}
2013-09-27 15:19:54 +00:00
func resolveGitDir() (string, string, error) {
2014-02-01 20:08:53 +00:00
wd, err := os.Getwd()
if err != nil {
2014-06-05 19:02:11 +00:00
return "", "", err
2013-10-04 17:09:03 +00:00
}
2013-09-27 15:19:54 +00:00
2014-02-01 20:08:53 +00:00
return recursiveResolveGitDir(wd)
2013-10-04 17:09:03 +00:00
}
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")
}
2014-02-01 20:08:53 +00:00
if filepath.Base(dir) == gitExt {
return filepath.Dir(dir), dir, nil
}
2014-02-01 19:52:55 +00:00
gitDir := filepath.Join(dir, gitExt)
if info, err := os.Stat(gitDir); err == nil {
if info.IsDir() {
return dir, gitDir, nil
} else {
return processDotGitFile(gitDir)
2014-02-01 19:52:55 +00:00
}
}
2014-02-01 20:08:53 +00:00
2014-02-01 19:52:55 +00:00
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: "
)