From 6fd6fd0975740c200e40ac08c5618bbd12630f2d Mon Sep 17 00:00:00 2001 From: shonenada Date: Thu, 15 Dec 2022 12:46:02 +0800 Subject: [PATCH 1/3] feat: Add support for editing commit message This commit adds support for editing the commit message before committing. The user is asked if they want to edit the commit message. If they answer `edit`, the commit message is opened in the editor specified by the EDITOR environment variable. If the user answers `no`, the commit is aborted. If the user answers `yes`, the commit is performed with the edited commit message. If the user answers anything else, the commit is performed with the original commit message. --- src/main.rs | 58 ++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index b067689..405badb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ use clap_verbosity_flag::{InfoLevel, Verbosity}; use log::{error, info}; use question::{Answer, Question}; use std::{ + fs, + fs::File, io::Write, process::{Command, Stdio}, str, @@ -142,7 +144,7 @@ async fn main() -> Result<(), ()> { sp.unwrap().stop_with_message("Finished Analyzing!".into()); } - let commit_msg = completion.choices[0].text.to_owned(); + let mut commit_msg = completion.choices[0].text.to_owned(); if cli.dry_run { info!("{}", commit_msg); @@ -154,17 +156,59 @@ async fn main() -> Result<(), ()> { ); if !cli.force { - let answer = Question::new("Do you want to continue? (Y/n)") - .yes_no() + let answer = Question::new("Do you want to continue? (Y)es / (E)dit / (N)o") + .accept("Yes") + .accept("yes") + .accept("Y") + .accept("y") + .accept("Edit") + .accept("edit") + .accept("E") + .accept("e") + .accept("No") + .accept("no") + .accept("N") + .accept("n") + .default(Answer::RESPONSE(String::from("yes"))) .until_acceptable() - .default(Answer::YES) .ask() .expect("Couldn't ask question."); - if answer == Answer::NO { - error!("Commit aborted by user."); - std::process::exit(1); + match answer { + Answer::RESPONSE(input) => { + let input_lower = input.to_lowercase(); + match input_lower.as_str() { + "no" | "n" => { + error!("Commit aborted by user."); + std::process::exit(1); + } + "edit" | "e" => { + let mut commit_msg_file = File::create(".auto_commit_msg") + .expect("Couldn't create temp file"); + commit_msg_file + .write_all(commit_msg.as_bytes()) + .expect("Couldn't write commit msg into temp file"); + + Command::new("/usr/bin/sh") + .arg("-c") + .arg("vim .auto_commit_msg") + .spawn() + .expect("Error: Failed to run editor") + .wait() + .expect("Error: Editor returned a non-zero status"); + + commit_msg = fs::read_to_string(".auto_commit_msg") + .expect("Cloudn't read commit message from file"); + fs::remove_file(".auto_commit_msg") + .expect("Failed to delete temp file"); + info!("Using new commit message:\n{}", commit_msg); + } + _ => {} + } + } + _ => {} } + info!("Committing Message..."); } } From b081b9080575524a0bf8d8f8acce0c285f00c564 Mon Sep 17 00:00:00 2001 From: shonenada Date: Thu, 15 Dec 2022 13:27:48 +0800 Subject: [PATCH 2/3] Add support for EDITOR env var This commit adds support for the EDITOR env var. If the EDITOR env var is set, it will be used as the editor to edit the commit message. If it is not set, vi will be used. --- src/main.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 405badb..0d740f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -183,6 +183,10 @@ async fn main() -> Result<(), ()> { std::process::exit(1); } "edit" | "e" => { + let editor = match std::env::var("EDITOR") { + Ok(val) => val, + Err(_) => "vi".to_string(), + }; let mut commit_msg_file = File::create(".auto_commit_msg") .expect("Couldn't create temp file"); commit_msg_file @@ -191,7 +195,7 @@ async fn main() -> Result<(), ()> { Command::new("/usr/bin/sh") .arg("-c") - .arg("vim .auto_commit_msg") + .arg(format!("{} .auto_commit_msg", editor)) .spawn() .expect("Error: Failed to run editor") .wait() From d8e951a20e20762536a1b7ba98004b58c96fb0d2 Mon Sep 17 00:00:00 2001 From: shonenada Date: Thu, 15 Dec 2022 13:35:04 +0800 Subject: [PATCH 3/3] Add support for editing commit messages This commit adds support for editing commit messages. The user can now edit the commit message before it is committed. This is useful for when the user wants to add more information to the commit message. Add a new feature This is a new feature that does something. It is a good feature. --- src/main.rs | 75 +++++++++++++++++++++++++---------------------------- 1 file changed, 36 insertions(+), 39 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0d740f8..be5f630 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,61 +156,58 @@ async fn main() -> Result<(), ()> { ); if !cli.force { - let answer = Question::new("Do you want to continue? (Y)es / (E)dit / (N)o") - .accept("Yes") + let answer = Question::new("Do you want to continue? [Y]es [e]dit [n]o") .accept("yes") - .accept("Y") .accept("y") - .accept("Edit") .accept("edit") - .accept("E") .accept("e") - .accept("No") .accept("no") - .accept("N") .accept("n") .default(Answer::RESPONSE(String::from("yes"))) .until_acceptable() .ask() .expect("Couldn't ask question."); - match answer { - Answer::RESPONSE(input) => { - let input_lower = input.to_lowercase(); - match input_lower.as_str() { - "no" | "n" => { + if let Answer::RESPONSE(input) = answer { + let input_lower = input.to_lowercase(); + match input_lower.as_str() { + "no" | "n" => { + error!("Commit aborted by user."); + std::process::exit(1); + } + + "edit" | "e" => { + let editor = match std::env::var("EDITOR") { + Ok(val) => val, + Err(_) => "vi".to_string(), + }; + let mut commit_msg_file = + File::create(".auto_commit_msg").expect("Couldn't create temp file"); + commit_msg_file + .write_all(commit_msg.as_bytes()) + .expect("Couldn't write commit msg into temp file"); + + Command::new("/usr/bin/sh") + .arg("-c") + .arg(format!("{} .auto_commit_msg", editor)) + .spawn() + .expect("Error: Failed to run editor") + .wait() + .expect("Error: Editor returned a non-zero status"); + + commit_msg = fs::read_to_string(".auto_commit_msg") + .expect("Cloudn't read commit message from file"); + fs::remove_file(".auto_commit_msg").expect("Failed to delete temp file"); + + if commit_msg.len() == 0 { error!("Commit aborted by user."); std::process::exit(1); } - "edit" | "e" => { - let editor = match std::env::var("EDITOR") { - Ok(val) => val, - Err(_) => "vi".to_string(), - }; - let mut commit_msg_file = File::create(".auto_commit_msg") - .expect("Couldn't create temp file"); - commit_msg_file - .write_all(commit_msg.as_bytes()) - .expect("Couldn't write commit msg into temp file"); - - Command::new("/usr/bin/sh") - .arg("-c") - .arg(format!("{} .auto_commit_msg", editor)) - .spawn() - .expect("Error: Failed to run editor") - .wait() - .expect("Error: Editor returned a non-zero status"); - - commit_msg = fs::read_to_string(".auto_commit_msg") - .expect("Cloudn't read commit message from file"); - fs::remove_file(".auto_commit_msg") - .expect("Failed to delete temp file"); - info!("Using new commit message:\n{}", commit_msg); - } - _ => {} + + info!("Using new commit message:\n{}", commit_msg); } + _ => {} } - _ => {} } info!("Committing Message...");