Skip to content

Commit e85b823

Browse files
committed
Merge branch 'ref_capture' of https://github.com/choldgraf/git-url-parse into new-version
2 parents d044674 + 3880dd0 commit e85b823

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

example/index.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ console.log(GitUrlParse("https://github.com/IonicaBizau/node-git-url-parse.git")
3737
// , owner: "IonicaBizau"
3838
// }
3939

40+
console.log(GitUrlParse("https://github.com/IonicaBizau/git-url-parse/blob/master/test/index.js"));
41+
// { protocols: [ 'https' ],
42+
// protocol: 'https',
43+
// port: null,
44+
// resource: 'github.com',
45+
// user: '',
46+
// pathname: '/IonicaBizau/git-url-parse/blob/master/test/index.js',
47+
// hash: '',
48+
// search: '',
49+
// href: 'https://github.com/IonicaBizau/git-url-parse/blob/master/test/index.js',
50+
// token: '',
51+
// toString: [Function],
52+
// source: 'github.com',
53+
// name: 'git-url-parse',
54+
// owner: 'IonicaBizau',
55+
// organization: '',
56+
// ref: 'master',
57+
// filepathtype: 'blob',
58+
// filepath: 'test/index.js',
59+
// full_name: 'IonicaBizau/git-url-parse' }
60+
4061
console.log(GitUrlParse("https://github.com/IonicaBizau/node-git-url-parse.git").toString("ssh"));
4162
// => "[email protected]:IonicaBizau/node-git-url-parse.git"
4263

lib/index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const gitUp = require("git-up");
2424
* - `source` (String): The Git provider (e.g. `"github.com"`).
2525
* - `owner` (String): The repository owner.
2626
* - `name` (String): The repository name.
27+
* - `ref` (String): The repository ref (e.g., "master" or "dev").
28+
* - `filepath` (String): A filepath relative to the repository root.
29+
* - `filepathtype` (String): The type of filepath in the url ("blob" or "tree").
2730
* - `full_name` (String): The owner and name values in the `owner/name` format.
2831
* - `toString` (Function): A function to stringify the parsed url into another url type.
2932
* - `organization` (String): The organization the owner belongs to. This is CloudForge specific.
@@ -60,10 +63,21 @@ function gitUrlParse(url) {
6063
break;
6164
default:
6265
splits = urlInfo.name.split("/");
63-
if (splits.length === 2) {
66+
if (splits.length >= 2) {
6467
urlInfo.owner = splits[0];
6568
urlInfo.name = splits[1];
6669
}
70+
71+
urlInfo.ref = "";
72+
urlInfo.filepathtype = "";
73+
urlInfo.filepath = "";
74+
if ((splits.length > 3) && (["blob", "tree"].indexOf(splits[2]) >= 0)) {
75+
urlInfo.filepathtype = splits[2];
76+
urlInfo.ref = splits[3];
77+
if (splits.length > 4) {
78+
urlInfo.filepath = splits.slice(4).join('/');
79+
}
80+
}
6781
break;
6882
}
6983

test/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const URLS = {
1010
, ftp: "ftp://github.com/IonicaBizau/git-url-parse"
1111
, ftps: "ftps://github.com/IonicaBizau/git-url-parse"
1212
, gitSsh: "git+ssh://[email protected]/IonicaBizau/git-url-parse.git"
13+
, ref: "https://github.com/IonicaBizau/git-url-parse/blob/master/test/index.js"
1314
};
1415

1516
tester.describe("parse urls", test => {
@@ -124,6 +125,19 @@ tester.describe("parse urls", test => {
124125
test.expect(res.name).toBe("name");
125126
});
126127

128+
// ref and filepath urls
129+
test.should("parse ref/filepath urls", () => {
130+
var res = gitUrlParse(URLS.ref);
131+
test.expect(res.protocol).toBe("https");
132+
test.expect(res.source).toBe("github.com");
133+
test.expect(res.owner).toBe("IonicaBizau");
134+
test.expect(res.name).toBe("git-url-parse");
135+
test.expect(res.href).toBe(URLS.ref);
136+
test.expect(res.ref).toBe("master");
137+
test.expect(res.filepathtype).toBe("blob");
138+
test.expect(res.filepath).toBe("test/index.js");
139+
});
140+
127141
test.should("parse subdomains", () => {
128142
var res = gitUrlParse("https://gist.github.com/owner/id");
129143
test.expect(res.source).toBe("github.com");

0 commit comments

Comments
 (0)