Search in sources :

Example 11 with Change

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

the class GitOriginTest method testChangesMerge.

@Test
public void testChangesMerge() throws Exception {
    // Need to "round" it since git doesn't store the milliseconds
    ZonedDateTime beforeTime = ZonedDateTime.now(ZoneId.systemDefault()).minusSeconds(1);
    String author = "John Name <john@name.com>";
    createBranchMerge(author);
    ImmutableList<Change<GitRevision>> changes = newReader().changes(origin.resolve(firstCommitRef), origin.resolve("HEAD")).getChanges();
    assertThat(changes).hasSize(3);
    assertThat(changes.get(0).getMessage()).isEqualTo("main1\n");
    assertThat(changes.get(1).getMessage()).isEqualTo("main2\n");
    assertThat(changes.get(2).getMessage()).contains("Merge branch 'feature'");
    for (Change<GitRevision> change : changes) {
        assertThat(change.getAuthor().getEmail()).isEqualTo("john@name.com");
        assertThat(change.getDateTime()).isAtLeast(beforeTime);
        assertThat(change.getDateTime()).isAtMost(ZonedDateTime.now(ZoneId.systemDefault()).plusSeconds(1));
    }
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) Change(com.google.copybara.Change) Test(org.junit.Test)

Example 12 with Change

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

the class GitOriginTest method testChanges.

@Test
public void testChanges() throws Exception {
    // Need to "round" it since git doesn't store the milliseconds
    ZonedDateTime beforeTime = ZonedDateTime.now(ZoneId.systemDefault()).minusSeconds(1);
    String author = "John Name <john@name.com>";
    singleFileCommit(author, "change2", "test.txt", "some content2");
    git("tag", "-m", "This is a tag", "0.1");
    singleFileCommit(author, "change3", "test.txt", "some content3");
    singleFileCommit(author, "change4", "test.txt", "some content4");
    ImmutableList<Change<GitRevision>> changes = newReader().changes(origin.resolve(firstCommitRef), origin.resolve("HEAD")).getChanges();
    assertThat(changes).hasSize(3);
    assertThat(changes.stream().map(c -> c.getRevision().getUrl()).allMatch(c -> c.startsWith("file://"))).isTrue();
    assertThat(changes.get(0).getMessage()).isEqualTo("change2\n");
    assertThat(changes.get(0).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1");
    assertThat(changes.get(1).getMessage()).isEqualTo("change3\n");
    assertThat(changes.get(1).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1-1-g" + changes.get(1).getRevision().asString().substring(0, 7));
    assertThat(changes.get(2).getMessage()).isEqualTo("change4\n");
    assertThat(changes.get(2).getRevision().associatedLabel("GIT_DESCRIBE_CHANGE_VERSION")).contains("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7));
    TransformWork work = TransformWorks.of(Paths.get(""), "some msg", console).withChanges(new Changes(changes.reverse(), ImmutableList.of()));
    assertThat(work.getLabel("GIT_DESCRIBE_CHANGE_VERSION")).isEqualTo("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7));
    assertThat(work.getAllLabels("GIT_DESCRIBE_CHANGE_VERSION").getImmutableList()).isEqualTo(ImmutableList.of("0.1-2-g" + changes.get(2).getRevision().asString().substring(0, 7), "0.1-1-g" + changes.get(1).getRevision().asString().substring(0, 7), "0.1"));
    for (Change<GitRevision> change : changes) {
        assertThat(change.getAuthor().getEmail()).isEqualTo("john@name.com");
        assertThat(change.getDateTime()).isAtLeast(beforeTime);
        assertThat(change.getDateTime()).isAtMost(ZonedDateTime.now(ZoneId.systemDefault()).plusSeconds(1));
    }
}
Also used : RecordsProcessCallDestination(com.google.copybara.testing.RecordsProcessCallDestination) GitTestUtil(com.google.copybara.testing.git.GitTestUtil) AuthoringMappingMode(com.google.copybara.authoring.Authoring.AuthoringMappingMode) ZonedDateTime(java.time.ZonedDateTime) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) TransformWork(com.google.copybara.TransformWork) LabelsAwareModule(com.google.copybara.config.LabelsAwareModule) VisitResult(com.google.copybara.ChangeVisitable.VisitResult) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Workflow(com.google.copybara.Workflow) Path(java.nio.file.Path) Reader(com.google.copybara.Origin.Reader) ImmutableSet(com.google.common.collect.ImmutableSet) PosixFilePermission(java.nio.file.attribute.PosixFilePermission) SkylarkTestExecutor(com.google.copybara.testing.SkylarkTestExecutor) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) FileSubjects.assertThatPath(com.google.copybara.testing.FileSubjects.assertThatPath) Revision(com.google.copybara.Revision) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) List(java.util.List) Iterables(com.google.common.collect.Iterables) Assert.assertThrows(org.junit.Assert.assertThrows) 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) Changes(com.google.copybara.Changes) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ChangesResponse(com.google.copybara.Origin.Reader.ChangesResponse) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) ModuleSet(com.google.copybara.ModuleSet) Before(org.junit.Before) Glob.createGlob(com.google.copybara.util.Glob.createGlob) EmptyReason(com.google.copybara.Origin.Reader.ChangesResponse.EmptyReason) 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) ValidationException(com.google.copybara.exception.ValidationException) UserPassword(com.google.copybara.git.GitCredential.UserPassword) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) Truth.assertThat(com.google.common.truth.Truth.assertThat) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) Paths(java.nio.file.Paths) TransformWorks(com.google.copybara.testing.TransformWorks) GitTestUtil.writeFile(com.google.copybara.testing.git.GitTestUtil.writeFile) Changes(com.google.copybara.Changes) ZonedDateTime(java.time.ZonedDateTime) TransformWork(com.google.copybara.TransformWork) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) Change(com.google.copybara.Change) Test(org.junit.Test)

Example 13 with Change

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

the class DummyOriginTest method testCanSpecifyMessage.

@Test
public void testCanSpecifyMessage() throws Exception {
    DummyOrigin origin = new DummyOrigin().addSimpleChange(/*timestamp*/
    4242, "foo msg");
    Authoring authoring = new Authoring(new Author("foo", "default.example.com"), AuthoringMappingMode.OVERWRITE, ImmutableSet.of());
    Reader<DummyRevision> reader = origin.newReader(Glob.ALL_FILES, authoring);
    ImmutableList<Change<DummyRevision>> changes = reader.changes(/*fromRef*/
    null, /*toRef*/
    origin.resolve("0")).getChanges();
    assertThat(changes).hasSize(1);
    assertThat(changes.get(0).getMessage()).isEqualTo("foo msg");
}
Also used : Authoring(com.google.copybara.authoring.Authoring) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) Test(org.junit.Test)

Example 14 with Change

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

the class MetadataModuleTest method testSquashNotesWithMerge.

@Test
public void testSquashNotesWithMerge() throws Exception {
    Changes changes = new Changes(ImmutableList.of(new Change<>(new DummyRevision("3"), ORIGINAL_AUTHOR, "merge", fakeDate(), ImmutableListMultimap.of(), /*changeFiles=*/
    null, /*merge=*/
    true, ImmutableList.of(new DummyRevision("10"), new DummyRevision("20"))), new Change<>(new DummyRevision("2"), ORIGINAL_AUTHOR, "change2", fakeDate(), ImmutableListMultimap.of()), new Change<>(new DummyRevision("1"), ORIGINAL_AUTHOR, "change1", fakeDate(), ImmutableListMultimap.of())), ImmutableList.of());
    TransformWork work = TransformWorks.of(workdir, "the message", testingConsole).withChanges(changes);
    // The default is to use merges, since git.origin does --first-parent by default
    skylark.<MetadataSquashNotes>eval("s", "s = metadata.squash_notes()").transform(work);
    assertThat(work.getMessage()).isEqualTo("Copybara import of the project:\n" + "\n" + "  - 3 merge by Foo Bar <foo@bar.com>\n" + "  - 2 change2 by Foo Bar <foo@bar.com>\n" + "  - 1 change1 by Foo Bar <foo@bar.com>\n");
    work = TransformWorks.of(workdir, "the message", testingConsole).withChanges(changes);
    skylark.<MetadataSquashNotes>eval("s", "s = metadata.squash_notes(use_merge = False)").transform(work);
    assertThat(work.getMessage()).isEqualTo("Copybara import of the project:\n" + "\n" + "  - 2 change2 by Foo Bar <foo@bar.com>\n" + "  - 1 change1 by Foo Bar <foo@bar.com>\n");
}
Also used : Changes(com.google.copybara.Changes) TransformWork(com.google.copybara.TransformWork) DummyRevision(com.google.copybara.testing.DummyRevision) TransformWorks.toChange(com.google.copybara.testing.TransformWorks.toChange) ProcessedChange(com.google.copybara.testing.RecordsProcessCallDestination.ProcessedChange) Change(com.google.copybara.Change) Test(org.junit.Test)

Example 15 with Change

use of com.google.copybara.Change 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);
        }
    };
}
Also used : Path(java.nio.file.Path) Iterables(com.google.common.collect.Iterables) Origin(com.google.copybara.Origin) ValidationException.checkCondition(com.google.copybara.exception.ValidationException.checkCondition) RepoException(com.google.copybara.exception.RepoException) Collections2(com.google.common.collect.Collections2) SubmoduleStrategy(com.google.copybara.git.GitOrigin.SubmoduleStrategy) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) Change(com.google.copybara.Change) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Endpoint(com.google.copybara.Endpoint) Issue(com.google.copybara.git.github.api.Issue) Splitter(com.google.common.base.Splitter) GeneralOptions(com.google.copybara.GeneralOptions) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) ImmutableSetMultimap(com.google.common.collect.ImmutableSetMultimap) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry) Label(com.google.copybara.git.github.api.Issue.Label) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) ImmutableMap(com.google.common.collect.ImmutableMap) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) CharMatcher(com.google.common.base.CharMatcher) ValidationException(com.google.copybara.exception.ValidationException) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) Set(java.util.Set) Console(com.google.copybara.util.console.Console) Sets(com.google.common.collect.Sets) GithubUtil.getProjectNameFromUrl(com.google.copybara.git.github.util.GithubUtil.getProjectNameFromUrl) TimeUnit(java.util.concurrent.TimeUnit) Authoring(com.google.copybara.authoring.Authoring) Glob(com.google.copybara.util.Glob) PullRequest(com.google.copybara.git.github.api.PullRequest) GithubUtil.asGithubUrl(com.google.copybara.git.github.util.GithubUtil.asGithubUrl) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) GithubUtil(com.google.copybara.git.github.util.GithubUtil) Collections(java.util.Collections) GithubPrUrl(com.google.copybara.git.github.util.GithubUtil.GithubPrUrl) ReaderImpl(com.google.copybara.git.GitOrigin.ReaderImpl) Change(com.google.copybara.Change) RepoException(com.google.copybara.exception.RepoException) BaselinesWithoutLabelVisitor(com.google.copybara.BaselinesWithoutLabelVisitor) EmptyChangeException(com.google.copybara.exception.EmptyChangeException) Nullable(javax.annotation.Nullable) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Aggregations

Change (com.google.copybara.Change)24 Test (org.junit.Test)14 Author (com.google.copybara.authoring.Author)12 Changes (com.google.copybara.Changes)11 DummyRevision (com.google.copybara.testing.DummyRevision)10 RepoException (com.google.copybara.exception.RepoException)8 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 TransformResult (com.google.copybara.TransformResult)6 DestinationEffect (com.google.copybara.DestinationEffect)5 Authoring (com.google.copybara.authoring.Authoring)5 EmptyChangeException (com.google.copybara.exception.EmptyChangeException)5 GitLogEntry (com.google.copybara.git.GitRepository.GitLogEntry)5 Nullable (javax.annotation.Nullable)5 ImmutableSet (com.google.common.collect.ImmutableSet)4 Glob (com.google.copybara.util.Glob)4 ZonedDateTime (java.time.ZonedDateTime)4 ArrayList (java.util.ArrayList)4