-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgit-url.js
executable file
·123 lines (105 loc) · 2.9 KB
/
git-url.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env node
require('shelljs/global');
var util = require('util');
var fs = require('fs');
var open = require('open');
var path = require('path');
var cli = require('commander');
cli
.description('create specific type of git web URL')
.usage('[options] (<file_path> [line_number]) | (<ref>) [remote]')
.option('-c, --clipboard', 'Send the generated URL to clipboard')
.option('-o, --open', 'Open the URL in browser')
.option('-b, --repo', 'Generate only the repository URL')
.parse(process.argv);
var arg1 = cli.args[0];
// is the first argument a file?
var fstate;
var file;
var branch;
var line;
var url;
if (arg1 && fs.existsSync(arg1)) {
fstate = fs.lstatSync(arg1);
}
var endpoint, origin, remote;
origin = cli.args[1] || exec('git origin', {silent: 1}).output.trim();
remote = exec('git remote show -n ' + origin, { silent: 1 }).output;
endpoint = remote.match(/(:?Fetch URL:)(.+)/)[2].trim();
var repo, user, project;
var ref;
var baseUrl, type;
// which type of git repository?
var host = endpoint.match(/(github).*?\.com/);
if(!host) {
host = endpoint.match(/(stash).*?\.com/);
}
if(host) {
baseUrl = 'https://' + host[0];
type = host[1];
}
if (type === 'github') {
host = endpoint.match(/([^:.\/]+?)\/([^:.\/]+?)\.git/);
user = host[2];
repo = host[1];
}
else if (type === 'stash') {
host = endpoint.match(/([^:.\/]+?)\/([^:.\/]+?)\.git/);
project = host[1];
repo = host[2];
url = util.format('projects/%s/repos/%s', project, repo);
}
// create link to the repo
if (cli.repo) {
url = type === 'github' ?
util.format('%s/%s', repo, user) :
util.format('projects/%s/repos/%s/', project, repo);
}
// create link to the file (line)
else if (fstate && (fstate.isFile() || fstate.isDirectory())) {
file = arg1;
line = cli.args[1];
branch = exec('git br', { silent: 1 }).output.trim();
if (type === 'github') {
url = util.format('/%s/%s', repo, user);
url = path.join(url, 'blob', branch, file);
if (line) {
url += '#L' + line;
}
}
else if (type === 'stash') {
url = util.format('projects/%s/repos/%s', project, repo);
url = path.join(url, 'browse', file + '?at=refs/heads/' + branch);
if (line) {
url += '#' + line;
}
}
}
// create link to the commit
else {
ref = exec([ 'git rev-parse --short', arg1 || 'HEAD' ].join(' '), { silent: 1 }).output;
if (type === 'github') {
url = util.format('%s/%s/commit/%s', repo, user, ref);
}
else if (type === 'stash') {
url = util.format('projects/%s/repos/%s/commits/%s', project, repo, ref);
}
}
// unrecognized repository type
if (!url) {
echo('sorry, unsupported git endpoint:' + endpoint);
exit(1);
}
url = [baseUrl, path.normalize(url)].join('/');
// open in browser
if(cli.open) {
open(url);
}
// where to put the created url
if (cli.clipboard) {
// TODO: cross-platform clipboard
exec(util.format('echo "%s" | pbcopy', url.trim()));
}
else {
echo(url);
}