From cc62bbcf62929961577ae76e19bda76ccfa765e9 Mon Sep 17 00:00:00 2001 From: Stefanos Mitropoulos Date: Sat, 2 Dec 2023 21:44:36 +0200 Subject: [PATCH] fix: resolve symlinks for startup and git --- pkg/app/entry_point.go | 5 +++++ pkg/commands/git.go | 47 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/pkg/app/entry_point.go b/pkg/app/entry_point.go index fb19f06f6c6d..df40854d890c 100644 --- a/pkg/app/entry_point.go +++ b/pkg/app/entry_point.go @@ -59,6 +59,11 @@ func Start(buildInfo *BuildInfo, integrationTest integrationTypes.IntegrationTes log.Fatal(err) } + absRepoPath, err = filepath.EvalSymlinks(cliArgs.RepoPath) + if err != nil { + log.Fatal(err) + } + if isRepo, err := isDirectoryAGitRepository(absRepoPath); err != nil || !isRepo { log.Fatal(absRepoPath + " is not a valid git repository.") } diff --git a/pkg/commands/git.go b/pkg/commands/git.go index 510661034168..564eb96d0153 100644 --- a/pkg/commands/git.go +++ b/pkg/commands/git.go @@ -64,10 +64,21 @@ func NewGitCommand( gitConfig git_config.IGitConfig, ) (*GitCommand, error) { currentPath, err := os.Getwd() + + if err != nil { + return nil, utils.WrapError(err) + } + + // Check if the current path is a symlink and walk it + absRepoPath, err := filepath.EvalSymlinks(currentPath) if err != nil { return nil, utils.WrapError(err) } + if absRepoPath != currentPath { + currentPath = absRepoPath + } + // converting to forward slashes for the sake of windows (which uses backwards slashes). We want everything // to have forward slashes internally currentPath = filepath.ToSlash(currentPath) @@ -136,7 +147,15 @@ func NewGitCommandAux( // common ones are: cmn, osCommand, dotGitDir, configCommands configCommands := git_commands.NewConfigCommands(cmn, gitConfig, repo) - gitCommon := git_commands.NewGitCommon(cmn, version, cmd, osCommand, repoPaths, repo, configCommands) + gitCommon := git_commands.NewGitCommon( + cmn, + version, + cmd, + osCommand, + repoPaths, + repo, + configCommands, + ) fileLoader := git_commands.NewFileLoader(gitCommon, cmd, configCommands) statusCommands := git_commands.NewStatusCommands(gitCommon) @@ -150,18 +169,34 @@ func NewGitCommandAux( diffCommands := git_commands.NewDiffCommands(gitCommon) fileCommands := git_commands.NewFileCommands(gitCommon) submoduleCommands := git_commands.NewSubmoduleCommands(gitCommon) - workingTreeCommands := git_commands.NewWorkingTreeCommands(gitCommon, submoduleCommands, fileLoader) + workingTreeCommands := git_commands.NewWorkingTreeCommands( + gitCommon, + submoduleCommands, + fileLoader, + ) rebaseCommands := git_commands.NewRebaseCommands(gitCommon, commitCommands, workingTreeCommands) stashCommands := git_commands.NewStashCommands(gitCommon, fileLoader, workingTreeCommands) patchBuilder := patch.NewPatchBuilder(cmn.Log, func(from string, to string, reverse bool, filename string, plain bool) (string, error) { return workingTreeCommands.ShowFileDiff(from, to, reverse, filename, plain) }) - patchCommands := git_commands.NewPatchCommands(gitCommon, rebaseCommands, commitCommands, statusCommands, stashCommands, patchBuilder) + patchCommands := git_commands.NewPatchCommands( + gitCommon, + rebaseCommands, + commitCommands, + statusCommands, + stashCommands, + patchBuilder, + ) bisectCommands := git_commands.NewBisectCommands(gitCommon) worktreeCommands := git_commands.NewWorktreeCommands(gitCommon) - branchLoader := git_commands.NewBranchLoader(cmn, cmd, branchCommands.CurrentBranchInfo, configCommands) + branchLoader := git_commands.NewBranchLoader( + cmn, + cmd, + branchCommands.CurrentBranchInfo, + configCommands, + ) commitFileLoader := git_commands.NewCommitFileLoader(cmn, cmd) commitLoader := git_commands.NewCommitLoader(cmn, cmd, statusCommands.RebaseMode, gitCommon) reflogCommitLoader := git_commands.NewReflogCommitLoader(cmn, cmd) @@ -232,5 +267,7 @@ func findWorktreeRoot(fs afero.Fs, currentPath string) (string, error) { } func VerifyInGitRepo(osCommand *oscommands.OSCommand) error { - return osCommand.Cmd.New(git_commands.NewGitCmd("rev-parse").Arg("--git-dir").ToArgv()).DontLog().Run() + return osCommand.Cmd.New(git_commands.NewGitCmd("rev-parse").Arg("--git-dir").ToArgv()). + DontLog(). + Run() }