我如何计算Github存储库的内容的校验和,使具有不同历史但内容相同的两个repos产生相同的校验和?
我试图用存储库的tarball计算校验和,但由于不同文件之间的时间戳不同,因此遇到了麻烦,而这些时间戳在其他方面是相同的。
发布于 2015-07-06 16:54:11
对于其他可能需要的人,这里有一个解决方案。
它从最近一次提交的树中的对象哈希创建一个散列。
console = { log: log };
// construct hash of a github repository
function getRepoHash(login, repo, cb) {
// get latest commit
ghapi('getRepoCommits', login, repo, { per_page: 1 }, function (err, res, commits) {
// get tree of last commit
ghapi('getRepoGitTree', login, repo, commits[0].sha, function (err, res, body) {
// construct an array of shas from tree
var shas = body.tree.map(function (obj) {
return obj.sha;
});
// hash the shas
var repoSha = sha1(shas.toString()).toString();
cb(null, repoSha);
})
})
}
// nodejs/io.js vs joyent/node
getRepoHash('nodejs', 'io.js', function (err, sha1) {
getRepoHash('joyent', 'node', function (err, sha2) {
var equal = sha1 === sha2;
console.log('nodejs/io.js '+ sha1)
console.log('joyent/node '+ sha2)
console.log('equal? '+ equal)
});
});
// logging
function log(message) {
var logger = document.getElementById('log');
logger.innerHTML += message + '<br>';
}<script src="https://cdnjs.cloudflare.com/ajax/libs/js-sha1/0.3.0/sha1.min.js"></script>
<script src="https://wzrd.in/standalone/ghapi@latest"></script>
<div id="log"></div>
https://stackoverflow.com/questions/31240445
复制相似问题