diff --git a/src/add.rs b/src/add.rs index f61e70b..f8d052e 100644 --- a/src/add.rs +++ b/src/add.rs @@ -1,12 +1,17 @@ use crate::{ change_type, changelog, config, entry, errors::AddError, - github::{extract_pr_info, get_git_info, get_open_pr, PRInfo}, + github::{commit, extract_pr_info, get_git_info, get_open_pr, PRInfo}, inputs, release, }; use std::borrow::BorrowMut; // Runs the logic to add an entry to the unreleased section of the changelog. +// +// After adding the new entry, the user is queried for a commit message to use +// to commit the changes. +// +// NOTE: the changes are NOT pushed to the origin when running the `add` command. pub async fn run(accept: bool) -> Result<(), AddError> { let config = config::load()?; let git_info = get_git_info(&config)?; @@ -68,7 +73,10 @@ pub async fn run(accept: bool) -> Result<(), AddError> { pr_number, ); - Ok(changelog.write(&changelog.path)?) + changelog.write(&changelog.path)?; + + let cm = inputs::get_commit_message(&config)?; + Ok(commit(cm.as_str())?) } /// Adds the given contents into a new entry in the unreleased section diff --git a/src/github.rs b/src/github.rs index 49289f4..98f23ba 100644 --- a/src/github.rs +++ b/src/github.rs @@ -121,6 +121,8 @@ fn get_current_local_branch() -> Result { /// Commits the current changes with the given commit message and pushes to the origin. pub fn commit_and_push(message: &str) -> Result<(), GitHubError> { + stage_changelog_changes()?; + match Command::new("git") .args(vec!["commit", "-a", "-m", message]) .status()? @@ -131,6 +133,35 @@ pub fn commit_and_push(message: &str) -> Result<(), GitHubError> { } } +/// Commits the current changes with the given commit message and pushes to the origin. +pub fn commit(message: &str) -> Result<(), GitHubError> { + stage_changelog_changes()?; + + if !Command::new("git") + .args(vec!["commit", "-m", message]) + .status()? + .success() + { + return Err(GitHubError::FailedToCommit); + } + + Ok(()) +} + +/// Adds the changelog to the staged changes in Git. +fn stage_changelog_changes() -> Result<(), GitHubError> { + // TODO: pass the changelog filename / path + if !Command::new("git") + .args(vec!["add", "CHANGELOG.md"]) + .status()? + .success() + { + return Err(GitHubError::FailedToCommit) + } + + Ok(()) +} + /// Tries to push the latest commits on the current branch. pub fn push() -> Result<(), GitHubError> { match Command::new("git")