locks: path formatting changes

* strip leading slash from paths
* disallow directory locking (+ test)
This commit is contained in:
Taylor Blau 2016-06-04 11:25:02 -06:00
parent 31e5ed01c8
commit 8fb44158b5
2 changed files with 54 additions and 2 deletions

@ -1,6 +1,7 @@
package commands
import (
"fmt"
"os"
"path/filepath"
"strings"
@ -44,7 +45,7 @@ func lockCommand(cmd *cobra.Command, args []string) {
path, err := lockPath(args[0])
if err != nil {
Error(err.Error())
Exit(err.Error())
}
s, resp := API.Locks.Lock(&api.LockRequest{
@ -66,6 +67,20 @@ func lockCommand(cmd *cobra.Command, args []string) {
Print("\n'%s' was locked (%s)", args[0], resp.Lock.Id)
}
// lockPaths relativizes the given filepath such that it is relative to the root
// path of the repository it is contained within, taking into account the
// working directory of the caller.
//
// If the root directory, working directory, or file cannot be
// determined/opened, an error will be returned. If the file in question is
// actually a directory, an error will be returned. Otherwise, the cleaned path
// will be returned.
//
// For example:
// - Working directory: /code/foo/bar/
// - Repository root: /code/foo/
// - File to lock: ./baz
// - Resolved path bar/baz
func lockPath(file string) (string, error) {
repo, err := git.RootDir()
if err != nil {
@ -78,8 +93,17 @@ func lockPath(file string) (string, error) {
}
abs := filepath.Join(wd, file)
path := strings.TrimPrefix(abs, repo)
return strings.TrimPrefix(abs, repo), nil
if stat, err := os.Stat(abs); err != nil {
return "", err
} else {
if stat.IsDir() {
return "", fmt.Errorf("lfs: cannot lock directory: %s", file)
}
return path[1:], nil
}
}
func init() {

@ -31,3 +31,31 @@ begin_test "locking a previously locked file"
grep "lock already created" <(git lfs lock "b.dat" 2>&1)
)
end_test
begin_test "locking a directory"
(
set -e
reponame="locking_directories"
setup_remote_repo "remote_$reponame"
clone_repo "remote_$reponame" "clone_$reponame"
git lfs track "*.dat"
mkdir dir
echo "a" > dir/a.dat
git add dir/a.dat .gitattributes
git commit -m "add dir/a.dat" | tee commit.log
grep "master (root-commit)" commit.log
grep "2 files changed" commit.log
grep "create mode 100644 dir/a.dat" commit.log
grep "create mode 100644 .gitattributes" commit.log
git push origin master 2>&1 | tee push.log
grep "master -> master" push.log
git lfs lock ./dir/ 2>&1 | tee lock.log
grep "cannot lock directory" lock.log
)
end_test