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