Skip to content

Commit 85738f3

Browse files
author
Dan Butvinik
committed
sign commit and update HEAD
1 parent e68e3ce commit 85738f3

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

index.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,45 @@ const fs = require('fs')
55

66
async function sign(message, privateKey, passphrase) {
77
const repo = await git.Clone(
8-
'https://github.com/dabutvin/test',
8+
'https://github.com/dabutvin/deepstream',
99
'/private/tmp/test'
1010
)
1111

1212
fs.writeFileSync('/private/tmp/test/nothing', 'data')
1313
const index = await repo.refreshIndex()
1414
await index.addAll()
1515
await index.write()
16-
const oid = await index.writeTree()
16+
const oid = await index.writeTreeTo(repo)
17+
const branch = await repo.getBranch('HEAD')
1718
const head = await git.Reference.nameToId(repo, 'HEAD')
1819
const parent = await repo.getCommit(head)
1920
const author = git.Signature.now('test', '[email protected]')
2021
const committer = git.Signature.now('test', '[email protected]')
21-
const commitId = await repo.createCommit(
22-
'HEAD',
22+
const commit = await repo.createCommitWithSignature(
2323
author,
2424
committer,
25-
'message',
25+
message,
2626
oid,
27-
[parent]
27+
[parent],
28+
'gpgsig',
29+
onSignature
2830
)
2931

30-
const privateKeyResult = (await openpgp.key.readArmored(privateKey)).keys[0]
31-
if (!privateKeyResult) throw new Error('unable to read private key')
32-
await privateKeyResult.decrypt(passphrase)
33-
const options = {
34-
message: openpgp.cleartext.fromText(message),
35-
privateKeys: [privateKeyResult],
36-
detached: true
32+
await branch.setTarget(commit.toString(), 'updating head')
33+
return commit.toString()
34+
35+
async function onSignature(tosign) {
36+
const privateKeyResult = (await openpgp.key.readArmored(privateKey)).keys[0]
37+
if (!privateKeyResult) throw new Error('unable to read private key')
38+
await privateKeyResult.decrypt(passphrase)
39+
const options = {
40+
message: openpgp.cleartext.fromText(tosign),
41+
privateKeys: [privateKeyResult],
42+
detached: true
43+
}
44+
const signed = await openpgp.sign(options)
45+
const signature = signed.signature.replace(/\r\n/g, '\n')
46+
return signature
3747
}
38-
const signed = await openpgp.sign(options)
39-
const signature = signed.signature.replace(/\r\n/g, '\n')
40-
return signature
4148
}
42-
4349
module.exports = sign

0 commit comments

Comments
 (0)