Skip to content

Commit

Permalink
add option to just commit and not push
Browse files Browse the repository at this point in the history
  • Loading branch information
MalteHerrmann committed Nov 8, 2024
1 parent c5f263a commit d4cbea5
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/add.rs
Original file line number Diff line number Diff line change
@@ -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)?;
Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ fn get_current_local_branch() -> Result<String, GitHubError> {

/// 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()?
Expand All @@ -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")
Expand Down

0 comments on commit d4cbea5

Please sign in to comment.