Skip to content

feat: use git commit --verbose template in external editor #2640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 34 additions & 6 deletions asyncgit/src/sync/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,26 @@ pub fn tag_commit(
}

/// Loads the comment prefix from config & uses it to prettify commit messages
///
/// Also removes any lines after the scissors line.
pub fn commit_message_prettify(
repo_path: &RepoPath,
message: String,
message: &str,
) -> Result<String> {
let comment_char = repo(repo_path)?
.config()?
.get_string("core.commentChar")
.ok()
.and_then(|char_string| char_string.chars().next())
.unwrap_or('#') as u8;

Ok(message_prettify(message, Some(comment_char))?)
.unwrap_or('#');
let scissors_line = format!("{comment_char} ------------------------ >8 ------------------------");
let message = message
.lines()
.take_while(|line| line != &scissors_line)
.collect::<Vec<_>>()
.join("\n");

Ok(message_prettify(message, Some(comment_char as u8))?)
}

#[cfg(test)]
Expand Down Expand Up @@ -468,7 +476,7 @@ mod tests {

let message = commit_message_prettify(
repo_path,
"#This is a test message\nTest".to_owned(),
"#This is a test message\nTest",
)?;

assert_eq!(message, "Test\n");
Expand All @@ -487,7 +495,27 @@ mod tests {

let message = commit_message_prettify(
repo_path,
";This is a test message\nTest".to_owned(),
";This is a test message\nTest",
)?;

assert_eq!(message, "Test\n");

Ok(())
}

#[test]
fn test_scissors_line() -> Result<()> {
let (_td, repo) = repo_init_empty().unwrap();

let root = repo.path().parent().unwrap();
let repo_path: &RepoPath =
&root.as_os_str().to_str().unwrap().into();

repo.config()?.set_str("core.commentChar", ";")?;

let message = commit_message_prettify(
repo_path,
";This is a test message\nTest\n; ------------------------ >8 ------------------------\nTest2\nTest3\nTest4",
)?;

assert_eq!(message, "Test\n");
Expand Down
3 changes: 2 additions & 1 deletion asyncgit/src/sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ pub use tags::{
pub use tree::{tree_file_content, tree_files, TreeFile};
pub use utils::{
get_head, get_head_tuple, repo_dir, repo_open_error,
stage_add_all, stage_add_file, stage_addremoved, Head,
repo_work_dir, stage_add_all, stage_add_file, stage_addremoved,
Head,
};

pub use git2::ResetType;
Expand Down
4 changes: 1 addition & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,7 @@ impl App {
Path::new(&path),
)
} else {
let changes =
self.status_tab.get_files_changes()?;
self.commit_popup.show_editor(changes)
self.commit_popup.show_editor()
};

if let Err(e) = result {
Expand Down
64 changes: 16 additions & 48 deletions src/popups/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use asyncgit::{
self, get_config_string, CommitId, HookResult,
PrepareCommitMsgSource, RepoPathRef, RepoState,
},
StatusItem, StatusItemType,
};
use crossterm::event::Event;
use easy_cast::Cast;
Expand All @@ -28,10 +27,11 @@ use ratatui::{
Frame,
};

use std::process::Command;
use std::{
fmt::Write as _,
fs::{read_to_string, File},
io::{Read, Write},
io::Read,
path::PathBuf,
str::FromStr,
};
Expand Down Expand Up @@ -144,47 +144,18 @@ impl CommitPopup {
}
}

const fn item_status_char(
item_type: StatusItemType,
) -> &'static str {
match item_type {
StatusItemType::Modified => "modified",
StatusItemType::New => "new file",
StatusItemType::Deleted => "deleted",
StatusItemType::Renamed => "renamed",
StatusItemType::Typechange => " ",
StatusItemType::Conflicted => "conflicted",
}
}

pub fn show_editor(
&mut self,
changes: Vec<StatusItem>,
) -> Result<()> {
let file_path = sync::repo_dir(&self.repo.borrow())?
.join("COMMIT_EDITMSG");
pub fn show_editor(&mut self) -> Result<()> {
let git_dir = sync::repo_dir(&self.repo.borrow())?;
let work_dir = sync::repo_work_dir(&self.repo.borrow())?;
let file_path = git_dir.join("COMMIT_EDITMSG");

{
let mut file = File::create(&file_path)?;
file.write_fmt(format_args!(
"{}\n",
self.input.get_text()
))?;
file.write_all(
strings::commit_editor_msg(&self.key_config)
.as_bytes(),
)?;

file.write_all(b"\n#\n# Changes to be committed:")?;

for change in changes {
let status_char =
Self::item_status_char(change.status);
let message =
format!("\n#\t{status_char}: {}", change.path);
file.write_all(message.as_bytes())?;
}
}
Command::new("git")
.arg("commit")
.arg("--verbose")
.env("EDITOR", "false")
.env("GIT_DIR", git_dir)
.env("GIT_WORK_TREE", work_dir)
.output()?;

ExternalEditorPopup::open_file_in_editor(
&self.repo.borrow(),
Expand All @@ -199,7 +170,7 @@ impl CommitPopup {
std::fs::remove_file(&file_path)?;

message =
commit_message_prettify(&self.repo.borrow(), message)?;
commit_message_prettify(&self.repo.borrow(), &message)?;
self.input.set_text(message);
self.input.show()?;

Expand All @@ -210,7 +181,7 @@ impl CommitPopup {
let msg = self.input.get_text().to_string();

if matches!(
self.commit_with_msg(msg)?,
self.commit_with_msg(&msg)?,
CommitResult::CommitDone
) {
self.options
Expand All @@ -227,10 +198,7 @@ impl CommitPopup {
Ok(())
}

fn commit_with_msg(
&mut self,
msg: String,
) -> Result<CommitResult> {
fn commit_with_msg(&mut self, msg: &str) -> Result<CommitResult> {
// on exit verify should always be on
let verify = self.verify;
self.verify = true;
Expand Down
6 changes: 0 additions & 6 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ pub fn commit_first_line_warning(count: usize) -> String {
pub const fn branch_name_invalid() -> &'static str {
"[invalid name]"
}
pub fn commit_editor_msg(_key_config: &SharedKeyConfig) -> String {
r"
# Edit your commit message
# Lines starting with '#' will be ignored"
.to_string()
}
pub fn stash_popup_title(_key_config: &SharedKeyConfig) -> String {
"Stash".to_string()
}
Expand Down
6 changes: 1 addition & 5 deletions src/tabs/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use asyncgit::{
},
sync::{BranchCompare, CommitId},
AsyncDiff, AsyncGitNotification, AsyncStatus, DiffParams,
DiffType, PushType, StatusItem, StatusParams,
DiffType, PushType, StatusParams,
};
use crossterm::event::Event;
use itertools::Itertools;
Expand Down Expand Up @@ -451,10 +451,6 @@ impl Status {
Ok(())
}

pub fn get_files_changes(&self) -> Result<Vec<StatusItem>> {
Ok(self.git_status_stage.last()?.items)
}

fn update_status(&mut self) -> Result<()> {
let stage_status = self.git_status_stage.last()?;
self.index.set_items(&stage_status.items)?;
Expand Down