Last active
December 22, 2015 22:59
-
-
Save chrisdickinson/6544035 to your computer and use it in GitHub Desktop.
git-fs-repo load file at path and hash
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
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