Skip to content

Instantly share code, notes, and snippets.

@chrisdickinson
Last active December 22, 2015 22:59
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 chrisdickinson/6544035 to your computer and use it in GitHub Desktop.
Save chrisdickinson/6544035 to your computer and use it in GitHub Desktop.
git-fs-repo load file at path and hash
var repo = require('./index')
, path = require('path')
, dir = path.resolve(
__dirname
, '..'
, '..'
, 'personal'
, 'plate'
, '.git'
)
repo(dir, function(err, git) {
load(git, git.ref('HEAD').hash, 'lib/tags/for.js', function(err, obj) {
// there'll either be an `err`, an `obj`, or absolutely nothing (if
// we couldn't find the last object.)
console.log(err, obj.data() + '')
})
})
function load(find, hash, filename, ready) {
var bits = filename.split(path.sep)
, last
// load the commit, find the tree hash associated
// with it, then start iterating the path.
find(hash, function(err, commit) {
if(err || commit.looseType !== 'commit') {
return ready(
err || new Error('expected commit, got ' + commit.looseType)
)
}
last = hash
find(commit.tree(), iter)
})
// for each bit, find the next member bit.
// fail if we've either looked something up that's
// not a tree (we can't recurse any further!) or
// if the tree doesn't contain the next step.
function iter(err, tree) {
if(err || !tree || tree.looseType !== 'tree') {
return ready(
err || new Error('could not find ' + last)
)
}
var next = bits.shift()
, entry
entry = tree.entries().filter(function(item) {
return item.name === next
})[0]
if(!entry) {
return ready(
err || new Error('could not find ' + last)
)
}
last = last + '/' + entry.name
// if there are no more bits left in the path,
// we're done! pass whatever comes out of `find`
// this time to the outer `ready` function.
find(entry.hash, bits.length ? iter : ready)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment