Skip to content

Commit

Permalink
feat(record): pass --(no-)gpg-sign flag to git commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyip committed Jun 14, 2023
1 parent 7e55067 commit 4eb2d7d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
4 changes: 4 additions & 0 deletions git-branchless-opts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,10 @@ pub struct RecordArgs {
/// if any.
#[clap(action, short = 'I', long = "insert")]
pub insert: bool,

/// Options for signing commits.
#[clap(flatten)]
pub sign_options: SignOptions,
}

/// Display a nice graph of the commits you've recently worked on.
Expand Down
55 changes: 39 additions & 16 deletions git-branchless-record/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::fmt::Write;
use std::time::SystemTime;

use git_branchless_invoke::CommandContext;
use git_branchless_opts::RecordArgs;
use git_branchless_opts::{RecordArgs, SignOptions};
use itertools::Itertools;
use lib::core::check_out::{check_out_commit, CheckOutCommitOptions};
use lib::core::config::get_restack_preserve_timestamps;
Expand Down Expand Up @@ -53,6 +53,7 @@ pub fn command_main(ctx: CommandContext, args: RecordArgs) -> EyreExitOr<()> {
create,
detach,
insert,
sign_options,
} = args;
record(
&effects,
Expand All @@ -62,6 +63,7 @@ pub fn command_main(ctx: CommandContext, args: RecordArgs) -> EyreExitOr<()> {
create,
detach,
insert,
&sign_options,
)
}

Expand All @@ -74,6 +76,7 @@ fn record(
branch_name: Option<String>,
detach: bool,
insert: bool,
sign_options: &SignOptions,
) -> EyreExitOr<()> {
let now = SystemTime::now();
let repo = Repo::from_dir(&git_run_info.working_directory)?;
Expand Down Expand Up @@ -147,19 +150,29 @@ fn record(
&snapshot,
event_tx_id,
message.as_deref(),
sign_options,
)?);
}
} else {
let args = {
let mut args = vec!["commit"];
if let Some(message) = &message {
args.extend(["--message", message]);
}
if working_copy_changes_type == WorkingCopyChangesType::Unstaged {
args.push("--all");
let mut args = vec!["commit"];
if let Some(message) = &message {
args.extend(["--message", message]);
}
if working_copy_changes_type == WorkingCopyChangesType::Unstaged {
args.push("--all");
}
let mut gpg_flag;
if let Some(keyid) = &sign_options.gpg_sign {
gpg_flag = String::from("--gpg-sign");
if !keyid.is_empty() {
gpg_flag.push('=');
gpg_flag.push_str(keyid);
}
args
};
args.push(&gpg_flag);
} else if sign_options.no_gpg_sign {
args.push("--no-gpg-sign");
}

try_exit_code!(git_run_info.run_direct_no_wrapping(Some(event_tx_id), &args)?);
}

Expand Down Expand Up @@ -224,6 +237,7 @@ fn record_interactive(
snapshot: &WorkingCopySnapshot,
event_tx_id: EventTransactionId,
message: Option<&str>,
sign_options: &SignOptions,
) -> EyreExitOr<()> {
let old_tree = snapshot.commit_stage0.get_tree()?;
let new_tree = snapshot.commit_unstaged.get_tree()?;
Expand Down Expand Up @@ -332,13 +346,22 @@ fn record_interactive(
&update_index_script,
)?;

let args = {
let mut args = vec!["commit"];
if let Some(message) = message {
args.extend(["--message", message]);
let mut args = vec!["commit"];
if let Some(message) = message {
args.extend(["--message", message]);
}
let mut gpg_flag;
if let Some(keyid) = &sign_options.gpg_sign {
gpg_flag = String::from("--gpg-sign");
if !keyid.is_empty() {
gpg_flag.push('=');
gpg_flag.push_str(keyid);
}
args
};
args.push(&gpg_flag);
} else if sign_options.no_gpg_sign {
args.push("--no-gpg-sign");
}

git_run_info.run_direct_no_wrapping(Some(event_tx_id), &args)
}

Expand Down

0 comments on commit 4eb2d7d

Please sign in to comment.