Search in sources :

Example 6 with GitLogEntry

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

the class GitDestinationIntegrateTest method testGitHubSemiFakeMerge.

@Test
public void testGitHubSemiFakeMerge() throws ValidationException, IOException, RepoException {
    Path workTree = Files.createTempDirectory("test");
    GitRepository repo = gitUtil.mockRemoteRepo("github.com/example/test_repo").withWorkTree(workTree);
    GitRevision firstChange = singleChange(workTree, repo, "ignore_me", "Feature1 change");
    GitRevision secondChange = singleChange(workTree, repo, "ignore_me2", "Feature2 change");
    repo.simpleCommand("update-ref", "refs/pull/20/head", secondChange.getSha1());
    GitDestination destination = destinationWithDefaultIntegrates();
    GitLogEntry previous = createBaseDestinationChange(destination);
    GitHubPrIntegrateLabel labelObj = new GitHubPrIntegrateLabel(repo, options.general, "example/test_repo", 20, "some_user:1234-foo.bar.baz%3", secondChange.getSha1());
    assertThat(labelObj.getProjectId()).isEqualTo("example/test_repo");
    assertThat(labelObj.getPrNumber()).isEqualTo(20L);
    assertThat(labelObj.getOriginBranch()).isEqualTo("some_user:1234-foo.bar.baz%3");
    assertThat(labelObj.mergeMessage(ImmutableList.of())).isEqualTo("Merge pull request #20 from some_user:1234-foo.bar.baz%3\n");
    String label = labelObj.toString();
    assertThat(label).isEqualTo("https://github.com/example/test_repo/pull/20" + " from some_user:1234-foo.bar.baz%3 " + secondChange.getSha1());
    migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=" + label + "\n", "some content");
    // Make sure commit adds new text
    String showResult = git("--git-dir", repoGitDir.toString(), "show", primaryBranch);
    assertThat(showResult).contains("some content");
    GitTesting.assertThatCheckout(repo(), primaryBranch).containsFile("test.txt", "some content").containsFile("ignore_me", "").containsFile("ignore_me2", "").containsNoMoreFiles();
    GitLogEntry merge = getLastMigratedChange(primaryBranch);
    assertThat(merge.getBody()).isEqualTo("Merge pull request #20 from some_user:1234-foo.bar.baz%3\n" + "\n" + "DummyOrigin-RevId: test\n");
    assertThat(Lists.transform(merge.getParents(), GitRevision::getSha1)).isEqualTo(Lists.newArrayList(previous.getCommit().getSha1(), secondChange.getSha1()));
    assertThat(console.getMessages().stream().filter(e -> e.getType() == MessageType.WARNING).collect(Collectors.toList())).isEmpty();
    label = new GitHubPrIntegrateLabel(repo, options.general, "example/test_repo", 20, "some_user:branch", firstChange.getSha1()).toString();
    assertThat(label).isEqualTo("https://github.com/example/test_repo/pull/20" + " from some_user:branch " + firstChange.getSha1());
    repo().withWorkTree(workTree).simpleCommand("reset", "--hard", "HEAD~1");
    migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=" + label + "\n", "some content");
    assertThat(console.getMessages().stream().filter(e -> e.getType() == MessageType.WARNING).findAny().get().getText()).contains("has more changes after " + firstChange.getSha1());
}
Also used : Path(java.nio.file.Path) Writer(com.google.copybara.Destination.Writer) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) Iterables(com.google.common.collect.Iterables) DummyRevision(com.google.copybara.testing.DummyRevision) Assert.assertThrows(org.junit.Assert.assertThrows) TransformResult(com.google.copybara.TransformResult) DummyOrigin(com.google.copybara.testing.DummyOrigin) RunWith(org.junit.runner.RunWith) RepoException(com.google.copybara.exception.RepoException) OptionsBuilder(com.google.copybara.testing.OptionsBuilder) MessageType(com.google.copybara.util.console.Message.MessageType) WriterContext(com.google.copybara.WriterContext) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) Strategy(com.google.copybara.git.GitIntegrateChanges.Strategy) GitTestUtil.getGitEnv(com.google.copybara.testing.git.GitTestUtil.getGitEnv) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) GitTesting(com.google.copybara.git.testing.GitTesting) TransformResults(com.google.copybara.testing.TransformResults) Path(java.nio.file.Path) Before(org.junit.Before) DEFAULT_TIMEOUT(com.google.copybara.util.CommandRunner.DEFAULT_TIMEOUT) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) DestinationStatus(com.google.copybara.Destination.DestinationStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Glob(com.google.copybara.util.Glob) FAKE_MERGE_AND_INCLUDE_FILES(com.google.copybara.git.GitIntegrateChanges.Strategy.FAKE_MERGE_AND_INCLUDE_FILES) Optional(java.util.Optional) INCLUDE_FILES(com.google.copybara.git.GitIntegrateChanges.Strategy.INCLUDE_FILES) FAKE_MERGE(com.google.copybara.git.GitIntegrateChanges.Strategy.FAKE_MERGE) Joiner(com.google.common.base.Joiner) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 7 with GitLogEntry

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

the class GitDestinationIntegrateTest method createBaseDestinationChange.

private GitLogEntry createBaseDestinationChange(GitDestination destination) throws IOException, RepoException, ValidationException {
    migrateOriginChange(destination, "Base change\n", "not important");
    GitLogEntry previous = getLastMigratedChange(primaryBranch);
    console.clearMessages();
    return previous;
}
Also used : GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 8 with GitLogEntry

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

the class GitDestinationIntegrateTest method testDestinationStatus.

/**
 * Checks that we can correctly read destination status (last fake merge commit created) after
 * an integration.
 *
 * <p>Don't change the file paths or contents of this test. It needs to be like this to generate
 * an empty diff with the feature branch (pure fake-merge).
 */
@Test
public void testDestinationStatus() throws ValidationException, IOException, RepoException {
    destinationFiles = Glob.createGlob(ImmutableList.of("foo/**"));
    Path gitDir = Files.createTempDirectory("gitdir");
    Path repoPath = Files.createTempDirectory("workdir");
    GitRepository repo = GitRepository.newBareRepo(gitDir, getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).init().withWorkTree(repoPath);
    singleChange(repoPath, repo, "base.txt", "first change");
    repo.simpleCommand("branch", "feature1");
    repo.forceCheckout("feature1");
    GitRevision feature = singleChange(repoPath, repo, "foo/a", "Feature 1 change");
    repo.forceCheckout(primaryBranch);
    // Just so that the migration doesn't fail since the git repo is a non-bare repo.
    repo.forceCheckout("feature1");
    GitDestination destination = destination("url = 'file://" + repo.getGitDir() + "'", String.format("push='%s'", primaryBranch));
    migrateOriginChange(destination, "Test change\n" + "\n" + GitModule.DEFAULT_INTEGRATE_LABEL + "=file://" + repo.getGitDir().toString() + " feature1\n", "foo/a", "", "the_rev");
    GitLogEntry featureMerge = getLastMigratedChange(primaryBranch, repo);
    assertThat(featureMerge.getBody()).isEqualTo("Merge of " + feature.getSha1() + "\n" + "\n" + DummyOrigin.LABEL_NAME + ": the_rev\n");
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("feature"), Glob.ALL_FILES.roots());
    DestinationStatus destinationStatus = destination.newWriter(writerContext).getDestinationStatus(destinationFiles, DummyOrigin.LABEL_NAME);
    assertWithMessage(gitDir.toString()).that(destinationStatus.getBaseline()).isEqualTo("the_rev");
}
Also used : Path(java.nio.file.Path) WriterContext(com.google.copybara.WriterContext) DestinationStatus(com.google.copybara.Destination.DestinationStatus) DummyRevision(com.google.copybara.testing.DummyRevision) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Test(org.junit.Test)

Example 9 with GitLogEntry

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

the class GitDestinationTest method checkLocalRepo.

private GitRepository checkLocalRepo(boolean dryRun) throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    Path scratchTree = Files.createTempDirectory("GitDestinationTest-testLocalRepo");
    Files.write(scratchTree.resolve("foo"), "foo\n".getBytes(UTF_8));
    repo().withWorkTree(scratchTree).add().force().files("foo").run();
    repo().withWorkTree(scratchTree).simpleCommand("commit", "-a", "-m", "change");
    String baseline = repo().withWorkTree(scratchTree).simpleCommand("rev-parse", "HEAD").getStdout().trim();
    Path localPath = Files.createTempDirectory("local_repo");
    options.gitDestination.localRepoPath = localPath.toString();
    Writer<GitRevision> writer = newWriter(dryRun);
    process(writer, new DummyRevision("origin_ref1"));
    // Path localPath = Files.createTempDirectory("local_repo");
    GitRepository localRepo = GitRepository.newRepo(/*verbose*/
    true, localPath, getEnv()).init();
    assertThatCheckout(localRepo, primaryBranch).containsFile("test.txt", "some content").containsNoMoreFiles();
    Files.write(workdir.resolve("test.txt"), "another content".getBytes(UTF_8));
    processWithBaseline(writer, destinationFiles, new DummyRevision("origin_ref2"), baseline);
    assertThatCheckout(localRepo, primaryBranch).containsFile("test.txt", "another content").containsNoMoreFiles();
    for (String ref : ImmutableList.of("HEAD", "copybara/local")) {
        ImmutableList<GitLogEntry> entries = localRepo.log(ref).run();
        assertThat(entries.get(0).getBody()).isEqualTo("" + "test summary\n" + "\n" + "DummyOrigin-RevId: origin_ref2\n");
        assertThat(entries.get(1).getBody()).isEqualTo("" + "test summary\n" + "\n" + "DummyOrigin-RevId: origin_ref1\n");
        assertThat(entries.get(2).getBody()).isEqualTo("change\n");
    }
    return localRepo;
}
Also used : Path(java.nio.file.Path) DummyRevision(com.google.copybara.testing.DummyRevision) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 10 with GitLogEntry

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

the class GitDestinationTest method testCheckerDescription_withDefault.

@Test
public void testCheckerDescription_withDefault() throws Exception {
    runDescriptionChecker(new DummyChecker(ImmutableSet.of("BAD"), "Message for failures"));
    ImmutableList<GitLogEntry> head = repo().log(primaryBranch).withLimit(1).run();
    assertThat(head).isNotEmpty();
    assertThat(head.get(0).getBody()).isEqualTo("" + "Message for failures\n" + "\n" + "DummyOrigin-RevId: ref2\n");
    assertThat(head.get(0).getAuthor()).isEqualTo(new Author("Dont", "rewri@te.me"));
}
Also used : Author(com.google.copybara.authoring.Author) DummyChecker(com.google.copybara.testing.DummyChecker) 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