Permalink
Browse files

add minimatch for filename matching

  • Loading branch information...
1 parent 304268b commit 5fb6b60b4a3fc1033f6c5b5447d7cdae303ae153 @leonchen committed Feb 27, 2013
Showing with 35 additions and 194 deletions.
  1. +1 −0 .gitignore
  2. +2 −2 README.md
  3. +0 −99 bin/nodefetcher
  4. +0 −32 bin/reader
  5. +0 −49 bin/rubyfetcher
  6. +29 −10 lib/gitFileSystem.ms
  7. +3 −2 package.json
View
@@ -1 +1,2 @@
*.swp
+node_modules
View
@@ -1,4 +1,4 @@
-gitplus
+gitfs
=======
-fast git data fetcher
+fast git data fetcher
View
@@ -1,99 +0,0 @@
-#! /usr/bin/env node
-var exec = require('child_process').exec;
-
-function debug(info) {
- //console.log("DEBUG: ", info);
-}
-
-source = '/Volumes/RamDisk/sr';
-
-//console.log(process.argv);
-
-function readSHA(target, sha, type, cb) {
- if (type instanceof Function) {
- cb = type;
- type = null;
- }
-
- getType(sha, type, function (err, type) {
- if (err) return cb(err);
- debug("target:"+target+", sha:"+sha+", type:"+type);
- if (type == 'commit') {
- parseCommit(sha, function (error, treeSHA) {
- if (error) return cb(error);
- readSHA(target, treeSHA, 'tree', cb);
- });
- } else if (type == 'tree') {
- getTree(target, sha, function (error, tree) {
- if (error) return cb(error);
- var subTrees = tree.split(/\n/).length;
- if (subTrees == 0 || subTrees > 1) {
- cb(null, tree);
- } else {
- var info = tree.split(/\s+/);
- if (info[1] == 'tree') {
- return listTree(info[2], cb);
- } else {
- return readSHA(target, info[2], info[1], cb);
- }
- }
- });
- } else if (type == 'blob') {
- exec('git cat-file -p '+sha, {cwd:source}, function (error, stdout, stderr) {
- cb(stderr, stdout);
- });
- }
- });
-
-}
-
-function getType(sha, type, cb) {
- if (!type) {
- exec('git cat-file -t '+sha, {cwd:source}, function (error, stdout, stderr) {
- cb(stderr, stdout.trim());
- });
- } else {
- cb(null, type);
- }
-};
-
-function parseCommit(sha, cb) {
- exec('git cat-file -p '+sha, {cwd:source}, function (error, stdout, stderr) {
- var treeSHA = stdout.split(/\s+/)[1];
- cb(stderr, treeSHA);
- });
-}
-
-function getTree(target, sha, cb) {
- target = target.replace(/\/$/, '');
- exec('git ls-tree '+sha+" "+target, {cwd:source}, function (error, stdout, stderr) {
- if (!stdout) cb('no object found for '+target);
- cb(stderr, stdout.trim());
- });
-}
-
-function listTree(sha, cb) {
- exec('git cat-file -p '+sha, {cwd:source}, function (error, stdout, stderr) {
- cb(stderr, stdout);
- });
-}
-
-var sha = process.argv[2];
-var target = process.argv[3];
-var times = parseInt(process.argv[4] || 1);
-
-var sTime = Date.now();
-
-for (var i=0; i<times; i++) {
-(function (i) {
-readSHA(target, sha, function (error, data) {
- if (error) {
- console.error(error);
- } else {
- //console.log(data);
- console.log(i);
- }
- //debug("finished in " + (Date.now() - sTime) + " ms");
-});
-})(i);
-}
View
@@ -1,32 +0,0 @@
-#! /usr/bin/env ruby
-
-def debug(info)
- puts "DEBUG: #{info.is_a?(String) ? info : info.inspect}"
-end
-
-ram_source = '/Volumes/RamDisk/sr'
-hd_source = '/Users/chenronghua/workspace/scarecrow-rules'
-
-source = ARGV[0]
-source = ram_source if source == 'ram'
-source = hd_source if source == 'hd'
-
-Dir.chdir(source)
-debug "git repo: #{source}"
-
-def readSHA(target, sha)
- `git checkout #{sha}`
- return IO.read(target)
-end
-
-sha = ARGV[1]
-target = ARGV[2]
-
-start_time = Time.now.to_f
-
-puts readSHA(target, sha)
-
-
-end_time = Time.now.to_f
-
-debug "finished in #{((end_time - start_time)*1000).to_i} ms"
View
@@ -1,49 +0,0 @@
-#! /usr/bin/env ruby
-
-def debug(info)
- #puts "DEBUG: #{info.is_a?(String) ? info : info.inspect}"
-end
-
-source = '/Volumes/RamDisk/sr'
-
-Dir.chdir(source)
-debug "git repo: #{source}"
-
-def readSHA(target, sha, type=nil)
-
- type = type || `git cat-file -t #{sha}`.chomp
- debug "target:#{target}, sha: #{sha}, type: #{type}"
-
- if type == 'commit'
- commit = `git cat-file -p #{sha}`
- tree = commit.split(/\s+/)[1]
- return readSHA(target, tree, 'tree')
- elsif type == 'tree'
- tree = `git ls-tree -r #{sha} #{target}`
- raise RuntimeError.new("no object found for #{target}") if tree.empty?
- # tree
- return tree if tree.split(/\n/).length > 1
- # blob
- info = tree.split(/\s+/)
- return readSHA(nil, info[2], info[1])
- elsif type == 'blob'
- return `git cat-file -p #{sha}`
- end
-end
-
-
-sha = ARGV[0]
-target = ARGV[1]
-times = (ARGV[2] || 1).to_i
-
-start_time = Time.now.to_f
-
-times.times do |i|
- readSHA(target, sha)
- puts i
-end
-
-
-end_time = Time.now.to_f
-
-debug "finished in #{((end_time - start_time)*1000).to_i} ms"
View
@@ -1,4 +1,5 @@
var exec = require('child_process').exec;
+var mm = require('minimatch');
export class GitFileSystem {
function initialize(options) {
@@ -17,7 +18,8 @@ export class GitFileSystem {
// output sha string
function sha(ref, cb) {
exec('git show-ref '+ref, { cwd: this.repo }, function (error, stdout, stderr) {
- if (error || stderr) return cb(error || stderr);
+ //if (error || stderr) return cb(error || stderr);
+ if (!stdout) cb('no sha found for '+ref);
cb(null, stdout.split(/\s+/)[0]);
});
}
@@ -30,7 +32,15 @@ export class GitFileSystem {
});
}
- // output lines with: mod type sha length name data
+ function readFiles(sha, files, cb) {
+ this.read(sha, files, cb);
+ }
+
+ function readFile(sha, file, cb) {
+ this.read(sha, file, cb);
+ }
+
+ // output lines with: mod type sha name data
function read(sha, target, type, cb) {
if (type instanceof Function) {
cb = type;
@@ -109,7 +119,7 @@ export class GitFileSystem {
exec('git cat-file -p '+sha, { cwd: this.repo }, function (error, stdout, stderr) {
if (error || stderr) return cb(error || stderr);
var treeSHA = stdout.split(/\s+/)[1];
- cb(stderr, treeSHA);
+ cb(null, treeSHA);
});
}
@@ -122,21 +132,22 @@ export class GitFileSystem {
if (tree.length < 1) return cb(null, []);
var count = 0;
+ var output = [];
var finish = function (err) {
count--;
- if (err) cb(err);
- if (count <= 0) cb(null, tree);
+ if (err) return cb(err);
+ if (count <= 0) cb(null, output);
};
tree.forEach(function (t, idx, tree) {
- if (t[1] == 'blob') {
+ if (t[1] == 'blob' && mm(t[3], target)) {
count++;
self.readBlob(t[2], function (err, data) {
- tree[idx].push(data);
+ output.push([t[3], data]);
finish(err);
});
}
});
- if (count == 0) cb(null, tree);
+ if (count == 0) cb(null, output);
});
}
@@ -156,19 +167,27 @@ export class GitFileSystem {
var tree = self.parseTreeOut(out);
if (tree.length == 0 || tree.length > 1) return cb(null, tree);
if (tree[0][1] == 'blob') return cb(null, tree);
- if (tree[0][4] == target) return self.listObj(tree[0][2], '', cb);
+ if (tree[0][3] == target) return self.listTreeBySHA(tree[0][2], cb);
});
}
function listTree(sha, target, cb) {
this.debugLog("list tree - sha:"+sha+", target:"+target);
- exec('git ls-tree -l '+sha+" "+target, { cwd: this.repo }, function (error, stdout, stderr) {
+ exec('git ls-tree '+sha+" "+target, { cwd: this.repo }, function (error, stdout, stderr) {
if (error || stderr) return cb(error || stderr);
if (!stdout) cb('no object found for '+target);
cb(null, stdout);
});
}
+ function listTreeBySHA(sha, cb) {
+ this.debugLog("list tree by sha - sha:"+sha);
+ exec('git cat-file -p '+sha, { cwd: this.repo }, function (error, stdout, stderr) {
+ if (error || stderr) return cb(error || stderr);
+ cb(null, stdout);
+ });
+ }
+
function parseTreeOut(out) {
out = out.trim();
if (!out) return [];
View
@@ -1,7 +1,7 @@
{
"name": "gitfs",
"description": "git data fetcher",
- "version": "0.0.6",
+ "version": "0.0.7",
"private": false,
"licenses": [ { "type": "MIT" }],
"author": "Leon Chen",
@@ -15,6 +15,7 @@
"gitfs": "./bin/gitfs"
},
"dependencies": {
- "mochiscript": ""
+ "mochiscript": "",
+ "minimatch": ""
}
}

0 comments on commit 5fb6b60

Please sign in to comment.