Search in sources :

Example 11 with GitLogEntry

use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.

the class GitHubPrDestinationTest method testWriteNomain.

@Test
public void testWriteNomain() throws ValidationException, IOException, RepoException {
    GitHubPrDestination d = skylark.eval("r", "r = git.github_pr_destination(" + "    url = 'https://github.com/foo'," + "    destination_ref = 'other'," + ")");
    DummyRevision dummyRevision = new DummyRevision("dummyReference", "feature");
    WriterContext writerContext = new WriterContext("piper_to_github_pr", "TEST", false, dummyRevision, Glob.ALL_FILES.roots());
    String branchName = Identity.computeIdentity("OriginGroupIdentity", dummyRevision.contextReference(), writerContext.getWorkflowName(), "copy.bara.sky", writerContext.getWorkflowIdentityUser());
    mockNoPullRequestsGet(branchName);
    gitUtil.mockApi("POST", "https://api.github.com/repos/foo/pulls", mockResponseAndValidateRequest("{\n" + "  \"id\": 1,\n" + "  \"number\": 12345,\n" + "  \"state\": \"open\",\n" + "  \"title\": \"test summary\",\n" + "  \"body\": \"test summary\"" + "}", MockRequestAssertion.equals("{\"base\":\"other\",\"body\":\"test summary\\n\",\"head\":\"" + branchName + "\",\"title\":\"test summary\"}")));
    Writer<GitRevision> writer = d.newWriter(writerContext);
    GitRepository remote = gitUtil.mockRemoteRepo("github.com/foo");
    addFiles(remote, "main", "first change", ImmutableMap.<String, String>builder().put("foo.txt", "").buildOrThrow());
    addFiles(remote, "other", "second change", ImmutableMap.<String, String>builder().put("foo.txt", "test").buildOrThrow());
    writeFile(this.workdir, "test.txt", "some content");
    writer.write(TransformResults.of(this.workdir, new DummyRevision("one")), Glob.ALL_FILES, console);
    assertThat(remote.refExists(branchName)).isTrue();
    assertThat(Iterables.transform(remote.log(branchName).run(), GitLogEntry::getBody)).containsExactly("first change\n", "second change\n", "test summary\n" + "\n" + "DummyOrigin-RevId: one\n");
}
Also used : WriterContext(com.google.copybara.WriterContext) DummyRevision(com.google.copybara.testing.DummyRevision) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 12 with GitLogEntry

use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.

the class GitMirrorTest method testMerge.

@Test
public void testMerge() throws Exception {
    Migration mirror = mergeInit();
    GitLogEntry destChange = repoChange(destRepo, "some_file", "Content", "destination only");
    GitLogEntry originChange = repoChange(originRepo, "some_other_file", "Content", "new change");
    mirror.run(workdir, ImmutableList.of());
    GitLogEntry merge = lastChange(destRepo, primaryBranch);
    // It is a merge
    assertThat(merge.getParents()).containsExactly(destChange.getCommit(), originChange.getCommit());
    // OSS branch is updated with origin version.
    assertThat(lastChange(destRepo, "oss").getCommit()).isEqualTo(originChange.getCommit());
}
Also used : Migration(com.google.copybara.config.Migration) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 13 with GitLogEntry

use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.

the class GitMirrorTest method testMergeConflict.

@Test
public void testMergeConflict() throws Exception {
    Migration mirror = mergeInit();
    GitLogEntry destChange = repoChange(destRepo, "some_file", "Hello", "destination only");
    GitLogEntry originChange = repoChange(originRepo, "some_file", "Bye", "new change");
    assertThat((assertThrows(ValidationException.class, new ThrowingRunnable() {

        @Override
        public void run() throws Throwable {
            mirror.run(workdir, ImmutableList.of());
        }
    }))).hasMessageThat().contains("Conflict merging refs/heads/" + primaryBranch);
}
Also used : Migration(com.google.copybara.config.Migration) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) ThrowingRunnable(org.junit.function.ThrowingRunnable) Test(org.junit.Test)

Example 14 with GitLogEntry

use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.

the class GitRepositoryTest method checkLog.

private void checkLog(boolean body, boolean includeFiles) throws IOException, RepoException, ValidationException {
    workdir = Files.createTempDirectory("workdir");
    this.repository = GitRepository.newBareRepo(Files.createTempDirectory("gitdir"), getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).withWorkTree(workdir).init();
    Files.write(workdir.resolve("foo.txt"), "foo fooo fooo".getBytes(UTF_8));
    repository.add().files("foo.txt").run();
    ZonedDateTime date = ZonedDateTime.now(ZoneId.of("-07:00")).truncatedTo(ChronoUnit.SECONDS);
    ZonedDateTime date2 = date.plus(1, ChronoUnit.SECONDS).withZoneSameInstant(ZoneId.of("-05:00"));
    repository.commit("Foo <foo@bara.com>", date, "message");
    // Test rename to check that we use --name-only with --no-renames
    Files.move(workdir.resolve("foo.txt"), workdir.resolve("bar.txt"));
    Files.write(workdir.resolve("baz.txt"), "baz baz baz".getBytes(UTF_8));
    repository.add().all().run();
    repository.commit("Bar <bar@bara.com>", date2, "message\n\nand\nparagraph");
    ImmutableList<GitLogEntry> entries = repository.log(defaultBranch).includeBody(body).includeFiles(includeFiles).run();
    assertThat(entries.size()).isEqualTo(2);
    assertThat(entries.get(0).getBody()).isEqualTo(body ? "message\n\nand\nparagraph\n" : null);
    assertThat(entries.get(1).getBody()).isEqualTo(body ? "message\n" : null);
    assertThat(entries.get(0).getAuthor()).isEqualTo(new Author("Bar", "bar@bara.com"));
    assertThat(entries.get(0).getCommitter()).isEqualTo(COMMITER);
    assertThat(entries.get(0).getAuthorDate()).isEqualTo(date2);
    assertThat(entries.get(1).getAuthor()).isEqualTo(new Author("FOO", "foo@bara.com"));
    assertThat(entries.get(1).getCommitter()).isEqualTo(COMMITER);
    assertThat(entries.get(1).getAuthorDate()).isEqualTo(date);
    assertThat(entries.get(0).getParents()).containsExactly(entries.get(1).getCommit());
    assertThat(entries.get(1).getParents()).isEmpty();
    assertThat(repository.simpleCommand("cat-file", "-t", entries.get(0).getTree()).getStdout().trim()).isEqualTo("tree");
    if (includeFiles) {
        assertThat(entries.get(0).getFiles()).containsExactly("foo.txt", "bar.txt", "baz.txt");
        assertThat(entries.get(1).getFiles()).containsExactly("foo.txt");
    } else {
        assertThat(entries.get(0).getFiles()).isNull();
        assertThat(entries.get(1).getFiles()).isNull();
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) Author(com.google.copybara.authoring.Author) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 15 with GitLogEntry

use of com.google.copybara.git.GitRepository.GitLogEntry in project copybara by google.

the class GitRepositoryTest method testBadCommitInLog.

@Test
public void testBadCommitInLog() throws RepoException, IOException {
    Files.write(workdir.resolve("foo.txt"), new byte[] {});
    repository.add().files("foo.txt").run();
    // 
    repository.simpleCommand("commit", "foo.txt", "-m", "message");
    GitLogEntry entry = Iterables.getOnlyElement(repository.log("HEAD").withLimit(1).run());
    String badCommit = "tree " + entry.getTree() + "\n" + "parent " + entry.getCommit().getSha1() + "\n" + "author Some User <example@example.com> 1528942829 --400\n" + "committer Some User <example@example.com> 1528942829 --400\n" + "\n" + "Allow to check and resolve symlinks";
    Files.write(workdir.resolve("commit"), badCommit.getBytes(UTF_8));
    String commitSha1 = repository.simpleCommand("hash-object", "-w", "-t", "commit", "--", workdir.resolve("commit").toAbsolutePath().toString()).getStdout().trim();
    entry = Iterables.getOnlyElement(repository.log(commitSha1).withLimit(1).run());
    ZonedDateTime epoch = ZonedDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
    assertThat(entry.getAuthorDate()).isEquivalentAccordingToCompareTo(epoch);
    assertThat(entry.getCommitDate()).isEquivalentAccordingToCompareTo(epoch);
}
Also used : ZonedDateTime(java.time.ZonedDateTime) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Aggregations

GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)29 Test (org.junit.Test)19 Path (java.nio.file.Path)12 DummyRevision (com.google.copybara.testing.DummyRevision)9 ImmutableList (com.google.common.collect.ImmutableList)6 WriterContext (com.google.copybara.WriterContext)6 Iterables (com.google.common.collect.Iterables)5 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)5 RepoException (com.google.copybara.exception.RepoException)5 ValidationException (com.google.copybara.exception.ValidationException)5 Glob (com.google.copybara.util.Glob)5 IOException (java.io.IOException)5 Optional (java.util.Optional)5 Joiner (com.google.common.base.Joiner)4 DestinationStatus (com.google.copybara.Destination.DestinationStatus)4 Author (com.google.copybara.authoring.Author)4 Lists (com.google.common.collect.Lists)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)3 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)3 Writer (com.google.copybara.Destination.Writer)3