Last active
August 29, 2015 14:17
-
-
Save mrjjwright/8bd97fa06a810e2e2fe6 to your computer and use it in GitHub Desktop.
JSON git like repository idea
This file contains hidden or 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
Immutable = require('immutable') | |
immutableDiff = require("immutablediff") | |
sha = require("stable-sha1") | |
class Repository | |
constructor: (initialData) -> | |
@workingCopy = Immutable.fromJS(initialData) | |
@objectMap = {} | |
@author = "[email protected]" | |
@head = undefined | |
branch: (currentBranch, name) -> | |
return { | |
name: name | |
} | |
updateIn: (arr, fn) -> | |
@workingCopy = @workingCopy.updateIn(arr, fn) | |
createCommit: (message) -> | |
ref = sha(@workingCopy) | |
@objectMap[ref] = @workingCopy | |
commit = Immutable.fromJS( | |
author: @author | |
date: Date.now() | |
message: message | |
ref: ref | |
parentId: @head | |
) | |
@head = sha(commit) | |
@objectMap[@head] = commit | |
return commit | |
getObjectById: (sha) -> | |
return @objectMap[sha] | |
diffFromParent: (commitId) -> | |
childCommit = @getObjectById(commitId) | |
if childCommit? | |
parentId = childCommit.get("parentId") | |
parentCommit = @getObjectById(parentId) | |
if parentCommit? | |
childObject = @getObjectById(childCommit.get("ref")) | |
parentObject = @getObjectById(parentCommit.get("ref")) | |
if childObject? and parentObject? | |
return immutableDiff(parentObject, childObject) | |
else | |
return Immutable.fromJS([]) | |
parentIdOfCommitId: (commitId) -> | |
commit = @getObjectById(commitId) | |
if commit? | |
commit.get("parentId") | |
dropboxRoot = "/Users/jwright/Dropbox/Disco" | |
project = | |
name: "Disco" | |
team: [] | |
ui: | |
components: [ | |
BuyIcon: | |
type: "Image" | |
image: "#{dropboxRoot}/images/buy.png" | |
] | |
screens:[ | |
name: "home" | |
root: | |
backgroundColor: "#fffff" | |
subviews:[ | |
label: | |
type: "BuyIcon" | |
] | |
] | |
repository = new Repository(project) | |
# Initial Commit | |
repository.createCommit("Initial") | |
# Add some team members | |
repository.updateIn(["team"], (team) -> | |
team.push("[email protected]") | |
) | |
repository.updateIn(["team"], (team) -> | |
team.push("[email protected]") | |
) | |
# Create another commit | |
repository.createCommit("Add team members") | |
diff = repository.diffFromParent(repository.head) | |
json = JSON.stringify(diff, 0, 2) | |
console.log(json) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment