Skip to content

Commit 049fbd0

Browse files
author
Dan Butvinik
committed
split up and add clone-and-commit example
1 parent 85738f3 commit 049fbd0

File tree

6 files changed

+115
-38
lines changed

6 files changed

+115
-38
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"type": "node",
99
"request": "launch",
1010
"name": "Tests",
11-
"program": "${workspaceFolder}/test.js"
11+
"program": "${workspaceFolder}/test/index.js"
1212
}
1313
]
1414
}

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,44 @@ Sign a commit with openpgp and nodegit
55
- https://github.com/nodegit/nodegit
66
- https://github.com/openpgpjs/openpgpjs
77

8+
## Install
9+
810
```
911
npm install pgp-commit
1012
```
1113

14+
## Example
15+
16+
See more examples in examples folder
17+
1218
```js
1319
const git = require('nodegit')
1420
const pgpCommit = require('pgp-commit')
1521

1622
run()
1723

1824
async function run() {
25+
// clone a repo
1926
const repo = await git.Clone('https://github.com/owner/repo', '/temp/repo')
27+
const commitMessage = 'add data to something'
28+
const author = git.Signature.now('test', '[email protected]')
29+
const committer = git.Signature.now('test', '[email protected]')
30+
31+
// make a change
2032
fs.writeFileSync('/temp/repo/nothing', 'nothing')
33+
34+
// stage the change
2135
const index = await repo.refreshIndex()
2236
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]')
2937

30-
const commitId = await pgpCommit(
31-
'HEAD',
38+
// make a signed commit
39+
const commitId = await pgpCommit({
40+
repo,
41+
commitMessage,
3242
author,
3343
committer,
34-
'commit message',
35-
oid,
36-
[parent]
37-
)
44+
passphrase,
45+
privateKey
46+
})
3847
}
3948
```

examples/clone-and-commit.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const git = require('nodegit')
2+
const fs = require('fs')
3+
const pgpCommit = require('..')
4+
5+
async function run() {
6+
// clone a repo
7+
const repo = await git.Clone(
8+
'https://github.com/dabutvin/deepstream',
9+
'/private/tmp/test'
10+
)
11+
const commitMessage = 'add data to something'
12+
const author = git.Signature.now('test', '[email protected]')
13+
const committer = git.Signature.now('test', '[email protected]')
14+
15+
// write a file to the repo
16+
fs.writeFileSync('/private/tmp/test/nothing', 'data\ndata\ndata')
17+
18+
// add to the stage
19+
const index = await repo.refreshIndex()
20+
await index.addAll()
21+
22+
// do the signed commit in place
23+
const commitId = await pgpCommit({
24+
repo,
25+
commitMessage,
26+
author,
27+
committer,
28+
passphrase: sample_password,
29+
privateKey: sample_private_key
30+
})
31+
console.log('made commit: ' + commitId)
32+
}
33+
34+
run()
35+
36+
const sample_private_key = `-----BEGIN PGP PRIVATE KEY BLOCK-----
37+
Version: BCPG C# v1.6.1.0
38+
39+
lQOsBFwSkAQBCACBfC6sDgn9A1Hv3y279+YNlqOeETB7zFw5flCAKWlw3osUSnft
40+
F+EdgTqH/sVxrxXaqfRpWNDHYD/WG/etSYiP8oaTsM9jWlH3gRQnXm/W1asku2fZ
41+
nVFJU0kJUWmwSHBVX/5/0qGCohIshMxuQXr73zYdoOj5G7Sdwzr7saoi8h//QK7G
42+
xbq8TZzLsPHELXhL9NKC7RGRi6ig1Jx6xHxu3Zx0ICtZCKshGtTsgMCKi7DYfRWj
43+
IbteGltrA6aMKWDjn68waxQXidkfwAS5mC5g04V1CaYKEgyefmlaZGJ4GQqjYGId
44+
g40K5koZJdjGYDQGms1SajguMn1lzwzuAhhdABEBAAH/AwMC57O2AZ40siBgwrbS
45+
lX8323pjaX5ke16EOb8lcaV0HK9lDVQ4iOoLq4QbyLRRexBVCQ/dxYbp9/Yc1R3h
46+
L/A7aBVykdG15tbQXg3DoTcvKvzkL0bW51NavxpXItmW4RAyWsZ3rlD9ADcceaex
47+
cysr6A2lZ4Ao+7wBG+rHc+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OIlwXXhd7/nOJjFXnjsZ7hTRu3danit7/dl5F
64+
s0IzD2i+KIfn2dH5wDqs0pPbvhJIWp/i3u4Q+kL4DxdNoqXog8+82qr5b6Is
65+
=rqAt
66+
-----END PGP PRIVATE KEY BLOCK-----`
67+
68+
const sample_password = 'testpassword'

index.js

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
const git = require('nodegit')
22
const openpgp = require('openpgp')
33

4-
const fs = require('fs')
5-
6-
async function sign(message, privateKey, passphrase) {
7-
const repo = await git.Clone(
8-
'https://github.com/dabutvin/deepstream',
9-
'/private/tmp/test'
10-
)
11-
12-
fs.writeFileSync('/private/tmp/test/nothing', 'data')
4+
async function sign({
5+
repo,
6+
author,
7+
committer,
8+
commitMessage,
9+
privateKey,
10+
passphrase
11+
}) {
1312
const index = await repo.refreshIndex()
14-
await index.addAll()
15-
await index.write()
1613
const oid = await index.writeTreeTo(repo)
17-
const branch = await repo.getBranch('HEAD')
14+
const currentBranch = await repo.getBranch('HEAD')
1815
const head = await git.Reference.nameToId(repo, 'HEAD')
1916
const parent = await repo.getCommit(head)
20-
const author = git.Signature.now('test', '[email protected]')
21-
const committer = git.Signature.now('test', '[email protected]')
2217
const commit = await repo.createCommitWithSignature(
2318
author,
2419
committer,
25-
message,
20+
commitMessage,
2621
oid,
2722
[parent],
2823
'gpgsig',
2924
onSignature
3025
)
31-
32-
await branch.setTarget(commit.toString(), 'updating head')
26+
await currentBranch.setTarget(commit.toString(), 'updating head')
3327
return commit.toString()
3428

3529
async function onSignature(tosign) {
@@ -46,4 +40,5 @@ async function sign(message, privateKey, passphrase) {
4640
return signature
4741
}
4842
}
43+
4944
module.exports = sign

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "PGP signing of commits with openpgp and nodegit",
55
"main": "index.js",
66
"scripts": {
7-
"test": "node test.js"
7+
"test": "node test/index.js"
88
},
99
"author": "dabutvin",
1010
"license": "MIT",

test.js renamed to test/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
const test = require('tape')
2-
const pgpCommit = require('.')
2+
const git = require('nodegit')
3+
const pgpCommit = require('..')
34

45
test('signs commits', async t => {
5-
let signature = await pgpCommit(
6-
'message',
7-
sample_private_key,
8-
sample_password
9-
)
10-
t.equal(signature, '')
6+
const author = git.Signature.now('test', '[email protected]')
7+
let commitId = await pgpCommit({
8+
author,
9+
committer: author,
10+
commitMessage: 'message',
11+
passphrase: sample_password,
12+
privateKey: sample_private_key,
13+
repo: null
14+
})
15+
t.equal(commitId, 'e854cfcc3b5d2b7eb4a235c671c37b9b3059b668')
1116
t.end()
1217
})
1318

0 commit comments

Comments
 (0)