use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class GitRepositoryTest method commitWithLargeDescription.
@Test
public void commitWithLargeDescription() throws IOException, RepoException, ValidationException {
String line = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\n";
int repeats = SOME_LARGE_INPUT_SIZE / line.getBytes(StandardCharsets.UTF_8).length;
StringBuilder descBuilder = new StringBuilder();
for (int i = 0; i < repeats; i++) {
descBuilder.append(line);
}
Files.write(workdir.resolve("foo.txt"), new byte[] {});
repository.add().files("foo.txt").run();
ZonedDateTime date = ZonedDateTime.now(ZoneId.of("-07:00")).truncatedTo(ChronoUnit.SECONDS);
String description = descBuilder.toString();
repository.commit("Foo <foo@bara.com>", date, description);
ImmutableList<GitLogEntry> entries = repository.log(defaultBranch).includeBody(true).includeFiles(false).run();
assertThat(Iterables.getOnlyElement(entries).getBody()).isEqualTo(description);
}
use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class GithubPROrigin method newReader.
@Override
public Reader<GitRevision> newReader(Glob originFiles, Authoring authoring) throws ValidationException {
return new ReaderImpl(url, originFiles, authoring, gitOptions, gitOriginOptions, generalOptions, /*includeBranchCommitLogs=*/
false, submoduleStrategy, firstParent) {
/**
* Disable rebase since this is controlled by useMerge field.
*/
@Override
protected void maybeRebase(GitRepository repo, GitRevision ref, Path workdir) throws RepoException, CannotResolveRevisionException {
}
@Override
public Optional<Baseline<GitRevision>> findBaseline(GitRevision startRevision, String label) throws RepoException, ValidationException {
if (!baselineFromBranch) {
return super.findBaseline(startRevision, label);
}
return findBaselinesWithoutLabel(startRevision, /*limit=*/
1).stream().map(e -> new Baseline<>(e.getSha1(), e)).findFirst();
}
@Override
public ImmutableList<GitRevision> findBaselinesWithoutLabel(GitRevision startRevision, int limit) throws RepoException, ValidationException {
String baseline = startRevision.associatedLabels().get(GITHUB_BASE_BRANCH_SHA1);
Preconditions.checkNotNull(baseline, "%s label should be present in %s", GITHUB_BASE_BRANCH_SHA1, startRevision);
GitRevision baselineRev = getRepository().resolveReference(baseline);
// Don't skip the first change as it is already the baseline
BaselinesWithoutLabelVisitor<GitRevision> visitor = new BaselinesWithoutLabelVisitor<>(originFiles, limit, /*skipFirst=*/
false);
visitChanges(baselineRev, visitor);
return visitor.getResult();
}
@Override
public Endpoint getFeedbackEndPoint() {
return new GitHubEndPoint(githubOptions, url);
}
/**
* Deal with the case of useMerge. We have a new commit (the merge) and first-parent from that
* commit doesn't work for this case.
*/
@Override
public ChangesResponse<GitRevision> changes(@Nullable GitRevision fromRef, GitRevision toRef) throws RepoException {
if (!useMerge) {
return super.changes(fromRef, toRef);
}
GitLogEntry merge = Iterables.getOnlyElement(getRepository().log(toRef.getSha1()).withLimit(1).run());
// Fast-forward merge
if (merge.getParents().size() == 1) {
return super.changes(fromRef, toRef);
}
// HEAD of the Pull Request
GitRevision gitRevision = merge.getParents().get(1);
ChangesResponse<GitRevision> prChanges = super.changes(fromRef, gitRevision);
// origin_files
if (prChanges.isEmpty()) {
return prChanges;
}
try {
return ChangesResponse.forChanges(ImmutableList.<Change<GitRevision>>builder().addAll(prChanges.getChanges()).add(change(merge.getCommit())).build());
} catch (EmptyChangeException e) {
throw new RepoException("Error getting the merge commit information: " + merge, e);
}
}
@Nullable
@Override
public String getGroupIdentity(GitRevision rev) throws RepoException {
return rev.associatedLabels().get(GITHUB_PR_NUMBER_LABEL);
}
};
}
use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class GithubPrDestinationTest method checkWrite.
private void checkWrite(String groupId) throws ValidationException, RepoException, IOException {
gitApiMockHttpTransport = new GitApiMockHttpTransport() {
@Override
protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
boolean isPulls = "https://api.github.com/repos/foo/pulls".equals(url);
if ("GET".equals(method) && isPulls) {
return "[]".getBytes(UTF_8);
} else if ("POST".equals(method) && isPulls) {
assertThat(request.getContentAsString()).isEqualTo("{\"base\":\"master\",\"body\":\"test summary\",\"head\":\"feature\",\"title\":\"test summary\"}");
return ("{\n" + " \"id\": 1,\n" + " \"number\": 12345,\n" + " \"state\": \"open\",\n" + " \"title\": \"test summary\",\n" + " \"body\": \"test summary\"" + "}").getBytes();
}
fail(method + " " + url);
throw new IllegalStateException();
}
};
GithubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + " url = 'https://github.com/foo'" + ")");
Writer<GitRevision> writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
false, groupId, /*oldWriter=*/
null);
GitRepository remote = localHubRepo("foo");
addFiles(remote, null, "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").build());
Files.write(this.workdir.resolve("test.txt"), "some content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), console);
Files.write(this.workdir.resolve("test.txt"), "other content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("two")), console);
// Use a new writer that shares the old state
writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
false, groupId, /*oldWriter=*/
writer);
Files.write(this.workdir.resolve("test.txt"), "and content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("three")), console);
console.assertThat().timesInLog(1, MessageType.INFO, "Pull Request https://github.com/foo/pull/12345 created using branch 'feature'.");
assertThat(remote.refExists("feature")).isTrue();
assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n", "test summary\n" + "\n" + "DummyOrigin-RevId: two\n", "test summary\n" + "\n" + "DummyOrigin-RevId: three\n");
// If we don't keep writer state (same as a new migration). We do a rebase of
// all the changes.
writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
false, groupId, /*oldWriter=*/
null);
Files.write(this.workdir.resolve("test.txt"), "and content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("four")), console);
assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: four\n");
}
use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class GithubPrDestinationTest method testWriteNoMaster.
@Test
public void testWriteNoMaster() throws ValidationException, IOException, RepoException {
gitApiMockHttpTransport = new GitApiMockHttpTransport() {
@Override
protected byte[] getContent(String method, String url, MockLowLevelHttpRequest request) throws IOException {
boolean isPulls = "https://api.github.com/repos/foo/pulls".equals(url);
if ("GET".equals(method) && isPulls) {
return "[]".getBytes(UTF_8);
} else if ("POST".equals(method) && isPulls) {
assertThat(request.getContentAsString()).isEqualTo("{\"base\":\"other\",\"body\":\"test summary\",\"head\":\"feature\",\"title\":\"test summary\"}");
return ("{\n" + " \"id\": 1,\n" + " \"number\": 12345,\n" + " \"state\": \"open\",\n" + " \"title\": \"test summary\",\n" + " \"body\": \"test summary\"" + "}").getBytes();
}
fail(method + " " + url);
throw new IllegalStateException();
}
};
GithubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + " url = 'https://github.com/foo'," + " destination_ref = 'other'," + ")");
Writer<GitRevision> writer = d.newWriter(Glob.ALL_FILES, /*dryRun=*/
false, "feature", /*oldWriter=*/
null);
GitRepository remote = localHubRepo("foo");
addFiles(remote, "master", "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").build());
addFiles(remote, "other", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").build());
Files.write(this.workdir.resolve("test.txt"), "some content".getBytes());
writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), console);
assertThat(remote.refExists("feature")).isTrue();
assertThat(Iterables.transform(remote.log("feature").run(), GitLogEntry::getBody)).containsExactly("first change\n", "second change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n");
}
use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.
the class ChangeReader method parseChanges.
private ImmutableList<Change<GitRevision>> parseChanges(ImmutableList<GitLogEntry> logEntries) throws RepoException {
ImmutableList.Builder<Change<GitRevision>> result = ImmutableList.builder();
GitRevision last = null;
for (GitLogEntry e : logEntries) {
// Keep the first commit if repeated (merge commits).
if (last != null && last.equals(e.getCommit())) {
continue;
}
last = e.getCommit();
result.add(new Change<>(e.getCommit().withUrl(url), filterAuthor(e.getAuthor()), e.getBody() + branchCommitLog(e.getCommit(), e.getParents()), e.getAuthorDate(), ChangeMessage.parseAllAsLabels(e.getBody()).labelsAsMultimap(), e.getFiles(), e.getParents().size() > 1, e.getParents()));
}
return result.build().reverse();
}
Aggregations