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