Skip to content

Commit a7a5e9b

Browse files
committed
feat(record): pass GPG-signing flag to git commit
1 parent 9c338ae commit a7a5e9b

File tree

3 files changed

+43
-18
lines changed

3 files changed

+43
-18
lines changed

git-branchless-lib/src/git/sign.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ pub enum SignOption {
1616
Disable,
1717
}
1818

19+
impl SignOption {
20+
/// GPG-signing flag to pass to Git.
21+
pub fn as_git_flag(&self) -> Option<String> {
22+
match self {
23+
Self::UseConfig => None,
24+
Self::UseConfigKey => Some("--gpg-sign".to_string()),
25+
Self::KeyOverride(keyid) => Some(format!("--gpg-sign={}", keyid)),
26+
Self::Disable => Some("--no-gpg-sign".to_string()),
27+
}
28+
}
29+
}
30+
1931
/// Get commit signer configured from CLI arguments and repository configurations.
2032
#[instrument]
2133
pub fn get_signer(

git-branchless-opts/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,10 @@ pub struct RecordArgs {
360360
/// if any.
361361
#[clap(action, short = 'I', long = "insert")]
362362
pub insert: bool,
363+
364+
/// Options for signing commits.
365+
#[clap(flatten)]
366+
pub sign_options: SignOptions,
363367
}
364368

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

git-branchless-record/src/lib.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use lib::core::rewrite::{
3131
};
3232
use lib::git::{
3333
process_diff_for_record, update_index, CategorizedReferenceName, FileMode, GitRunInfo,
34-
MaybeZeroOid, NonZeroOid, Repo, ResolvedReferenceInfo, Stage, UpdateIndexCommand,
34+
MaybeZeroOid, NonZeroOid, Repo, ResolvedReferenceInfo, SignOption, Stage, UpdateIndexCommand,
3535
WorkingCopyChangesType, WorkingCopySnapshot,
3636
};
3737
use lib::try_exit_code;
@@ -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.into(),
6567
)
6668
}
6769

@@ -74,6 +76,7 @@ fn record(
7476
branch_name: Option<String>,
7577
detach: bool,
7678
insert: bool,
79+
sign_option: &SignOption,
7780
) -> EyreExitOr<()> {
7881
let now = SystemTime::now();
7982
let repo = Repo::from_dir(&git_run_info.working_directory)?;
@@ -147,19 +150,22 @@ fn record(
147150
&snapshot,
148151
event_tx_id,
149152
message.as_deref(),
153+
sign_option,
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");
160-
}
161-
args
162-
};
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 sign_flag = sign_option.as_git_flag();
165+
if let Some(flag) = &sign_flag {
166+
args.push(flag);
167+
}
168+
163169
try_exit_code!(git_run_info.run_direct_no_wrapping(Some(event_tx_id), &args)?);
164170
}
165171

@@ -224,6 +230,7 @@ fn record_interactive(
224230
snapshot: &WorkingCopySnapshot,
225231
event_tx_id: EventTransactionId,
226232
message: Option<&str>,
233+
sign_option: &SignOption,
227234
) -> EyreExitOr<()> {
228235
let old_tree = snapshot.commit_stage0.get_tree()?;
229236
let new_tree = snapshot.commit_unstaged.get_tree()?;
@@ -332,13 +339,15 @@ fn record_interactive(
332339
&update_index_script,
333340
)?;
334341

335-
let args = {
336-
let mut args = vec!["commit"];
337-
if let Some(message) = message {
338-
args.extend(["--message", message]);
339-
}
340-
args
341-
};
342+
let mut args = vec!["commit"];
343+
if let Some(message) = message {
344+
args.extend(["--message", message]);
345+
}
346+
let sign_flag = sign_option.as_git_flag();
347+
if let Some(flag) = &sign_flag {
348+
args.push(flag);
349+
}
350+
342351
git_run_info.run_direct_no_wrapping(Some(event_tx_id), &args)
343352
}
344353

0 commit comments

Comments
 (0)