Search in sources :

Example 1 with InsideGitDirException

use of com.google.copybara.util.InsideGitDirException in project copybara by google.

the class PatchTransformation method transform.

@Override
public void transform(TransformWork work) throws ValidationException, IOException {
    for (int i = 0; i < patches.size(); i++) {
        ConfigFile<?> patch = patches.get(i);
        work.getConsole().info(String.format("Applying patch %d/%d: '%s'.", i + 1, patches.size(), patch.path()));
        try {
            DiffUtil.patch(work.getCheckoutDir(), patch.content(), excludedPaths, SLASHES_TO_STRIP, options.isVerbose(), reverse, options.getEnvironment());
        } catch (IOException ioException) {
            work.getConsole().error("Error applying patch: " + ioException.getMessage());
            throw new ValidationException(ioException, "Error applying patch.");
        } catch (InsideGitDirException e) {
            throw new ValidationException("Cannot use patch.apply because Copybara temporary directory (%s) is inside a git" + " directory (%s). Please remove the git repository or use %s flag.", e.getPath(), e.getGitDirPath(), OUTPUT_ROOT_FLAG);
        }
    }
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) IOException(java.io.IOException) InsideGitDirException(com.google.copybara.util.InsideGitDirException)

Example 2 with InsideGitDirException

use of com.google.copybara.util.InsideGitDirException in project copybara by google.

the class WorkflowRunHelper method doMigrate.

private ImmutableList<DestinationEffect> doMigrate(O rev, @Nullable O lastRev, Console processConsole, Metadata metadata, Changes changes, @Nullable String destinationBaseline, @Nullable O changeIdentityRevision) throws IOException, RepoException, ValidationException {
    Path checkoutDir = workdir.resolve("checkout");
    try (ProfilerTask ignored = profiler().start("prepare_workdir")) {
        processConsole.progress("Cleaning working directory");
        if (Files.exists(workdir)) {
            FileUtil.deleteRecursively(workdir);
        }
        Files.createDirectories(checkoutDir);
    }
    processConsole.progress("Checking out the change");
    try (ProfilerTask ignored = profiler().start("origin.checkout", profiler().taskType(workflow.getOrigin().getType()))) {
        originReader.checkout(rev, checkoutDir);
    }
    // Remove excluded origin files.
    PathMatcher originFiles = workflow.getOriginFiles().relativeTo(checkoutDir);
    processConsole.progress("Removing excluded origin files");
    int deleted = FileUtil.deleteFilesRecursively(checkoutDir, FileUtil.notPathMatcher(originFiles));
    if (deleted != 0) {
        processConsole.info(String.format("Removed %d files from workdir that do not match origin_files", deleted));
    }
    Path originCopy = null;
    if (workflow.getReverseTransformForCheck() != null) {
        try (ProfilerTask ignored = profiler().start("reverse_copy")) {
            workflow.getConsole().progress("Making a copy or the workdir for reverse checking");
            originCopy = Files.createDirectories(workdir.resolve("origin"));
            FileUtil.copyFilesRecursively(checkoutDir, originCopy, FAIL_OUTSIDE_SYMLINKS);
        }
    }
    TransformWork transformWork = new TransformWork(checkoutDir, metadata, changes, workflow.getConsole(), new MigrationInfo(getOriginLabelName(), writer), resolvedRef).withLastRev(lastRev).withCurrentRev(rev);
    try (ProfilerTask ignored = profiler().start("transforms")) {
        workflow.getTransformation().transform(transformWork);
    }
    if (workflow.getReverseTransformForCheck() != null) {
        workflow.getConsole().progress("Checking that the transformations can be reverted");
        Path reverse;
        try (ProfilerTask ignored = profiler().start("reverse_copy")) {
            reverse = Files.createDirectories(workdir.resolve("reverse"));
            FileUtil.copyFilesRecursively(checkoutDir, reverse, FAIL_OUTSIDE_SYMLINKS);
        }
        try (ProfilerTask ignored = profiler().start("reverse_transform")) {
            workflow.getReverseTransformForCheck().transform(new TransformWork(reverse, new Metadata(transformWork.getMessage(), transformWork.getAuthor()), changes, workflow.getConsole(), new MigrationInfo(/*originLabel=*/
            null, null), resolvedRef));
        }
        String diff;
        try {
            diff = new String(DiffUtil.diff(originCopy, reverse, workflow.isVerbose(), workflow.getGeneralOptions().getEnvironment()), StandardCharsets.UTF_8);
        } catch (InsideGitDirException e) {
            throw new ValidationException("Cannot use 'reversible_check = True' because Copybara temporary directory (%s) is" + " inside a git directory (%s). Please remove the git repository or use %s flag.", e.getPath(), e.getGitDirPath(), OUTPUT_ROOT_FLAG);
        }
        if (!diff.trim().isEmpty()) {
            workflow.getConsole().error("Non reversible transformations:\n" + DiffUtil.colorize(workflow.getConsole(), diff));
            throw new ValidationException("Workflow '%s' is not reversible", workflow.getName());
        }
    }
    workflow.getConsole().progress("Checking that destination_files covers all files in transform result");
    new ValidateDestinationFilesVisitor(workflow.getDestinationFiles(), checkoutDir).verifyFilesToWrite();
    // TODO(malcon): Pass metadata object instead
    TransformResult transformResult = new TransformResult(checkoutDir, rev, transformWork.getAuthor(), transformWork.getMessage(), resolvedRef, workflow.getName(), changes, rawSourceRef);
    if (destinationBaseline != null) {
        transformResult = transformResult.withBaseline(destinationBaseline);
    }
    transformResult = transformResult.withAskForConfirmation(workflow.isAskForConfirmation()).withIdentity(workflow.getMigrationIdentity(changeIdentityRevision, transformWork));
    ImmutableList<DestinationEffect> result;
    try (ProfilerTask ignored = profiler().start("destination.write", profiler().taskType(workflow.getDestination().getType()))) {
        result = writer.write(transformResult, processConsole);
    }
    Verify.verifyNotNull(result, "Destination returned a null result.");
    Verify.verify(!result.isEmpty(), "Destination " + writer + " returned an empty set of effects");
    return result;
}
Also used : Path(java.nio.file.Path) ValidationException(com.google.copybara.exception.ValidationException) InsideGitDirException(com.google.copybara.util.InsideGitDirException) PathMatcher(java.nio.file.PathMatcher) ProfilerTask(com.google.copybara.profiler.Profiler.ProfilerTask)

Aggregations

ValidationException (com.google.copybara.exception.ValidationException)2 InsideGitDirException (com.google.copybara.util.InsideGitDirException)2 ProfilerTask (com.google.copybara.profiler.Profiler.ProfilerTask)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 PathMatcher (java.nio.file.PathMatcher)1