commands/unlock: make strings translatable

Move the string for lockRemoteHelp variable into the actual help output.
We cannot translate a global variable because at initialization time the
locale string won't have been loaded and we won't be able to load the
proper string.  Since the variable is now unused, remove it.
This commit is contained in:
brian m. carlson 2021-12-13 18:49:04 +00:00
parent d458827544
commit da2a92c6dd
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
3 changed files with 14 additions and 17 deletions

@ -15,8 +15,7 @@ import (
) )
var ( var (
lockRemote string lockRemote string
lockRemoteHelp = "specify which remote to use when interacting with locks"
) )
func lockCommand(cmd *cobra.Command, args []string) { func lockCommand(cmd *cobra.Command, args []string) {

@ -2,12 +2,12 @@ package commands
import ( import (
"encoding/json" "encoding/json"
"fmt"
"os" "os"
"github.com/git-lfs/git-lfs/v3/errors" "github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/git" "github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/locking" "github.com/git-lfs/git-lfs/v3/locking"
"github.com/git-lfs/git-lfs/v3/tr"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -25,8 +25,6 @@ type unlockFlags struct {
Force bool Force bool
} }
var unlockUsage = "Usage: git lfs unlock (--id my-lock-id | <path>)"
type unlockResponse struct { type unlockResponse struct {
Id string `json:"id,omitempty"` Id string `json:"id,omitempty"`
Path string `json:"path,omitempty"` Path string `json:"path,omitempty"`
@ -53,7 +51,7 @@ func unlockCommand(cmd *cobra.Command, args []string) {
if hasPath == hasId { if hasPath == hasId {
// If there is both an `--id` AND a `<path>`, or there is // If there is both an `--id` AND a `<path>`, or there is
// neither, print the usage and quit. // neither, print the usage and quit.
Exit(unlockUsage) Exit(tr.Tr.Get("Exactly one of --id or a set of paths must be provided"))
} }
if len(lockRemote) > 0 { if len(lockRemote) > 0 {
@ -72,7 +70,7 @@ func unlockCommand(cmd *cobra.Command, args []string) {
path, err := lockPath(pathspec) path, err := lockPath(pathspec)
if err != nil { if err != nil {
if !unlockCmdFlags.Force { if !unlockCmdFlags.Force {
locks = handleUnlockError(locks, "", path, fmt.Errorf("Unable to determine path: %v", err.Error())) locks = handleUnlockError(locks, "", path, errors.New(tr.Tr.Get("Unable to determine path: %v", err.Error())))
success = false success = false
continue continue
} }
@ -107,13 +105,13 @@ func unlockCommand(cmd *cobra.Command, args []string) {
err := lockClient.UnlockFileById(unlockCmdFlags.Id, unlockCmdFlags.Force) err := lockClient.UnlockFileById(unlockCmdFlags.Id, unlockCmdFlags.Force)
if err != nil { if err != nil {
locks = handleUnlockError(locks, unlockCmdFlags.Id, "", fmt.Errorf("Unable to unlock %v: %v", unlockCmdFlags.Id, errors.Cause(err))) locks = handleUnlockError(locks, unlockCmdFlags.Id, "", errors.New(tr.Tr.Get("Unable to unlock %v: %v", unlockCmdFlags.Id, errors.Cause(err))))
success = false success = false
} else if !locksCmdFlags.JSON { } else if !locksCmdFlags.JSON {
Print("Unlocked Lock %s", unlockCmdFlags.Id) Print(tr.Tr.Get("Unlocked Lock %s", unlockCmdFlags.Id))
} }
} else { } else {
Error(unlockUsage) Exit(tr.Tr.Get("Exactly one of --id or a set of paths must be provided"))
} }
if locksCmdFlags.JSON { if locksCmdFlags.JSON {
@ -145,9 +143,9 @@ func unlockAbortIfFileModified(path string) error {
if modified { if modified {
if unlockCmdFlags.Force { if unlockCmdFlags.Force {
// Only a warning // Only a warning
Error("Warning: unlocking with uncommitted changes because --force") Error(tr.Tr.Get("warning: unlocking with uncommitted changes because --force"))
} else { } else {
return fmt.Errorf("Cannot unlock file with uncommitted changes") return errors.New(tr.Tr.Get("Cannot unlock file with uncommitted changes"))
} }
} }
@ -174,7 +172,7 @@ func unlockAbortIfFileModifiedById(id string, lockClient *locking.Client) error
func init() { func init() {
RegisterCommand("unlock", unlockCommand, func(cmd *cobra.Command) { RegisterCommand("unlock", unlockCommand, func(cmd *cobra.Command) {
cmd.Flags().StringVarP(&lockRemote, "remote", "r", "", lockRemoteHelp) cmd.Flags().StringVarP(&lockRemote, "remote", "r", "", "specify which remote to use when interacting with locks")
cmd.Flags().StringVarP(&unlockCmdFlags.Id, "id", "i", "", "unlock a lock by its ID") cmd.Flags().StringVarP(&unlockCmdFlags.Id, "id", "i", "", "unlock a lock by its ID")
cmd.Flags().BoolVarP(&unlockCmdFlags.Force, "force", "f", false, "forcibly break another user's lock(s)") cmd.Flags().BoolVarP(&unlockCmdFlags.Force, "force", "f", false, "forcibly break another user's lock(s)")
cmd.Flags().BoolVarP(&locksCmdFlags.JSON, "json", "", false, "print output in json") cmd.Flags().BoolVarP(&locksCmdFlags.JSON, "json", "", false, "print output in json")

@ -137,7 +137,7 @@ begin_test "unlock multiple files"
git lfs lock a.dat git lfs lock a.dat
git lfs lock b.dat git lfs lock b.dat
git lfs unlock *.dat >log 2>&1 git lfs unlock *.dat >log 2>&1
grep "Usage:" log && exit 1 grep "Exactly one of --id or a set of paths must be provided" log && exit 1
true true
) )
end_test end_test
@ -321,7 +321,7 @@ begin_test "unlocking a lock without sufficient info"
assert_server_lock "$reponame" "$id" assert_server_lock "$reponame" "$id"
git lfs unlock 2>&1 | tee unlock.log git lfs unlock 2>&1 | tee unlock.log
grep "Usage: git lfs unlock" unlock.log grep "Exactly one of --id or a set of paths must be provided" unlock.log
assert_server_lock "$reponame" "$id" assert_server_lock "$reponame" "$id"
) )
end_test end_test
@ -372,7 +372,7 @@ begin_test "unlocking a lock with ambiguous arguments"
exit 1 exit 1
fi fi
grep "Usage:" unlock.log grep "Exactly one of --id or a set of paths must be provided" unlock.log
assert_server_lock "$reponame" "$id" assert_server_lock "$reponame" "$id"
) )
end_test end_test
@ -393,7 +393,7 @@ begin_test "unlocking a lock while uncommitted with --force"
# should allow with --force # should allow with --force
git lfs unlock --force "modforce.dat" 2>&1 | tee unlock.log git lfs unlock --force "modforce.dat" 2>&1 | tee unlock.log
grep "Warning: unlocking with uncommitted changes" unlock.log grep "warning: unlocking with uncommitted changes" unlock.log
refute_server_lock "$reponame" "$id" refute_server_lock "$reponame" "$id"
) )
end_test end_test