Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Use git hash-object for blob id if available #38

Merged
merged 1 commit into from
Jun 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Formatter.prototype.sourceFiles = function(data) {

source_files.push({
name: fileName,
blob_id: git.calculateBlobId(content),
blob_id: git.blobId(elem.file, content),
coverage: JSON.stringify(coverage)
});
});
Expand Down
31 changes: 24 additions & 7 deletions git_info.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
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) {
Expand Down Expand Up @@ -41,12 +49,21 @@ module.exports = {
});
},

calculateBlobId: function(content) {
var header = 'blob ' + content.length + '\0';
var store = header + content;
var shasum = crypto.createHash('sha1');
shasum.update(store);
return shasum.digest("hex");
}
calculateBlobId: calculateBlobId,

blobId: function(path, content) {
var stdout, returnBlobId = null;

try {
stdout = childProcess.execSync("git hash-object " + path, { stdio: [0, "pipe", "ignore"] });
} catch (e) { }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT of adding a log here of "git hash-object failed. Falling back to manual calculation"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this called for every covered file, logging that out could get pretty noisy. On CI environments where git isn't installed, the operation would fail consistently for every file, potentially creating a lot of output.

I'd lean towards falling back gracefully, which we're doing here.


if (stdout) {
returnBlobId = stdout.toString().trim();
} else {
returnBlobId = calculateBlobId(content);
}

return returnBlobId;
}
};