Repository navigation
Description
Summary
Repository#commit() takes two different code paths depending on whether
CommitOptions.signature is set. The signed path creates the commit object via
commit_signed() and never updates the reference, so updateRef is silently
ignored. The unsigned path handles updateRef correctly.
The result is that one option changes the behaviour of an unrelated option,
with no error and no warning. The returned commit is unreachable from any ref.
Environment
- es-git
0.7.0(built from2222d34) - Node.js v24.15.0
- darwin arm64
Reproduction
import fs from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { initRepository } from 'es-git';
const SIG = '-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v1\n\niQEcBAABAgAGBQJTest123\n-----END PGP SIGNATURE-----';
const author = { name: 'Repro Bot', email: 'repro@example.com' };
const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'repro-'));
const repo = await initRepository(dir);
// seed commit
await fs.writeFile(path.join(dir, 'a.txt'), 'a');
let index = repo.index();
index.addPath('a.txt');
const base = repo.commit(repo.getTree(index.writeTree()), 'base', {
updateRef: 'HEAD',
author,
committer: author,
});
// second commit, signed, asking for HEAD to be updated
await fs.writeFile(path.join(dir, 'b.txt'), 'b');
index = repo.index();
index.addPath('b.txt');
const tree = repo.getTree(index.writeTree());
const oid = repo.commit(tree, 'signed commit', {
updateRef: 'HEAD',
author,
committer: author,
parents: [base],
signature: SIG,
});
console.log('returned oid :', oid);
console.log('HEAD now :', repo.head().target());
console.log('HEAD === oid :', repo.head().target() === oid); // expected true
console.log('HEAD === base :', repo.head().target() === base); // HEAD never movedActual output
returned oid : 2e1658e103d5f7fe7e1e76c30e86b22c07d53b2a
HEAD now : af8c15998c7358f18a2d540bde7452fbbd3df893
HEAD === oid : false
HEAD === base : true
Removing only the signature option from that same call gives the documented
behaviour:
returned oid : d274e2d16bc6d0389f07862384669e6ce6481b4a
HEAD now : d274e2d16bc6d0389f07862384669e6ce6481b4a
HEAD === oid : true
HEAD === base : false
The signature itself is stored correctly — extractSignature(oid) returns it.
Only the reference update is missing.
Expected
From the reference docs for commit:
If the
updateRefis notnull, name of the reference that will be updated
to point to this commit. If the reference is not direct, it will be resolved
to a direct reference. Use "HEAD" to update the HEAD of the current branch
and make it point to this commit. If the reference doesn't exist yet, it will
be created.
Root cause
src/commit.rs: update_ref is destructured at L408 but only consumed in the
else branch at L452. The signed branch (L436-449) calls
git2::Repository::commit_signed(), which wraps
git_commit_create_with_signature — that function takes no reference name and
only writes the commit object, so there is no way for it to update a ref.
Notes
docs/usage/commit.mddemonstrates exactly this combination (updateRef: 'HEAD'
together withsignature), so the published example does not do what the
surrounding text says.tests/commit.spec.ts(create signed commit) passesupdateRef: 'HEAD'but
never asserts that HEAD moved — unlike the unsigned test directly above it,
which checks viarevwalk.pushHead(). The suite passes with the bug present.
Suggested fix
After commit_signed() returns the OID, update the reference when update_ref
is Some, matching what libgit2 does internally on the unsigned path: resolve
the reference, create it if it does not exist, and set its target with a reflog
message. A regression test asserting repo.head().target() === oid on the
signed path would close the gap in the existing test.
Activity
xxxxxxjun commentedon Aug 16, 2026
I'd like to take this one. Please let me know if it's already being worked on.
marshallku commentedon Aug 18, 2026
Hi @xxxxxxjun I was working on this, but I think this could be a good first issue, so you can feel free to take this issue!
fix: Honor updateRef when creating signed commits