From a2343785ccf32e8ad38fb0127e760fb347a35bf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Magiera?= Date: Tue, 5 Sep 2017 16:18:27 +0200 Subject: [PATCH] Create git-ipld example --- examples/traverse-ipld-graphs/README.md | 2 + .../0f328c91df28c5c01b9e9f9f7e663191fa156593 | Bin 0 -> 186 bytes .../177bf18bc707d82b21cdefd0b43b38fc8c5c13fe | Bin 0 -> 22 bytes .../23cc25f631cb076d5de5036c87678ea713cbaa6a | Bin 0 -> 29 bytes .../4e425dba7745a781f0712c9a01455899e8c0c249 | Bin 0 -> 72 bytes .../6850c7be7136e6be00976ddbae80671b945c3e9d | Bin 0 -> 150 bytes .../a5095353cd62a178663dd26efc2d61f4f61bccbe | Bin 0 -> 41 bytes .../dc9bd15e8b81b6565d3736f9c308bd1bba60f33a | Bin 0 -> 72 bytes .../e68e6f6e31857877a79fd6b3956898436bb5a76f | Bin 0 -> 233 bytes .../ee62b3d206cb23f939208898f32d8708c0e3fa3c | Bin 0 -> 41 bytes .../ee71cef5001b84b0314438f76cf0acd338a2fd21 | Bin 0 -> 28 bytes examples/traverse-ipld-graphs/git.js | 74 ++++++++++++++++++ 12 files changed, 76 insertions(+) create mode 100644 examples/traverse-ipld-graphs/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593 create mode 100644 examples/traverse-ipld-graphs/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe create mode 100644 examples/traverse-ipld-graphs/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a create mode 100644 examples/traverse-ipld-graphs/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249 create mode 100644 examples/traverse-ipld-graphs/git-objects/6850c7be7136e6be00976ddbae80671b945c3e9d create mode 100644 examples/traverse-ipld-graphs/git-objects/a5095353cd62a178663dd26efc2d61f4f61bccbe create mode 100644 examples/traverse-ipld-graphs/git-objects/dc9bd15e8b81b6565d3736f9c308bd1bba60f33a create mode 100644 examples/traverse-ipld-graphs/git-objects/e68e6f6e31857877a79fd6b3956898436bb5a76f create mode 100644 examples/traverse-ipld-graphs/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c create mode 100644 examples/traverse-ipld-graphs/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21 create mode 100644 examples/traverse-ipld-graphs/git.js diff --git a/examples/traverse-ipld-graphs/README.md b/examples/traverse-ipld-graphs/README.md index a9fdd28d0a..3cbba0849c 100644 --- a/examples/traverse-ipld-graphs/README.md +++ b/examples/traverse-ipld-graphs/README.md @@ -17,6 +17,8 @@ This tutorial goes through several operations over IPLD graphs using the [DAG AP ## [traverse through a slice of the ethereum blockchain](./eth.js) +## [traverse through a git repo](./git.js) + ## Video of the demos Find a video with a walkthrough of this examples on Youtube: diff --git a/examples/traverse-ipld-graphs/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593 b/examples/traverse-ipld-graphs/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593 new file mode 100644 index 0000000000000000000000000000000000000000..4fc8373bcd9be6ee605436668ca20a3b839826a7 GIT binary patch literal 186 zcmb7-O$x#=5QSam6tfp((wJ!iMO?ViTjXa-BTYw21dngP6S#Z4kN3SKwU!2Yw1>7c zLry+sZP`VqBU!T9jD}{P47v_t41}{WL4|i)WQT_=8{C9pdiIosulx!1wel%Ry@0lO bChciJIO2e)dfWdR|G!lCt+WzW_$kyE+mJZY literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe b/examples/traverse-ipld-graphs/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe new file mode 100644 index 0000000000000000000000000000000000000000..b762482e170f3501ec3d4b2ffbf8542543a42941 GIT binary patch literal 22 dcmYew$xl)+G+}VhEK%?b@NrQ{NzKjY0su;N24MgI literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a b/examples/traverse-ipld-graphs/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a new file mode 100644 index 0000000000000000000000000000000000000000..d50e6d186b399089c91d990b04d795bb99f898e7 GIT binary patch literal 29 kcmYew$xl)+GGs_i&d66#_ejmj$=6W`&o9bJQB>yw0EN8>qW}N^ literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249 b/examples/traverse-ipld-graphs/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249 new file mode 100644 index 0000000000000000000000000000000000000000..f6de31c9fc49d2d041ff559f0f37166e31c652fb GIT binary patch literal 72 zcmXRZN=;QTGhr|^FfcPQQ3!H%bn$g%5U>8&eVqMj^KE>Tl$StL}B_YPa$w|;*35M z#6w1b(cU`mTyX6;NHnb#-}=T#j5atKRsL+CKCSC5t6Uy0U2(-fPI%BcUGH>Cpl&W2 f5{`I>bz4xC8&eVqMw2+xBI`Q|Ci=o)~$XNCY|SM Ee(tzLtN;K2 literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c b/examples/traverse-ipld-graphs/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c new file mode 100644 index 0000000000000000000000000000000000000000..78a038b49be60bc37986ac28bea9c7fdd8182d6a GIT binary patch literal 41 xcmXRZN=;QTHfAt1FfcbYRme!q$;oF>KBM~0@HBgF>{I5P_Vm8x!lzed0RRmp4&49% literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21 b/examples/traverse-ipld-graphs/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21 new file mode 100644 index 0000000000000000000000000000000000000000..27296bc7a78359868dda94a9423c666ba5376af2 GIT binary patch literal 28 jcmYew$xl)+GGIte&d66#&q&S5$yX@PFUm<#RObQ!fU*d- literal 0 HcmV?d00001 diff --git a/examples/traverse-ipld-graphs/git.js b/examples/traverse-ipld-graphs/git.js new file mode 100644 index 0000000000..c52c7ecd46 --- /dev/null +++ b/examples/traverse-ipld-graphs/git.js @@ -0,0 +1,74 @@ +'use strict' + +const createNode = require('./create-node.js') +const asyncEach = require('async/each') +const path = require('path') +const multihashing = require('multihashing-async') +const Block = require('ipfs-block') +const CID = require('cids') +const fs = require('fs') + +createNode((err, ipfs) => { + if (err) { + throw err + } + + console.log('\nStart of the example:') + + const gitObjects = [ + path.join(__dirname, '/git-objects/0f328c91df28c5c01b9e9f9f7e663191fa156593'), + path.join(__dirname, '/git-objects/177bf18bc707d82b21cdefd0b43b38fc8c5c13fe'), + path.join(__dirname, '/git-objects/23cc25f631cb076d5de5036c87678ea713cbaa6a'), + path.join(__dirname, '/git-objects/4e425dba7745a781f0712c9a01455899e8c0c249'), + path.join(__dirname, '/git-objects/6850c7be7136e6be00976ddbae80671b945c3e9d'), + path.join(__dirname, '/git-objects/a5095353cd62a178663dd26efc2d61f4f61bccbe'), + path.join(__dirname, '/git-objects/dc9bd15e8b81b6565d3736f9c308bd1bba60f33a'), + path.join(__dirname, '/git-objects/e68e6f6e31857877a79fd6b3956898436bb5a76f'), + path.join(__dirname, '/git-objects/ee62b3d206cb23f939208898f32d8708c0e3fa3c'), + path.join(__dirname, '/git-objects/ee71cef5001b84b0314438f76cf0acd338a2fd21') + ] + + asyncEach(gitObjects, (gitObjectsPath, cb) => { + const data = fs.readFileSync(gitObjectsPath) + + multihashing(data, 'sha1', (err, multihash) => { + if (err) { + return cb(err) + } + const cid = new CID(1, 'git-raw', multihash) + console.log(cid.toBaseEncodedString()) + + ipfs.block.put(new Block(data, cid), cb) + }) + }, (err) => { + if (err) { + throw err + } + + const v1tag = 'z8mWaGfwSWLMPJ6Q2JdsAjGiXTf61Nbue' + + function errOrLog(comment) { + return (err, result) => { + if (err) { + throw err + } + + if (Buffer.isBuffer(result.value)) { //Blobs (files) are returned as buffer instance + result.value = result.value.toString() + } + + console.log('-'.repeat(80)) + console.log(comment) + console.log(result.value) + } + } + + + ipfs.dag.get(v1tag + '/', errOrLog('Tag object:')) + ipfs.dag.get(v1tag + '/object/message', errOrLog('Tagged commit message:')) + ipfs.dag.get(v1tag + '/object/parents/0/message', errOrLog('Parent of tagged commit:')) + + ipfs.dag.get(v1tag + '/object/tree/src/hash/hello/hash', errOrLog('/src/hello file:')) + ipfs.dag.get(v1tag + '/object/parents/0/tree/src/hash/hello/hash', errOrLog('previous version of /src/hello file:')) + }) +})