Skip to content

Commit

Permalink
Add commit insert to insert functionality
Browse files Browse the repository at this point in the history
This change allows a new commit to be added in the default state of pick.
  • Loading branch information
MitMaro committed Apr 8, 2021
1 parent 59c05c4 commit 25fa070
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 13 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
- Support for multiple key bindings per configuration ([#437](https://github.com/MitMaro/git-interactive-rebase-tool/pull/437))
- Open external editor from visual mode ([#442](https://github.com/MitMaro/git-interactive-rebase-tool/pull/442))
- Delete selected lines from the todo list ([#443](https://github.com/MitMaro/git-interactive-rebase-tool/pull/443))
- Insert new exec, label, reset or merge line ([#454](https://github.com/MitMaro/git-interactive-rebase-tool/pull/454))
- Insert new exec, commit, label, reset or merge line ([#454](https://github.com/MitMaro/git-interactive-rebase-tool/pull/454), [#458](https://github.com/MitMaro/git-interactive-rebase-tool/pull/458))
- Support home and end in list view ([#455](https://github.com/MitMaro/git-interactive-rebase-tool/pull/455))

### Fixed
Expand Down
19 changes: 11 additions & 8 deletions src/insert/line_type.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
#[derive(Clone, Debug, PartialEq)]
pub enum LineType {
Cancel,
Pick,
Exec,
Label,
Reset,
Merge,
Cancel,
Reset,
}

impl ToString for LineType {
fn to_string(&self) -> String {
match *self {
Self::Cancel => String::from("<cancel>"),
Self::Pick => String::from("pick"),
Self::Exec => String::from("exec"),
Self::Label => String::from("label"),
Self::Reset => String::from("reset"),
Self::Merge => String::from("merge"),
Self::Cancel => String::from("<cancel>"),
Self::Reset => String::from("reset"),
}
}
}
Expand All @@ -28,11 +30,12 @@ mod tests {
#[rstest(
line_type,
expected,
case::cancel(&LineType::Cancel, "<cancel>"),
case::pick(&LineType::Pick, "pick"),
case::exec(&LineType::Exec, "exec"),
case::exec(&LineType::Label, "label"),
case::exec(&LineType::Reset, "reset"),
case::exec(&LineType::Merge, "merge"),
case::exec(&LineType::Cancel, "<cancel>"),
case::label(&LineType::Label, "label"),
case::merge(&LineType::Merge, "merge"),
case::reset(&LineType::Reset, "reset"),
)]
fn to_string(line_type: &LineType, expected: &str) {
assert_eq!(line_type.to_string(), String::from(expected));
Expand Down
4 changes: 3 additions & 1 deletion src/insert/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl ProcessModule for Insert {
if !content.is_empty() {
let line = match self.line_type {
LineType::Exec => Line::new_exec(content.as_str()),
LineType::Pick => Line::new_pick(content.as_str()),
LineType::Label => Line::new_label(content.as_str()),
LineType::Reset => Line::new_reset(content.as_str()),
LineType::Merge => Line::new_merge(content.as_str()),
Expand All @@ -94,14 +95,15 @@ impl Insert {

let mut action_choices = Choice::new(vec![
(LineType::Exec, 'e', String::from("exec <command>")),
(LineType::Pick, 'p', String::from("pick <hash>")),
(LineType::Label, 'l', String::from("label <label>")),
(LineType::Reset, 'r', String::from("reset <label>")),
(
LineType::Merge,
'm',
String::from("merge [-C <commit> | -c <commit>] <label> [# <oneline>]"),
),
(LineType::Cancel, 'c', String::from("Cancel add line")),
(LineType::Cancel, 'q', String::from("Cancel add line")),
]);
action_choices.set_prompt(vec![ViewLine::from("Select the type of line to insert:")]);
let mut edit_view_data = ViewData::new();
Expand Down
48 changes: 45 additions & 3 deletions src/insert/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ fn render_prompt() {
"",
"{BODY}",
"{Normal}e) exec <command>",
"{Normal}p) pick <hash>",
"{Normal}l) label <label>",
"{Normal}r) reset <label>",
"{Normal}m) merge [-C <commit> | -c <commit>] <label> [# <oneline>]",
"{Normal}c) Cancel add line",
"{Normal}q) Cancel add line",
"",
"{IndicatorColor}Please choose an option."
);
Expand All @@ -44,12 +45,12 @@ fn prompt_cancel() {
process_module_test(
&[],
ViewState::default(),
&[Input::Character('c')],
&[Input::Character('q')],
|mut test_context: TestContext<'_>| {
let mut module = Insert::new();
assert_process_result!(
test_context.handle_input(&mut module),
input = Input::Character('c'),
input = Input::Character('q'),
state = State::List
);
},
Expand Down Expand Up @@ -94,6 +95,47 @@ fn edit_render_exec() {
);
}

#[test]
#[serial_test::serial]
fn edit_render_pick() {
process_module_test(
&[],
ViewState::default(),
&[
Input::Character('p'),
Input::Character('a'),
Input::Character('b'),
Input::Character('c'),
Input::Enter,
],
|mut test_context: TestContext<'_>| {
let mut module = Insert::new();
test_context.handle_n_inputs(&mut module, 4);
let view_data = test_context.build_view_data(&mut module);
assert_rendered_output!(
view_data,
"{TITLE}",
"{LEADING}",
"{IndicatorColor}Enter contents of the new line. Empty content cancels creation of a new line.",
"",
"{BODY}",
"{Normal,Dimmed}pick {Normal}abc{Normal,Underline} ",
"{TRAILING}",
"{IndicatorColor}Enter to finish"
);
assert_process_result!(
test_context.handle_input(&mut module),
input = Input::Enter,
state = State::List
);
assert_eq!(
test_context.rebase_todo_file.get_line(0).unwrap().to_text(),
"pick abc "
);
},
);
}

#[test]
#[serial_test::serial]
fn edit_render_label() {
Expand Down
19 changes: 19 additions & 0 deletions src/todo_file/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ impl Line {
}
}

pub(crate) fn new_pick(hash: &str) -> Self {
Self {
action: Action::Pick,
content: String::from(""),
hash: String::from(hash),
mutated: false,
}
}

pub(crate) fn new_break() -> Self {
Self {
action: Action::Break,
Expand Down Expand Up @@ -261,6 +270,16 @@ mod tests {
assert_eq!(&Line::new(line).unwrap(), expected);
}

#[test]
fn line_new_pick() {
assert_eq!(Line::new_pick("abc123"), Line {
action: Action::Pick,
hash: String::from("abc123"),
content: String::from(""),
mutated: false,
});
}

#[test]
fn line_new_break() {
assert_eq!(Line::new_break(), Line {
Expand Down

0 comments on commit 25fa070

Please sign in to comment.