Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 8e96b1c

Browse files
committed
Create git-ipld example
1 parent bfc58d6 commit 8e96b1c

11 files changed

+74
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

examples/traverse-ipld-graphs/git.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict'
2+
3+
const createNode = require('./create-node.js')
4+
const asyncEach = require('async/each')
5+
const path = require('path')
6+
const multihashing = require('multihashing-async')
7+
const Block = require('ipfs-block')
8+
const CID = require('cids')
9+
const fs = require('fs')
10+
11+
createNode((err, ipfs) => {
12+
if (err) {
13+
throw err
14+
}
15+
16+
console.log('\nStart of the example:')
17+
18+
const gitObjects = [
19+
path.join(__dirname, '/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593'),
20+
path.join(__dirname, '/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe'),
21+
path.join(__dirname, '/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a'),
22+
path.join(__dirname, '/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249'),
23+
path.join(__dirname, '/git-objects/6850c7be7136e6be00976ddbae80671b945c3e9d'),
24+
path.join(__dirname, '/git-objects/a5095353cd62a178663dd26efc2d61f4f61bccbe'),
25+
path.join(__dirname, '/git-objects/dc9bd15e8b81b6565d3736f9c308bd1bba60f33a'),
26+
path.join(__dirname, '/git-objects/e68e6f6e31857877a79fd6b3956898436bb5a76f'),
27+
path.join(__dirname, '/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c'),
28+
path.join(__dirname, '/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21')
29+
]
30+
31+
asyncEach(gitObjects, (gitObjectsPath, cb) => {
32+
const data = fs.readFileSync(gitObjectsPath)
33+
34+
multihashing(data, 'sha1', (err, multihash) => {
35+
if (err) {
36+
cb(err)
37+
}
38+
const cid = new CID(1, 'git-raw', multihash)
39+
console.log(cid.toBaseEncodedString())
40+
41+
ipfs.block.put(new Block(data, cid), cb)
42+
})
43+
}, (err) => {
44+
if (err) {
45+
throw err
46+
}
47+
48+
const v1tag = 'z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue'
49+
50+
function errOrLog(comment) {
51+
return (err, result) => {
52+
if (err) {
53+
throw err
54+
}
55+
56+
if (Buffer.isBuffer(result.value)) { //Blobs (files) are returned as buffer instance
57+
result.value = result.value.toString()
58+
}
59+
60+
console.log('-'.repeat(80))
61+
console.log(comment)
62+
console.log(result.value)
63+
}
64+
}
65+
66+
67+
ipfs.dag.get(v1tag + '/', errOrLog('Tag object:'))
68+
ipfs.dag.get(v1tag + '/object/message', errOrLog('Tagged commit message:'))
69+
ipfs.dag.get(v1tag + '/object/parents/0/message', errOrLog('Parent of tagged commit:'))
70+
71+
ipfs.dag.get(v1tag + '/object/tree/src/hash/hello/hash', errOrLog('/src/hello file:'))
72+
ipfs.dag.get(v1tag + '/object/parents/0/tree/src/hash/hello/hash', errOrLog('previous version of /src/hello file:'))
73+
})
74+
})

0 commit comments

Comments
 (0)