diff options
author | Richard Burke <rich.g.burke@gmail.com> | 2019-01-30 22:05:05 +0000 |
---|---|---|
committer | Richard Burke <rich.g.burke@gmail.com> | 2019-01-30 22:05:05 +0000 |
commit | 0758c64604a8a0803dd23a1f2b0b70db8804a32d (patch) | |
tree | 5fc28cdbc6b6ba0da5322b93982e85677b28b90b | |
parent | eb7d8330ba27ff773a0ec6d0bf761ae08d5c9e33 (diff) |
Set shell command working directory to be the root of the repository
-rw-r--r-- | cmd/grv/git_command_repo_controller.go | 2 | ||||
-rw-r--r-- | cmd/grv/grv.go | 4 | ||||
-rw-r--r-- | cmd/grv/repo_data.go | 17 | ||||
-rw-r--r-- | cmd/grv/repo_data_loader.go | 13 |
4 files changed, 23 insertions, 13 deletions
diff --git a/cmd/grv/git_command_repo_controller.go b/cmd/grv/git_command_repo_controller.go index 5057cdf..895b98e 100644 --- a/cmd/grv/git_command_repo_controller.go +++ b/cmd/grv/git_command_repo_controller.go @@ -290,7 +290,7 @@ func (controller *GitCommandRepoController) runGitCommand(args ...string) (err e log.Debugf("Running command: %v %v", gitBinary, strings.Join(args, " ")) cmd := exec.Command(gitBinary, args...) - cmd.Env = controller.repoData.GenerateGitCommandEnvironment() + cmd.Env, cmd.Dir = controller.repoData.GenerateGitCommandEnvironment() if err = cmd.Run(); err != nil { err = fmt.Errorf("Git command failed: %v", err) diff --git a/cmd/grv/grv.go b/cmd/grv/grv.go index 393e45b..db64fc5 100644 --- a/cmd/grv/grv.go +++ b/cmd/grv/grv.go @@ -539,7 +539,7 @@ func (grv *GRV) runCommand(action Action) (err error) { cmd.Stderr = arg.stderr } - cmd.Env = grv.repoData.GenerateGitCommandEnvironment() + cmd.Env, cmd.Dir = grv.repoData.GenerateGitCommandEnvironment() if arg.interactive { grv.ui.Suspend() @@ -649,7 +649,7 @@ func (grv *GRV) runFileSystemMonitorLoop(waitGroup *sync.WaitGroup, exitCh <-cha channels := grv.channels.Channels() eventCh := make(chan fs.EventInfo, 1) repoGitDir := grv.repoData.Path() - repoFilePath := strings.TrimSuffix(repoGitDir, GitRepositoryDirectoryName+"/") + repoFilePath := grv.repoData.RepositoryRootPath() watchDir := repoFilePath + "..." if err := fs.Watch(watchDir, eventCh, fs.All); err != nil { diff --git a/cmd/grv/repo_data.go b/cmd/grv/repo_data.go index 7a96a8f..4a5905f 100644 --- a/cmd/grv/repo_data.go +++ b/cmd/grv/repo_data.go @@ -10,9 +10,7 @@ import ( ) const ( - // GitRepositoryDirectoryName is the name of the git directory in a git repository - GitRepositoryDirectoryName = ".git" - updatedRefChannelSize = 256 + updatedRefChannelSize = 256 ) // OnRefsLoaded is called when all refs have been loaded and processed @@ -55,9 +53,10 @@ type RefStateListener interface { type RepoData interface { EventListener Path() string + RepositoryRootPath() string Workdir() string UserEditor() (string, error) - GenerateGitCommandEnvironment() []string + GenerateGitCommandEnvironment() (env []string, rootDir string) Reload(ReloadResult) LoadHead() error LoadRefs(OnRefsLoaded) @@ -1065,11 +1064,17 @@ func (repoData *RepositoryData) Initialise(repoSupplier RepoSupplier) (err error return repoData.LoadHead() } -// Path returns the file patch location of the repository +// Path returns the file path location of the repository func (repoData *RepositoryData) Path() string { return repoData.repoDataLoader.Path() } +// RepositoryRootPath returns the root working directory of the repository +func (repoData *RepositoryData) RepositoryRootPath() string { + _, rootDir := repoData.GenerateGitCommandEnvironment() + return rootDir +} + // Workdir returns working directory file path for the repository func (repoData *RepositoryData) Workdir() string { return repoData.repoDataLoader.Workdir() @@ -1561,6 +1566,6 @@ func (repoData *RepositoryData) handleViewRemovedEvent(event Event) { // GenerateGitCommandEnvironment populates git environment variables for // the current repository -func (repoData *RepositoryData) GenerateGitCommandEnvironment() []string { +func (repoData *RepositoryData) GenerateGitCommandEnvironment() (env []string, rootDir string) { return repoData.repoDataLoader.GenerateGitCommandEnvironment() } diff --git a/cmd/grv/repo_data_loader.go b/cmd/grv/repo_data_loader.go index b901541..37cb00f 100644 --- a/cmd/grv/repo_data_loader.go +++ b/cmd/grv/repo_data_loader.go @@ -28,6 +28,8 @@ const ( rdlCommitLimitDateFormat = "2006-01-02" rdlCommitLimitDateTimeFormat = "2006-01-02 15:04:05" rdlCommitLimitDateTimeZoneFormat = "2006-01-02 15:04:05-0700" + // GitRepositoryDirectoryName is the name of the git directory in a git repository + GitRepositoryDirectoryName = ".git" ) var diffErrorRegex = regexp.MustCompile(`Invalid (regexp|collation character)`) @@ -1435,7 +1437,7 @@ func (repoDataLoader *RepoDataLoader) runGitCLIDiff(gitCommand []string, diffTyp } cmd := exec.Command(repoDataLoader.gitBinary(), gitCommand...) - cmd.Env = repoDataLoader.GenerateGitCommandEnvironment() + cmd.Env, cmd.Dir = repoDataLoader.GenerateGitCommandEnvironment() var stdout, stderr bytes.Buffer cmd.Stdout = &stdout @@ -1534,8 +1536,8 @@ func (repoDataLoader *RepoDataLoader) UserEditor() (editor string, err error) { // GenerateGitCommandEnvironment populates git environment variables for // the current repository -func (repoDataLoader *RepoDataLoader) GenerateGitCommandEnvironment() []string { - env := os.Environ() +func (repoDataLoader *RepoDataLoader) GenerateGitCommandEnvironment() (env []string, rootDir string) { + env = os.Environ() gitDir := repoDataLoader.Path() if gitDir != "" { @@ -1545,9 +1547,12 @@ func (repoDataLoader *RepoDataLoader) GenerateGitCommandEnvironment() []string { workdir := repoDataLoader.Workdir() if workdir != "" { env = append(env, fmt.Sprintf("GIT_WORK_TREE=%v", workdir)) + rootDir = workdir + } else { + rootDir = strings.TrimSuffix(gitDir, GitRepositoryDirectoryName) } - return env + return } // RepositoryState returns the current repository state |