Implement MatchAuthorToCommitterDate for applicable strategies

Bug: https://crbug.com/732968
Change-Id: I0408d3c871d9120855c180d849cafc791f872b23
This commit is contained in:
Patrick Hiesel
2017-06-22 15:34:53 +02:00
parent 5a31635f30
commit a573bd72e6
5 changed files with 58 additions and 2 deletions

View File

@@ -42,6 +42,7 @@ import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.common.data.Permission; import com.google.gerrit.common.data.Permission;
import com.google.gerrit.extensions.api.changes.SubmitInput; import com.google.gerrit.extensions.api.changes.SubmitInput;
import com.google.gerrit.extensions.api.projects.BranchInput; import com.google.gerrit.extensions.api.projects.BranchInput;
import com.google.gerrit.extensions.api.projects.ConfigInput;
import com.google.gerrit.extensions.api.projects.ProjectInput; import com.google.gerrit.extensions.api.projects.ProjectInput;
import com.google.gerrit.extensions.client.ChangeStatus; import com.google.gerrit.extensions.client.ChangeStatus;
import com.google.gerrit.extensions.client.InheritableBoolean; import com.google.gerrit.extensions.client.InheritableBoolean;
@@ -989,6 +990,31 @@ public abstract class AbstractSubmit extends AbstractDaemonTest {
assertThat(input.generateLockFailures).containsExactly(false); assertThat(input.generateLockFailures).containsExactly(false);
} }
@Test
public void authorAndCommitDateAreEqual() throws Exception {
assume().that(getSubmitType()).isNotEqualTo(SubmitType.FAST_FORWARD_ONLY);
ConfigInput ci = new ConfigInput();
ci.matchAuthorToCommitterDate = InheritableBoolean.TRUE;
gApi.projects().name(project.get()).config(ci);
RevCommit initialHead = getRemoteHead();
testRepo.reset(initialHead);
PushOneCommit.Result change = createChange("Change 1", "b", "b");
testRepo.reset(initialHead);
PushOneCommit.Result change2 = createChange("Change 2", "c", "c");
if (getSubmitType() == SubmitType.MERGE_IF_NECESSARY
|| getSubmitType() == SubmitType.REBASE_IF_NECESSARY) {
// Merge another change so that change2 is not a fast-forward
submit(change.getChangeId());
}
submit(change2.getChangeId());
assertAuthorAndCommitDateEquals(getRemoteHead());
}
private void setChangeStatusToNew(PushOneCommit.Result... changes) throws Exception { private void setChangeStatusToNew(PushOneCommit.Result... changes) throws Exception {
for (PushOneCommit.Result change : changes) { for (PushOneCommit.Result change : changes) {
try (BatchUpdate bu = try (BatchUpdate bu =
@@ -1142,6 +1168,12 @@ public abstract class AbstractSubmit extends AbstractDaemonTest {
assertThat(actual.getTimeZone()).isEqualTo(expected.getTimeZone()); assertThat(actual.getTimeZone()).isEqualTo(expected.getTimeZone());
} }
protected void assertAuthorAndCommitDateEquals(RevCommit commit) {
assertThat(commit.getAuthorIdent().getWhen()).isEqualTo(commit.getCommitterIdent().getWhen());
assertThat(commit.getAuthorIdent().getTimeZone())
.isEqualTo(commit.getCommitterIdent().getTimeZone());
}
protected void assertSubmitter(String changeId, int psId) throws Exception { protected void assertSubmitter(String changeId, int psId) throws Exception {
assertSubmitter(changeId, psId, admin); assertSubmitter(changeId, psId, admin);
} }

View File

@@ -67,6 +67,7 @@ public class RebaseChangeOp implements BatchUpdateOp {
private boolean copyApprovals = true; private boolean copyApprovals = true;
private boolean detailedCommitMessage; private boolean detailedCommitMessage;
private boolean postMessage = true; private boolean postMessage = true;
private boolean matchAuthorToCommitterDate = false;
private RevCommit rebasedCommit; private RevCommit rebasedCommit;
private PatchSet.Id rebasedPatchSetId; private PatchSet.Id rebasedPatchSetId;
@@ -131,6 +132,11 @@ public class RebaseChangeOp implements BatchUpdateOp {
return this; return this;
} }
public RebaseChangeOp setMatchAuthorToCommitterDate(boolean matchAuthorToCommitterDate) {
this.matchAuthorToCommitterDate = matchAuthorToCommitterDate;
return this;
}
@Override @Override
public void updateRepo(RepoContext ctx) public void updateRepo(RepoContext ctx)
throws MergeConflictException, InvalidChangeOperationException, RestApiException, IOException, throws MergeConflictException, InvalidChangeOperationException, RestApiException, IOException,
@@ -263,6 +269,11 @@ public class RebaseChangeOp implements BatchUpdateOp {
} else { } else {
cb.setCommitter(ctx.getIdentifiedUser().newCommitterIdent(ctx.getWhen(), ctx.getTimeZone())); cb.setCommitter(ctx.getIdentifiedUser().newCommitterIdent(ctx.getWhen(), ctx.getTimeZone()));
} }
if (matchAuthorToCommitterDate) {
cb.setAuthor(
new PersonIdent(
cb.getAuthor(), cb.getCommitter().getWhen(), cb.getCommitter().getTimeZone()));
}
ObjectId objectId = ctx.getInserter().insert(cb); ObjectId objectId = ctx.getInserter().insert(cb);
ctx.getInserter().flush(); ctx.getInserter().flush();
return ctx.getRevWalk().parseCommit(objectId); return ctx.getRevWalk().parseCommit(objectId);

View File

@@ -248,6 +248,7 @@ public class MergeUtil {
mergeCommit.setAuthor(originalCommit.getAuthorIdent()); mergeCommit.setAuthor(originalCommit.getAuthorIdent());
mergeCommit.setCommitter(cherryPickCommitterIdent); mergeCommit.setCommitter(cherryPickCommitterIdent);
mergeCommit.setMessage(commitMsg); mergeCommit.setMessage(commitMsg);
matchAuthorToCommitterDate(project, mergeCommit);
return rw.parseCommit(inserter.insert(mergeCommit)); return rw.parseCommit(inserter.insert(mergeCommit));
} }
throw new MergeConflictException("merge conflict"); throw new MergeConflictException("merge conflict");
@@ -857,4 +858,14 @@ public class MergeUtil {
throw new ResourceNotFoundException(e.getMessage()); throw new ResourceNotFoundException(e.getMessage());
} }
} }
private static void matchAuthorToCommitterDate(ProjectState project, CommitBuilder commit) {
if (project.isMatchAuthorToCommitterDate()) {
commit.setAuthor(
new PersonIdent(
commit.getAuthor(),
commit.getCommitter().getWhen(),
commit.getCommitter().getTimeZone()));
}
}
} }

View File

@@ -28,7 +28,8 @@ class MergeOneOp extends SubmitStrategyOp {
@Override @Override
public void updateRepoImpl(RepoContext ctx) throws IntegrationException, IOException { public void updateRepoImpl(RepoContext ctx) throws IntegrationException, IOException {
PersonIdent caller = PersonIdent caller =
ctx.getIdentifiedUser().newCommitterIdent(ctx.getWhen(), ctx.getTimeZone()); ctx.getIdentifiedUser()
.newCommitterIdent(args.serverIdent.getWhen(), args.serverIdent.getTimeZone());
if (args.mergeTip.getCurrentTip() == null) { if (args.mergeTip.getCurrentTip() == null) {
throw new IllegalStateException( throw new IllegalStateException(
"cannot merge commit " "cannot merge commit "

View File

@@ -179,7 +179,8 @@ public class RebaseSubmitStrategy extends SubmitStrategy {
.setDetailedCommitMessage(rebaseAlways) .setDetailedCommitMessage(rebaseAlways)
// Do not post message after inserting new patchset because there // Do not post message after inserting new patchset because there
// will be one about change being merged already. // will be one about change being merged already.
.setPostMessage(false); .setPostMessage(false)
.setMatchAuthorToCommitterDate(args.project.isMatchAuthorToCommitterDate());
try { try {
rebaseOp.updateRepo(ctx); rebaseOp.updateRepo(ctx);
} catch (MergeConflictException | NoSuchChangeException e) { } catch (MergeConflictException | NoSuchChangeException e) {