Search in sources :

Example 11 with Command

use of com.google.copybara.shell.Command 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)

Example 12 with Command

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

the class GitRepository method executeGit.

private static CommandOutputWithStatus executeGit(Path cwd, Iterable<String> params, GitEnvironment gitEnv, boolean verbose, int maxLogLines) throws CommandException {
    List<String> allParams = new ArrayList<>(Iterables.size(params) + 1);
    allParams.add(gitEnv.resolveGitBinary());
    Iterables.addAll(allParams, params);
    Command cmd = new Command(Iterables.toArray(allParams, String.class), gitEnv.getEnvironment(), cwd.toFile());
    CommandRunner runner = new CommandRunner(cmd).withVerbose(verbose);
    return maxLogLines >= 0 ? runner.withMaxStdOutLogLines(maxLogLines).execute() : runner.execute();
}
Also used : Command(com.google.copybara.shell.Command) ArrayList(java.util.ArrayList) CommandRunner(com.google.copybara.util.CommandRunner)

Example 13 with Command

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

the class DiffUtil method patch.

/**
 * Applies the diff into a directory tree.
 *
 * <p>{@code diffContents} is the result of invoking {@link DiffUtil#diff}.
 */
public static void patch(Path rootDir, byte[] diffContents, ImmutableList<String> excludedPaths, int stripSlashes, boolean verbose, boolean reverse, Map<String, String> environment) throws IOException, InsideGitDirException {
    if (diffContents.length == 0) {
        return;
    }
    Preconditions.checkArgument(stripSlashes >= 0, "stripSlashes must be >= 0.");
    checkNotInsideGitRepo(rootDir, verbose, environment);
    ImmutableList.Builder<String> params = ImmutableList.builder();
    // Show verbose output unconditionally since it is helpful for debugging issues with patches.
    params.add(resolveGitBinary(environment), "apply", "-v", "--stat", "--apply", "-p" + stripSlashes);
    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("Error executing 'git apply': " + e.getMessage() + ". Stderr: \n" + e.getOutput().getStderr(), e);
    } catch (CommandException e) {
        throw new IOException("Error executing 'git apply'", e);
    }
}
Also used : Command(com.google.copybara.shell.Command) ImmutableList(com.google.common.collect.ImmutableList) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException)

Example 14 with Command

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

the class GitRepository method gitAllowNonZeroExit.

/**
 * Execute git allowing non-zero exit codes. This will only allow program non-zero exit codes
 * (0-10. The upper bound is arbitrary). And will still fail for exit codes like 127 (Command not
 * found).
 */
private CommandOutputWithStatus gitAllowNonZeroExit(byte[] stdin, Iterable<String> params, Duration defaultTimeout) throws RepoException {
    try {
        List<String> allParams = new ArrayList<>();
        allParams.add(gitEnv.resolveGitBinary());
        allParams.addAll(addGitDirAndWorkTreeParams(params));
        Command cmd = new Command(Iterables.toArray(allParams, String.class), gitEnv.getEnvironment(), getCwd().toFile());
        return new CommandRunner(cmd, defaultTimeout).withVerbose(verbose).withInput(stdin).execute();
    } catch (BadExitStatusWithOutputException e) {
        CommandOutputWithStatus output = e.getOutput();
        int exitCode = e.getOutput().getTerminationStatus().getExitCode();
        if (NON_CRASH_ERROR_EXIT_CODES.contains(exitCode)) {
            return output;
        }
        throw throwUnknownGitError(output, params);
    } catch (CommandException e) {
        throw new RepoException("Error executing 'git': " + e.getMessage(), e);
    }
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) Command(com.google.copybara.shell.Command) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) ArrayList(java.util.ArrayList) CommandException(com.google.copybara.shell.CommandException) RepoException(com.google.copybara.exception.RepoException) CommandRunner(com.google.copybara.util.CommandRunner)

Example 15 with Command

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

the class GitCredential method fill.

/**
 * Execute 'git credential fill' for a url
 *
 * @param cwd the directory to execute the command. This is important if credential configuration
 * is set in the local git config.
 * @param url url to get the credentials from
 * @return a username and password
 * @throws RepoException If the url doesn't have protocol (For example https://), the url is
 * not valid or username/password couldn't be found
 */
public UserPassword fill(Path cwd, String url) throws RepoException, ValidationException {
    Map<String, String> env = gitEnv.withNoGitPrompt().getEnvironment();
    URI uri;
    try {
        uri = URI.create(url);
    } catch (IllegalArgumentException e) {
        throw new ValidationException("Cannot get credentials for " + url, e);
    }
    String protocol = uri.getScheme();
    checkCondition(!Strings.isNullOrEmpty(protocol), "Cannot find the protocol for %s", url);
    String host = uri.getHost();
    Command cmd = new Command(new String[] { gitBinary, "credential", "fill" }, env, cwd.toFile());
    String request = format("protocol=%s\nhost=%s\n", protocol, host);
    if (!Strings.isNullOrEmpty(uri.getPath())) {
        request += format("path=%s\n", CharMatcher.is('/').trimLeadingFrom(uri.getPath()));
    }
    request += "\n";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayOutputStream err = new ByteArrayOutputStream();
    try {
        // DON'T REPLACE THIS WITH CommandRunner.execute(). WE DON'T WANT TO ACCIDENTALLY LOG THE
        // PASSWORD!
        CommandResult result = cmd.execute(new ByteArrayInputStream(request.getBytes(UTF_8)), new TimeoutKillableObserver(timeout), out, err);
        if (!result.getTerminationStatus().success()) {
            throw new RepoException("Error getting credentials:\n" + new String(err.toByteArray(), UTF_8));
        }
    } catch (BadExitStatusException e) {
        String errStr = new String(err.toByteArray(), UTF_8);
        checkCondition(!errStr.contains("could not read"), "Interactive prompting of passwords for git is disabled," + " use git credential store before calling Copybara.");
        throw new RepoException("Error getting credentials:\n" + errStr, e);
    } catch (CommandException e) {
        throw new RepoException("Error getting credentials", e);
    }
    Map<String, String> map = Splitter.on(NEW_LINE).omitEmptyStrings().withKeyValueSeparator("=").split(new String(out.toByteArray(), UTF_8));
    if (!map.containsKey("username")) {
        throw new RepoException("git credentials for " + url + " didn't return a username");
    }
    if (!map.containsKey("password")) {
        throw new RepoException("git credentials for " + url + " didn't return a password");
    }
    return new UserPassword(map.get("username"), map.get("password"));
}
Also used : ValidationException(com.google.copybara.exception.ValidationException) TimeoutKillableObserver(com.google.copybara.shell.TimeoutKillableObserver) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RepoException(com.google.copybara.exception.RepoException) CommandException(com.google.copybara.shell.CommandException) URI(java.net.URI) CommandResult(com.google.copybara.shell.CommandResult) Command(com.google.copybara.shell.Command) ByteArrayInputStream(java.io.ByteArrayInputStream) BadExitStatusException(com.google.copybara.shell.BadExitStatusException)

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