Created
January 15, 2016 23:44
-
-
Save tehbeard/c19cd9a0d88797a9b01a to your computer and use it in GitHub Desktop.
Experiments with js-git
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
Trying to understand generators and yield, and create a listRefs() implementation for file backed repos. |
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
"use strict"; | |
var bodec = require('bodec'); | |
var path = require('path'); | |
var run = require('gen-run'); | |
module.exports = function (repo, fs) { | |
repo.listRefs = listRefs; | |
function _readPath(_path, cb) { | |
if (!cb) { return _readPath.bind(this, _path); } | |
run(function* () { | |
var refPackFilePath = path.join(repo.rootPath, 'packed-refs'); | |
var refFolderPath = path.join(repo.rootPath, _path); | |
var foundPaths = []; | |
try{ | |
var packedRefRawData = yield fs.readFile.bind(this, refPackFilePath); | |
var refPackEntries = bodec.toRaw(packedRefRawData) | |
.split("\n") | |
.filter((e)=> e.length > 0 && e.charAt(0) != '#') | |
.map((e)=>e.split(" ")[1]); | |
foundPaths = refPackEntries; | |
}catch(e){} | |
try { | |
var subPaths = yield fs.readDir.bind(this, refFolderPath); | |
if(subPaths){ | |
for( let subPath of subPaths){ | |
var _underPaths = yield _readPath(_path + "/" + subPath); | |
foundPaths = foundPaths.concat(_underPaths); | |
} | |
} | |
cb(null, foundPaths); | |
} catch (e) { | |
console.log("ERR", e); | |
cb(null, [_path]); | |
} | |
}); | |
} | |
function listRefs(cb) { | |
if (!cb) { return listRefs.bind(this); } | |
run(function* (){ | |
var refs = yield _readPath('refs'); | |
var refMap = {}; | |
for( let ref of refs){ | |
refMap[ref] = yield repo.readRef(ref); | |
} | |
cb(null, refMap); | |
}); | |
} | |
} |
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
var path = require('path'); | |
var run = require('gen-run'); | |
// This provides symbolic names for the octal modes used by git trees. | |
var modes = require('js-git/lib/modes'); | |
// Create a repo by creating a plain object. | |
var repo = { | |
// rootPath: path.join('E:/projects/es6mvc/.git/') | |
rootPath: path.join('E:/workspace/BeardStat/.git') | |
}; | |
require('./refs')(repo, require("git-node-fs")) | |
require('js-git/mixins/fs-db')( | |
repo, | |
require("git-node-fs") | |
); | |
require('js-git/mixins/create-tree')(repo); | |
require('js-git/mixins/pack-ops')(repo); | |
require('js-git/mixins/walkers')(repo); | |
require('js-git/mixins/read-combiner')(repo); | |
require('js-git/mixins/formats')(repo); | |
console.log(repo); | |
repo.listRefs(console.log.bind(console)); | |
// run(function* () { | |
// var commitHash = yield repo.readRef('refs/heads/master'); | |
// var commit = yield repo.loadAs("commit", commitHash); | |
// var walker = yield repo.treeWalk(commit.tree); | |
// var file; | |
// while (file = yield walker.read(), file !== undefined) { | |
// if (modes.isFile(file.mode)) { | |
// var msg = `${file.path} - ${modes.toType(file.mode) }` | |
// console.log(msg); | |
// } | |
// } | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment