diff options
author | Samy Pessé <samypesse@gmail.com> | 2015-09-15 12:22:54 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2015-09-15 12:22:54 +0200 |
commit | 2a52326a454a23444bd8f0395a9ab8a1f5f68831 (patch) | |
tree | cf5f447e84e3b4ac2d74d7092d8c522bd6e67b79 /lib/utils/git.js | |
parent | 87e4ee1eeb9918cbf151407c66b3377014612d5d (diff) | |
download | gitbook-2a52326a454a23444bd8f0395a9ab8a1f5f68831.zip gitbook-2a52326a454a23444bd8f0395a9ab8a1f5f68831.tar.gz gitbook-2a52326a454a23444bd8f0395a9ab8a1f5f68831.tar.bz2 |
Improve conrefs to handle all absolute file paths correctly
Add test for it
Diffstat (limited to 'lib/utils/git.js')
-rw-r--r-- | lib/utils/git.js | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/lib/utils/git.js b/lib/utils/git.js index 5f17395..6eb9681 100644 --- a/lib/utils/git.js +++ b/lib/utils/git.js @@ -6,6 +6,7 @@ var path = require("path"); var crc = require("crc"); var exec = Q.denodeify(require("child_process").exec); var URI = require("URIjs"); +var pathUtil = require("./path"); var fs = require("./fs"); @@ -89,7 +90,6 @@ function cloneGitRepo(host, ref) { }); } - // Get file from a git repo function resolveFileFromGit(giturl) { if (_.isString(giturl)) giturl = parseGitUrl(giturl); @@ -104,9 +104,26 @@ function resolveFileFromGit(giturl) { }); }; +// Return root of git repo from a filepath +function resolveGitRoot(filepath) { + var relativeToGit, repoId + + // No git repo cloned, or file is not in a git repository + if (!GIT_TMP || !pathUtil.isInRoot(GIT_TMP, filepath)) return null; + + // Extract first directory (is the repo id) + relativeToGit = path.relative(GIT_TMP, filepath); + repoId = _.first(relativeToGit.split(path.sep)); + if (!repoId) return; + + // Return an absolute file + return path.resolve(GIT_TMP, repoId); +}; + module.exports = { checkUrl: checkGitUrl, parseUrl: parseGitUrl, - resolveFile: resolveFileFromGit + resolveFile: resolveFileFromGit, + resolveRoot: resolveGitRoot }; |