locking: reject multiple paths

Currently, when trying to lock or unlock multiple paths, we simply
ignore paths other than the first.  This is surprising to users, so
let's print an error in this case.  In addition, let's have both
commands exit unsuccessfully to indicate failure.

Note that we don't just allow multiple paths because the JSON format
can't handle multiple items.
This commit is contained in:
brian m. carlson 2021-06-17 13:25:25 +00:00
parent 5028013731
commit 9fad6ff488
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
4 changed files with 49 additions and 5 deletions

@ -19,9 +19,8 @@ var (
)
func lockCommand(cmd *cobra.Command, args []string) {
if len(args) == 0 {
Print("Usage: git lfs lock <path>")
return
if len(args) != 1 {
Exit("Usage: git lfs lock <path>")
}
path, err := lockPath(args[0])

@ -29,9 +29,10 @@ var unlockUsage = "Usage: git lfs unlock (--id my-lock-id | <path>)"
func unlockCommand(cmd *cobra.Command, args []string) {
hasPath := len(args) > 0
hasId := len(unlockCmdFlags.Id) > 0
if hasPath == hasId {
if hasPath == hasId || len(args) > 1 {
// If there is both an `--id` AND a `<path>`, or there is
// neither, print the usage and quit.
// neither, or there are multiple paths, print the usage and
// quit.
Exit(unlockUsage)
}

@ -74,6 +74,27 @@ begin_test "lock with bad ref"
)
end_test
begin_test "lock multiple files"
(
set -e
reponame="lock-multiple-files"
setup_remote_repo "$reponame"
clone_repo "$reponame" "$reponame"
git lfs track "*.dat"
echo "a" > a.dat
echo "b" > b.dat
git add .gitattributes a.dat b.dat
git commit -m "add dat files"
git push origin main:other
git lfs lock *.dat >log 2>&1 && exit 1
grep "Usage:" log
)
end_test
begin_test "create lock with server using client cert"
(
set -e

@ -119,6 +119,29 @@ begin_test "unlocking a lock by id with bad ref"
)
end_test
begin_test "unlock multiple files"
(
set -e
reponame="unlock-multiple-files"
setup_remote_repo "$reponame"
clone_repo "$reponame" "$reponame"
git lfs track "*.dat"
echo "a" > a.dat
echo "b" > b.dat
git add .gitattributes a.dat b.dat
git commit -m "add dat files"
git push origin main:other
git lfs lock a.dat
git lfs lock b.dat
git lfs unlock *.dat >log 2>&1 && exit 1
grep "Usage:" log
)
end_test
begin_test "unlocking a file makes it readonly"
(
set -e