-
Notifications
You must be signed in to change notification settings - Fork 403
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
How to correctly create GPG-signed commits? #507
Comments
I feel that this is a workaround and should be unnecessary, but I found a way to add the commit and have it show up in use std::fs;
use git2::Repository;
use gpgme::{Context, Protocol, SignMode};
fn main() -> Result<(), Box<dyn std::error::Error>> {
fs::create_dir_all("/tmp/git2").unwrap();
let mut ctx = Context::from_protocol(Protocol::OpenPgp)?;
let repo = Repository::init("/tmp/git2").unwrap();
let mut index = repo.index()?;
index.add_all(["."].iter(), git2::IndexAddOption::DEFAULT, None)?;
index.write()?;
let tree_id = repo.index()?.write_tree()?;
let sig = repo.signature()?;
let mut parents = Vec::new();
if let Some(parent) = repo.head().ok().map(|h| h.target().unwrap()) {
parents.push(repo.find_commit(parent)?);
}
let parents = parents.iter().collect::<Vec<_>>();
let buf =
repo.commit_create_buffer(&sig, &sig, "test", &repo.find_tree(tree_id)?, &parents)?;
let contents = std::str::from_utf8(&buf).unwrap().to_string();
let mut outbuf = Vec::new();
ctx.set_armor(true);
ctx.sign(SignMode::Detached, buf.as_str().unwrap(), &mut outbuf)?;
let out = std::str::from_utf8(&outbuf).unwrap();
let ret = repo.commit_signed(&contents, &out, None)?;
let commit = repo.find_commit(ret)?;
repo.branch("master", &commit, false)?; // :-)
println!("{:?}", ret);
Ok(())
} |
@cole-h Sorry for bringing up an old issue, but were you able to resolve the issue of updating |
Basically, have a look at my So the process is twofold:
Hope this helps. |
Following examples around the internet (and in this very issue tracker), I have created a repo, staged modified files, and committed these files. However, I have not found a way to commit with a valid GPG signature. Below is example code that exhibits this problem:
Example code
If I comment out
repo.commit()
and uncomment the related code forrepo.commit_signed()
, it does create a commit, but does not update HEAD (is this as-intended inlibgit2
?).Did I overlook a way to manually point HEAD to an
Oid
? Am I doing something wrong?EDIT: I have to actually sign the contents with GPG. Duh.
The text was updated successfully, but these errors were encountered: