Search in sources :

Example 21 with Change

use of com.google.copybara.revision.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(Objects.requireNonNull(ref.getUrl()));
                InputStream returned = transport.open(url);
                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.getUrl(), 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.revision.Change) URL(java.net.URL)

Example 22 with Change

use of com.google.copybara.revision.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.revision.Change)

Example 23 with Change

use of com.google.copybara.revision.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.revision.Change) Nullable(javax.annotation.Nullable)

Example 24 with Change

use of com.google.copybara.revision.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.revision.Change) GitLogEntry(com.google.copybara.git.GitRepository.GitLogEntry)

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