Skip to content

Commit

Permalink
fix opening relative paths in external edtiro (closes #184)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed Jul 9, 2020
1 parent 49d99c5 commit 4f731f6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 12 additions & 2 deletions asyncgit/src/sync/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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)?;
Expand Down
15 changes: 15 additions & 0 deletions src/components/externaleditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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));
}
Expand All @@ -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();
Expand All @@ -61,6 +75,7 @@ impl ExternalEditorComponent {
})?;

Command::new(command)
.current_dir(work_dir)
.args(editor)
.status()
.map_err(|e| anyhow!("\"{}\": {}", command, e))?;
Expand Down

0 comments on commit 4f731f6

Please sign in to comment.