Skip to content

Commit 4f731f6

Browse files
author
Stephan Dilly
committed
fix opening relative paths in external edtiro (closes #184)
1 parent 49d99c5 commit 4f731f6

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Fixed
1010
- removed unmaintained dependency `spin` ([#172](https://github.com/extrawurst/gitui/issues/172))
11+
- fix opening relative paths in external editor ([#184](https://github.com/extrawurst/gitui/issues/184))
1112

1213
## [0.8.1] - 2020-07-07
1314

asyncgit/src/sync/utils.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn is_bare_repo(repo_path: &str) -> Result<bool> {
2828
}
2929

3030
///
31-
pub fn repo(repo_path: &str) -> Result<Repository> {
31+
pub(crate) fn repo(repo_path: &str) -> Result<Repository> {
3232
let repo = Repository::open_ext(
3333
repo_path,
3434
RepositoryOpenFlags::empty(),
@@ -43,10 +43,20 @@ pub fn repo(repo_path: &str) -> Result<Repository> {
4343
}
4444

4545
///
46-
pub fn work_dir(repo: &Repository) -> &Path {
46+
pub(crate) fn work_dir(repo: &Repository) -> &Path {
4747
repo.workdir().expect("unable to query workdir")
4848
}
4949

50+
///
51+
pub fn repo_work_dir(repo_path: &str) -> Result<String> {
52+
let repo = repo(repo_path)?;
53+
if let Some(workdir) = work_dir(&repo).to_str() {
54+
Ok(workdir.to_string())
55+
} else {
56+
Err(Error::Generic("invalid workdir".to_string()))
57+
}
58+
}
59+
5060
///
5161
pub fn get_head(repo_path: &str) -> Result<CommitId> {
5262
let repo = repo(repo_path)?;

src/components/externaleditor.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
ui::{self, style::SharedTheme},
88
};
99
use anyhow::{anyhow, Result};
10+
use asyncgit::{sync::utils::repo_work_dir, CWD};
1011
use crossterm::{
1112
event::Event,
1213
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
@@ -38,6 +39,14 @@ impl ExternalEditorComponent {
3839

3940
/// opens file at given `path` in an available editor
4041
pub fn open_file_in_editor(path: &Path) -> Result<()> {
42+
let work_dir = repo_work_dir(CWD)?;
43+
44+
let path = if path.is_relative() {
45+
Path::new(&work_dir).join(path)
46+
} else {
47+
path.into()
48+
};
49+
4150
if !path.exists() {
4251
return Err(anyhow!("file not found: {:?}", path));
4352
}
@@ -52,6 +61,11 @@ impl ExternalEditorComponent {
5261
.or_else(|| env::var("VISUAL").ok())
5362
.or_else(|| env::var("EDITOR").ok())
5463
.unwrap_or_else(|| String::from("vi"));
64+
65+
//TODO: check the path.to_str result and return err on None because
66+
//otherwise this will pretty likely fail in the command stage otherwise
67+
//and https://github.com/extrawurst/gitui/issues/184 showed how weird
68+
//'vi' handles opening not existing files
5569
editor.push_str(&format!(" {}", path.to_string_lossy()));
5670

5771
let mut editor = editor.split_whitespace();
@@ -61,6 +75,7 @@ impl ExternalEditorComponent {
6175
})?;
6276

6377
Command::new(command)
78+
.current_dir(work_dir)
6479
.args(editor)
6580
.status()
6681
.map_err(|e| anyhow!("\"{}\": {}", command, e))?;

0 commit comments

Comments
 (0)