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