Skip to content

Commit

Permalink
Merge branch 'fix-source-2parts-sld' of https://github.com/pvdlg/git-…
Browse files Browse the repository at this point in the history
…url-parse into new-version
  • Loading branch information
IonicaBizau committed Jul 13, 2018
2 parents 67ae07c + 13dae80 commit 1912236
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
12 changes: 7 additions & 5 deletions lib/index.js
@@ -1,7 +1,6 @@
"use strict";

const gitUp = require("git-up");
const parseDomain = require('parse-domain');

/**
* gitUrlParse
Expand Down Expand Up @@ -45,12 +44,14 @@ function gitUrlParse(url) {
, splits = null
;

urlInfo.toString = function (type) {
urlInfo.toString = function (type) {
return gitUrlParse.stringify(this, type);
};

const parsedResource = parseDomain(urlInfo.resource);
urlInfo.source = parsedResource ? parsedResource.domain + '.' + parsedResource.tld : urlInfo.resource;
urlInfo.source = sourceParts.length > 2
? sourceParts.slice(1 - sourceParts.length).join(".")
: urlInfo.source = urlInfo.resource
;

// Note: Some hosting services (e.g. Visual Studio Team Services) allow whitespace characters
// in the repository and owner names so we decode the URL pieces to get the correct result
Expand All @@ -59,9 +60,10 @@ function gitUrlParse(url) {
urlInfo.owner = decodeURIComponent(urlInfo.user);

switch (urlInfo.source) {
case "cloudforge.com":
case "git.cloudforge.com":
urlInfo.owner = urlInfo.user;
urlInfo.organization = sourceParts[0];
urlInfo.source = "cloudforge.com";
break;
case "visualstudio.com":
splits = urlInfo.name.split("/");
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -29,8 +29,7 @@
"test": "test"
},
"dependencies": {
"git-up": "^2.0.0",
"parse-domain": "^2.0.0"
"git-up": "^2.0.0"
},
"devDependencies": {
"browserify": "^13.0.1",
Expand Down
18 changes: 8 additions & 10 deletions test/index.js
Expand Up @@ -171,6 +171,14 @@ tester.describe("parse urls", test => {
test.expect(res.toString()).toBe("https://companyname.visualstudio.com/DefaultCollection/MyProject/_git/MyRepo");
});

// custom git hosted URL with 2 parts SLD
test.should("parse Gih hosted urls with two parts SLD", () => {
var res = gitUrlParse("https://domain.git.com.cn/owner/name.git");
test.expect(res.source).toBe("git.com.cn");
test.expect(res.owner).toBe("owner");
test.expect(res.name).toBe("name");
});

// Handle URL encoded names of owners and repositories
test.should("https URLs with URL encoded characters", () => {
var res = gitUrlParse("https://companyname.visualstudio.com/My%20Project/_git/My%20Repo");
Expand Down Expand Up @@ -291,14 +299,4 @@ tester.describe("parse urls", test => {
res.user = "user";
test.expect(res.toString()).toBe("http://user@github.com/owner/name.git");
});

test.it("custom url", () => {
var res = gitUrlParse("https://git.test.com.cn/a/b");
test.expect(res.source).toBe('test.com.cn');
})

test.it("local urls", () => {
var res = gitUrlParse("http://machine:8080/tfs/Collection/Project.Name/_git/repository");
test.expect(res.source).toBe('machine');
})
});

0 comments on commit 1912236

Please sign in to comment.