Search in sources :

Example 1 with Change

use of com.google.copybara.revision.Change in project copybara by google.

the class ChangeReader method parseChanges.

private ImmutableList<Change<HgRevision>> parseChanges(ImmutableList<HgLogEntry> logEntries) throws RepoException {
    ImmutableList.Builder<Change<HgRevision>> result = ImmutableList.builder();
    for (HgLogEntry entry : logEntries) {
        HgRevision rev = new HgRevision(entry.getGlobalId());
        if (NULL_GLOBAL_ID.equals(rev.getGlobalId())) {
            continue;
        }
        Author user;
        try {
            user = AuthorParser.parse(entry.getUser());
        } catch (InvalidAuthorException e) {
            console.warn(String.format("Cannot parse commit user and email: %s", e.getMessage()));
            user = authoring.orElseThrow(() -> new RepoException("No default author provided.")).getDefaultAuthor();
        }
        ImmutableList<HgRevision> parents = entry.getParents().stream().map(HgRevision::new).collect(ImmutableList.toImmutableList());
        result.add(new Change<>(rev, user, entry.getDescription(), entry.getZonedDate(), ChangeMessage.parseAllAsLabels(entry.getDescription()).labelsAsMultimap(), ImmutableSet.copyOf(entry.getFiles()), parents.size() > 1, parents));
    }
    return result.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.revision.Change) RepoException(com.google.copybara.exception.RepoException) HgLogEntry(com.google.copybara.hg.HgRepository.HgLogEntry) InvalidAuthorException(com.google.copybara.authoring.InvalidAuthorException)

Example 2 with Change

use of com.google.copybara.revision.Change in project copybara by google.

the class HgVisitorUtil method visitChanges.

/**
 * Visits Hg changes, up to the termination point specified by the visitor.
 */
static void visitChanges(HgRevision start, ChangesVisitor visitor, ChangeReader.Builder queryChanges, GeneralOptions generalOptions, String type, int visitChangePageSize) throws RepoException {
    Preconditions.checkNotNull(start);
    int offset = 0;
    boolean finished = false;
    try (ProfilerTask ignore = generalOptions.profiler().start(type + "/visit_changes")) {
        while (!finished) {
            ImmutableList<Change<HgRevision>> result;
            try (ProfilerTask ignore2 = generalOptions.profiler().start(String.format("hg_log_%d_%d", offset, visitChangePageSize))) {
                try {
                    result = queryChanges.setSkip(offset).setLimit(visitChangePageSize).build().run(start.getGlobalId()).reverse();
                } catch (ValidationException e) {
                    throw new RepoException(String.format("Error querying changes: %s", e.getMessage()), e.getCause());
                }
            }
            if (result.isEmpty()) {
                break;
            }
            offset += result.size();
            for (Change<HgRevision> current : result) {
                if (visitor.visit(current) == VisitResult.TERMINATE) {
                    finished = true;
                    break;
                }
            }
        }
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Change(com.google.copybara.revision.Change) RepoException(com.google.copybara.exception.RepoException)

Example 3 with Change

use of com.google.copybara.revision.Change in project copybara by google.

the class GitDestinationTest method testChangeDescriptionEmpty_setRevId.

@Test
public void testChangeDescriptionEmpty_setRevId() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    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");
    DummyRevision originRef = new DummyRevision("origin_ref");
    WriterContext writerContext = new WriterContext("GitDestinationTest", "test", true, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = destination().newWriter(writerContext);
    ValidationException e = assertThrows(ValidationException.class, () -> writer.write(TransformResults.of(workdir, originRef).withSummary("").withLabelFinder(s -> ImmutableList.of()).withSetRevId(false), Glob.createGlob(ImmutableList.of("**"), ImmutableList.of("test.txt")), console));
    assertThat(e).hasMessageThat().contains("Change description is empty");
}
Also used : Path(java.nio.file.Path) TransformResult(com.google.copybara.TransformResult) ZonedDateTime(java.time.ZonedDateTime) WriterContext(com.google.copybara.WriterContext) VisitResult(com.google.copybara.ChangeVisitable.VisitResult) Author(com.google.copybara.authoring.Author) GitTestUtil.getGitEnv(com.google.copybara.testing.git.GitTestUtil.getGitEnv) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ZoneOffset(java.time.ZoneOffset) GitTesting(com.google.copybara.git.testing.GitTesting) Path(java.nio.file.Path) DEFAULT_TIMEOUT(com.google.copybara.util.CommandRunner.DEFAULT_TIMEOUT) ImmutableSet(com.google.common.collect.ImmutableSet) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) ImmutableMap(com.google.common.collect.ImmutableMap) GitTesting.assertThatCheckout(com.google.copybara.git.testing.GitTesting.assertThatCheckout) DestinationStatus(com.google.copybara.Destination.DestinationStatus) Instant(java.time.Instant) ZoneId(java.time.ZoneId) CheckerException(com.google.copybara.checks.CheckerException) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Writer(com.google.copybara.Destination.Writer) DummyRevision(com.google.copybara.testing.DummyRevision) Assert.assertThrows(org.junit.Assert.assertThrows) DestinationEffect(com.google.copybara.effect.DestinationEffect) 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) ChangeMessage(com.google.copybara.ChangeMessage) ArrayList(java.util.ArrayList) Truth(com.google.common.truth.Truth) ImmutableList(com.google.common.collect.ImmutableList) TransformResults(com.google.copybara.testing.TransformResults) CommandOutput(com.google.copybara.util.CommandOutput) Nullable(javax.annotation.Nullable) Before(org.junit.Before) Functions(com.google.common.base.Functions) ChangeMessage.parseMessage(com.google.copybara.ChangeMessage.parseMessage) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) DummyChecker(com.google.copybara.testing.DummyChecker) ValidationException(com.google.copybara.exception.ValidationException) UserPassword(com.google.copybara.git.GitCredential.UserPassword) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Maps(com.google.common.collect.Maps) Baseline(com.google.copybara.Origin.Baseline) Glob(com.google.copybara.util.Glob) Type(com.google.copybara.effect.DestinationEffect.Type) Change(com.google.copybara.revision.Change) TransformWorks(com.google.copybara.testing.TransformWorks) GitTestUtil.writeFile(com.google.copybara.testing.git.GitTestUtil.writeFile) Changes(com.google.copybara.revision.Changes) WriterContext(com.google.copybara.WriterContext) ValidationException(com.google.copybara.exception.ValidationException) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 4 with Change

use of com.google.copybara.revision.Change in project copybara by google.

the class GitDestinationTest method testChangeDescriptionEmpty_empty_commit.

@Test
public void testChangeDescriptionEmpty_empty_commit() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    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");
    DummyRevision originRef = new DummyRevision("origin_ref");
    WriterContext writerContext = new WriterContext("GitDestinationTest", "test", true, new DummyRevision("origin_ref1"), Glob.ALL_FILES.roots());
    Writer<GitRevision> writer = destination().newWriter(writerContext);
    ValidationException e = assertThrows(EmptyChangeException.class, () -> writer.write(TransformResults.of(workdir, originRef).withSummary("").withLabelFinder(s -> ImmutableList.of()).withSetRevId(false), Glob.createGlob(ImmutableList.of("nothing_to_be_found"), ImmutableList.of("test.txt")), console));
    assertThat(e).hasMessageThat().contains("Migration of the revision resulted in an empty change");
}
Also used : Path(java.nio.file.Path) TransformResult(com.google.copybara.TransformResult) ZonedDateTime(java.time.ZonedDateTime) WriterContext(com.google.copybara.WriterContext) VisitResult(com.google.copybara.ChangeVisitable.VisitResult) Author(com.google.copybara.authoring.Author) GitTestUtil.getGitEnv(com.google.copybara.testing.git.GitTestUtil.getGitEnv) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) ZoneOffset(java.time.ZoneOffset) GitTesting(com.google.copybara.git.testing.GitTesting) Path(java.nio.file.Path) DEFAULT_TIMEOUT(com.google.copybara.util.CommandRunner.DEFAULT_TIMEOUT) ImmutableSet(com.google.common.collect.ImmutableSet) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) ImmutableMap(com.google.common.collect.ImmutableMap) GitTesting.assertThatCheckout(com.google.copybara.git.testing.GitTesting.assertThatCheckout) DestinationStatus(com.google.copybara.Destination.DestinationStatus) Instant(java.time.Instant) ZoneId(java.time.ZoneId) CheckerException(com.google.copybara.checks.CheckerException) List(java.util.List) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Writer(com.google.copybara.Destination.Writer) DummyRevision(com.google.copybara.testing.DummyRevision) Assert.assertThrows(org.junit.Assert.assertThrows) DestinationEffect(com.google.copybara.effect.DestinationEffect) 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) ChangeMessage(com.google.copybara.ChangeMessage) ArrayList(java.util.ArrayList) Truth(com.google.common.truth.Truth) ImmutableList(com.google.common.collect.ImmutableList) TransformResults(com.google.copybara.testing.TransformResults) CommandOutput(com.google.copybara.util.CommandOutput) Nullable(javax.annotation.Nullable) Before(org.junit.Before) Functions(com.google.common.base.Functions) ChangeMessage.parseMessage(com.google.copybara.ChangeMessage.parseMessage) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) TestingConsole(com.google.copybara.util.console.testing.TestingConsole) Files(java.nio.file.Files) UTF_8(java.nio.charset.StandardCharsets.UTF_8) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) DummyChecker(com.google.copybara.testing.DummyChecker) ValidationException(com.google.copybara.exception.ValidationException) UserPassword(com.google.copybara.git.GitCredential.UserPassword) Iterables.getOnlyElement(com.google.common.collect.Iterables.getOnlyElement) IOException(java.io.IOException) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Maps(com.google.common.collect.Maps) Baseline(com.google.copybara.Origin.Baseline) Glob(com.google.copybara.util.Glob) Type(com.google.copybara.effect.DestinationEffect.Type) Change(com.google.copybara.revision.Change) TransformWorks(com.google.copybara.testing.TransformWorks) GitTestUtil.writeFile(com.google.copybara.testing.git.GitTestUtil.writeFile) Changes(com.google.copybara.revision.Changes) WriterContext(com.google.copybara.WriterContext) ValidationException(com.google.copybara.exception.ValidationException) DummyRevision(com.google.copybara.testing.DummyRevision) Test(org.junit.Test)

Example 5 with Change

use of com.google.copybara.revision.Change in project copybara by google.

the class GitDestinationTest method testVisit.

@Test
public void testVisit() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    DummyRevision ref1 = new DummyRevision("origin_ref1");
    DummyRevision ref2 = new DummyRevision("origin_ref2");
    Files.write(workdir.resolve("test.txt"), "Visit me".getBytes(UTF_8));
    process(firstCommitWriter(), ref1);
    Files.write(workdir.resolve("test.txt"), "Visit me soon".getBytes(UTF_8));
    process(newWriter(), ref2);
    List<Change<?>> visited = new ArrayList<>();
    newWriter().visitChanges(null, input -> {
        visited.add(input);
        return input.getLabels().get(DummyOrigin.LABEL_NAME).contains("origin_ref1") ? VisitResult.TERMINATE : VisitResult.CONTINUE;
    });
    assertThat(visited).hasSize(2);
    assertThat(visited.get(0).getLabels().get(DummyOrigin.LABEL_NAME)).containsExactly("origin_ref2");
    assertThat(visited.get(1).getLabels().get(DummyOrigin.LABEL_NAME)).containsExactly("origin_ref1");
}
Also used : DummyRevision(com.google.copybara.testing.DummyRevision) ArrayList(java.util.ArrayList) Change(com.google.copybara.revision.Change) Test(org.junit.Test)

Aggregations

Change (com.google.copybara.revision.Change)24 Test (org.junit.Test)15 Author (com.google.copybara.authoring.Author)13 Changes (com.google.copybara.revision.Changes)11 DummyRevision (com.google.copybara.testing.DummyRevision)11 ValidationException (com.google.copybara.exception.ValidationException)8 Path (java.nio.file.Path)8 ImmutableList (com.google.common.collect.ImmutableList)7 WriterContext (com.google.copybara.WriterContext)7 RepoException (com.google.copybara.exception.RepoException)7 TransformResult (com.google.copybara.TransformResult)6 ZonedDateTime (java.time.ZonedDateTime)6 DestinationEffect (com.google.copybara.effect.DestinationEffect)5 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)4 ImmutableSet (com.google.common.collect.ImmutableSet)4 Truth.assertThat (com.google.common.truth.Truth.assertThat)4 Authoring (com.google.copybara.authoring.Authoring)4 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)4 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)4 OptionsBuilder (com.google.copybara.testing.OptionsBuilder)4