Skip to content

Commit

Permalink
feat(commit): add and update files
Browse files Browse the repository at this point in the history
  • Loading branch information
ABWassim authored and oknozor committed Mar 8, 2024
1 parent 1c478ed commit 0666ffe
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/bin/cog/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ struct CommitArgs {
/// Override and add the <SKIP_CI_OVERRIDE> string to the commit
#[arg(long = "skip-ci-override")]
skip_ci_override: Option<String>,

/// Add files to the commit (similar to git add .)
#[arg(short, long = "add")]
add_files: bool,

/// Update but doesn't add files to the commit (similar to git add -u .)
#[arg(short, long = "update")]
update_files: bool,
}

fn main() -> Result<()> {
Expand Down Expand Up @@ -621,6 +629,8 @@ fn main() -> Result<()> {
sign,
skip_ci,
skip_ci_override,
add_files,
update_files,
}) => {
let cocogitto = CocoGitto::get()?;
cocogitto.run_commit_hook(CommitHook::PreCommit)?;
Expand Down Expand Up @@ -657,6 +667,8 @@ fn main() -> Result<()> {
footer,
breaking,
sign,
add_files,
update_files,
};

cocogitto.conventional_commit(opts)?;
Expand Down
10 changes: 10 additions & 0 deletions src/command/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub struct CommitOptions<'a> {
pub footer: Option<String>,
pub breaking: bool,
pub sign: bool,
pub add_files: bool,
pub update_files: bool,
}

impl CocoGitto {
Expand All @@ -42,6 +44,14 @@ impl CocoGitto {
// Validate the message
conventional_commit_parser::parse(&conventional_message)?;

if opts.add_files {
self.repository.add_all()?;
}

if opts.update_files {
self.repository.update_all()?;
}

// Git commit
let sign = opts.sign || self.repository.gpg_sign();
fs::write(self.prepare_edit_message_path(), &conventional_message)?;
Expand Down
8 changes: 7 additions & 1 deletion src/git/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ impl Repository {

pub(crate) fn add_all(&self) -> Result<(), Git2Error> {
let mut index = self.0.index()?;
index.add_all(["*"], IndexAddOption::DEFAULT, None)?;
index.add_all(["."], IndexAddOption::DEFAULT, None)?;
index.write().map_err(Git2Error::GitAddError)
}

pub(crate) fn update_all(&self) -> Result<(), Git2Error> {
let mut index = self.0.index()?;
index.update_all(["."], None)?;
index.write().map_err(Git2Error::GitAddError)
}

Expand Down

0 comments on commit 0666ffe

Please sign in to comment.