Skip to content

Commit

Permalink
Add id command
Browse files Browse the repository at this point in the history
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

<!-- ps-id: b267ff0a-e9c3-4ca8-9d95-ca21d5a18720 -->
  • Loading branch information
drewdeponte committed Jul 6, 2024
1 parent 3934708 commit 19807d5
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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",
Expand All @@ -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),
Expand All @@ -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")]
Expand Down
13 changes: 13 additions & 0 deletions src/commands/id.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
40 changes: 40 additions & 0 deletions src/ps/public/id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use super::super::super::ps;
use super::super::private::git;

#[derive(Debug)]
pub enum IdError {
OpenGitConfigFailed(Box<dyn std::error::Error>),
AddPatchIdsFailed(Box<dyn std::error::Error>),
Unhandled(Box<dyn std::error::Error>),
}

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()))
}
1 change: 1 addition & 0 deletions src/ps/public/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 19807d5

Please sign in to comment.