-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodefetcher
executable file
·99 lines (86 loc) · 2.39 KB
/
nodefetcher
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#! /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);
}