config: don't look for English text in error message

When we run Git to find the .git directory, we look for the text "not a
git repository" to check if we got something that isn't a Git
repository, and if so, we suppress the message.

However, this is error prone.  We've already seen changes in this text
from different versions of Git, and obviously if the user is invoking
Git LFS with a different locale (say, Spanish or French), the error
message will not contain this text, leading to an ugly error message
when running git lfs env.

Let's instead check for the expected exit status and assume that we're
not in a Git repository if that's the case.  We change the error we
produce to wrap the exec.ExitStatus error using the Go 1.13 %w parameter
so that it can be read using the Go 1.13 error chain functions.
This commit is contained in:
brian m. carlson 2021-10-29 17:21:46 +00:00
parent 44b8b6aba6
commit 0a731aa225
No known key found for this signature in database
GPG Key ID: 2D0C9BC12F82B3A1
3 changed files with 51 additions and 3 deletions

@ -13,6 +13,7 @@ import (
"time"
"unicode"
"github.com/git-lfs/git-lfs/v3/errors"
"github.com/git-lfs/git-lfs/v3/fs"
"github.com/git-lfs/git-lfs/v3/git"
"github.com/git-lfs/git-lfs/v3/tools"
@ -375,8 +376,7 @@ func (c *Configuration) loadGitDirs() {
if err != nil {
errMsg := err.Error()
tracerx.Printf("Error running 'git rev-parse': %s", errMsg)
if !strings.Contains(strings.ToLower(errMsg),
"not a git repository") {
if errors.ExitStatus(err) != 128 {
fmt.Fprintf(os.Stderr, "Error: %s\n", errMsg)
}
c.gitDir = &gitdir

@ -780,7 +780,7 @@ func GitDir() (string, error) {
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("failed to call git rev-parse --git-dir: %v %v: %v", err, string(out), buf.String())
return "", fmt.Errorf("failed to call git rev-parse --git-dir: %w %v: %v", err, string(out), buf.String())
}
path := strings.TrimSpace(string(out))
return tools.CanonicalizePath(path, false)

@ -1041,6 +1041,54 @@ UploadTransfers=basic,lfs-standalone-file,ssh
)
end_test
begin_test "env outside a repository"
(
set -e
unset_vars
# This may or may not work, depending on the system, but it should at least
# potentially cause Git to print non-English messages.
export LC_ALL=fr_FR.UTF-8
localmedia="$(native_path "lfs/objects")"
lfsstorage=lfs
tempdir="$(native_path "lfs/tmp")"
envVars=$(printf "%s" "$(env | grep "^GIT_")")
expected=$(printf '%s
%s
LocalWorkingDir=
LocalGitDir=
LocalGitStorageDir=
LocalMediaDir=%s
LocalReferenceDirs=
TempDir=%s
ConcurrentTransfers=8
TusTransfers=false
BasicTransfersOnly=false
SkipDownloadErrors=false
FetchRecentAlways=false
FetchRecentRefsDays=7
FetchRecentCommitsDays=0
FetchRecentRefsIncludeRemotes=true
PruneOffsetDays=3
PruneVerifyRemoteAlways=false
PruneRemoteName=origin
LfsStorageDir=%s
AccessDownload=none
AccessUpload=none
DownloadTransfers=basic,lfs-standalone-file,ssh
UploadTransfers=basic,lfs-standalone-file,ssh
%s
%s
' "$(git lfs version)" "$(git version)" "$localmedia" "$tempdir" "$lfsstorage" "$envVars" "$envInitConfig")
# We redirect the standard error here because we should not get any error
# messages, and if we do, we want to fail.
actual=$(git lfs env 2>&1 | grep -v "^GIT_EXEC_PATH=")
contains_same_elements "$expected" "$actual"
)
end_test
begin_test "env with duplicate endpoints"
(
set -e