Skip to content

Commit 60759d7

Browse files
author
Stephan Dilly
committed
open external editor from commit popup
this puts all commit logic into one 'view' and allows editing amend commit messages in the external editor aswell.
1 parent 0a24c2c commit 60759d7

File tree

4 files changed

+31
-29
lines changed

4 files changed

+31
-29
lines changed

src/components/changes.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,6 @@ impl Component for ChangesComponent {
236236
some_selection,
237237
self.focused(),
238238
));
239-
out.push(CommandInfo::new(
240-
commands::COMMIT_OPEN_EDITOR,
241-
!self.is_empty(),
242-
self.focused() || force_all,
243-
));
244239
out.push(
245240
CommandInfo::new(
246241
commands::COMMIT_OPEN,
@@ -271,15 +266,6 @@ impl Component for ChangesComponent {
271266
.push_back(InternalEvent::OpenCommit);
272267
Ok(true)
273268
}
274-
keys::OPEN_COMMIT_EDITOR
275-
if !self.is_working_dir
276-
&& !self.is_empty() =>
277-
{
278-
self.queue
279-
.borrow_mut()
280-
.push_back(InternalEvent::SuspendPolling);
281-
Ok(true)
282-
}
283269
keys::STATUS_STAGE_FILE => {
284270
try_or_popup!(
285271
self,

src/components/commit.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ use super::{
55
use crate::{
66
get_app_config_path, keys,
77
queue::{InternalEvent, NeedsUpdate, Queue},
8-
strings,
9-
strings::{commands, COMMIT_EDITOR_MSG},
8+
strings::{self, commands},
109
ui::style::SharedTheme,
1110
};
1211
use anyhow::{anyhow, Result};
@@ -67,6 +66,12 @@ impl Component for CommitComponent {
6766
self.can_amend(),
6867
true,
6968
));
69+
70+
out.push(CommandInfo::new(
71+
commands::COMMIT_OPEN_EDITOR,
72+
true,
73+
true,
74+
));
7075
}
7176

7277
visibility_blocking(self)
@@ -88,6 +93,12 @@ impl Component for CommitComponent {
8893
self.amend()?;
8994
}
9095

96+
keys::OPEN_COMMIT_EDITOR => {
97+
self.queue
98+
.borrow_mut()
99+
.push_back(InternalEvent::SuspendPolling);
100+
}
101+
91102
_ => (),
92103
};
93104

@@ -137,9 +148,15 @@ impl CommitComponent {
137148
let mut config_path: PathBuf = get_app_config_path()?;
138149
config_path.push(COMMIT_MSG_FILE_NAME);
139150

140-
let mut file = File::create(&config_path)?;
141-
file.write_all(COMMIT_EDITOR_MSG.as_bytes())?;
142-
drop(file);
151+
{
152+
//TODO: use a tmpfile here
153+
let mut file = File::create(&config_path)?;
154+
file.write_fmt(format_args!(
155+
"{}\n",
156+
self.input.get_text()
157+
))?;
158+
file.write_all(strings::COMMIT_EDITOR_MSG.as_bytes())?;
159+
}
143160

144161
let mut editor = env::var("GIT_EDTIOR")
145162
.ok()
@@ -156,9 +173,8 @@ impl CommitComponent {
156173
})?;
157174

158175
io::stdout().execute(LeaveAlternateScreen)?;
159-
160176
defer! {
161-
io::stdout().execute(EnterAlternateScreen).expect("failed to reset terminal");
177+
io::stdout().execute(EnterAlternateScreen).expect("reset terminal");
162178
}
163179

164180
Command::new(command)
@@ -168,6 +184,7 @@ impl CommitComponent {
168184

169185
let mut message = String::new();
170186

187+
//TODO: see above
171188
let mut file = File::open(&config_path)?;
172189
file.read_to_string(&mut message)?;
173190
drop(file);
@@ -184,9 +201,9 @@ impl CommitComponent {
184201
})
185202
.collect();
186203

187-
if !message.chars().all(char::is_whitespace) {
188-
return self.commit_msg(message);
189-
}
204+
let message = message.trim().to_string();
205+
206+
self.input.set_text(message);
190207

191208
Ok(())
192209
}

src/keys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub const EXIT_POPUP: KeyEvent = no_mod(KeyCode::Esc);
3333
pub const CLOSE_MSG: KeyEvent = no_mod(KeyCode::Enter);
3434
pub const OPEN_COMMIT: KeyEvent = no_mod(KeyCode::Char('c'));
3535
pub const OPEN_COMMIT_EDITOR: KeyEvent =
36-
with_mod(KeyCode::Char('C'), KeyModifiers::SHIFT);
36+
with_mod(KeyCode::Char('e'), KeyModifiers::CONTROL);
3737
pub const OPEN_HELP: KeyEvent = no_mod(KeyCode::Char('h'));
3838
pub const MOVE_LEFT: KeyEvent = no_mod(KeyCode::Left);
3939
pub const MOVE_RIGHT: KeyEvent = no_mod(KeyCode::Right);

src/strings.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ pub static COMMIT_TITLE: &str = "Commit";
1515
pub static COMMIT_TITLE_AMEND: &str = "Commit (Amend)";
1616
pub static COMMIT_MSG: &str = "type commit message..";
1717
pub static COMMIT_EDITOR_MSG: &str = r##"
18-
# Enter your commit message
19-
# Lines starting with '#' will be ignored
20-
# Empty commit message will abort the commit"##;
18+
# Edit your commit message
19+
# Lines starting with '#' will be ignored"##;
2120
pub static STASH_POPUP_TITLE: &str = "Stash";
2221
pub static STASH_POPUP_MSG: &str = "type name (optional)";
2322
pub static CONFIRM_TITLE_RESET: &str = "Reset";
@@ -155,7 +154,7 @@ pub mod commands {
155154
);
156155
///
157156
pub static COMMIT_OPEN_EDITOR: CommandText = CommandText::new(
158-
"Commit editor [C]",
157+
"Open editor [^e]",
159158
"open commit editor (available in non-empty stage)",
160159
CMD_GROUP_COMMIT,
161160
);

0 commit comments

Comments
 (0)