Skip to content
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

feat: add git add + commit + update #34

Merged
merged 1 commit into from
Oct 28, 2024
Merged
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
3 changes: 3 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub enum StError {
Color::Blue.paint("st restack")
)]
NeedsRestack(String),
/// A commit message is required with --all or --update
#[error("Commit message is required with --all or --update")]
CommitMessageRequired,
/// The working tree is dirty.
#[error("Working tree is dirty. Please commit or stash changes before continuing.")]
WorkingTreeDirty,
Expand Down
44 changes: 43 additions & 1 deletion src/subcommands/local/create.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
//! `create` subcommand.

use crate::{ctx::StContext, errors::StResult, git::RepositoryExt};
use crate::{
ctx::StContext,
errors::{StError, StResult},
git::RepositoryExt,
};
use clap::Args;
use git2::IndexAddOption;
use nu_ansi_term::Color;

/// CLI arguments for the `create` subcommand.
Expand All @@ -10,7 +15,17 @@ pub struct CreateCmd {
/// Name of the new branch to create.
#[clap(index = 1)]
branch_name: Option<String>,
/// Stage all changes before creating branch
#[clap(short = 'a', long = "all")]
all: bool,
/// Stage only tracked files before creating branch
#[clap(short, long = "update")]
update: bool,
/// Specify a commit message
#[clap(short, long, requires = "all", conflicts_with = "update")]
message: Option<String>,
}

impl CreateCmd {
/// Run the `create` subcommand.
pub fn run(self, mut ctx: StContext<'_>) -> StResult<()> {
Expand All @@ -25,6 +40,33 @@ impl CreateCmd {
None => inquire::Text::new("Name of new branch:").prompt()?,
};

// Stage changes if requested
if self.all || self.update {
let message = self.message.ok_or(StError::CommitMessageRequired)?;

// Get the index.
let mut index = ctx.repository.index()?;

// Stage changes based on flag.
if self.all {
index.add_all(vec!["*"], IndexAddOption::DEFAULT, None)?;
} else if self.update {
index.update_all(vec!["*"], None)?;
}
index.write()?;

// Create the commit.
let sig = ctx.repository.signature()?;
// Get the tree.
let tree_id = ctx.repository.index()?.write_tree()?;
let tree = ctx.repository.find_tree(tree_id)?;
// Get the parent commit.
let parent_commit = ctx.repository.head()?.peel_to_commit()?;
// Create the commit.
ctx.repository
.commit(Some("HEAD"), &sig, &sig, &message, &tree, &[&parent_commit])?;
}

// Check if the working tree is clean.
if !ctx.repository.is_working_tree_clean()? {
return Err(crate::errors::StError::WorkingTreeDirty);
Expand Down