From 1a104903ab933fd6b681286745a1f84c479492bd Mon Sep 17 00:00:00 2001 From: Bruno Meneguele Date: Wed, 5 Jan 2022 17:02:34 -0300 Subject: [PATCH] config: fix worktree git config dir path The config code doesn't follow the worktree git config file when the user calls for a command that depends on that from within a `git worktree` directory. In this cases, the .git path is in fact a file instead of a valid directory, which turns to point to the actual worktree configuration dir. Instead of realying in a hardcoded path (`.git/lab`), make use of the `git rev-parse --git-dir` command already available in the git commands code (internal/git/git.go) and which follows the worktree situation. Signed-off-by: Bruno Meneguele --- internal/config/config.go | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 9cd84c87..38754706 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -339,10 +339,19 @@ func LoadMainConfig() (string, string, string, string, bool) { } // default path of worktree lab.toml file -var ( - WorkTreePath string = ".git/lab" - WorkTreeName string = "lab" -) +var WorktreeConfigName string = "lab" + +// worktreeConfigPath gets the current git config path using the +// `git rev-parse` command, which considers the worktree's gitdir path placed +// into the .git file. +func worktreeConfigPath() string { + gitDir, err := git.Dir() + if err != nil { + log.Fatal(err) + } + + return gitDir + "/lab" +} // LoadConfig loads a config file specified by configpath and configname. // The configname must not have a '.toml' extension. If configpath and/or @@ -352,10 +361,10 @@ func LoadConfig(configpath string, configname string) *viper.Viper { targetConfig.SetConfigType("toml") if configpath == "" { - configpath = WorkTreePath + configpath = worktreeConfigPath() } if configname == "" { - configname = WorkTreeName + configname = WorktreeConfigName } targetConfig.AddConfigPath(configpath) targetConfig.SetConfigName(configname)