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();
}
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);
}
}
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);
}
}
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);
}
}
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();
}
Aggregations