Skip to content

Commit

Permalink
Implement more commands, tag to 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
creationix committed Jun 4, 2013
1 parent 0088192 commit 37c6a18
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
10 changes: 10 additions & 0 deletions checkout.js
@@ -0,0 +1,10 @@
#!/usr/bin/env node

var program = require('commander');

program
.version(require('./package.json').version)
.usage('ref')
.parse(process.argv);

throw new Error("TODO: Implement checkout command");
10 changes: 10 additions & 0 deletions cli.js
@@ -0,0 +1,10 @@
#!/usr/bin/env node

var program = require('commander');

program
.version(require('./package.json').version)
.command("clone [url] [options]", "Clone a remote repository from [url]")
.command("checkout [ref]", "Checkout working directory to a named branch or tag for [ref]")
.parse(process.argv);

37 changes: 29 additions & 8 deletions clone.js
Expand Up @@ -14,23 +14,41 @@ var bops = require('bops');
var crypto = require('crypto');
var streamToSink = require('min-stream-node/common.js').streamToSink;

if (process.argv.length < 3) {
console.log("Usage: %s %s repo [target]\n", process.argv[0], process.argv[1]);
var program = require('commander');

program
.version(require('./package.json').version)
.usage('url [options]')
.option('-t, --target [target]', 'Set a custom target directory')
.option('-b, --bare', 'Bare clone')
.parse(process.argv);


if (program.args.length !== 1) {
program.outputHelp();
process.exit(1);
}

var options = urlParse(process.argv[2]);
var options = urlParse(program.args[0]);
options.port = options.port ? parseInt(options.port, 10) : 9418;

if (options.protocol !== "git:") {
console.error("Invalid URL: " + JSON.stringify(program.args[0]));
throw new Error("Sorry, only git:// protocol is implemented so far");
}


var baseExp = /([^\/.]*)(\.git)?$/;
options.target = process.argv[3] || options.pathname.match(baseExp)[1];
options.target = program.target || options.pathname.match(baseExp)[1];

console.log("Cloning into '%s'...", options.target);
var gitDir;
if (program.bare) {
gitDir = options.target + ".git";
console.log("Cloning bare repo into '%s'...", gitDir);
}
else {
gitDir = pathJoin(options.target, ".git");
console.log("Cloning repo into '%s'...", options.target);
}
tcp.connect(options.hostname, options.port, function (err, socket) {
if (err) throw err;

Expand All @@ -48,7 +66,6 @@ tcp.connect(options.hostname, options.port, function (err, socket) {

function onStream(err, sources) {
if (err) throw err;
var gitDir = pathJoin(options.target, ".git");
var HEAD;
Object.keys(sources.refs).forEach(function (ref) {
var hash = sources.refs[ref];
Expand Down Expand Up @@ -78,6 +95,7 @@ function onStream(err, sources) {
}, function (err) {
if (err) throw err;
console.log("Receiving objects: 100% (" + total + "/" + total + "), done.");
if (program.bare) return;
checkout(gitDir, "HEAD", checkError);
});
}
Expand Down Expand Up @@ -202,7 +220,7 @@ function sha1(buf) {
return crypto
.createHash('sha1')
.update(buf)
.digest('hex')
.digest('hex');
}

function hashToPath(gitDir, hash) {
Expand Down Expand Up @@ -265,9 +283,12 @@ function loadObject(gitDir, hash, callback) {
}



function checkout(gitDir, ref, callback) {
// Get the hash ref points to
loadRef(gitDir, ref, function (err, hash) {
if (err) return callback(err);
// Get the commit the hash points to
loadObject(gitDir, hash, function (err, object) {
if (err) return callback(err);
object = parseObject(object);
Expand Down
8 changes: 5 additions & 3 deletions package.json
@@ -1,13 +1,15 @@
{
"name": "js-git-node",
"version": "0.0.1",
"version": "0.0.2",
"description": "A sample CLI tool using js-git for node.js",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"bin": {
"git-clone": "clone.js"
"jsgit-clone": "clone.js",
"jsgit-checkout": "checkout.js",
"jsgit": "cli.js"
},
"repository": {
"type": "git",
Expand All @@ -25,7 +27,7 @@
"git-pkt-line": "0.0.1",
"git-list-pack": "0.0.10",
"min-stream": "0.0.4",
"git-hydrate-pack": "0.0.3",
"git-hydrate-pack": "0.0.4",
"mkdirp": "~0.3.5"
},
"devDependencies": {},
Expand Down

0 comments on commit 37c6a18

Please sign in to comment.