This repository has been archived by the owner on Oct 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
js-git-ipld <-> pando library #53
Closed
Closed
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
65d0bab
refactor
corydickson c418140
refactor
corydickson b4756c7
remove js file
corydickson c7ea229
add test file
corydickson a0b85cb
v0.3.0
corydickson 1da5696
test boilerplates
corydickson 3791e87
Fix dependency issues
osarrouy eb13479
try to get dag-cbor in test
corydickson 34aa323
refactor testing framework
corydickson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import { IPLD } from 'ipld' | ||
import IPFS from 'ipfs-http-client' | ||
import IPLDGit from 'ipld-git' | ||
import CID from 'cids' | ||
import { cidToSha } from 'ipld-git/src/util/util.js' | ||
import orderBy from 'lodash.orderby' | ||
import uniqWith from 'lodash.uniqwith' | ||
|
||
const ipfs = IPFS({ host: 'ipfs.infura.io', port: '5001', protocol: 'https' }) | ||
const ipld = new IPLD({ blockService: ipfs.block, formats: [IPLDGit] }) | ||
|
||
export interface IAuthorOrCommitter { | ||
name: string | ||
email: string | ||
date: string | ||
} | ||
|
||
export interface ITree { [path: string]: string } | ||
|
||
export interface ILinkedDataCommit { | ||
author: IAuthorOrCommitter | ||
committer: IAuthorOrCommitter | ||
message: string | ||
parents: ITree[] | null | ||
tree: ITree | ||
} | ||
|
||
export interface IModifiedTree { | ||
[path: string]: { | ||
lastEdit: { | ||
date: string | ||
message: string | ||
} | ||
mode: string | number | ||
blob: string | ||
} | ||
} | ||
|
||
export const fetchFilesFromTree = (hash: string, path?: string) => { | ||
return new Promise(async (resolve, reject) => { | ||
let files = {} | ||
const cid = new CID(hash) | ||
|
||
ipld.get(cid, async (err, result) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
const tree = result.value | ||
for (let entry in tree) { | ||
const _path = path ? path + '/' + entry : entry | ||
if (tree[entry].mode === '40000') { | ||
const _files = await fetchFilesFromTree(tree[entry]['hash']['/'], _path) | ||
files = { ...files, ..._files } | ||
} else { | ||
files[_path].lastEdit = { | ||
date: tree[entry]['committer'].date, | ||
message: tree[entry]['message'] | ||
} | ||
files[_path].mode = tree[entry].mode | ||
files[_path].blob = new CID(tree[entry]['hash']['/']).toBaseEncodedString() | ||
} | ||
} | ||
resolve(files as IModifiedTree) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
export const fetchHistory = async (hash:string, history: Commit[]): Promise<Commit[]> => { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const commit = await fetchCommit(hash) | ||
history.push(commit) | ||
|
||
for (let parent of commit.parents) { | ||
history = await fetchHistory(parent['/'], history) | ||
} | ||
|
||
resolve(history) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
} | ||
|
||
export const fetchCommit = async (hash: string): Promise<Commit> => { | ||
return new Promise((resolve, reject) => { | ||
try { | ||
const cid = new CID(hash) | ||
ipld.get(cid, async (err, result) => { | ||
if (err) { | ||
reject(err) | ||
} else { | ||
const IPLDCommit = result.value as ILinkedDataCommit | ||
const commit = new Commit(IPLDCommit) | ||
await commit.fetchModifiedTree() | ||
resolve(commit) | ||
} | ||
}) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
} | ||
|
||
export const fetchOrderedHistory = async (hash: string, formerHistory: Commit[]) => { | ||
let history = await fetchHistory(hash, formerHistory) | ||
history = uniqWith(history, (one, two) => { | ||
return one.cid === two.cid | ||
}) | ||
|
||
history = orderBy( | ||
history, | ||
[ | ||
commit => { | ||
// https://github.com/git/git/blob/v2.3.0/Documentation/date-formats.txt | ||
/* eslint-disable-next-line no-unused-vars */ | ||
const [timestamp, offset] = commit.author.date.split(' ') | ||
return timestamp | ||
}, | ||
], | ||
['desc'] | ||
) | ||
|
||
return history | ||
} | ||
|
||
export default class Commit { | ||
cid: string | ||
sha: string | ||
author: IAuthorOrCommitter | ||
committer: IAuthorOrCommitter | ||
message: string | ||
parents: ITree[] | string[] | ||
tree: ITree | IModifiedTree | {} | ||
|
||
constructor(commit: ILinkedDataCommit) { | ||
if (commit.tree.hasOwnProperty('/')) { | ||
const _cid = new CID(commit.tree['/']) | ||
let _parents : string[] = commit.parents ? | ||
commit.parents.map(parent => (new CID(Object.values(parent)[0]).toBaseEncodedString)) : [] | ||
this.cid = _cid | ||
this.sha = cidToSha(commit.tree['/']).toString('hex') | ||
this.author = commit.author | ||
this.committer = commit.committer | ||
this.message = commit.message | ||
this.tree = commit.tree | ||
this.parents = _parents | ||
} | ||
} | ||
|
||
public async fetchModifiedTree(): Promise<void> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I came upon this solution given that we couldn't traverse the |
||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const files = await fetchFilesFromTree(this.tree['/']) | ||
this.tree = files | ||
resolve() | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
|
||
} | ||
|
||
public async getOrderdHistory(): Promise<Commit[]> { | ||
return new Promise(async (resolve, reject) => { | ||
try { | ||
const orderedHistory = await fetchOrderedHistory(this.cid, []) | ||
resolve(orderedHistory) | ||
} catch (err) { | ||
reject(err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
//export function convertCommitToIPLDCommit(commit: Commit): ILinkedDataCommit {} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,22 +37,7 @@ app.store(async (state, event) => { | |
switch (event.event) { | ||
case 'UpdateRef': | ||
try { | ||
let history = await fetchHistory(event.returnValues.hash, []) | ||
history = uniqWith(history, (one, two) => { | ||
return one.cid === two.cid | ||
}) | ||
history = orderBy( | ||
history, | ||
[ | ||
commit => { | ||
// https://github.com/git/git/blob/v2.3.0/Documentation/date-formats.txt | ||
/* eslint-disable-next-line no-unused-vars */ | ||
const [timestamp, offset] = commit.author.date.split(' ') | ||
return timestamp | ||
}, | ||
], | ||
['desc'] | ||
) | ||
let history = await fetchOrderedHistory(event.returnValues.hash, []) | ||
state.branches[branchFromRef(event.returnValues.ref)] = history | ||
} catch (err) { | ||
console.error('Failed to load commit history due to:', err) | ||
|
@@ -156,3 +141,25 @@ const fetchFilesFromTree = (hash, path) => { | |
}) | ||
}) | ||
} | ||
|
||
const fetchOrderedHistory = async (hash, formerHistory) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the final version of this PR, we'll deprecate all of these helper functions in this file and import them from |
||
let history = await fetchHistory(hash, formerHistory) | ||
history = uniqWith(history, (one, two) => { | ||
return one.cid === two.cid | ||
}) | ||
|
||
history = orderBy( | ||
history, | ||
[ | ||
commit => { | ||
// https://github.com/git/git/blob/v2.3.0/Documentation/date-formats.txt | ||
/* eslint-disable-next-line no-unused-vars */ | ||
const [timestamp, offset] = commit.author.date.split(' ') | ||
return timestamp | ||
}, | ||
], | ||
['desc'] | ||
) | ||
|
||
return history | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"exclude": ["node_modules", "**/*.spec.ts"], | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"module": "CommonJS", | ||
"outDir": "./build", | ||
"target": "ES2017", | ||
"allowJs": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": ["tslint:latest", "tslint-config-prettier"], | ||
"rules": { "curly": [true, "ignore-same-line"], "interface-name": false, "variable-name": false } | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it supposed to be :
parents: ILinkedCommitData[]
?