Skip to content

Commit

Permalink
Update clone command to new API
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Sep 14, 2013
1 parent 3cf5387 commit e7786b6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 72 deletions.
8 changes: 6 additions & 2 deletions bin/cli.js
Expand Up @@ -3,9 +3,13 @@
var program = require('commander');

program
.version(require('js-git/package.json').version)
.command("clone <url>", "Clone a repository into a new directory")
.version(require('git-node').version)
.command("ls-remote <url>", "List remote refs")
.command("clone <url>", "Clone a repository into a new directory")
.command("dump <target>", "Dump the current file tree")
.command("fetch <url>", "Fetch updates from remote")
.command("log", "Show local history")
.command("switch <branch>", "Switch branch HEAD points to")
.parse(process.argv);

if (process.argv.length < 3) {
Expand Down
101 changes: 31 additions & 70 deletions bin/clone.js
@@ -1,87 +1,48 @@
#!/usr/bin/env node
// Bootstrap the platform to run on node.js
require('js-git/lib/platform.js')(require('js-git-node-platform'));

// Load the libraries
var urlParse = require('url').parse;
var git = require('git-node');
var program = require('commander');
var autoProto = require('js-git/protocols/auto.js');
var fsDb = require('js-git/lib/fs-db.js');
var wrap = require('js-git/lib/repo.js');
var serial = require('js-git/helpers/serial.js');
var parallel = require('js-git/helpers/parallel.js');
var parallelData = require('js-git/helpers/parallel-data.js');
var pathResolve = require('path').resolve;
var basename = require('path').basename;
var existsSync = require('fs').existsSync;

program
.version(require('js-git/package.json').version)
.usage('[options] [--] <url> [<dir>]')
.option('--bare', 'create a bare repository')
.option('-b, --branch <branch>', 'checkout <branch> instead of the remote\'s HEAD')
.option('-t, --tag <tag>', 'checkout <tag> instead of the remote\'s HEAD')
.option('--ref <branch/tag/ref>', 'checkout to specefic branch, tag, or ref')
.option('--depth <num>', 'do a shallow clone with num commits deep')
.option('-q', 'Be quiet; don\t show progress')
.parse(process.argv);


if (program.args.length < 1 || program.args.length > 2) {
program.outputHelp();
process.exit(1);
}

var url = program.args[0];
var opts = urlParse(url);
if (!opts.protocol) {
opts = urlParse("ssh://" + url);
}

var baseExp = /([^\/.]*)(\.git)?$/;
opts.target = program.args[1];
if (!opts.target) {
opts.target = opts.pathname.match(baseExp)[1];
if (program.bare) opts.target += '.git';
}

program.ref = "HEAD";
if (program.branch) {
program.ref = "refs/heads/" + program.branch;
var remote = git.remote(url);
var target = program.args[1] || basename(remote.pathname, ".git") + ".git";
if (existsSync(target)) {
console.error("Target already exists: %s", target);
process.exit(-1);
}
else if (program.tag) {
program.ref = "refs/tags/" + program.tag;
var repo = git.repo(target);
var opts = {};
if (!program.Q) opts.onProgress = onProgress;
if (program.ref) opts.want = program.ref;
if (program.depth) opts.depth = parseInt(program.depth, 10);
if (!program.Q) console.log("Cloning %s to %s..", url, target);
repo.fetch(remote, opts, onDone);

function onProgress(progress) {
process.stderr.write(progress);
}

opts.target = pathResolve(process.cwd(), opts.target);

var connection = autoProto(opts);
var repo = wrap(fsDb(opts.target, opts.bare));

var config = {
includeTag: true,
onProgress: function (data) {
process.stdout.write(data);
},
onError: function (data) {
process.stderr.write(data);
function onDone() {
if (program.ref) {
repo.resolveHashish(program.ref, function (err, hash) {
if (err) throw err;
repo.updateHead(hash, function (err) {
if (err) throw err;
});
});
}
};

parallelData({
init: repo.init(),
pack: connection.fetch(config),
}, function (err, result) {
if (err) throw err;
serial(
parallel(
repo.importRefs(result.pack.refs),
repo.unpack(result.pack, config)
),
connection.close(),
function (callback) {
if (repo.bare) return callback();
repo.checkout(program.ref, callback);
}
)(function (err) {
if (err) throw err;

console.log("DONE");
});
});

if (!program.Q) console.log("Done.");
}

0 comments on commit e7786b6

Please sign in to comment.