diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-01-26 10:12:26 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-01-26 10:12:26 +0100 |
commit | 93bc86a0fe65943ac2ce0af28a8d81d438d17dbe (patch) | |
tree | c46871b95585f0fca2ce354c00baf19403fd464d | |
parent | 75b217fc246be108368ed0e3c3adda3dc85fd615 (diff) | |
download | gitbook-93bc86a0fe65943ac2ce0af28a8d81d438d17dbe.zip gitbook-93bc86a0fe65943ac2ce0af28a8d81d438d17dbe.tar.gz gitbook-93bc86a0fe65943ac2ce0af28a8d81d438d17dbe.tar.bz2 |
Fix git url parsing for content references
-rw-r--r-- | lib/utils/git.js | 26 | ||||
-rw-r--r-- | package.json | 2 | ||||
-rw-r--r-- | test/git.js | 2 |
3 files changed, 12 insertions, 18 deletions
diff --git a/lib/utils/git.js b/lib/utils/git.js index f7f63e7..9a669db 100644 --- a/lib/utils/git.js +++ b/lib/utils/git.js @@ -4,8 +4,8 @@ var url = require("url"); var tmp = require("tmp"); var path = require("path"); var crc = require("crc"); -var ngu = require('normalize-git-url'); var exec = Q.denodeify(require("child_process").exec); +var URI = require("URIjs"); var fs = require("./fs"); @@ -25,31 +25,25 @@ function validateSha(str) { // Parse and extract infos function parseGitUrl(giturl) { - var ref, parts; + var ref, uri, fileParts, filepath; if (!checkGitUrl(giturl)) return null; giturl = giturl.slice(GIT_PREFIX.length); - if (!url.parse(giturl).protocol) { - giturl = "ssh://"+giturl; - } - - var normalized = ngu(giturl); - - parts = url.parse(normalized.url); - ref = normalized.branch; + uri = new URI(giturl); + ref = uri.fragment() || "master"; + uri.fragment(null); // Extract file inside the repo (after the .git) - var fileParts = parts.pathname.split(".git"); - var filepath = fileParts.length > 1? fileParts.slice(1).join(".git") : ""; + fileParts =uri.path().split(".git"); + filepath = fileParts.length > 1? fileParts.slice(1).join(".git") : ""; if (filepath[0] == "/") filepath = filepath.slice(1); - // Rebuild - parts.pathname = _.first(fileParts)+".git"; - var githost = url.format(parts); + // Recreate pathname without the real filename + uri.path(_.first(fileParts)+".git"); return { - host: githost, + host: uri.toString(), ref: ref || "master", filepath: filepath }; diff --git a/package.json b/package.json index 596862e..2240841 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "tmp": "0.0.24", "crc": "3.2.1", "bash-color": "0.0.3", - "normalize-git-url": "1.0.0" + "URIjs": "1.14.1" }, "devDependencies": { "mocha": "1.18.2", diff --git a/test/git.js b/test/git.js index b23c83e..9d48606 100644 --- a/test/git.js +++ b/test/git.js @@ -25,7 +25,7 @@ describe('GIT parser and getter', function () { it('should correctly parse an ssh url', function() { var parts = git.parseUrl("git+git@github.com:GitbookIO/gitbook.git/directory/README.md#e1594cde2c32e4ff48f6c4eff3d3d461743d74e1"); assert(parts); - assert.equal(parts.host, "ssh://git@github.com/GitbookIO/gitbook.git"); + assert.equal(parts.host, "git@github.com:GitbookIO/gitbook.git"); assert.equal(parts.ref, "e1594cde2c32e4ff48f6c4eff3d3d461743d74e1"); assert.equal(parts.filepath, "directory/README.md"); }); |