message: moving the looping behaviour to the finalize function
diff: @@ -82,17 +82,29 @@ class Change {
  async finalize(commitOpts) {
  let newCommit = await this._makeCommit(commitOpts);
  - let commitId;
  + let delayTime = 500;
  + let mergeCommit;
  + while (delayTime <= 5000) {
  + mergeCommit = await this._makeMergeCommit(newCommit, commitOpts);
  +
  + try {
  if(this.fetchOpts) {
  - commitId = await this._pushCommit(newCommit, commitOpts);
  + await this._pushCommit(mergeCommit);
  } else {
  - let mergeCommit = await this._makeMergeCommit(newCommit, commitOpts);
  await this._applyCommit(mergeCommit);
  - commitId = mergeCommit.id().tostrS();
  + }
  + } catch (err) {
  + await delay(delayTime);
  + log.warn('Failed to finalize commit "%s"', err);
  +
  + delayTime *= 2;
  }
  - return commitId;
  + return mergeCommit.id().tostrS();
  + }
  +
  + throw new Error('Failed to finalise commit and could not recover');
  }
  async _makeCommit(commitOpts) {
  @@ -108,10 +120,7 @@ class Change {
  return Commit.lookup(this.repo, commitOid);
  }
  - async _pushCommit(newCommit, commitOpts) {
  - let delayTime = 500;
  - while (delayTime <= 5000) {
  - let mergeCommit = await this._makeMergeCommit(newCommit, commitOpts);
  + async _pushCommit(mergeCommit) {
  const remoteBranchName = `temp-remote-${crypto.randomBytes(20).toString('hex')}`;
  await Branch.create(this.repo, remoteBranchName, mergeCommit, false);
  @@ -119,18 +128,11 @@ class Change {
  try {
  await remote.push([`refs/heads/${remoteBranchName}:refs/heads/${this.targetBranch}`], this.fetchOpts);
  - return newCommit.id().tostrS();
  } catch (err) {
  - // pull remote and retry with longer delay
  - await delay(delayTime);
  + // pull remote before allowing process to continue
  await this.repo.fetchAll(this.fetchOpts);
  -
  - log.warn('Push to remote failed "%s"', err);
  -
  - delayTime *= 2;
  - }
  + throw err;
  }
  - throw new Error('Push failed');
  }
  async _makeMergeCommit(newCommit, commitOpts) {
  