Search in sources :

Example 6 with CommandOutputWithStatus

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();
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) AccessValidationException(com.google.copybara.exception.AccessValidationException) ValidationException(com.google.copybara.exception.ValidationException) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) PathMatcher(java.nio.file.PathMatcher) Matcher(com.google.re2j.Matcher) RepoException(com.google.copybara.exception.RepoException) CommandException(com.google.copybara.shell.CommandException) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with CommandOutputWithStatus

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;
}
Also used : CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CannotResolveRevisionException(com.google.copybara.exception.CannotResolveRevisionException)

Example 8 with CommandOutputWithStatus

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);
    }
}
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 9 with CommandOutputWithStatus

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");
}
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 10 with CommandOutputWithStatus

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);
    }
}
Also used : Path(java.nio.file.Path) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) CommandRunner(com.google.copybara.util.CommandRunner) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Aggregations

CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)17 Command (com.google.copybara.shell.Command)10 CommandException (com.google.copybara.shell.CommandException)7 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)7 CommandRunner (com.google.copybara.util.CommandRunner)7 RepoException (com.google.copybara.exception.RepoException)6 ImmutableList (com.google.common.collect.ImmutableList)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ValidationException (com.google.copybara.exception.ValidationException)2 Matcher (com.google.re2j.Matcher)2 IOException (java.io.IOException)2 AccessValidationException (com.google.copybara.exception.AccessValidationException)1 CannotResolveRevisionException (com.google.copybara.exception.CannotResolveRevisionException)1 BufferedWriter (java.io.BufferedWriter)1 Path (java.nio.file.Path)1 PathMatcher (java.nio.file.PathMatcher)1 LinkedHashSet (java.util.LinkedHashSet)1 Matcher (java.util.regex.Matcher)1