2015-07-06 64 views
0

如何計算Github存儲庫內容的校驗和,以便兩個具有不同歷史記錄但內容相同的repos產生相同校驗和?Github Repository的校驗和

我試圖使用存儲庫的tarball來計算校驗和,但由於文件之間的時間戳不同而導致麻煩,因爲這些文件之間的時間戳相同。

回答

0

以下是可能需要其他人的解決方案。

它從最新提交的樹中的對象的散列創建散列。

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/[email protected]"></script> 
 
<div id="log"></div>