Code coverage report for lib/git/url_prefix.js

Statements: 100% (12 / 12)      Branches: 100% (2 / 2)      Functions: 100% (1 / 1)      Lines: 100% (12 / 12)      Ignored: none     

All files » lib/git/ » url_prefix.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 2627 27 27 27                   27 4 4 4 3   1   4     27  
var fs = require('fs');
var path = require('path');
var urlFromGit = require('github-url-from-git');
var getRemoteOrigin = require('remote-origin-url');
 
/**
 * Given a a root directory, find its git configuration and figure out
 * the HTTPS URL at the base of that GitHub repository.
 *
 * @param {string} root path at the base of this local repo
 * @returns {string} base HTTPS url of the GitHub repository
 * @throws {Error} if the root is not a git repo
 */
function getGithubURLPrefix(root) {
  var head = fs.readFileSync(path.join(root, '.git', 'HEAD'), 'utf8');
  var branch = head.match(/ref\: (.*)/);
  if (branch) {
    var sha = fs.readFileSync(path.join(root, '.git', branch[1]), 'utf8');
  } else {
    sha = head;
  }
  return urlFromGit(getRemoteOrigin.sync(root)) + '/blob/' + sha.trim() + '/';
}
 
module.exports = getGithubURLPrefix;