Search in sources :

Example 1 with CommandRunner

use of com.google.copybara.util.CommandRunner in project copybara by google.

the class GitRepository method executeGit.

private static CommandOutputWithStatus executeGit(Path cwd, Iterable<String> params, Map<String, String> env, boolean verbose, int maxLogLines) throws CommandException {
    List<String> allParams = new ArrayList<>(Iterables.size(params) + 1);
    allParams.add(resolveGitBinary(env));
    Iterables.addAll(allParams, params);
    Command cmd = new Command(Iterables.toArray(allParams, String.class), env, 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 2 with CommandRunner

use of com.google.copybara.util.CommandRunner 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) throws RepoException {
    try {
        List<String> allParams = new ArrayList<>();
        allParams.add(resolveGitBinary(environment));
        allParams.addAll(addGitDirAndWorkTreeParams(params));
        Command cmd = new Command(Iterables.toArray(allParams, String.class), environment, getCwd().toFile());
        return new CommandRunner(cmd).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 3 with CommandRunner

use of com.google.copybara.util.CommandRunner 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 4 with CommandRunner

use of com.google.copybara.util.CommandRunner in project copybara by google.

the class HgRepository method executeHg.

private CommandOutputWithStatus executeHg(Path cwd, Iterable<String> params, int maxLogLines, Duration timeout) throws CommandException {
    List<String> allParams = new ArrayList<>(Iterables.size(params) + 1);
    // TODO(jlliu): resolve Hg binary here
    allParams.add("hg");
    Iterables.addAll(allParams, params);
    Command cmd = new Command(Iterables.toArray(allParams, String.class), null, cwd.toFile());
    // TODO(jlliu): have environment vars
    CommandRunner runner = new CommandRunner(cmd, timeout).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 5 with CommandRunner

use of com.google.copybara.util.CommandRunner 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)

Aggregations

CommandRunner (com.google.copybara.util.CommandRunner)18 Command (com.google.copybara.shell.Command)17 Test (org.junit.Test)10 CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)7 ArrayList (java.util.ArrayList)5 CommandException (com.google.copybara.shell.CommandException)4 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)3 RepoException (com.google.copybara.exception.RepoException)2 KillableObserver (com.google.copybara.shell.KillableObserver)2 CommandTimeoutException (com.google.copybara.util.CommandTimeoutException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ImmutableList (com.google.common.collect.ImmutableList)1 GitEnvironment (com.google.copybara.git.GitEnvironment)1 AbnormalTerminationException (com.google.copybara.shell.AbnormalTerminationException)1 Killable (com.google.copybara.shell.Killable)1 TerminationStatus (com.google.copybara.shell.TerminationStatus)1 CommandExecutor (com.google.copybara.util.CommandRunner.CommandExecutor)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1