Skip to content

Commit c58aeca

Browse files
committed
Single cmd handling file, clone command
1 parent 208b9d1 commit c58aeca

File tree

9 files changed

+127
-26
lines changed

9 files changed

+127
-26
lines changed

.gitignore

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
node_modules
2-
tags
3-
.zedstate
1+
node_modules

bin/cli.js

-17
This file was deleted.

bin/jsgit.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
3+
var program = require('commander');
4+
var clone = require('../commands/clone');
5+
6+
program
7+
.version(require('git-node').version);
8+
9+
program
10+
.command('clone <url> [dir]')
11+
.description('Clone a repository into a new directory')
12+
.option('--ref <branch/tag/ref>', 'checkout to specefic branch, tag, or ref', 'HEAD')
13+
.option('--depth <num>', 'do a shallow clone with num commits deep')
14+
.option('--mirror', 'not supported yet (will act as a "git fetch")')
15+
.option('-v, --verbose', 'show detailed output')
16+
.action(function(url, dir, options) {
17+
console.log('git clone %s to %s', url, dir);
18+
console.log('mirror? ' + (options.mirror ? 'true' : 'false'));
19+
clone(url, dir, options);
20+
});
21+
22+
// .command("ls-remote <url>", "List remote refs")
23+
// .command("fetch <url>", "Download objects and refs from another repository")
24+
// .command("log", "Show local history")
25+
// .command("export <target>", "Export tree at HEAD as real files to target")
26+
27+
program.parse(process.argv);
28+
29+
if (process.argv.length < 3) {
30+
program.outputHelp();
31+
process.exit(1);
32+
}

commands/clone.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#!/usr/bin/env node
2+
var git = require('git-node');
3+
var fs = require('fs');
4+
var path = require('path');
5+
var pathJoin = path.join;
6+
var mkdirp = require('mkdirp');
7+
8+
function clone(url, dir, options) {
9+
var remote = git.remote(url);
10+
var target = dir || path.basename(remote.pathname, ".git");
11+
var clonePath = target;
12+
if (!options.mirror) target += '/.git';
13+
var repo = git.repo(target);
14+
15+
var fetchOpts = {};
16+
if (options.verbose) fetchOpts.onProgress = onProgress;
17+
if (options.ref) fetchOpts.want = options.ref;
18+
if (options.depth) fetchOpts.depth = parseInt(options.depth, 10);
19+
20+
if (!fs.existsSync(target)) {
21+
mkdirp.sync(target);
22+
}
23+
24+
if (options.mirror) {
25+
console.log("Cloning %s into bare repository %s", url, target);
26+
}
27+
else {
28+
console.log("Cloning %s to %s..", url, target);
29+
}
30+
31+
repo.fetch(remote, fetchOpts, onFetch);
32+
33+
function onProgress(progress) {
34+
process.stderr.write(progress);
35+
}
36+
37+
function onFetch() {
38+
if (!options.mirror) {
39+
repo.resolveHashish(options.ref, function (err, hash) {
40+
if (err) throw err;
41+
repo.updateHead(hash, function (err) {
42+
if (err) throw err;
43+
// copied from export.js
44+
var read;
45+
repo.treeWalk("HEAD", function (err, stream) {
46+
if (options.verbose) console.log('walking...');
47+
if (err) throw err;
48+
read = stream.read;
49+
return read(function() {
50+
read(onEntry);
51+
});
52+
});
53+
54+
function onEntry(err, entry) {
55+
if (options.verbose) console.log('on entry...');
56+
if (err) throw err;
57+
if (!entry) return;
58+
var path = pathJoin(clonePath, entry.path);
59+
if (options.verbose) {
60+
var colorPath = "\x1B[34m" + path.replace(/\//g, "\x1B[1;34m/\x1B[0;34m") + "\x1B[0m";
61+
console.log("%s %s", entry.hash, colorPath);
62+
}
63+
if (entry.type === "tree") {
64+
return fs.mkdir(path, onDone);
65+
}
66+
if (entry.type === "blob") {
67+
return fs.writeFile(path, entry.body, onDone);
68+
}
69+
return read(onEntry);
70+
}
71+
72+
function onDone(err) {
73+
if (err) throw err;
74+
return read(onEntry);
75+
}
76+
});
77+
});
78+
}
79+
80+
// create config with remote
81+
fs.writeFile(target + '/config', '[remote "origin"]\r\n\turl = ' + url, function(err) {
82+
if (err) throw err;
83+
if (options.verbose) {
84+
console.log('Created repo config');
85+
console.log("Done.");
86+
}
87+
})
88+
}
89+
}
90+
91+
module.exports = clone;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

package.json

+3-6
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,7 @@
44
"description": "A command-line git client powered by js-git and node.js",
55
"main": "index.js",
66
"bin": {
7-
"jsgit-fetch": "./bin/fetch.js",
8-
"jsgit-log": "./bin/log.js",
9-
"jsgit-export": "./bin/export.js",
10-
"jsgit-ls-remote": "./bin/ls-remote.js",
11-
"jsgit": "./bin/cli.js"
7+
"jsgit": "./bin/jsgit.js"
128
},
139
"repository": {
1410
"type": "git",
@@ -19,7 +15,8 @@
1915
],
2016
"dependencies": {
2117
"commander": "~2.0.0",
22-
"git-node": "~0.1.1"
18+
"git-node": "~0.1.1",
19+
"mkdirp": "~0.3.5"
2320
},
2421
"devDependencies": {},
2522
"author": "Tim Caswell <[email protected]>",

0 commit comments

Comments
 (0)