-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-dump
executable file
·164 lines (142 loc) · 3.92 KB
/
git-dump
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/usr/bin/env node
/**
* Dump the contents of a remote git repository without directory listing
* enabled
*
* Author: Dave Eddy <dave@daveeddy.com>
* Date: October 20, 2016
* License: MIT
*/
var execFile = require('child_process').execFile;
var fs = require('fs');
var path = require('path');
var util = require('util');
var mkdirp = require('mkdirp');
var request = require('request');
var vasync = require('vasync');
var baseurl = process.argv[2];
if (!baseurl) {
console.error('Usage: git-dump <url> [dir=.] [workers=3]');
process.exit(1);
}
baseurl = baseurl.replace(/\/+$/, '');
var dir = process.argv[3] || process.cwd();
try {
mkdirp.sync(dir);
} catch (e) {}
process.chdir(dir);
var workers = +process.argv[4] || 3;
var STARTING_FILES = [
'HEAD',
'objects/info/packs',
'description',
'config',
'COMMIT_EDITMSG',
'index',
'packed-refs',
'refs/heads/master',
'refs/remotes/origin/HEAD',
'refs/stash',
'logs/HEAD',
'logs/refs/heads/master',
'logs/refs/remotes/origin/HEAD',
'info/refs',
'info/exclude'
];
function extracthashes(s) {
return s.match(/[0-9a-fA-F]{40}/g) || [];
}
function extractpacks(s) {
return s.match(/pack-([0-9a-fA-F]{40})/g) || [];
}
function hashtofile(s) {
return util.format('objects/%s/%s',
s.substr(0, 2),
s.substr(2));
}
function filetohash(f) {
return f.replace('objects/', '').replace(/\//g, '');
}
var downloaded = {};
var queue = vasync.queue(function (f, cb) {
f = f.replace(/^\/*/, '');
// we've already processed this file
if (downloaded.hasOwnProperty(f)) {
console.log('%s already downloaded', f);
cb();
return;
}
// mkdir all parent direcotries of the file
var dirname = path.dirname(f);
mkdirp(dirname, function (err) {
if (err) {
cb(err);
return;
}
// GET the url constructed
var url = util.format('%s/%s', baseurl, f);
request({url: url, encoding: null}, function (err, res, body) {
downloaded[f] = true;
if (err) {
cb(err);
return;
}
if (res.statusCode < 200 || res.statusCode >= 300) {
cb(new Error('bad statusCode: ' + res.statusCode));
return;
}
// write the file out
fs.writeFile(f, body, function (err) {
if (err) {
cb(err);
return;
}
console.log('> GET %s --> %s', url, f);
var hashes = extracthashes(body.toString('utf8'));
if (hashes.length > 0) {
console.log('found %d hashes in %s', hashes.length, f);
queue.push(hashes.map(function (hash) {
return hashtofile(hash);
}));
}
// were done if the file can not be converted in a hash
if (!f.match(/\/[a-f0-9]{2}\/[a-f0-9]{38}/)) {
cb();
return;
}
// print the file based on hash and find more hashes
var hash = filetohash(f);
execFile('git', ['cat-file', '-p', hash],
function (err, stdout, stderr) {
if (err) {
// this is ok to ignore
cb();
return;
}
hashes = extracthashes(stdout);
if (hashes.length > 0) {
console.log('found %d hashes in `git cat-file -p %s`',
hashes.length, filetohash(f));
hashes = hashes.map(function (hash) {
return hashtofile(hash);
});
queue.push(hashes);
}
var packs = extractpacks(stdout);
if (packs.length > 0) {
console.log('found %d packs in `git cat-file -p %s`',
packs.length, filetohash(f));
var newpacks = [];
packs.forEach(function (pack) {
newpacks.push(util.format('objects/pack/%s.pack', pack));
newpacks.push(util.format('objects/pack/%s.idx', pack));
});
queue.push(newpacks);
}
cb();
});
});
});
});
}, workers);
queue.push(STARTING_FILES);