From b02f5701bd5a07bf2b261cc68d9f9ae2bbaea8f3 Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Thu, 9 Feb 2017 14:03:24 -0700 Subject: [PATCH] Fix `lfs unlock --force` on a missing file --- commands/command_lock.go | 9 ++++++--- commands/command_unlock.go | 2 +- test/test-unlock.sh | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/commands/command_lock.go b/commands/command_lock.go index da4ca5dd..2188cc37 100644 --- a/commands/command_lock.go +++ b/commands/command_lock.go @@ -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 } } diff --git a/commands/command_unlock.go b/commands/command_unlock.go index e4dd8fa5..03756c47 100644 --- a/commands/command_unlock.go +++ b/commands/command_unlock.go @@ -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()) } diff --git a/test/test-unlock.sh b/test/test-unlock.sh index 0fb422f1..f26cd3fc 100755 --- a/test/test-unlock.sh +++ b/test/test-unlock.sh @@ -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