Skip to content

Commit 4eb2d7d

Browse files
committed
feat(record): pass --(no-)gpg-sign flag to git commit
1 parent 7e55067 commit 4eb2d7d

File tree

2 files changed

+43
-16
lines changed
  • git-branchless-opts/src
  • git-branchless-record/src

2 files changed

+43
-16
lines changed

git-branchless-opts/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ pub struct RecordArgs {
340340
/// if any.
341341
#[clap(action, short = 'I', long = "insert")]
342342
pub insert: bool,
343+
344+
/// Options for signing commits.
345+
#[clap(flatten)]
346+
pub sign_options: SignOptions,
343347
}
344348

345349
/// Display a nice graph of the commits you've recently worked on.

git-branchless-record/src/lib.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::fmt::Write;
1515
use std::time::SystemTime;
1616

1717
use git_branchless_invoke::CommandContext;
18-
use git_branchless_opts::RecordArgs;
18+
use git_branchless_opts::{RecordArgs, SignOptions};
1919
use itertools::Itertools;
2020
use lib::core::check_out::{check_out_commit, CheckOutCommitOptions};
2121
use lib::core::config::get_restack_preserve_timestamps;
@@ -53,6 +53,7 @@ pub fn command_main(ctx: CommandContext, args: RecordArgs) -> EyreExitOr<()> {
5353
create,
5454
detach,
5555
insert,
56+
sign_options,
5657
} = args;
5758
record(
5859
&effects,
@@ -62,6 +63,7 @@ pub fn command_main(ctx: CommandContext, args: RecordArgs) -> EyreExitOr<()> {
6263
create,
6364
detach,
6465
insert,
66+
&sign_options,
6567
)
6668
}
6769

@@ -74,6 +76,7 @@ fn record(
7476
branch_name: Option<String>,
7577
detach: bool,
7678
insert: bool,
79+
sign_options: &SignOptions,
7780
) -> EyreExitOr<()> {
7881
let now = SystemTime::now();
7982
let repo = Repo::from_dir(&git_run_info.working_directory)?;
@@ -147,19 +150,29 @@ fn record(
147150
&snapshot,
148151
event_tx_id,
149152
message.as_deref(),
153+
sign_options,
150154
)?);
151155
}
152156
} else {
153-
let args = {
154-
let mut args = vec!["commit"];
155-
if let Some(message) = &message {
156-
args.extend(["--message", message]);
157-
}
158-
if working_copy_changes_type == WorkingCopyChangesType::Unstaged {
159-
args.push("--all");
157+
let mut args = vec!["commit"];
158+
if let Some(message) = &message {
159+
args.extend(["--message", message]);
160+
}
161+
if working_copy_changes_type == WorkingCopyChangesType::Unstaged {
162+
args.push("--all");
163+
}
164+
let mut gpg_flag;
165+
if let Some(keyid) = &sign_options.gpg_sign {
166+
gpg_flag = String::from("--gpg-sign");
167+
if !keyid.is_empty() {
168+
gpg_flag.push('=');
169+
gpg_flag.push_str(keyid);
160170
}
161-
args
162-
};
171+
args.push(&gpg_flag);
172+
} else if sign_options.no_gpg_sign {
173+
args.push("--no-gpg-sign");
174+
}
175+
163176
try_exit_code!(git_run_info.run_direct_no_wrapping(Some(event_tx_id), &args)?);
164177
}
165178

@@ -224,6 +237,7 @@ fn record_interactive(
224237
snapshot: &WorkingCopySnapshot,
225238
event_tx_id: EventTransactionId,
226239
message: Option<&str>,
240+
sign_options: &SignOptions,
227241
) -> EyreExitOr<()> {
228242
let old_tree = snapshot.commit_stage0.get_tree()?;
229243
let new_tree = snapshot.commit_unstaged.get_tree()?;
@@ -332,13 +346,22 @@ fn record_interactive(
332346
&update_index_script,
333347
)?;
334348

335-
let args = {
336-
let mut args = vec!["commit"];
337-
if let Some(message) = message {
338-
args.extend(["--message", message]);
349+
let mut args = vec!["commit"];
350+
if let Some(message) = message {
351+
args.extend(["--message", message]);
352+
}
353+
let mut gpg_flag;
354+
if let Some(keyid) = &sign_options.gpg_sign {
355+
gpg_flag = String::from("--gpg-sign");
356+
if !keyid.is_empty() {
357+
gpg_flag.push('=');
358+
gpg_flag.push_str(keyid);
339359
}
340-
args
341-
};
360+
args.push(&gpg_flag);
361+
} else if sign_options.no_gpg_sign {
362+
args.push("--no-gpg-sign");
363+
}
364+
342365
git_run_info.run_direct_no_wrapping(Some(event_tx_id), &args)
343366
}
344367

0 commit comments

Comments
 (0)