Skip to content

Commit a6bce24

Browse files
authored
fix core-editor ignored (#414) (#419)
1 parent 7a6dc2b commit a6bce24

File tree

2 files changed

+68
-10
lines changed

2 files changed

+68
-10
lines changed

asyncgit/src/sync/utils.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,29 @@ pub fn stage_addremoved(repo_path: &str, path: &Path) -> Result<()> {
144144
Ok(())
145145
}
146146

147+
/// get string from config
148+
pub fn get_config_string(
149+
repo_path: &str,
150+
key: &str,
151+
) -> Result<Option<String>> {
152+
let repo = repo(repo_path)?;
153+
let cfg = repo.config()?;
154+
155+
// this code doesnt match what the doc says regarding what
156+
// gets returned when but it actually works
157+
let entry_res = cfg.get_entry(key);
158+
159+
let entry = match entry_res {
160+
Ok(ent) => ent,
161+
Err(_) => return Ok(None),
162+
};
163+
164+
if !entry.has_value() {
165+
Ok(None)
166+
} else {
167+
Ok(entry.value().map(|s| s.to_string()))
168+
}
169+
}
147170
/// helper function
148171
pub(crate) fn bytes2string(bytes: &[u8]) -> Result<String> {
149172
Ok(String::from_utf8(bytes.to_vec())?)
@@ -177,7 +200,23 @@ mod tests {
177200
false
178201
);
179202
}
203+
#[test]
204+
fn test_get_config() {
205+
let bad_dir_cfg =
206+
get_config_string("oodly_noodly", "this.doesnt.exist");
207+
assert!(bad_dir_cfg.is_err());
180208

209+
let (_td, repo) = repo_init().unwrap();
210+
let path = repo.path();
211+
let rpath = path.as_os_str().to_str().unwrap();
212+
let bad_cfg = get_config_string(rpath, "this.doesnt.exist");
213+
assert!(bad_cfg.is_ok());
214+
assert!(bad_cfg.unwrap().is_none());
215+
// repo init sets user.name
216+
let good_cfg = get_config_string(rpath, "user.name");
217+
assert!(good_cfg.is_ok());
218+
assert!(good_cfg.unwrap().is_some());
219+
}
181220
#[test]
182221
fn test_staging_one_file() {
183222
let file_path = Path::new("file1.txt");

src/components/externaleditor.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use crate::{
88
ui::{self, style::SharedTheme},
99
};
1010
use anyhow::{anyhow, bail, Result};
11-
use asyncgit::{sync::utils::repo_work_dir, CWD};
11+
use asyncgit::{
12+
sync::utils::get_config_string, sync::utils::repo_work_dir, CWD,
13+
};
1214
use crossterm::{
1315
event::Event,
1416
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
@@ -66,26 +68,43 @@ impl ExternalEditorComponent {
6668

6769
let editor = env::var("GIT_EDITOR")
6870
.ok()
71+
.or_else(|| get_config_string(CWD, "core.editor").ok()?)
6972
.or_else(|| env::var("VISUAL").ok())
7073
.or_else(|| env::var("EDITOR").ok())
7174
.unwrap_or_else(|| String::from("vi"));
7275

7376
// TODO: proper handling arguments containing whitespaces
7477
// This does not do the right thing if the input is `editor --something "with spaces"`
75-
let mut editor = editor.split_whitespace();
7678

77-
let command = editor.next().ok_or_else(|| {
78-
anyhow!("unable to read editor command")
79-
})?;
79+
// deal with "editor name with spaces" p1 p2 p3
80+
// and with "editor_no_spaces" p1 p2 p3
81+
// does not address spaces in pn
82+
let mut echars = editor.chars().peekable();
83+
84+
let command: String = if *echars.peek().ok_or_else(|| {
85+
anyhow!("editor configuration set to empty string")
86+
})? == '\"'
87+
{
88+
echars
89+
.by_ref()
90+
.skip(1)
91+
.take_while(|c| *c != '\"')
92+
.collect()
93+
} else {
94+
echars.by_ref().take_while(|c| *c != ' ').collect()
95+
};
96+
97+
let remainder_str = echars.collect::<String>();
98+
let remainder = remainder_str.split_whitespace();
8099

81-
let mut editor: Vec<&OsStr> =
82-
editor.map(|s| OsStr::new(s)).collect();
100+
let mut args: Vec<&OsStr> =
101+
remainder.map(|s| OsStr::new(s)).collect();
83102

84-
editor.push(path.as_os_str());
103+
args.push(path.as_os_str());
85104

86-
Command::new(command)
105+
Command::new(command.clone())
87106
.current_dir(work_dir)
88-
.args(editor)
107+
.args(args)
89108
.status()
90109
.map_err(|e| anyhow!("\"{}\": {}", command, e))?;
91110

0 commit comments

Comments
 (0)