Program: 39069
Project: 39069
Buggy source:
private void appendPatchSet(final Change.Id changeId,final ReceiveCommand cmd) throws IOException, OrmException {
  final RevCommit c=rp.getRevWalk().parseCommit(cmd.getNewId());
  rp.getRevWalk().parseBody(c);
  if (!validCommitter(cmd,c)) {
    return;
  }
  final Account.Id me=userAccount.getId();
  final ReplaceResult result;
  final Set<Account.Id> oldReviewers=new HashSet<Account.Id>();
  final Set<Account.Id> oldCC=new HashSet<Account.Id>();
  result=db.run(new OrmRunnable<ReplaceResult,ReviewDb>(){
    public ReplaceResult run(    final ReviewDb db,    final Transaction txn,    final boolean isRetry) throws OrmException {
      final Change change;
      if (isRetry) {
        change=db.changes().get(changeId);
        if (change == null) {
          reject(cmd,"String_Node_Str" + changeId.get() + "String_Node_Str");
          return null;
        }
        if (change.getStatus().isClosed()) {
          reject(cmd,"String_Node_Str" + changeId.get() + "String_Node_Str");
          return null;
        }
        if (change.getDest() == null || !proj.getNameKey().equals(change.getDest().getParentKey())) {
          reject(cmd,"String_Node_Str" + changeId.get() + "String_Node_Str"+ proj.getName());
          return null;
        }
      }
 else {
        change=changeCache.get(changeId);
      }
      final HashSet<String> existingRevisions=new HashSet<String>();
      for (      final PatchSet ps : db.patchSets().byChange(changeId)) {
        if (ps.getRevision() != null) {
          existingRevisions.add(ps.getRevision().get());
        }
      }
      if (existingRevisions.contains(c.name())) {
        reject(cmd,"String_Node_Str");
        return null;
      }
      for (      final RevCommit p : c.getParents()) {
        if (existingRevisions.contains(p.name())) {
          reject(cmd,"String_Node_Str");
          return null;
        }
      }
      final PatchSet ps=new PatchSet(change.newPatchSetId());
      ps.setCreatedOn(new Timestamp(System.currentTimeMillis()));
      ps.setUploader(userAccount.getId());
      final PatchSetImporter imp=new PatchSetImporter(server,db,proj.getNameKey(),repo,c,ps,true);
      imp.setTransaction(txn);
      try {
        imp.run();
      }
 catch (      IOException e) {
        throw new OrmException(e);
      }
      final Ref mergedInto=findMergedInto(change.getDest().get(),c);
      final ReplaceResult result=new ReplaceResult();
      result.mergedIntoRef=mergedInto != null ? mergedInto.getName() : null;
      result.change=change;
      result.patchSet=ps;
      result.info=imp.getPatchSetInfo();
      final Account.Id authorId=imp.getPatchSetInfo().getAuthor() != null ? imp.getPatchSetInfo().getAuthor().getAccount() : null;
      final Account.Id committerId=imp.getPatchSetInfo().getCommitter() != null ? imp.getPatchSetInfo().getCommitter().getAccount() : null;
      boolean haveAuthor=false;
      boolean haveCommitter=false;
      final Set<Account.Id> haveApprovals=new HashSet<Account.Id>();
      oldReviewers.clear();
      oldCC.clear();
      result.approvals=db.changeApprovals().byChange(change.getId()).toList();
      for (      ChangeApproval a : result.approvals) {
        haveApprovals.add(a.getAccountId());
        if (a.getValue() != 0) {
          oldReviewers.add(a.getAccountId());
        }
 else {
          oldCC.add(a.getAccountId());
        }
        if (!haveAuthor && authorId != null && a.getAccountId().equals(authorId)) {
          haveAuthor=true;
        }
        if (!haveCommitter && committerId != null && a.getAccountId().equals(committerId)) {
          haveCommitter=true;
        }
        if (me.equals(a.getAccountId())) {
          if (a.getValue() > 0 && ApprovalCategory.SUBMIT.equals(a.getCategoryId())) {
            a.clear();
          }
 else {
          }
        }
 else         if (a.getValue() > 0) {
          a.clear();
        }
      }
      final ChangeMessage msg=new ChangeMessage(new ChangeMessage.Key(change.getId(),ChangeUtil.messageUUID(db)),me,ps.getCreatedOn());
      msg.setMessage("String_Node_Str" + ps.getPatchSetId() + "String_Node_Str");
      db.changeMessages().insert(Collections.singleton(msg),txn);
      result.msg=msg;
      if (result.mergedIntoRef != null) {
        markChangeMergedByPush(db,txn,result);
      }
 else {
        change.setStatus(Change.Status.NEW);
        change.setCurrentPatchSet(imp.getPatchSetInfo());
        ChangeUtil.updated(change);
        db.changeApprovals().update(result.approvals,txn);
        db.changes().update(Collections.singleton(change),txn);
      }
      final List<ApprovalType> allTypes=Common.getGerritConfig().getApprovalTypes();
      if (allTypes.size() > 0) {
        final ApprovalCategory.Id catId=allTypes.get(allTypes.size() - 1).getCategory().getId();
        if (authorId != null && haveApprovals.add(authorId)) {
          insertDummyChangeApproval(result,authorId,catId,db,txn);
        }
        if (committerId != null && haveApprovals.add(committerId)) {
          insertDummyChangeApproval(result,committerId,catId,db,txn);
        }
        for (        final Account.Id reviewer : reviewerId) {
          if (haveApprovals.add(reviewer)) {
            insertDummyChangeApproval(result,reviewer,catId,db,txn);
          }
        }
      }
      return result;
    }
  }
);
  if (result != null) {
    final PatchSet ps=result.patchSet;
    final RefUpdate ru=repo.updateRef(ps.getRefName());
    ru.setNewObjectId(c);
    ru.disableRefLog();
    if (ru.update(rp.getRevWalk()) != RefUpdate.Result.NEW) {
      throw new IOException("String_Node_Str" + ps.getRefName() + "String_Node_Str"+ repo.getDirectory()+ "String_Node_Str"+ ru.getResult());
    }
    PushQueue.scheduleUpdate(proj.getNameKey(),ru.getName());
    cmd.setResult(ReceiveCommand.Result.OK);
    try {
      final ReplacePatchSetSender cm=new ReplacePatchSetSender(server,result.change);
      cm.setFrom(me);
      cm.setPatchSet(ps,result.info);
      cm.setChangeMessage(result.msg);
      cm.setReviewDb(db);
      cm.addReviewers(reviewerId);
      cm.addExtraCC(ccId);
      cm.addReviewers(oldReviewers);
      cm.addExtraCC(oldCC);
      cm.send();
    }
 catch (    EmailException e) {
      log.error("String_Node_Str" + ps.getId(),e);
    }
  }
  sendMergedEmail(result);
}

Fixed version (ground truth):
private void appendPatchSet(final Change.Id changeId,final ReceiveCommand cmd) throws IOException, OrmException {
  final RevCommit c=rp.getRevWalk().parseCommit(cmd.getNewId());
  rp.getRevWalk().parseBody(c);
  if (!validCommitter(cmd,c)) {
    return;
  }
  final Account.Id me=userAccount.getId();
  final ReplaceResult result;
  final Set<Account.Id> oldReviewers=new HashSet<Account.Id>();
  final Set<Account.Id> oldCC=new HashSet<Account.Id>();
  result=db.run(new OrmRunnable<ReplaceResult,ReviewDb>(){
    public ReplaceResult run(    final ReviewDb db,    final Transaction txn,    final boolean isRetry) throws OrmException {
      final Change change;
      if (isRetry) {
        change=db.changes().get(changeId);
        if (change == null) {
          reject(cmd,"String_Node_Str" + changeId.get() + "String_Node_Str");
          return null;
        }
        if (change.getStatus().isClosed()) {
          reject(cmd,"String_Node_Str" + changeId.get() + "String_Node_Str");
          return null;
        }
        if (change.getDest() == null || !proj.getNameKey().equals(change.getDest().getParentKey())) {
          reject(cmd,"String_Node_Str" + changeId.get() + "String_Node_Str"+ proj.getName());
          return null;
        }
      }
 else {
        change=changeCache.get(changeId);
      }
      final HashSet<ObjectId> existingRevisions=new HashSet<ObjectId>();
      for (      final PatchSet ps : db.patchSets().byChange(changeId)) {
        if (ps.getRevision() != null) {
          final String revIdStr=ps.getRevision().get();
          try {
            existingRevisions.add(ObjectId.fromString(revIdStr));
          }
 catch (          IllegalArgumentException e) {
            log.warn("String_Node_Str" + ps.getId() + "String_Node_Str"+ revIdStr);
            reject(cmd,"String_Node_Str");
            return null;
          }
        }
      }
      if (existingRevisions.contains(c.copy())) {
        reject(cmd,"String_Node_Str");
        return null;
      }
      for (      final ObjectId commitId : existingRevisions) {
        try {
          final RevCommit prior=rp.getRevWalk().parseCommit(commitId);
          if (rp.getRevWalk().isMergedInto(prior,c)) {
            reject(cmd,"String_Node_Str");
            return null;
          }
        }
 catch (        IOException e) {
          log.error("String_Node_Str" + changeId + "String_Node_Str"+ commitId.name(),e);
          reject(cmd,"String_Node_Str");
          return null;
        }
      }
      final PatchSet ps=new PatchSet(change.newPatchSetId());
      ps.setCreatedOn(new Timestamp(System.currentTimeMillis()));
      ps.setUploader(userAccount.getId());
      final PatchSetImporter imp=new PatchSetImporter(server,db,proj.getNameKey(),repo,c,ps,true);
      imp.setTransaction(txn);
      try {
        imp.run();
      }
 catch (      IOException e) {
        throw new OrmException(e);
      }
      final Ref mergedInto=findMergedInto(change.getDest().get(),c);
      final ReplaceResult result=new ReplaceResult();
      result.mergedIntoRef=mergedInto != null ? mergedInto.getName() : null;
      result.change=change;
      result.patchSet=ps;
      result.info=imp.getPatchSetInfo();
      final Account.Id authorId=imp.getPatchSetInfo().getAuthor() != null ? imp.getPatchSetInfo().getAuthor().getAccount() : null;
      final Account.Id committerId=imp.getPatchSetInfo().getCommitter() != null ? imp.getPatchSetInfo().getCommitter().getAccount() : null;
      boolean haveAuthor=false;
      boolean haveCommitter=false;
      final Set<Account.Id> haveApprovals=new HashSet<Account.Id>();
      oldReviewers.clear();
      oldCC.clear();
      result.approvals=db.changeApprovals().byChange(change.getId()).toList();
      for (      ChangeApproval a : result.approvals) {
        haveApprovals.add(a.getAccountId());
        if (a.getValue() != 0) {
          oldReviewers.add(a.getAccountId());
        }
 else {
          oldCC.add(a.getAccountId());
        }
        if (!haveAuthor && authorId != null && a.getAccountId().equals(authorId)) {
          haveAuthor=true;
        }
        if (!haveCommitter && committerId != null && a.getAccountId().equals(committerId)) {
          haveCommitter=true;
        }
        if (me.equals(a.getAccountId())) {
          if (a.getValue() > 0 && ApprovalCategory.SUBMIT.equals(a.getCategoryId())) {
            a.clear();
          }
 else {
          }
        }
 else         if (a.getValue() > 0) {
          a.clear();
        }
      }
      final ChangeMessage msg=new ChangeMessage(new ChangeMessage.Key(change.getId(),ChangeUtil.messageUUID(db)),me,ps.getCreatedOn());
      msg.setMessage("String_Node_Str" + ps.getPatchSetId() + "String_Node_Str");
      db.changeMessages().insert(Collections.singleton(msg),txn);
      result.msg=msg;
      if (result.mergedIntoRef != null) {
        markChangeMergedByPush(db,txn,result);
      }
 else {
        change.setStatus(Change.Status.NEW);
        change.setCurrentPatchSet(imp.getPatchSetInfo());
        ChangeUtil.updated(change);
        db.changeApprovals().update(result.approvals,txn);
        db.changes().update(Collections.singleton(change),txn);
      }
      final List<ApprovalType> allTypes=Common.getGerritConfig().getApprovalTypes();
      if (allTypes.size() > 0) {
        final ApprovalCategory.Id catId=allTypes.get(allTypes.size() - 1).getCategory().getId();
        if (authorId != null && haveApprovals.add(authorId)) {
          insertDummyChangeApproval(result,authorId,catId,db,txn);
        }
        if (committerId != null && haveApprovals.add(committerId)) {
          insertDummyChangeApproval(result,committerId,catId,db,txn);
        }
        for (        final Account.Id reviewer : reviewerId) {
          if (haveApprovals.add(reviewer)) {
            insertDummyChangeApproval(result,reviewer,catId,db,txn);
          }
        }
      }
      return result;
    }
  }
);
  if (result != null) {
    final PatchSet ps=result.patchSet;
    final RefUpdate ru=repo.updateRef(ps.getRefName());
    ru.setNewObjectId(c);
    ru.disableRefLog();
    if (ru.update(rp.getRevWalk()) != RefUpdate.Result.NEW) {
      throw new IOException("String_Node_Str" + ps.getRefName() + "String_Node_Str"+ repo.getDirectory()+ "String_Node_Str"+ ru.getResult());
    }
    PushQueue.scheduleUpdate(proj.getNameKey(),ru.getName());
    cmd.setResult(ReceiveCommand.Result.OK);
    try {
      final ReplacePatchSetSender cm=new ReplacePatchSetSender(server,result.change);
      cm.setFrom(me);
      cm.setPatchSet(ps,result.info);
      cm.setChangeMessage(result.msg);
      cm.setReviewDb(db);
      cm.addReviewers(reviewerId);
      cm.addExtraCC(ccId);
      cm.addReviewers(oldReviewers);
      cm.addExtraCC(oldCC);
      cm.send();
    }
 catch (    EmailException e) {
      log.error("String_Node_Str" + ps.getId(),e);
    }
  }
  sendMergedEmail(result);
}