Search in sources :

Example 16 with Change

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

the class FolderOrigin method newReader.

@Override
public Reader<FolderRevision> newReader(Glob originFiles, Authoring authoring) throws ValidationException {
    return new Reader<FolderRevision>() {

        @Override
        public void checkout(FolderRevision ref, Path workdir) throws RepoException, ValidationException {
            try {
                FileUtil.copyFilesRecursively(ref.path, workdir, copySymlinkStrategy, originFiles);
                FileUtil.addPermissionsAllRecursively(workdir, FILE_PERMISSIONS);
            } catch (AbsoluteSymlinksNotAllowed e) {
                throw new ValidationException(String.format("Cannot copy files into the workdir: Some" + " symlinks refer to locations outside of the folder and" + " 'materialize_outside_symlinks' config option was not used:\n" + "  %s -> %s\n", e.getSymlink(), e.getDestinationFile()));
            } catch (IOException e) {
                throw new RepoException(String.format("Cannot copy files into the workdir:\n" + "  origin folder: %s\n" + "  workdir: %s", ref.path, workdir), e);
            }
        }

        @Override
        public ChangesResponse<FolderRevision> changes(@Nullable FolderRevision fromRef, FolderRevision toRef) throws RepoException {
            // Ignore fromRef since a folder doesn't have history of changes
            return ChangesResponse.forChanges(ImmutableList.of(change(toRef)));
        }

        @Override
        public boolean supportsHistory() {
            return false;
        }

        @Override
        public Change<FolderRevision> change(FolderRevision ref) throws RepoException {
            return new Change<>(ref, author, message, ref.readTimestamp(), ImmutableListMultimap.of());
        }

        @Override
        public void visitChanges(FolderRevision start, ChangesVisitor visitor) throws RepoException {
            visitor.visit(change(start));
        }
    };
}
Also used : Path(java.nio.file.Path) AbsoluteSymlinksNotAllowed(com.google.copybara.util.AbsoluteSymlinksNotAllowed) ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) RepoException(com.google.copybara.exception.RepoException) Change(com.google.copybara.Change) Nullable(javax.annotation.Nullable)

Example 17 with Change

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

the class ChangeReader method parseChanges.

private ImmutableList<Change<GitRevision>> parseChanges(ImmutableList<GitLogEntry> logEntries) throws RepoException {
    ImmutableList.Builder<Change<GitRevision>> result = ImmutableList.builder();
    GitRevision last = null;
    for (GitLogEntry e : logEntries) {
        // Keep the first commit if repeated (merge commits).
        if (last != null && last.equals(e.getCommit())) {
            continue;
        }
        last = e.getCommit();
        result.add(new Change<>(e.getCommit().withUrl(url), filterAuthor(e.getAuthor()), e.getBody() + branchCommitLog(e.getCommit(), e.getParents()), e.getAuthorDate(), ChangeMessage.parseAllAsLabels(e.getBody()).labelsAsMultimap(), e.getFiles(), e.getParents().size() > 1, e.getParents()));
    }
    return result.build().reverse();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) Change(com.google.copybara.Change) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

Example 18 with Change

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

the class GitVisitorUtil method visitChanges.

/**
 * Visits
 */
static void visitChanges(GitRevision start, ChangesVisitor visitor, ChangeReader.Builder queryChanges, GeneralOptions generalOptions, String type, int visitChangePageSize) throws RepoException, ValidationException {
    Preconditions.checkNotNull(start);
    int skip = 0;
    boolean finished = false;
    try (ProfilerTask ignore = generalOptions.profiler().start(type + "/visit_changes")) {
        while (!finished) {
            ImmutableList<Change<GitRevision>> result;
            try (ProfilerTask ignore2 = generalOptions.profiler().start("git_log_" + skip + "_" + visitChangePageSize)) {
                result = queryChanges.setSkip(skip).setLimit(visitChangePageSize).build().run(start.getSha1()).reverse();
            }
            if (result.isEmpty()) {
                break;
            }
            skip += result.size();
            for (Change<GitRevision> current : result) {
                if (visitor.visit(current) == VisitResult.TERMINATE) {
                    finished = true;
                    break;
                }
            }
        }
    }
    if (skip == 0) {
        throw new CannotResolveRevisionException("Cannot resolve reference " + start.getSha1());
    }
}
Also used : ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException) Change(com.google.copybara.Change)

Example 19 with Change

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

the class RemoteArchiveOrigin method newReader.

@Override
public Reader<RemoteArchiveRevision> newReader(Glob originFiles, Authoring authoring) {
    return new Reader<RemoteArchiveRevision>() {

        @Override
        public void checkout(RemoteArchiveRevision ref, Path workdir) throws ValidationException {
            try {
                // TODO(joshgoldman): Add richer ref object and ability to restrict download by host/url
                URL url = new URL(ref.path.toString());
                InputStream returned = transport.open(new URL(ref.path.toString()));
                try (ProfilerTask ignored = profiler.start("remote_file_" + url);
                    ZipInputStream zipInputStream = remoteFileOptions.getZipInputStream(returned)) {
                    ZipEntry zipEntry;
                    while (((zipEntry = zipInputStream.getNextEntry()) != null)) {
                        if (originFiles.relativeTo(workdir.toAbsolutePath()).matches(workdir.resolve(Path.of(zipEntry.getName()))) && !zipEntry.isDirectory()) {
                            Files.createDirectories(workdir.resolve(zipEntry.getName()).getParent());
                            MoreFiles.asByteSink(workdir.resolve(zipEntry.getName())).writeFrom(zipInputStream);
                        }
                    }
                }
            } catch (IOException e) {
                throw new ValidationException(String.format("Could not unzip file: %s \n%s", ref.path, e.getMessage()));
            }
        }

        @Override
        public ChangesResponse<RemoteArchiveRevision> changes(RemoteArchiveRevision fromRef, RemoteArchiveRevision toRef) throws RepoException {
            return ChangesResponse.forChanges(ImmutableList.of(change(toRef)));
        }

        @Override
        public Change<RemoteArchiveRevision> change(RemoteArchiveRevision ref) throws RepoException {
            return new Change<>(ref, author, message, ref.readTimestamp(), ImmutableListMultimap.of());
        }

        @Override
        public void visitChanges(RemoteArchiveRevision start, ChangesVisitor visitor) throws RepoException {
            visitor.visit(change(start));
        }
    };
}
Also used : Path(java.nio.file.Path) ZipInputStream(java.util.zip.ZipInputStream) ValidationException(com.google.copybara.exception.ValidationException) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Change(com.google.copybara.Change) URL(java.net.URL)

Example 20 with Change

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

the class GitDestinationTest method testTag.

@Test
public void testTag() throws Exception {
    fetch = primaryBranch;
    push = primaryBranch;
    Files.write(workdir.resolve("test.txt"), "some content".getBytes(UTF_8));
    options.setForce(true);
    WriterContext writerContext = new WriterContext("piper_to_github", "TEST", false, new DummyRevision("test"), Glob.ALL_FILES.roots());
    evalDestination().newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref1")), destinationFiles, console);
    options.setForce(false);
    Changes changes = new Changes(ImmutableList.of(new Change<>(new DummyRevision("ref2"), new Author("foo", "foo@foo.com"), "message", ZonedDateTime.now(ZoneOffset.UTC), ImmutableListMultimap.of("my_label", "12345"))), ImmutableList.of());
    Files.write(workdir.resolve("test.txt"), "some content 2".getBytes(UTF_8));
    evalDestinationWithTag(null).newWriter(writerContext).write(TransformResults.of(workdir, new DummyRevision("ref2")).withChanges(changes).withSummary("message_tag"), destinationFiles, console);
    CommandOutput commandOutput = repo().simpleCommand("tag", "-n9");
    assertThat(commandOutput.getStdout()).matches(".*test_v1.*message_tag\n" + ".*\n" + ".*DummyOrigin-RevId: ref2\n");
}
Also used : Changes(com.google.copybara.Changes) WriterContext(com.google.copybara.WriterContext) CommandOutput(com.google.copybara.util.CommandOutput) DummyRevision(com.google.copybara.testing.DummyRevision) Author(com.google.copybara.authoring.Author) Change(com.google.copybara.Change) Test(org.junit.Test)

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