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));
}
};
}
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());
}
}
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));
}
};
}
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();
}
Aggregations