Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Support GPG-signing commits #966

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions git-branchless-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ eyre = "0.6.8"
futures = "0.3.28"
git-record = { version = "0.3", path = "../git-record" }
git2 = { version = "0.17.2", default-features = false }
git2-ext = "0.6.0"
indicatif = { version = "0.17.5", features = ["improved_unicode"] }
itertools = "0.10.3"
lazy_static = "1.4.0"
Expand Down
29 changes: 25 additions & 4 deletions git-branchless-lib/src/core/rewrite/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::core::formatting::Pluralize;
use crate::core::repo_ext::RepoExt;
use crate::git::{
BranchType, CategorizedReferenceName, GitRunInfo, MaybeZeroOid, NonZeroOid, ReferenceName,
Repo, ResolvedReferenceInfo,
Repo, ResolvedReferenceInfo, SignOption,
};
use crate::util::{ExitCode, EyreExitOr};

Expand Down Expand Up @@ -435,7 +435,8 @@ mod in_memory {
use crate::core::rewrite::move_branches;
use crate::core::rewrite::plan::{OidOrLabel, RebaseCommand, RebasePlan};
use crate::git::{
CherryPickFastError, CherryPickFastOptions, GitRunInfo, MaybeZeroOid, NonZeroOid, Repo,
self, CherryPickFastError, CherryPickFastOptions, GitRunInfo, MaybeZeroOid, NonZeroOid,
Repo,
};
use crate::util::EyreExitOr;

Expand Down Expand Up @@ -498,6 +499,7 @@ mod in_memory {
force_on_disk: _,
resolve_merge_conflicts: _, // May be needed once we can resolve merge conflicts in memory.
check_out_commit_options: _, // Caller is responsible for checking out to new HEAD.
sign_option,
} = options;

let mut current_oid = rebase_plan.first_dest_oid;
Expand Down Expand Up @@ -535,6 +537,8 @@ mod in_memory {
.count();
let (effects, progress) = effects.start_operation(OperationType::RebaseCommits);

let signer = git::get_signer(&repo, sign_option)?;

for command in rebase_plan.commands.iter() {
match command {
RebaseCommand::CreateLabel { label_name } => {
Expand Down Expand Up @@ -628,12 +632,12 @@ mod in_memory {
};
let rebased_commit_oid = repo
.create_commit(
None,
&commit_to_apply.get_author(),
&committer_signature,
commit_message,
&commit_tree,
vec![&current_commit],
signer.as_deref(),
)
.wrap_err("Applying rebased commit")?;

Expand Down Expand Up @@ -748,12 +752,12 @@ mod in_memory {
};
let rebased_commit_oid = repo
.create_commit(
None,
&replacement_commit.get_author(),
&committer_signature,
replacement_commit_message,
&replacement_tree,
parents.iter().collect(),
signer.as_deref(),
)
.wrap_err("Applying rebased commit")?;

Expand Down Expand Up @@ -864,6 +868,7 @@ mod in_memory {
force_on_disk: _,
resolve_merge_conflicts: _,
check_out_commit_options,
sign_option: _,
} = options;

// Note that if an OID has been mapped to multiple other OIDs, then the last
Expand Down Expand Up @@ -959,6 +964,7 @@ mod on_disk {
force_on_disk: _,
resolve_merge_conflicts: _,
check_out_commit_options: _, // Checkout happens after rebase has concluded.
sign_option,
} = options;

let (effects, _progress) = effects.start_operation(OperationType::InitializeRebase);
Expand Down Expand Up @@ -1073,6 +1079,16 @@ mod on_disk {
)
})?;

let gpg_sign_opt_file = rebase_state_dir.join("gpg_sign_opt");
if let Some(sign_flag) = sign_option.as_rebase_flag(repo)? {
std::fs::write(&gpg_sign_opt_file, sign_flag).wrap_err_with(|| {
format!(
"Writing `gpg_sign_opt` to: {:?}",
gpg_sign_opt_file.as_path()
)
})?;
}

let end_file_path = rebase_state_dir.join("end");
std::fs::write(
end_file_path.as_path(),
Expand Down Expand Up @@ -1132,6 +1148,7 @@ mod on_disk {
force_on_disk: _,
resolve_merge_conflicts: _,
check_out_commit_options: _, // Checkout happens after rebase has concluded.
sign_option: _,
} = options;

match write_rebase_state_to_disk(effects, git_run_info, repo, rebase_plan, options)? {
Expand Down Expand Up @@ -1176,6 +1193,9 @@ pub struct ExecuteRebasePlanOptions {

/// If `HEAD` was moved, the options for checking out the new `HEAD` commit.
pub check_out_commit_options: CheckOutCommitOptions,

/// GPG-sign commits.
pub sign_option: SignOption,
}

/// The result of executing a rebase plan.
Expand Down Expand Up @@ -1221,6 +1241,7 @@ pub fn execute_rebase_plan(
force_on_disk,
resolve_merge_conflicts,
check_out_commit_options: _,
sign_option: _,
} = options;

if !force_on_disk {
Expand Down
2 changes: 2 additions & 0 deletions git-branchless-lib/src/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod oid;
mod reference;
mod repo;
mod run;
mod sign;
mod snapshot;
mod status;
mod test;
Expand All @@ -27,6 +28,7 @@ pub use repo::{
Time,
};
pub use run::{GitRunInfo, GitRunOpts, GitRunResult};
pub use sign::{get_signer, SignOption};
pub use snapshot::{WorkingCopyChangesType, WorkingCopySnapshot};
pub use status::{FileMode, FileStatus, StatusEntry};
pub use test::{
Expand Down
79 changes: 65 additions & 14 deletions git-branchless-lib/src/git/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use chrono::NaiveDateTime;
use cursive::theme::BaseColor;
use cursive::utils::markup::StyledString;
use git2::DiffOptions;
use git2_ext::ops::Sign;
use itertools::Itertools;
use thiserror::Error;
use tracing::{instrument, warn};
Expand Down Expand Up @@ -1152,31 +1153,81 @@ impl Repo {
}

/// Create a new commit.
#[instrument]
#[instrument(skip(signer))]
pub fn create_commit(
&self,
update_ref: Option<&str>,
author: &Signature,
committer: &Signature,
message: &str,
tree: &Tree,
parents: Vec<&Commit>,
signer: Option<&dyn Sign>,
) -> Result<NonZeroOid> {
let parents = parents
.iter()
.map(|commit| &commit.inner)
.collect::<Vec<_>>();
let oid = self
.inner
.commit(
update_ref,
&author.inner,
&committer.inner,
message,
&tree.inner,
parents.as_slice(),
)
.map_err(Error::CreateCommit)?;
let oid = git2_ext::ops::commit(
&self.inner,
&author.inner,
&committer.inner,
message,
&tree.inner,
parents.as_slice(),
signer,
)
.map_err(Error::CreateCommit)?;
Ok(make_non_zero_oid(oid))
}

/// Amend a commit with all non-`None` values
#[instrument(skip(signer))]
pub fn amend_commit(
&self,
commit_to_amend: &Commit,
author: Option<&Signature>,
committer: Option<&Signature>,
message: Option<&str>,
tree: Option<&Tree>,
signer: Option<&dyn Sign>,
) -> Result<NonZeroOid> {
macro_rules! owning_unwrap_or {
($name:ident, $value_source:expr) => {
let owned_value;
let $name = if let Some(value) = $name {
value
} else {
owned_value = $value_source;
&owned_value
};
};
}
owning_unwrap_or!(author, commit_to_amend.get_author());
owning_unwrap_or!(committer, commit_to_amend.get_committer());
owning_unwrap_or!(
message,
commit_to_amend
.inner
.message_raw()
.ok_or(Error::DecodeUtf8 {
item: "raw message",
})?
);
owning_unwrap_or!(tree, commit_to_amend.get_tree()?);

let parents = commit_to_amend.get_parents();
let parents = parents.iter().map(|parent| &parent.inner).collect_vec();

let oid = git2_ext::ops::commit(
&self.inner,
&author.inner,
&committer.inner,
message,
&tree.inner,
parents.as_slice(),
signer,
)
.map_err(Error::Amend)?;
Ok(make_non_zero_oid(oid))
}

Expand Down Expand Up @@ -1365,12 +1416,12 @@ impl Repo {
vec![]
};
let dehydrated_commit_oid = self.create_commit(
None,
&signature,
&signature,
&message,
&dehydrated_tree,
parents.iter().collect_vec(),
None,
)?;
let dehydrated_commit = self.find_commit_or_fail(dehydrated_commit_oid)?;
Ok(dehydrated_commit)
Expand Down
Loading