修改使用nodegit提交元数据

时间:2017-09-14 07:27:53

标签: node.js git 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', 'new@email.com', 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)
})

无错误地工作,但不应用任何更改。我做错了什么?

1 个答案:

答案 0 :(得分:1)

尽可能使用提供的工具。

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

git filter-branch --env-filter '
export GIT_COMMITTER_NAME="New name"
export GIT_COMMITTER_EMAIL="new@email.com"
export GIT_AUTHOR_NAME="New name"
export GIT_AUTHOR_EMAIL="new@email.com"
' --tag-name-filter cat -- --branches --tags

有关完整命令,请参阅GitHub help