Skip to content

Commit 82f788e

Browse files
committed
refactor
1 parent aacc326 commit 82f788e

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

gitoxide-core/src/repository/commit.rs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,18 @@ pub fn verify(repo: gix::Repository, rev_spec: Option<&str>) -> Result<()> {
4747
Ok(())
4848
}
4949

50-
/// Note that this is a quick first prototype that lacks some of the features provided by `git
51-
/// verify-commit`.
50+
/// Note that this is a quick first prototype that lacks some of the features provided by `git verify-commit`.
5251
pub fn sign(repo: gix::Repository, rev_spec: Option<&str>, mut out: impl std::io::Write) -> Result<()> {
5352
let rev_spec = rev_spec.unwrap_or("HEAD");
5453
let object = repo
5554
.rev_parse_single(format!("{rev_spec}^{{commit}}").as_str())?
5655
.object()?;
5756
let mut commit_ref = object.to_commit_ref();
57+
if commit_ref.extra_headers().pgp_signature().is_some() {
58+
gix::trace::info!("The commit {id} is already signed, did nothing", id = object.id);
59+
writeln!(out, "{id}", id = object.id)?;
60+
return Ok(());
61+
}
5862

5963
let mut cmd: std::process::Command = gix::command::prepare("gpg").into();
6064
cmd.args([
@@ -66,6 +70,7 @@ pub fn sign(repo: gix::Repository, rev_spec: Option<&str>, mut out: impl std::io
6670
])
6771
.stdin(Stdio::piped())
6872
.stdout(Stdio::piped());
73+
6974
gix::trace::debug!("About to execute {cmd:?}");
7075
let mut child = cmd.spawn()?;
7176
child.stdin.take().expect("to be present").write_all(&object.data)?;
@@ -75,28 +80,13 @@ pub fn sign(repo: gix::Repository, rev_spec: Option<&str>, mut out: impl std::io
7580
}
7681

7782
let mut signed_data = Vec::new();
78-
child
79-
.stdout
80-
.take()
81-
.expect("to be present")
82-
.read_to_end(&mut signed_data)?;
83-
84-
let extra_header: Cow<'_, BStr> = Cow::Owned(BString::new(signed_data));
85-
86-
assert!(
87-
!commit_ref
88-
.extra_headers
89-
.iter()
90-
.any(|(header_name, _)| *header_name == BStr::new(SIGNATURE_FIELD_NAME)),
91-
"Commit is already signed, doing nothing"
92-
);
83+
child.stdout.expect("to be present").read_to_end(&mut signed_data)?;
9384

9485
commit_ref
9586
.extra_headers
96-
.push((BStr::new(SIGNATURE_FIELD_NAME), extra_header));
87+
.push((BStr::new(SIGNATURE_FIELD_NAME), Cow::Owned(BString::new(signed_data))));
9788

9889
let signed_id = repo.write_object(&commit_ref)?;
99-
10090
writeln!(&mut out, "{signed_id}")?;
10191

10292
Ok(())

src/plumbing/options/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,8 +912,9 @@ pub mod commit {
912912
/// A specification of the revision to verify, or the current `HEAD` if unset.
913913
rev_spec: Option<String>,
914914
},
915-
/// Sign a commit and print the signed commit's id to stdout. This command does not change
916-
/// symbolic refs.
915+
/// Sign a commit and print the signed commit's id to stdout.
916+
///
917+
/// This command does not change symbolic refs.
917918
Sign {
918919
/// A specification of the revision to sign, or the current `HEAD` if unset.
919920
rev_spec: Option<String>,

0 commit comments

Comments
 (0)