2017-09-14 56 views
0

我試圖使用nodegit更改提交中的電子郵件。使用nodegit修改提交元數據

這裏是我的代碼:

var Git = require('nodegit') 

Git.Repository.open('repo') 
.then(repository => { 
    return repository.getBranchCommit('dev') 
}) 
.then(commit => { 
    var eventEmitter = commit.history(); 
    return new Promise(resolve => { 
    eventEmitter.on('end', commits => { 
     // Use commits 
     commits.forEach(async (commit) => { 
     const newSignature = Git.Signature.create('New name', '[email protected]', commit.time(), commit.timeOffset()); 
     await commit.amend(
      commit.id(), 
      newSignature, 
      newSignature, 
      commit.messageEncoding(), 
      commit.message(), 
      commit.getTree(), 
     ) 
     .then(e => { 
      console.log('e', e) 
      console.log('commit email', commit.committer().email()) // returns old email 
     }) 
     .catch(err => console.log('err', err)) 
     }) 
     resolve() 
    }); 

    eventEmitter.on('error', error => { 
     // Use error 
     console.log('error', error) 
    }); 

    eventEmitter.start() 
    }) 
}) 
.then(() => { 
    console.log('done') 
}) 
.catch(err => { 
    console.error('ERROR:', err) 
}) 

工程沒有錯誤,但沒有施加的變化。我究竟做錯了什麼?

+0

這裏真正的問題是,你不能簡單地修改歷史上更深層的承諾,並希望一切都會通過「自動魔法」重新連接自己。一旦你需要改變這些提交,你實際上需要創建一個新的提交併通過'parent-id'自己連接它們。 – petrpulc

回答

1

如果可能,請使用提供的工具。

git filter-branch命令是您可能需要的一切。

git filter-branch --env-filter ' 
export GIT_COMMITTER_NAME="New name" 
export GIT_COMMITTER_EMAIL="[email protected]" 
export GIT_AUTHOR_NAME="New name" 
export GIT_AUTHOR_EMAIL="[email protected]" 
' --tag-name-filter cat -- --branches --tags 

查看GitHub help查看完整的命令。

+0

我需要更復雜的東西。有了filter-branch,我必須用bash腳本編寫它,這對我來說更加困難 – stkvtflw

+0

好吧,但是你需要自己解決很多事情。最重要的是單個提交重新連接到新的(過濾)分支。而且,順便說一下,使用'env-filter',你只需重新定義環境變量,不需要擴展腳本......答案已更新。 – petrpulc

+0

感謝您的努力@petrpulc!雖然,我知道這一點,我沒有問題,取代元數據。我現在需要的是創建腳本,它將恢復這種變化 – stkvtflw