2013-09-23 00:34:53 +00:00
|
|
|
package gitmedia
|
2013-09-22 23:04:06 +00:00
|
|
|
|
2013-09-27 14:39:58 +00:00
|
|
|
import (
|
2014-01-24 01:30:48 +00:00
|
|
|
"fmt"
|
2013-09-27 15:19:54 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-09-27 14:39:58 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2013-09-22 23:04:06 +00:00
|
|
|
const Version = "0.0.1"
|
2013-09-27 14:39:58 +00:00
|
|
|
|
2013-09-27 15:19:54 +00:00
|
|
|
var (
|
|
|
|
LargeSizeThreshold = 5 * 1024 * 1024
|
|
|
|
TempDir = filepath.Join(os.TempDir(), "git-media")
|
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
|
2013-09-27 15:19:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TempFile() (*os.File, error) {
|
|
|
|
return ioutil.TempFile(TempDir, "")
|
|
|
|
}
|
2013-09-27 14:39:58 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-01-24 01:30:48 +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)
|
2014-01-24 01:30:48 +00:00
|
|
|
|
2014-05-22 22:38:36 +00:00
|
|
|
for _, e := range osEnviron {
|
2014-01-24 01:30:48 +00:00
|
|
|
if !strings.Contains(e, "GIT_") {
|
|
|
|
continue
|
|
|
|
}
|
2014-02-01 20:11:33 +00:00
|
|
|
env = append(env, e)
|
2014-01-24 01:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2014-05-28 17:32:15 +00:00
|
|
|
var err error
|
|
|
|
LocalWorkingDir, LocalGitDir, err = resolveGitDir()
|
2014-06-03 20:41:48 +00:00
|
|
|
if err == nil {
|
|
|
|
LocalMediaDir = filepath.Join(LocalGitDir, "media")
|
|
|
|
LocalLogDir = filepath.Join(LocalMediaDir, "logs")
|
|
|
|
queueDir = setupQueueDir()
|
2014-05-28 17:32:15 +00:00
|
|
|
|
2014-06-03 20:41:48 +00:00
|
|
|
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-06-03 20:41:48 +00:00
|
|
|
}
|
2013-09-27 15:19:54 +00:00
|
|
|
}
|
2013-10-04 17:09:03 +00:00
|
|
|
}
|
2013-09-27 15:19:54 +00:00
|
|
|
|
2014-05-28 17:32:15 +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
|
|
|
}
|
|
|
|
|
2014-05-28 17:32:15 +00:00
|
|
|
func recursiveResolveGitDir(dir string) (string, string, error) {
|
|
|
|
if len(dir) == 1 && dir[0] == os.PathSeparator {
|
|
|
|
return "", "", fmt.Errorf("Git repository not found")
|
|
|
|
}
|
|
|
|
|
2014-02-01 20:08:53 +00:00
|
|
|
if filepath.Base(dir) == gitExt {
|
2014-05-28 17:32:15 +00:00
|
|
|
return filepath.Dir(dir), dir, nil
|
2013-10-04 20:08:42 +00:00
|
|
|
}
|
2014-02-01 19:52:55 +00:00
|
|
|
|
|
|
|
gitDir := filepath.Join(dir, gitExt)
|
|
|
|
if info, err := os.Stat(gitDir); err == nil {
|
|
|
|
if info.IsDir() {
|
2014-05-28 17:32:15 +00:00
|
|
|
return dir, gitDir, nil
|
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))
|
|
|
|
}
|
|
|
|
|
|
|
|
const gitExt = ".git"
|