Skip to content

Commit

Permalink
Merge pull request #963 from github/fix-submod-lfsconfig
Browse files Browse the repository at this point in the history
Fix submodule lfsconfig
  • Loading branch information
technoweenie committed Feb 2, 2016
2 parents 20560c5 + a6df20a commit 45b0574
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 89 deletions.
31 changes: 31 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,38 @@ func GetCommitSummary(commit string) (*CommitSummary, error) {
msg := fmt.Sprintf("Unexpected output from git show: %v", string(out))
return nil, errors.New(msg)
}
}

func GitAndRootDirs() (string, string, error) {
cmd := execCommand("git", "rev-parse", "--git-dir", "--show-toplevel")
out, err := cmd.CombinedOutput()
output := string(out)
if err != nil {
return "", "", fmt.Errorf("Failed to call git rev-parse --git-dir --show-toplevel: %q", output)
}

paths := strings.Split(output, "\n")
pathLen := len(paths)

if pathLen == 0 {
return "", "", fmt.Errorf("Bad git rev-parse output: %q", output)
}

absGitDir, err := filepath.Abs(paths[0])
if err != nil {
return "", "", fmt.Errorf("Error converting %q to absolute: %s", paths[0], err)
}

if pathLen == 1 || len(paths[1]) == 0 {
return absGitDir, "", nil
}

absRootDir, err := filepath.Abs(paths[1])
if err != nil {
return "", "", fmt.Errorf("Error converting %q to absolute: %s", paths[1], err)
}

return absGitDir, absRootDir, nil
}

func RootDir() (string, error) {
Expand Down
94 changes: 7 additions & 87 deletions lfs/lfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func InRepo() bool {

func ResolveDirs() {
var err error
LocalWorkingDir, LocalGitDir, err = resolveGitDir()
LocalGitDir, LocalWorkingDir, err = git.GitAndRootDirs()
if err == nil {
LocalGitStorageDir = resolveGitStorageDir(LocalGitDir)
LocalMediaDir = filepath.Join(LocalGitStorageDir, "lfs", "objects")
Expand All @@ -118,11 +118,16 @@ func ResolveDirs() {
if err := os.MkdirAll(LocalObjectTempDir, tempDirPerms); err != nil {
panic(fmt.Errorf("Error trying to create temp directory in '%s': %s", TempDir, err))
}
} else {
errMsg := err.Error()
tracerx.Printf("Error running 'git rev-parse': %s", errMsg)
if !strings.Contains(errMsg, "Not a git repository") {
fmt.Fprintf(os.Stderr, "Error: %s\n", errMsg)
}
}
}

func init() {

tracerx.DefaultKey = "GIT"
tracerx.Prefix = "trace git-lfs: "

Expand All @@ -141,91 +146,6 @@ func init() {
)
}

func resolveGitDir() (string, string, error) {
gitDir := Config.Getenv("GIT_DIR")
workTree := Config.Getenv("GIT_WORK_TREE")

if gitDir != "" {
return processGitDirVar(gitDir, workTree)
}

workTreeR, gitDirR, err := resolveGitDirFromCurrentDir()
if err != nil {
return "", "", err
}

if workTree != "" {
return processWorkTreeVar(gitDirR, workTree)
}

return workTreeR, gitDirR, nil
}

func processGitDirVar(gitDir, workTree string) (string, string, error) {
if workTree != "" {
return processWorkTreeVar(gitDir, workTree)
}

// See `core.worktree` in `man git-config`:
// “If --git-dir or GIT_DIR is specified but none of --work-tree, GIT_WORK_TREE and
// core.worktree is specified, the current working directory is regarded as the top
// level of your working tree.”

wd, err := os.Getwd()
if err != nil {
return "", "", err
}

return wd, gitDir, nil
}

func processWorkTreeVar(gitDir, workTree string) (string, string, error) {
// See `core.worktree` in `man git-config`:
// “The value [of core.worktree, GIT_WORK_TREE, or --work-tree] can be an absolute path
// or relative to the path to the .git directory, which is either specified
// by --git-dir or GIT_DIR, or automatically discovered.”

if filepath.IsAbs(workTree) {
return workTree, gitDir, nil
}

base := filepath.Dir(filepath.Clean(gitDir))
absWorkTree := filepath.Join(base, workTree)
return absWorkTree, gitDir, nil
}

func resolveGitDirFromCurrentDir() (string, string, error) {

// Get root of the git working dir
gitDir, err := git.GitDir()
if err != nil {
return "", "", err
}

// Allow this to fail, will do so if GIT_DIR isn't set but GIT_WORK_TREE is rel
// Dealt with by parent
rootDir, _ := git.RootDir()

return rootDir, gitDir, nil
}

func resolveDotGitFile(file string) (string, string, error) {
// The local working directory is the directory the `.git` file is located in.
wd := filepath.Dir(file)

// The `.git` file tells us where the submodules `.git` directory is.
gitDir, err := processDotGitFile(file)
if err != nil {
return "", "", err
}

return wd, gitDir, nil
}

func processDotGitFile(file string) (string, error) {
return processGitRedirectFile(file, gitPtrPrefix)
}

func processGitRedirectFile(file, prefix string) (string, error) {
data, err := ioutil.ReadFile(file)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions test/test-commit-delete-push.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ begin_test "commit, delete, then push"
git rm deleted.dat
git commit -m "did not need deleted.dat after all"

GIT_TRACE=1 git lfs push origin master --dry-run 2>&1 | tee dryrun.log
git lfs push origin master --dry-run 2>&1 | tee dryrun.log
grep "push ee31ef227442936872744b50d3297385c08b40ffc7baeaf34a39e6d81d6cd9ee => deleted.dat" dryrun.log
grep "push 3428719b7688c78a0cc8ba4b9e80b4e464c815fbccfd4b20695a15ffcefc22af => added.dat" dryrun.log

git log
GIT_TRACE=1 git push origin master 2>&1 > push.log || {
git push origin master 2>&1 > push.log || {
cat push.log
git lfs logs last
exit 1
Expand Down
75 changes: 75 additions & 0 deletions test/test-submodule-lfsconfig.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env bash

. "test/testlib.sh"
lfsname="submodule-config-test-lfs"
reponame="submodule-config-test-repo"
submodname="submodule-config-test-submodule"

begin_test "submodule env with .lfsconfig"
(
set -e

# setup dummy repo with lfs store
# no git data will be pushed, just lfs objects
setup_remote_repo "$lfsname"
echo $GITSERVER/$lfsname.git/info/lfs

# setup submodule
setup_remote_repo "$submodname"
clone_repo "$submodname" submod
mkdir dir
git config -f .lfsconfig lfs.url "$GITSERVER/$lfsname.git/info/lfs"
git lfs track "*.dat"
submodcontent="submodule lfs file"
submodoid=$(calc_oid "$submodcontent")
printf "$submodcontent" > dir/test.dat
git add .lfsconfig .gitattributes dir
git commit -m "create submodule"
git push origin master

assert_server_object "$lfsname" "$submodoid"

# setup repo with submodule
setup_remote_repo "$reponame"
clone_repo "$reponame" repo
git config -f .lfsconfig lfs.url "$GITSERVER/$lfsname.git/info/lfs"
git submodule add "$GITSERVER/$submodname" sub
git submodule update
git lfs track "*.dat"
mkdir dir
repocontent="repository lfs file"
repooid=$(calc_oid "$repocontent")
printf "$repocontent" > dir/test.dat
git add .gitattributes .lfsconfig .gitmodules dir sub
git commit -m "create repo"
git push origin master

assert_server_object "$lfsname" "$repooid"

echo "repo"
git lfs env | tee env.log
grep "Endpoint=$GITSERVER/$lfsname.git/info/lfs (auth=basic)$" env.log

cd sub
echo "./sub"
git lfs env | tee env.log
grep "Endpoint=$GITSERVER/$lfsname.git/info/lfs (auth=basic)$" env.log

cd dir
echo "./sub/dir"
git lfs env | tee env.log
grep "Endpoint=$GITSERVER/$lfsname.git/info/lfs (auth=basic)$" env.log
)
end_test

begin_test "submodule update --init --remote with .lfsconfig"
(
set -e
clone_repo "$reponame" clone
grep "$repocontent" dir/test.dat

git submodule update --init --remote

grep "$submodcontent" sub/dir/test.dat
)
end_test
1 change: 1 addition & 0 deletions test/test-submodule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ begin_test "submodule local git dir"
git push origin master

grep "sub module" sub/dir/README || {
echo "submodule not setup correctly?"
cat sub/dir/README
exit 1
}
Expand Down

0 comments on commit 45b0574

Please sign in to comment.