Fix lfs unlock --force on a missing file

This commit is contained in:
risk danger olson 2017-02-09 14:03:24 -07:00
parent 5a55c028a8
commit b02f5701bd
3 changed files with 33 additions and 4 deletions

@ -72,15 +72,18 @@ func lockPath(file string) (string, error) {
abs := filepath.Join(wd, file)
path := strings.TrimPrefix(abs, repo)
if len(path) > 1 {
path = path[1:]
}
if stat, err := os.Stat(abs); err != nil {
return "", err
return path, err
} else {
if stat.IsDir() {
return "", fmt.Errorf("lfs: cannot lock directory: %s", file)
return path, fmt.Errorf("lfs: cannot lock directory: %s", file)
}
return path[1:], nil
return path, nil
}
}

@ -29,7 +29,7 @@ func unlockCommand(cmd *cobra.Command, args []string) {
if len(args) != 0 {
path, err := lockPath(args[0])
if err != nil {
if err != nil && !unlockCmdFlags.Force {
Exit("Unable to determine path: %v", err.Error())
}

@ -19,6 +19,32 @@ begin_test "unlocking a lock by path"
)
end_test
begin_test "force unlocking lock with missing file"
(
set -e
reponame="force-unlock-missing-file"
setup_remote_repo_with_file "$reponame" "a.dat"
GITLFSLOCKSENABLED=1 git lfs lock "a.dat" | tee lock.log
id=$(grep -oh "\((.*)\)" lock.log | tr -d "()")
assert_server_lock "$reponame" "$id"
git rm a.dat
git commit -m "a.dat"
rm *.log *.json # ensure clean git status
git status
GITLFSLOCKSENABLED=1 git lfs unlock "a.dat" 2>&1 | tee unlock.log
grep "Unable to determine path" unlock.log
assert_server_lock "$reponame" "$id"
rm unlock.log
GITLFSLOCKSENABLED=1 git lfs unlock --force "a.dat" 2>&1 | tee unlock.log
refute_server_lock "$reponame" "$id"
)
end_test
begin_test "unlocking a lock (--json)"
(
set -e