1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2025-03-23 21:51:07 +02:00
John Whitley 3d9f1e02e5 Refactor repo_paths.go to use git rev-parse
This changes GetRepoPaths() to pull information from `git rev-parse`
instead of effectively reimplementing git's logic for pathfinding. This
change fixes issues with bare repos, esp. versioned homedir use cases,
by aligning lazygit's path handling to what git itself does.

This change also enables lazygit to run from arbitrary subdirectories of
a repository, including correct handling of symlinks, including "deep"
symlinks into a repo, worktree, a repo's submodules, etc.

Integration tests are now resilient against unintended side effects from
the host's environment variables. Of necessity, $PATH and $TERM are the
only env vars allowed through now.
2024-01-24 08:40:01 +01:00

66 lines
2.1 KiB
Go

package components
import (
"fmt"
"os"
)
const (
// These values will be passed to lazygit
LAZYGIT_ROOT_DIR = "LAZYGIT_ROOT_DIR"
SANDBOX_ENV_VAR = "SANDBOX"
TEST_NAME_ENV_VAR = "TEST_NAME"
WAIT_FOR_DEBUGGER_ENV_VAR = "WAIT_FOR_DEBUGGER"
// These values will be passed to both lazygit and shell commands
GIT_CONFIG_GLOBAL_ENV_VAR = "GIT_CONFIG_GLOBAL"
// We pass PWD because if it's defined, Go will use it as the working directory
// rather than make a syscall to the OS, and that means symlinks won't be resolved,
// which is good to test for.
PWD = "PWD"
// We set $HOME and $GIT_CONFIG_NOGLOBAL during integrationt tests so
// that older versions of git that don't respect $GIT_CONFIG_GLOBAL
// will find the correct global config file for testing
HOME = "HOME"
GIT_CONFIG_NOGLOBAL = "GIT_CONFIG_NOGLOBAL"
// These values will be passed through to lazygit and shell commands, with their
// values inherited from the host environment
PATH = "PATH"
TERM = "TERM"
)
// Tests will inherit these environment variables from the host environment, rather
// than the test runner deciding the values itself.
// All other environment variables present in the host environment will be ignored.
// Having such a minimal list ensures that lazygit behaves the same across different test environments.
var hostEnvironmentAllowlist = [...]string{
PATH,
TERM,
}
// Returns a copy of the environment filtered by
// hostEnvironmentAllowlist
func allowedHostEnvironment() []string {
env := []string{}
for _, envVar := range hostEnvironmentAllowlist {
env = append(env, fmt.Sprintf("%s=%s", envVar, os.Getenv(envVar)))
}
return env
}
func NewTestEnvironment(rootDir string) []string {
env := allowedHostEnvironment()
// Set $HOME to control the global git config location for git
// versions <= 2.31.8
env = append(env, fmt.Sprintf("%s=%s", HOME, testPath(rootDir)))
// $GIT_CONFIG_GLOBAL controls global git config location for git
// versions >= 2.32.0
env = append(env, fmt.Sprintf("%s=%s", GIT_CONFIG_GLOBAL_ENV_VAR, globalGitConfigPath(rootDir)))
return env
}