PushOneCommit: Use simple counter for Change-Id

Using a simple counter makes it clearer that the generated Change-Ids
are guaranteed to be unique.

Change-Id: I0154bb05ea1969a2684b247ce1661d65591526ad
This commit is contained in:
Dave Borowitz 2017-04-05 12:45:23 -04:00 committed by David Pursehouse
parent 6942451166
commit ca7069f766

View File

@ -37,6 +37,7 @@ import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import org.eclipse.jgit.api.TagCommand;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.PersonIdent;
@ -122,6 +123,18 @@ public class PushOneCommit {
}
}
private static AtomicInteger CHANGE_ID_COUNTER = new AtomicInteger();
private static String nextChangeId() {
// Tests use a variety of mechanisms for setting temporary timestamps, so we can't guarantee
// that the PersonIdent (or any other field used by the Change-Id generator) for any two test
// methods in the same acceptance test class are going to be different. But tests generally
// assume that Change-Ids are unique unless otherwise specified. So, don't even bother trying to
// reuse JGit's Change-Id generator, just do the simplest possible thing and convert a counter
// to hex.
return String.format("%040x", CHANGE_ID_COUNTER.incrementAndGet());
}
private final ChangeNotes.Factory notesFactory;
private final ApprovalsUtil approvalsUtil;
private final Provider<InternalChangeQuery> queryProvider;
@ -267,7 +280,7 @@ public class PushOneCommit {
if (changeId != null) {
commitBuilder = testRepo.amendRef("HEAD").insertChangeId(changeId.substring(1));
} else {
commitBuilder = testRepo.branch("HEAD").commit().insertChangeId();
commitBuilder = testRepo.branch("HEAD").commit().insertChangeId(nextChangeId());
}
commitBuilder.message(subject).author(i).committer(new PersonIdent(i, testRepo.getDate()));
}