Created
March 19, 2012 07:24
-
-
Save bennage/2100688 to your computer and use it in GitHub Desktop.
downloading the contents of a repo
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 assert = require('assert'); | |
var https = require('https'); | |
var fs = require('fs'); | |
var repo = '/login/repo-name/'; | |
var output_path = './a/folder'; | |
function https_get(options, success) { | |
return https.get(options, function(res) { | |
if (res.statusCode !== 200) { | |
console.log('statusCode: ', res.statusCode); | |
console.log('headers: ', res.headers); | |
} else { | |
success(res); | |
} | |
}).on('error', function(e) { | |
console.error(e); | |
}); | |
} | |
function git(options, next) { | |
https_get(options, function(res) { | |
var buffer = ''; | |
res.on('data', function(d) { | |
buffer += d; | |
}); | |
res.on('end', function() { | |
next(null, buffer); | |
}); | |
}); | |
} | |
function downloadBlob(blob) { | |
var path = repo + blob.branch + '/' + blob.path; | |
var root = 'raw.github.com'; | |
console.log(root + path); | |
git(blob.size, { | |
host: root, | |
path: path | |
}, function(err, buffer) { | |
console.log('writing ' + blob.path); | |
fs.writeFile(output_path + blob.path, buffer, 'binary'); | |
}); | |
} | |
function getTree(sha, branch) { | |
git({ | |
host: 'api.github.com', | |
path: '/repos' + repo + 'git/trees/' + sha + '?recursive=1' | |
}, function(err, buffer) { | |
var o = JSON.parse(buffer); | |
o.tree.filter(function(x) { | |
console.dir(x); | |
return x.type === 'blob'; | |
}).map(function(x) { | |
return { | |
sha: x.sha, | |
path: x.path, | |
size: x.size, | |
branch: branch | |
}; | |
}).forEach(downloadBlob); | |
o.tree.filter(function(x) { | |
return x.type === 'tree'; | |
}).forEach(function(x) { | |
getTree(x.sha, branch); | |
}); | |
}); | |
} | |
function getCommit(commit) { | |
git({ | |
host: 'api.github.com', | |
path: '/repos' + repo + 'git/commits/' + commit | |
}, function(err, buffer) { | |
var o = JSON.parse(buffer); | |
getTree(o.tree.sha, commit); | |
}); | |
} | |
git({ | |
host: 'api.github.com', | |
path: '/repos' + repo + 'git/refs/heads/master' | |
}, function(err, buffer) { | |
var o = JSON.parse(buffer); | |
getCommit(o.object.sha); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment