fix opening relative paths in external edtiro (closes #184)

This commit is contained in:
Stephan Dilly 2020-07-09 17:47:38 +02:00
parent 49d99c502c
commit 4f731f6acc
3 changed files with 28 additions and 2 deletions

View file

@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- removed unmaintained dependency `spin` ([#172](https://github.com/extrawurst/gitui/issues/172))
- fix opening relative paths in external editor ([#184](https://github.com/extrawurst/gitui/issues/184))
## [0.8.1] - 2020-07-07

View file

@ -28,7 +28,7 @@ pub fn is_bare_repo(repo_path: &str) -> Result<bool> {
}
///
pub fn repo(repo_path: &str) -> Result<Repository> {
pub(crate) fn repo(repo_path: &str) -> Result<Repository> {
let repo = Repository::open_ext(
repo_path,
RepositoryOpenFlags::empty(),
@ -43,10 +43,20 @@ pub fn repo(repo_path: &str) -> Result<Repository> {
}
///
pub fn work_dir(repo: &Repository) -> &Path {
pub(crate) fn work_dir(repo: &Repository) -> &Path {
repo.workdir().expect("unable to query workdir")
}
///
pub fn repo_work_dir(repo_path: &str) -> Result<String> {
let repo = repo(repo_path)?;
if let Some(workdir) = work_dir(&repo).to_str() {
Ok(workdir.to_string())
} else {
Err(Error::Generic("invalid workdir".to_string()))
}
}
///
pub fn get_head(repo_path: &str) -> Result<CommitId> {
let repo = repo(repo_path)?;

View file

@ -7,6 +7,7 @@ use crate::{
ui::{self, style::SharedTheme},
};
use anyhow::{anyhow, Result};
use asyncgit::{sync::utils::repo_work_dir, CWD};
use crossterm::{
event::Event,
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
@ -38,6 +39,14 @@ impl ExternalEditorComponent {
/// opens file at given `path` in an available editor
pub fn open_file_in_editor(path: &Path) -> Result<()> {
let work_dir = repo_work_dir(CWD)?;
let path = if path.is_relative() {
Path::new(&work_dir).join(path)
} else {
path.into()
};
if !path.exists() {
return Err(anyhow!("file not found: {:?}", path));
}
@ -52,6 +61,11 @@ impl ExternalEditorComponent {
.or_else(|| env::var("VISUAL").ok())
.or_else(|| env::var("EDITOR").ok())
.unwrap_or_else(|| String::from("vi"));
//TODO: check the path.to_str result and return err on None because
//otherwise this will pretty likely fail in the command stage otherwise
//and https://github.com/extrawurst/gitui/issues/184 showed how weird
//'vi' handles opening not existing files
editor.push_str(&format!(" {}", path.to_string_lossy()));
let mut editor = editor.split_whitespace();
@ -61,6 +75,7 @@ impl ExternalEditorComponent {
})?;
Command::new(command)
.current_dir(work_dir)
.args(editor)
.status()
.map_err(|e| anyhow!("\"{}\": {}", command, e))?;