use of com.google.copybara.util.CommandOutputWithStatus in project copybara by google.
the class GitRepository method lsRemote.
private static ImmutableMap<String, String> lsRemote(Path cwd, String url, Collection<String> refs, GitEnvironment gitEnv, int maxLogLines, Collection<String> flags) throws RepoException, ValidationException {
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
List<String> args = Lists.newArrayList("ls-remote");
args.addAll(flags);
try {
args.add(validateUrl(url));
} catch (ValidationException e) {
throw new RepoException("Invalid url: " + url, e);
}
args.addAll(refs);
CommandOutputWithStatus output;
try {
output = executeGit(cwd, args, gitEnv, false, maxLogLines);
} catch (BadExitStatusWithOutputException e) {
String errMsg = String.format("Error running ls-remote for '%s' and refs '%s': Exit code %s, Output:\n%s", url, refs, e.getOutput().getTerminationStatus().getExitCode(), e.getOutput().getStderr());
if (e.getOutput().getStderr().contains("Please make sure you have the correct access rights")) {
throw new ValidationException(errMsg, e);
}
throw new RepoException(errMsg, e);
} catch (CommandException e) {
throw new RepoException(String.format("Error running ls-remote for '%s' and refs '%s'", url, refs), e);
}
if (output.getTerminationStatus().success()) {
for (String line : Splitter.on('\n').split(output.getStdout())) {
if (line.isEmpty()) {
continue;
}
Matcher matcher = LS_REMOTE_OUTPUT_LINE.matcher(line);
if (!matcher.matches()) {
throw new RepoException("Unexpected format for ls-remote output: " + line);
}
result.put(matcher.group(2), matcher.group(1));
if (DEFAULT_BRANCH_PATTERN.matches(line)) {
// we have a ref: line, indicating that we were looking for a symbolic ref. bail.
break;
}
}
}
return result.buildOrThrow();
}
use of com.google.copybara.util.CommandOutputWithStatus in project copybara by google.
the class GitRepository method parseRef.
/**
* Resolves a git reference to the SHA-1 reference
*/
public String parseRef(String ref) throws RepoException, CannotResolveRevisionException {
// Runs rev-list on the reference and remove the extra newline from the output.
CommandOutputWithStatus result = gitAllowNonZeroExit(NO_INPUT, ImmutableList.of("rev-list", "-1", ref, "--"), DEFAULT_TIMEOUT);
if (!result.getTerminationStatus().success()) {
throw new CannotResolveRevisionException("Cannot find reference '" + ref + "'");
}
String sha1 = result.getStdout().trim();
Verify.verify(SHA1_PATTERN.matcher(sha1).matches(), "Should be resolved to a SHA-1: %s", sha1);
return sha1;
}
use of com.google.copybara.util.CommandOutputWithStatus 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.util.CommandOutputWithStatus 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.util.CommandOutputWithStatus in project copybara by google.
the class CommandRunnerTest method testCommandWithVerboseLargeOutput.
@Test
public void testCommandWithVerboseLargeOutput() throws Exception {
Path largeFile = Files.createTempDirectory("test").resolve("large_file.txt");
String singleLine = Strings.repeat("A", 100);
try (BufferedWriter writer = Files.newBufferedWriter(largeFile)) {
for (int i = 0; i < LINES_SIZE; i++) {
writer.write(singleLine + "\n");
}
}
Command command = new Command(new String[] { "cat", largeFile.toAbsolutePath().toString() });
CommandOutputWithStatus result = runCommand(new CommandRunner(command).withVerbose(true));
assertThat(result.getTerminationStatus().success()).isTrue();
for (int i = 1; i < LINES_SIZE - 1; i++) {
assertThat(logLines.get(i)).endsWith(singleLine);
}
}
Aggregations