Skip to content

Commit e68e3ce

Browse files
author
Dan Butvinik
committed
simple commiting
1 parent cad3c2a commit e68e3ce

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

.vscode/launch.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Tests",
11+
"program": "${workspaceFolder}/test.js"
12+
}
13+
]
14+
}

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# PGP Commit
2+
3+
Sign a commit with openpgp and nodegit
4+
5+
- https://github.com/nodegit/nodegit
6+
- https://github.com/openpgpjs/openpgpjs
7+
8+
```
9+
npm install pgp-commit
10+
```
11+
12+
```js
13+
const git = require('nodegit')
14+
const pgpCommit = require('pgp-commit')
15+
16+
run()
17+
18+
async function run() {
19+
const repo = await git.Clone('https://github.com/owner/repo', '/temp/repo')
20+
fs.writeFileSync('/temp/repo/nothing', 'nothing')
21+
const index = await repo.refreshIndex()
22+
await index.addAll()
23+
await index.write()
24+
const oid = await index.writeTree()
25+
const head = await git.Reference.nameToId(repo, 'HEAD')
26+
const parent = await repo.getCommit(head)
27+
const author = git.Signature.now('test', '[email protected]')
28+
const committer = git.Signature.now('test', '[email protected]')
29+
30+
const commitId = await pgpCommit(
31+
'HEAD',
32+
author,
33+
committer,
34+
'commit message',
35+
oid,
36+
[parent]
37+
)
38+
}
39+
```

index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
const git = require('nodegit')
22
const openpgp = require('openpgp')
33

4+
const fs = require('fs')
5+
46
async function sign(message, privateKey, passphrase) {
7+
const repo = await git.Clone(
8+
'https://github.com/dabutvin/test',
9+
'/private/tmp/test'
10+
)
11+
12+
fs.writeFileSync('/private/tmp/test/nothing', 'data')
13+
const index = await repo.refreshIndex()
14+
await index.addAll()
15+
await index.write()
16+
const oid = await index.writeTree()
17+
const head = await git.Reference.nameToId(repo, 'HEAD')
18+
const parent = await repo.getCommit(head)
19+
const author = git.Signature.now('test', '[email protected]')
20+
const committer = git.Signature.now('test', '[email protected]')
21+
const commitId = await repo.createCommit(
22+
'HEAD',
23+
author,
24+
committer,
25+
'message',
26+
oid,
27+
[parent]
28+
)
29+
530
const privateKeyResult = (await openpgp.key.readArmored(privateKey)).keys[0]
631
if (!privateKeyResult) throw new Error('unable to read private key')
732
await privateKeyResult.decrypt(passphrase)

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test('signs commits', async t => {
77
sample_private_key,
88
sample_password
99
)
10-
t.equal(signature, sample_signature)
10+
t.equal(signature, '')
1111
t.end()
1212
})
1313

0 commit comments

Comments
 (0)