Search in sources :

Example 1 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, 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 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) 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 Command

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

the class DiffUtil method checkNotInsideGitRepo.

/**
 * It is very common for users to have a git repo for their $HOME, so that they can version
 * their configurations. Unfortunately this fails for the default output directory (inside
 * $HOME).
 */
private static void checkNotInsideGitRepo(Path path, boolean verbose, Map<String, String> env) throws IOException, InsideGitDirException {
    try {
        Command cmd = new Command(new String[] { resolveGitBinary(env), "rev-parse", "--git-dir" }, env, path.toFile());
        String gitDir = new CommandRunner(cmd).withVerbose(verbose).execute().getStdout().trim();
        // If it doesn't fail it means taht we are inside a git directory
        throw new InsideGitDirException(String.format("Cannot diff/patch because Copybara temporary directory (%s) is inside a git" + " directory (%s).", path, gitDir), gitDir, path);
    } catch (BadExitStatusWithOutputException e) {
        // Some git versions return "Not", others "not"
        if (!e.getOutput().getStderr().contains(/*N*/
        "ot a git repository")) {
            throw new IOException("Error executing rev-parse", e);
        }
    } catch (CommandException e) {
        throw new IOException("Error executing rev-parse", e);
    }
}
Also used : Command(com.google.copybara.shell.Command) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException)

Example 4 with Command

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

the class DiffUtil method diff.

/**
 * Calculates the diff between two sibling directory trees.
 *
 * <p>Returns the diff as an encoding-independent {@code byte[]} that can be write to a file or
 * fed directly into {@link DiffUtil#patch}.
 */
public static byte[] diff(Path one, Path other, boolean verbose, Map<String, String> environment) throws IOException, InsideGitDirException {
    Preconditions.checkArgument(one.getParent().equals(other.getParent()), "Paths 'one' and 'other' must be sibling directories.");
    checkNotInsideGitRepo(one, verbose, environment);
    Path root = one.getParent();
    String[] params = new String[] { "git", "diff", "--no-color", "--", root.relativize(one).toString(), root.relativize(other).toString() };
    Command cmd = new Command(params, environment, root.toFile());
    try {
        new CommandRunner(cmd).withVerbose(verbose).execute();
        return EMPTY_DIFF;
    } catch (BadExitStatusWithOutputException e) {
        CommandOutput output = e.getOutput();
        // git diff returns exit status 0 when contents are identical, or 1 when they are different
        if (!Strings.isNullOrEmpty(output.getStderr())) {
            throw new IOException(String.format("Error executing 'git diff': %s. Stderr: \n%s", e.getMessage(), output.getStderr()), e);
        }
        return output.getStdoutBytes();
    } catch (CommandException e) {
        throw new IOException("Error executing 'patch'", e);
    }
}
Also used : Path(java.nio.file.Path) Command(com.google.copybara.shell.Command) IOException(java.io.IOException) CommandException(com.google.copybara.shell.CommandException)

Example 5 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)

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