Search in sources :

Example 1 with GitRevision

use of com.google.copybara.git.GitRevision in project copybara by google.

the class WorkflowTest method testFirstParentAlreadyImportedInNoFirstParent.

/**
 * Regression that test that when using git.origin with first_parent = False, if the first parent
 * of a merge is already imported and the merge is a no-op, we detect the import as
 * EmptyChangeException instead of detecting the non-first parent as the change to import
 */
@Test
public void testFirstParentAlreadyImportedInNoFirstParent() throws Exception {
    Path originPath = Files.createTempDirectory("origin");
    GitRepository origin = GitRepository.newRepo(/*verbose*/
    true, originPath, getGitEnv()).init();
    String primaryBranch = origin.getPrimaryBranch();
    options.setForce(false);
    options.workflowOptions.initHistory = true;
    String config = "core.workflow(" + "    name = 'default'," + "    origin = git.origin( url = 'file://" + origin.getWorkTree() + "',\n" + "                         ref = '" + primaryBranch + "',\n" + "                         first_parent = False),\n" + "    destination = testing.destination(),\n" + "    authoring = " + authoring + "," + "    origin_files = glob(['included/**'])," + "    mode = '" + WorkflowMode.SQUASH + "'," + ")\n";
    Migration workflow = loadConfig(config).getMigration("default");
    addGitFile(originPath, origin, "included/foo.txt", "");
    GitRevision lastRev = commit(origin, "last_rev");
    workflow.run(workdir, ImmutableList.of());
    assertThat(destination.processed.get(0).getOriginRef()).isEqualTo(lastRev);
    origin.simpleCommand("checkout", "-b", "feature");
    addGitFile(originPath, origin, "included/foo.txt", "SHOULD NOT BE IN PRIMARY");
    commit(origin, "feature commit");
    origin.simpleCommand("revert", "HEAD");
    addGitFile(originPath, origin, "excluded/bar.txt", "don't migrate!");
    commit(origin, "excluded commit");
    origin.simpleCommand("checkout", primaryBranch);
    origin.simpleCommand("merge", "-s", "ours", "feature");
    EmptyChangeException e = assertThrows(EmptyChangeException.class, () -> workflow.run(workdir, ImmutableList.of()));
    assertThat(e).hasMessageThat().matches("No changes from " + lastRev.getSha1() + " up to .* match any origin_files.*");
}
Also used : Path(java.nio.file.Path) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) GitRepository(com.google.copybara.git.GitRepository) GitRevision(com.google.copybara.git.GitRevision) Migration(com.google.copybara.config.Migration) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Test(org.junit.Test)

Example 2 with GitRevision

use of com.google.copybara.git.GitRevision in project copybara by google.

the class WorkflowTest method testTestWorkflowWithDiffInOrigin.

@Test
public void testTestWorkflowWithDiffInOrigin() throws Exception {
    GitRepository remote = GitRepository.newBareRepo(Files.createTempDirectory("gitdir"), getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).withWorkTree(workdir);
    remote.init();
    String primaryBranch = remote.getPrimaryBranch();
    Files.write(workdir.resolve("foo.txt"), new byte[] {});
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    GitRevision lastRev = remote.resolveReference(primaryBranch);
    Files.write(workdir.resolve("bar.txt"), "change content".getBytes(UTF_8));
    remote.add().files("bar.txt").run();
    remote.simpleCommand("commit", "bar.txt", "-m", "message_s");
    TestingConsole testingConsole = new TestingConsole().respondYes();
    options.workflowOptions.lastRevision = lastRev.getSha1();
    options.setWorkdirToRealTempDir().setConsole(testingConsole).setHomeDir(StandardSystemProperty.USER_HOME.value());
    Workflow<?, ?> workflow = (Workflow<?, ?>) new SkylarkTestExecutor(options).loadConfig("core.workflow(\n" + "    name = 'foo',\n" + "    origin = git.origin(url='" + remote.getGitDir() + "',\n" + "                        ref = '" + primaryBranch + "'\n" + "    ),\n" + "    destination = folder.destination(),\n" + "    mode = 'ITERATIVE',\n" + "    authoring = " + authoring + ",\n" + "    transformations = [metadata.replace_message(''),],\n" + ")\n").getMigration("foo");
    workflow.getWorkflowOptions().diffInOrigin = true;
    workflow.run(workdir, ImmutableList.of(primaryBranch));
    testingConsole.assertThat().onceInLog(MessageType.WARNING, "Change 1 of 1 \\(.*\\)\\: Continue to migrate with '" + workflow.getMode() + "'" + " to " + workflow.getDestination().getType() + "\\?");
}
Also used : GitRepository(com.google.copybara.git.GitRepository) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitRevision(com.google.copybara.git.GitRevision) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Test(org.junit.Test)

Example 3 with GitRevision

use of com.google.copybara.git.GitRevision in project copybara by google.

the class WorkflowTest method testTestWorkflowWithDiffInOriginAndRespondNo.

@Test
public void testTestWorkflowWithDiffInOriginAndRespondNo() throws Exception {
    GitRepository remote = GitRepository.newBareRepo(Files.createTempDirectory("gitdir"), getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).withWorkTree(workdir);
    remote.init();
    String primaryBranch = remote.getPrimaryBranch();
    Files.write(workdir.resolve("foo.txt"), new byte[] {});
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    GitRevision lastRev = remote.resolveReference(primaryBranch);
    Files.write(workdir.resolve("bar.txt"), "change content".getBytes(UTF_8));
    remote.add().files("bar.txt").run();
    remote.simpleCommand("commit", "bar.txt", "-m", "message_s");
    TestingConsole testingConsole = new TestingConsole().respondNo();
    options.workflowOptions.lastRevision = lastRev.getSha1();
    options.setWorkdirToRealTempDir().setConsole(testingConsole).setHomeDir(StandardSystemProperty.USER_HOME.value());
    Workflow<?, ?> workflow = (Workflow<?, ?>) new SkylarkTestExecutor(options).loadConfig("core.workflow(\n" + "    name = 'foo',\n" + "    origin = git.origin(url='" + remote.getGitDir() + "'),\n" + "    destination = folder.destination(),\n" + "    mode = 'ITERATIVE',\n" + "    authoring = " + authoring + ",\n" + "    transformations = [metadata.replace_message(''),],\n" + ")\n").getMigration("foo");
    workflow.getWorkflowOptions().diffInOrigin = true;
    ChangeRejectedException e = assertThrows(ChangeRejectedException.class, () -> workflow.run(workdir, ImmutableList.of(primaryBranch)));
    assertThat(e.getMessage()).contains("User aborted execution: did not confirm diff in origin changes.");
}
Also used : GitRepository(com.google.copybara.git.GitRepository) ChangeRejectedException(com.google.copybara.exception.ChangeRejectedException) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitRevision(com.google.copybara.git.GitRevision) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Test(org.junit.Test)

Example 4 with GitRevision

use of com.google.copybara.git.GitRevision in project copybara by google.

the class WorkflowTest method testWorkflowWithEmptyDiffInOrigin.

@Test
public void testWorkflowWithEmptyDiffInOrigin() throws Exception {
    GitRepository remote = GitRepository.newBareRepo(Files.createTempDirectory("gitdir"), getGitEnv(), /*verbose=*/
    true, DEFAULT_TIMEOUT, /*noVerify=*/
    false).withWorkTree(workdir);
    remote.init();
    String primaryBranch = remote.getPrimaryBranch();
    Files.write(workdir.resolve("foo.txt"), new byte[] {});
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    GitRevision lastRev = remote.resolveReference(primaryBranch);
    Files.write(workdir.resolve("foo.txt"), "change content".getBytes(UTF_8));
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    Files.write(workdir.resolve("foo.txt"), new byte[] {});
    remote.add().files("foo.txt").run();
    remote.simpleCommand("commit", "foo.txt", "-m", "message_a");
    TestingConsole testingConsole = new TestingConsole().respondYes();
    options.workflowOptions.lastRevision = lastRev.getSha1();
    options.general.force = false;
    options.setWorkdirToRealTempDir().setConsole(testingConsole).setHomeDir(StandardSystemProperty.USER_HOME.value());
    Workflow<?, ?> workflow = (Workflow<?, ?>) new SkylarkTestExecutor(options).loadConfig("core.workflow(\n" + "    name = 'foo',\n" + String.format("    origin = git.origin(url='%s', ref='%s'),\n", remote.getGitDir(), primaryBranch) + "    destination = folder.destination(),\n" + "    mode = 'ITERATIVE',\n" + "    authoring = " + authoring + ",\n" + "    transformations = [metadata.replace_message(''),],\n" + ")\n").getMigration("foo");
    workflow.getWorkflowOptions().diffInOrigin = true;
    workflow.run(workdir, ImmutableList.of(primaryBranch));
    testingConsole.assertThat().onceInLog(MessageType.WARNING, ".*No difference at diff_in_origin.*");
}
Also used : GitRepository(com.google.copybara.git.GitRepository) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) GitRevision(com.google.copybara.git.GitRevision) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Test(org.junit.Test)

Aggregations

GitRepository (com.google.copybara.git.GitRepository)4 GitRevision (com.google.copybara.git.GitRevision)4 Test (org.junit.Test)4 SkylarkTestExecutor (com.google.copybara.testing.SkylarkTestExecutor)3 TestingConsole (com.google.copybara.util.console.testing.TestingConsole)3 Migration (com.google.copybara.config.Migration)1 ChangeRejectedException (com.google.copybara.exception.ChangeRejectedException)1 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)1 FileSubjects.assertThatPath (com.google.copybara.testing.FileSubjects.assertThatPath)1 Path (java.nio.file.Path)1