Skip to content

Commit

Permalink
Merge pull request #22 from hotoo/fixpath
Browse files Browse the repository at this point in the history
Fixpath
  • Loading branch information
hotoo committed Jul 23, 2015
2 parents 6dd8b1a + 8e26200 commit 32f57e6
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 29 deletions.
13 changes: 0 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,6 @@ git.example.com:
...
```

You can git alias in ~/.gitconfig:

```
[alias]
open = !gitopen $@
```

Then you can use command like:

```
$ git open
```

## gitopen Commands

### $ gitopen
Expand Down
10 changes: 8 additions & 2 deletions bin/gitopen
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ var gitremote = require('../lib/gitremote');
var gitresolve = require('../lib/');
var commander = require('./open-commander');

var cwb = gitremote.getCurrentBranch(process.cwd()) || 'master';
var cwd = process.cwd();
var cwb = gitremote.getCurrentBranch(cwd) || 'master';
var gitroot = gitremote.getGitRootPath(cwd);
// 1. commander
// 2. if help then print help.
// 3. if version then print version.
var command = commander(process.argv, cwb);
var command = commander(process.argv, {
cwd: cwd,
cwb: cwb,
root: gitroot,
});
// 4. if @profile then open @profile
if (command.category === 'profile') {
var username = command.args.username;
Expand Down
10 changes: 8 additions & 2 deletions bin/hgopen
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ var hgremote = require('../lib/hgremote');
var gitresolve = require('../lib/');
var commander = require('./open-commander');

var cwb = hgremote.getCurrentBranch(process.cwd) || 'default';
var cwd = process.cwd();
var cwb = hgremote.getCurrentBranch(cwd) || 'default';
var hgroot = hgremote.getHgRootPath(cwd);
// 1. commander
// 2. if help then print help.
// 3. if version then print version.
var command = commander(process.argv, cwb);
var command = commander(process.argv, {
cwd: cwd,
cwb: cwb,
root: hgroot,
});
// 4. if @profile then open @profile
if (command.category === 'profile') {
var username = command.args.username;
Expand Down
29 changes: 17 additions & 12 deletions bin/open-commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@
var fs = require('fs');
var path = require('path');
var commander = require('commander');
var gitremote = require('../lib/gitremote');

// resolve absolute path to relative path base repository root.
function resolve(filepath, cwd, root) {
return path.normalize(path.join(cwd, filepath))
.replace(root, '');
}

// @param {argv} process.argv
// @param {String} cwb, current working branch name.
module.exports = function(argv, cwb) {
module.exports = function(argv, option) {
commander
.version(require('../package.json').version)
.option('-p, --path <path>', 'CWD path')
Expand All @@ -17,8 +22,8 @@ module.exports = function(argv, cwb) {

var options = {
category: 'home',
cwd: commander.path || process.cwd(),
hash: commander.branch || cwb || 'master',
cwd: commander.path || option.cwd || process.cwd(),
hash: commander.branch || option.cwb || 'master',
remote: commander.remote || 'origin',
protocol: 'https',
verbose: commander.verbose,
Expand Down Expand Up @@ -63,14 +68,14 @@ module.exports = function(argv, cwb) {
// current working branch name.
if (commander.args.length === 1) { // gitopen pr
options.args = {
'branch-B': cwb,
'branch-B': option.cwb,
};
} else if (commander.args.length === 2) { // gitopen pr branchName
match = RE_BRANCH_COMPARE.exec(commander.args[1]);
if (match) { // gitopen pr a...b
options.args = {
'branch-A': match[1],
'branch-B': match[2] || cwb,
'branch-B': match[2] || option.cwb,
};
} else { // gitopen pr branchName
options.args = {
Expand All @@ -80,7 +85,7 @@ module.exports = function(argv, cwb) {
} else if (commander.args.length >= 3) {
options.args = {
'branch-A': commander.args[1],
'branch-B': commander.args[2] || cwb,
'branch-B': commander.args[2] || option.cwb,
};
}
break;
Expand Down Expand Up @@ -191,20 +196,20 @@ module.exports = function(argv, cwb) {
};
} else {
// FILE/DIR PATH
var filepath = path.normalize(category);
if (fs.existsSync(filepath)) {
var stat = fs.statSync(filepath);
if (fs.existsSync(category)) {
var filepath = resolve(category, option.cwd, option.root); // path.normalize(category)
var stat = fs.statSync(category);
if (stat.isFile()) {
options.category = 'blob';
options.args = {
cwd: path.dirname(filepath),
path: '/' + filepath,
path: filepath,
};
} else if (stat.isDirectory()) {
options.category = 'tree';
options.args = {
cwd: path.resolve(filepath, options.cwd),
path: '/' + filepath,
path: filepath,
};
}
} else {
Expand Down
9 changes: 9 additions & 0 deletions lib/gitremote.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,12 @@ exports.getCurrentBranch = function getCurrentBranch(cwd) {
}
return null;
};

exports.getGitRootPath = function(cwd) {
try {
return child_process.execSync('git rev-parse --show-toplevel', {cwd: cwd})
.toString().trim();
} catch (ex) {
return null;
}
};
9 changes: 9 additions & 0 deletions lib/hgremote.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,12 @@ exports.getCurrentBranch = function getCurrentBranch(cwd) {
return null;
}
};

exports.getHgRootPath = function(cwd) {
try {
return child_process.execSync('hg root', {cwd: cwd})
.toString().trim();
} catch (ex) {
return null;
}
};
17 changes: 17 additions & 0 deletions test/gitopen.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,23 @@ describe('$ gitopen', function () {
});
});
});

var git_command_case_in_subdir = [
['../README.md :master', '/hotoo/gitopen/blob/master/README.md'],
['../README.md -b master', '/hotoo/gitopen/blob/master/README.md'],
[':master ../lib', '/hotoo/gitopen/tree/master/lib'],
['-b master ../lib', '/hotoo/gitopen/tree/master/lib'],
];
git_command_case_in_subdir.forEach(function(testcase) {
var cmd = testcase[0] ? ' ' + testcase[0] : '';
it('$ cd bin && gitopen' + cmd, function (done) {
child_process.exec('cd bin && ./gitopen --verbose' + cmd, function(err, stdout) {
should(err).not.be.ok();
stdout.should.be.containEql('URL: ' + (RE_URL.test(testcase[1]) ? testcase[1] : 'https://github.com' + testcase[1]) + '\n');
done();
});
});
});
});


Expand Down

0 comments on commit 32f57e6

Please sign in to comment.