0

I'm trying to change emails in commits using nodegit.

here is my code:

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)
})

Works without errors, but no changes being applied. What am I doing wrong?

stkvtflw
  • 12,092
  • 26
  • 78
  • 155
  • The real problem here is that you cannot simply amend a commit deeper in history and hope that everything will reconnect itself by "auto-magic". Once you need to change those commits, you actually need to create a new ones and connect them yourself by `parent-id`. – petrpulc Sep 14 '17 at 09:05

1 Answers1

0

Use provided tools if possible.

git filter-branch command is everything you possibly need.

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

See GitHub help for full command.

petrpulc
  • 940
  • 6
  • 22
  • i need something more complicated. With filter-branch i have to write it in bash script, which is more difficult for me – stkvtflw Sep 14 '17 at 08:53
  • Well, but then you need to solve a lot of things along the way yourself. Most importantly the reconnection of individual commits into new (filtered) branch. And, btw, with the `env-filter` you are just redefining the environment variables, no extended scripting needed... Answer updated. – petrpulc Sep 14 '17 at 08:56
  • thank you for your efforts @petrpulc! Though, i know this and i have no problem replacing metadata. What i need now is to create script, which will revert such changes – stkvtflw Sep 14 '17 at 09:43
  • 1
    @stkvtflw If you use `git filter-branch` a backup branch is created: `refs/original/refs/heads/`; otherwise you need to remember the original HEAD position (i.e. use `reflog`); all with a strict assumption that you are working on local copy of the repository – petrpulc Sep 14 '17 at 11:58