Search in sources :

Example 6 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class PatchingOptions method patchWithGnuPatch.

private void patchWithGnuPatch(Path rootDir, byte[] diffContents, int stripSlashes, boolean verbose, boolean reverse, Map<String, String> environment) throws IOException {
    ImmutableList.Builder<String> params = ImmutableList.builder();
    // Show verbose output unconditionally since it is helpful for debugging issues with patches.
    // When the patch file doesn't match the file exactly, GNU patch creates backup files, but we
    // disable creating those as they don't make sense for Copybara and otherwise they would need
    // to be excluded
    // See: http://b/112639930
    params.add(patchBin, "--no-backup-if-mismatch", "-t", "-p" + stripSlashes);
    if (reverse) {
        params.add("-R");
    }
    // Only apply in the direction requested. Yes, -R --forward semantics is that it reverses
    // and only applies if can be applied like that (-R will try to apply reverse and forward).
    params.add("--forward");
    Command cmd = new Command(params.build().toArray(new String[0]), environment, rootDir.toFile());
    try {
        CommandOutputWithStatus output = generalOptions.newCommandRunner(cmd).withVerbose(verbose).withInput(diffContents).execute();
        System.err.println(output);
    } catch (BadExitStatusWithOutputException e) {
        throw new IOException(String.format("Error executing 'patch': %s. Stderr: \n%s", e.getMessage(), e.getOutput().getStdout()), e);
    } catch (CommandException e) {
        throw new IOException("Error executing 'patch'", e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException)

Example 7 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class PatchingOptions method patchWithGitApply.

private static void patchWithGitApply(Path rootDir, byte[] diffContents, ImmutableList<String> excludedPaths, int stripSlashes, boolean verbose, boolean reverse, Map<String, String> environment, @Nullable Path gitDir) throws IOException, InsideGitDirException {
    GitEnvironment gitEnv = new GitEnvironment(environment);
    if (gitDir == null) {
        checkNotInsideGitRepo(rootDir, verbose, gitEnv);
    }
    ImmutableList.Builder<String> params = ImmutableList.builder();
    // Show verbose output unconditionally since it is helpful for debugging issues with patches.
    params.add(gitEnv.resolveGitBinary());
    if (gitDir != null) {
        params.add("--git-dir=" + gitDir.normalize().toAbsolutePath());
    }
    params.add("apply", "-v", "--stat", "--apply", "-p" + stripSlashes);
    if (gitDir != null) {
        params.add("--3way");
    }
    for (String excludedPath : excludedPaths) {
        params.add("--exclude", excludedPath);
    }
    if (reverse) {
        params.add("-R");
    }
    params.add("-");
    Command cmd = new Command(params.build().toArray(new String[0]), environment, rootDir.toFile());
    try {
        new CommandRunner(cmd).withVerbose(verbose).withInput(diffContents).execute();
    } catch (BadExitStatusWithOutputException e) {
        throw new IOException(String.format("Error executing 'git apply': %s. Stderr: \n%s", e.getMessage(), e.getOutput().getStderr()), e);
    } catch (CommandException e) {
        throw new IOException("Error executing 'git apply'", e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) ImmutableList(com.google.common.collect.ImmutableList) GitEnvironment(com.google.copybara.git.GitEnvironment) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException) CommandRunner(com.google.copybara.util.CommandRunner)

Example 8 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class CommandRunnerTest method testCommandWithVerbose.

@Test
public void testCommandWithVerbose() throws Exception {
    Command command = new Command(new String[] { "echo", "hello", "world" });
    CommandOutputWithStatus result = runCommand(new CommandRunner(command).withVerbose(true));
    assertThat(result.getTerminationStatus().success()).isTrue();
    assertThat(result.getStdout()).isEqualTo("hello world\n");
    assertThat(outContent.toByteArray()).isEmpty();
    String stderr = new String(errContent.toByteArray(), UTF_8);
    // Using contains() because stderr also gets all the logs
    assertThat(stderr).contains("hello world\n");
    // Verify that logging is redirected, even with verbose
    assertLogContains("Executing [echo hello world]", "'echo' STDOUT: hello world", "Command 'echo' finished");
}
Also used : Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 9 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class CommandRunnerTest method testTimeoutCustomExitCode.

@Test
public void testTimeoutCustomExitCode() throws Exception {
    Command command = bashCommand("" + "trap 'exit 42' SIGTERM SIGINT\n" + "echo stdout msg\n" + ">&2 echo stderr msg\n" + "sleep 10\n");
    CommandTimeoutException e = assertThrows(CommandTimeoutException.class, () -> runCommand(new CommandRunner(command, Duration.ofSeconds(1))));
    assertThat(e.getTimeout()).isEquivalentAccordingToCompareTo(Duration.ofSeconds(1));
    assertThat(e.getOutput().getStderr()).contains("stderr msg");
    assertThat(e.getMessage()).containsMatch("Command '.*' killed by Copybara after timeout \\(1s\\)");
    assertThat(e.getOutput().getStdout()).contains("stdout msg");
}
Also used : CommandTimeoutException(com.google.copybara.util.CommandTimeoutException) Command(com.google.copybara.shell.Command) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Example 10 with Command

use of com.google.copybara.shell.Command in project copybara by google.

the class CommandRunnerTest method testCommandWithExtraOutput.

@Test
public void testCommandWithExtraOutput() throws Exception {
    Command command = bashCommand("" + "echo hello\n" + "echo >&2 world\n");
    ByteArrayOutputStream stdoutCollector = new ByteArrayOutputStream();
    ByteArrayOutputStream stderrCollector = new ByteArrayOutputStream();
    runCommand(new CommandRunner(command).withStdErrStream(stderrCollector).withStdOutStream(stdoutCollector));
    String stderr = new String(stderrCollector.toByteArray(), UTF_8);
    String stdout = new String(stdoutCollector.toByteArray(), UTF_8);
    assertThat(stderr).contains("world");
    assertThat(stdout).contains("hello");
}
Also used : Command(com.google.copybara.shell.Command) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CommandRunner(com.google.copybara.util.CommandRunner) Test(org.junit.Test)

Aggregations

Command (com.google.copybara.shell.Command)28 CommandRunner (com.google.copybara.util.CommandRunner)17 CommandException (com.google.copybara.shell.CommandException)14 CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)10 Test (org.junit.Test)10 IOException (java.io.IOException)8 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)7 ImmutableList (com.google.common.collect.ImmutableList)6 ArrayList (java.util.ArrayList)5 Path (java.nio.file.Path)4 RepoException (com.google.copybara.exception.RepoException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ValidationException (com.google.copybara.exception.ValidationException)2 KillableObserver (com.google.copybara.shell.KillableObserver)2 CommandTimeoutException (com.google.copybara.util.CommandTimeoutException)2 Matcher (java.util.regex.Matcher)2 GitEnvironment (com.google.copybara.git.GitEnvironment)1 AbnormalTerminationException (com.google.copybara.shell.AbnormalTerminationException)1 BadExitStatusException (com.google.copybara.shell.BadExitStatusException)1 CommandResult (com.google.copybara.shell.CommandResult)1