From 19807d599c6c4117657d09c9d907a56857d42f21 Mon Sep 17 00:00:00 2001 From: Drew De Ponte Date: Sat, 6 Jul 2024 00:38:23 -0400 Subject: [PATCH] Add id command Add id command so that users would be able to explicitly add patch ids to commits in their stack. [changelog] added: id subcommand to add patch ids to commits missing patch ids --- src/cli.rs | 9 +++++++++ src/commands/id.rs | 13 +++++++++++++ src/commands/mod.rs | 1 + src/lib.rs | 1 + src/main.rs | 1 + src/ps/public/id.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/ps/public/mod.rs | 1 + 7 files changed, 66 insertions(+) create mode 100644 src/commands/id.rs create mode 100644 src/ps/public/id.rs diff --git a/src/cli.rs b/src/cli.rs index d02fa25..d9ba6a2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -77,6 +77,7 @@ pub enum Command { /// (b) - Create a branch for a patch or patch series #[command(name = "branch", alias = "b")] Branch(BranchCmdOpts), + /// (int) - Integrate the specified patch into the patch stacks upstream /// remote #[command(name = "integrate", alias = "int")] @@ -192,9 +193,11 @@ stack. /// Pull changes down from upstream and rebase stack on top #[command(name = "pull")] Pull, + /// (rr) - Request review of the specified patch #[command(name = "request-review", alias = "rr")] RequestReview(RequestReview), + /// Output the sha of specified patch to stdout #[command( name = "sha", @@ -213,6 +216,11 @@ either a Git alias or a shell alias for that command. " )] Sha(ShaCmdOpts), + + /// Rebase current branch adding patch ids to any patches missing them + #[command(name = "id")] + Id, + /// Show the identified patch in raw form #[command(name = "show")] Show(ShowCmdOpts), @@ -231,6 +239,7 @@ either a Git alias or a shell alias for that command. /// to the stack that you were on when you switched into isolation mode by running `gps iso`. #[command(name = "isolate", alias = "iso")] Isolate(IsolateCmdOpts), + /// (co) - Checkout the patch identified by the patch-index, leaving you /// in a headless state. #[command(name = "checkout", alias = "co")] diff --git a/src/commands/id.rs b/src/commands/id.rs new file mode 100644 index 0000000..c19e337 --- /dev/null +++ b/src/commands/id.rs @@ -0,0 +1,13 @@ +use super::utils::print_error_chain; +use gps as ps; + +pub fn id(color: bool) { + let res = ps::id(); + match res { + Ok(_) => {} + Err(e) => { + print_error_chain(color, e.into()); + std::process::exit(1); + } + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index f4525e6..3a822ad 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -7,6 +7,7 @@ pub mod backup_stack; pub mod branch; pub mod checkout; pub mod fetch; +pub mod id; pub mod integrate; pub mod isolate; pub mod list; diff --git a/src/lib.rs b/src/lib.rs index 5a233c7..7527e59 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ pub use ps::public::backup_stack::backup_stack; pub use ps::public::branch::{branch, BranchError}; pub use ps::public::checkout::checkout; pub use ps::public::fetch::fetch; +pub use ps::public::id::id; pub use ps::public::integrate; pub use ps::public::isolate::{isolate, IsolateError}; pub use ps::public::latest_github_release::{newer_release_available, notify_of_newer_release}; diff --git a/src/main.rs b/src/main.rs index b9a8164..6db4f5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,6 +42,7 @@ fn main() { cli::Command::Sha(opts) => { commands::sha::sha(opts.patch_index, cli.color, opts.exclude_newline) } + cli::Command::Id => commands::id::id(cli.color), cli::Command::Show(opts) => commands::show::show(opts.patch_index_or_range), cli::Command::Isolate(opts) => { commands::isolate::isolate(opts.patch_index_or_range, cli.color) diff --git a/src/ps/public/id.rs b/src/ps/public/id.rs new file mode 100644 index 0000000..b655e17 --- /dev/null +++ b/src/ps/public/id.rs @@ -0,0 +1,40 @@ +use super::super::super::ps; +use super::super::private::git; + +#[derive(Debug)] +pub enum IdError { + OpenGitConfigFailed(Box), + AddPatchIdsFailed(Box), + Unhandled(Box), +} + +impl std::fmt::Display for IdError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::OpenGitConfigFailed(e) => { + write!(f, "Failed to open git config, {}", e) + } + Self::AddPatchIdsFailed(e) => write!(f, "add patch ids failed, {}", e), + Self::Unhandled(e) => write!(f, "{}", e), + } + } +} + +impl std::error::Error for IdError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + Self::OpenGitConfigFailed(e) => Some(e.as_ref()), + Self::AddPatchIdsFailed(e) => Some(e.as_ref()), + Self::Unhandled(e) => Some(e.as_ref()), + } + } +} + +pub fn id() -> Result<(), IdError> { + let repo = git::create_cwd_repo().map_err(|e| IdError::Unhandled(e.into()))?; + + let config = + git2::Config::open_default().map_err(|e| IdError::OpenGitConfigFailed(e.into()))?; + + ps::add_patch_ids(&repo, &config).map_err(|e| IdError::AddPatchIdsFailed(e.into())) +} diff --git a/src/ps/public/mod.rs b/src/ps/public/mod.rs index 3f3cce4..21b5958 100644 --- a/src/ps/public/mod.rs +++ b/src/ps/public/mod.rs @@ -2,6 +2,7 @@ pub mod backup_stack; pub mod branch; pub mod checkout; pub mod fetch; +pub mod id; pub mod integrate; pub mod isolate; pub mod latest_github_release;