From 0666ffed9c93942751258622c4fbf08ba2e779c2 Mon Sep 17 00:00:00 2001 From: ABWassim Date: Thu, 7 Mar 2024 10:05:12 +0100 Subject: [PATCH] feat(commit): add and update files --- src/bin/cog/main.rs | 12 ++++++++++++ src/command/commit.rs | 10 ++++++++++ src/git/repository.rs | 8 +++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/bin/cog/main.rs b/src/bin/cog/main.rs index 55ecb35f..f6509d61 100644 --- a/src/bin/cog/main.rs +++ b/src/bin/cog/main.rs @@ -363,6 +363,14 @@ struct CommitArgs { /// Override and add the string to the commit #[arg(long = "skip-ci-override")] skip_ci_override: Option, + + /// 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<()> { @@ -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)?; @@ -657,6 +667,8 @@ fn main() -> Result<()> { footer, breaking, sign, + add_files, + update_files, }; cocogitto.conventional_commit(opts)?; diff --git a/src/command/commit.rs b/src/command/commit.rs index 61a9b1df..c00d3275 100644 --- a/src/command/commit.rs +++ b/src/command/commit.rs @@ -16,6 +16,8 @@ pub struct CommitOptions<'a> { pub footer: Option, pub breaking: bool, pub sign: bool, + pub add_files: bool, + pub update_files: bool, } impl CocoGitto { @@ -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)?; diff --git a/src/git/repository.rs b/src/git/repository.rs index 8c8cc678..f75ac364 100644 --- a/src/git/repository.rs +++ b/src/git/repository.rs @@ -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) }