Permalink
Browse files

support for downloading private github repositories

  • Loading branch information...
1 parent 1587766 commit ed9e194034de94669bed63827033c01e0ab83f63 @cadorn cadorn committed Sep 9, 2011
Showing with 56 additions and 3 deletions.
  1. +56 −3 lib/pinf-loader-js/downloader.js
@@ -13,6 +13,8 @@ var MAX_PARALLEL_DOWNLOADS = 2,
var directoriesVerified = false;
+// TODO: This module should go into pinf/core
+
var Downloader = exports.Downloader = function(options)
{
this.basePath = options.basePath;
@@ -395,9 +397,8 @@ Downloader.prototype.doDownload = function(url, path, callback)
}
}
- NET.download(url, path + ".tmp", function()
- {
- FILE.rename(path + ".tmp", path);
+ function finalizeDoDownload()
+ {
if (USE_CACHE)
{
API.FILE.mkdirs(API.FILE.dirname(cachePath), 0775);
@@ -410,5 +411,57 @@ Downloader.prototype.doDownload = function(url, path, callback)
}
delete currentlyDownloading[url + "::" + path];
callback();
+ }
+
+ NET.download(url, path + ".tmp", function(response)
+ {
+ if (response.status != 200)
+ {
+ // TODO: This should be more generic
+ var m;
+ if (response.status == 404 && (m = url.match(/^https?:\/\/github.com\/([^\/]*)\/([^\/]*)\/zipball\/(.*)$/)))
+ {
+ var repoPath = FILE.dirname(path) + "/__repo__";
+
+ function exportFromGit()
+ {
+ SYSTEM.exec("cd " + repoPath + " ; git archive --format zip --output " + path + ".tmp" + " --prefix repo/ " + m[3], function(stdout, stderr)
+ {
+ if (stderr)
+ {
+ console.error(stderr);
+ throw new Error("Error exporting from '" + path + ".repo" + "'.");
+ }
+ FILE.rename(path + ".tmp", path);
+ finalizeDoDownload();
+ });
+ }
+
+ if (FILE.exists(repoPath))
+ {
+ SYSTEM.exec("cd " + repoPath + " ; git pull origin", function(stdout, stderr)
+ {
+ // TODO: Ensure update was successful
+ exportFromGit();
+ });
+ }
+ else
+ {
+ SYSTEM.exec("git clone " + "git@github.com:" + m[1] + "/" + m[2] + ".git " + repoPath, function(stdout, stderr)
+ {
+ if (stderr)
+ {
+ console.error(stderr);
+ throw new Error("Error cloning '" + "git@github.com:" + m[1] + "/" + m[2] + ".git" + "'.");
+ }
+ exportFromGit();
+ });
+ }
+ return;
+ }
+ throw new Error("Error downloading from URL '" + url + "'. Status: " + response.status);
+ }
+ FILE.rename(path + ".tmp", path);
+ finalizeDoDownload();
});
}

0 comments on commit ed9e194

Please sign in to comment.