Skip to content

Instantly share code, notes, and snippets.

@mrjjwright
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrjjwright/8bd97fa06a810e2e2fe6 to your computer and use it in GitHub Desktop.
Save mrjjwright/8bd97fa06a810e2e2fe6 to your computer and use it in GitHub Desktop.
JSON git like repository idea
Immutable = require('immutable')
immutableDiff = require("immutablediff")
sha = require("stable-sha1")
class Repository
constructor: (initialData) ->
@workingCopy = Immutable.fromJS(initialData)
@objectMap = {}
@author = "mrjjwright@gmail.com"
@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("jeff@afj.com")
)
repository.updateIn(["team"], (team) ->
team.push("clarke@afj.com")
)
# 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