all files / javascript-test-reporter/ git_info.js

97.22% Statements 35/36
64.29% Branches 9/14
100% Functions 9/9
97.22% Lines 35/36
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70                                                                      
var crypto = require('crypto');
var childProcess = require('child_process');
 
function calculateBlobId(content) {
  var header  = 'blob ' + content.length + '\0';
  var store   = header + content;
  var shasum  = crypto.createHash('sha1');
  shasum.update(store);
  return shasum.digest("hex");
}
 
module.exports = {
 
  head: function(cb) {
    childProcess.exec("git log -1 --pretty=format:%H", function (error, stdout, stderr) {
      return cb(error, stdout);
    });
  },
 
  committedAt: function(cb) {
    childProcess.exec("git log -1 --pretty=format:%ct", function (error, stdout, stderr) {
      var result = null;
      var timestamp = null;
      Eif (stdout) {
        timestamp = parseInt(stdout, 10);
        Eif (!isNaN(timestamp) && timestamp !== 0) {
          result = timestamp;
        }
      }
      return cb(error, result);
    });
  },
 
  branch: function(cb) {
    childProcess.exec("git branch", function (error, stdout, stderr) {
      var returnBranch = null;
      Eif (stdout) {
        var branches = stdout.split("\n");
        branches.forEach(function(val) {
          if(val.charAt(0) === "*") {
            returnBranch = val;
          }
        });
        Eif (returnBranch) {
          returnBranch = returnBranch.replace("* ", "");
        }
      }
      return cb(error, returnBranch);
    });
  },
 
  calculateBlobId: calculateBlobId,
 
  blobId: function(path, content) {
    var stdout, returnBlobId = null;
 
    try {
      stdout = childProcess.execSync("git hash-object " + path, { stdio: [0, "pipe", "ignore"] });
    } catch (e) { }
 
    Iif (stdout) {
      returnBlobId = stdout.toString().trim();
    } else {
      returnBlobId = calculateBlobId(content);
    }
 
    return returnBlobId;
  }
};