use of com.google.copybara.shell.CommandException 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);
}
}
use of com.google.copybara.shell.CommandException in project copybara by google.
the class GitRepository method lsRemote.
/**
* Runs a git ls-remote from the current directory for a repository url. Assumes the path to the
* git binary is already set. You don't have to be in a git repository to run this command. Does
* not work with remote names.
*
* @param url - see <repository> in git help ls-remote
* @param refs - see <refs> in git help ls-remote
* @param env - determines where the Git binaries are
* @param maxLogLines - Limit log lines to the number specified. -1 for unlimited
* @return - a map of refs to sha1 from the git ls-remote output.
*/
public static Map<String, String> lsRemote(String url, Collection<String> refs, Map<String, String> env, int maxLogLines) throws RepoException {
ImmutableMap.Builder<String, String> result = ImmutableMap.builder();
List<String> args = Lists.newArrayList("ls-remote", validateUrl(url));
args.addAll(refs);
CommandOutputWithStatus output;
try {
output = executeGit(FileSystems.getDefault().getPath("."), args, env, false, maxLogLines);
} catch (BadExitStatusWithOutputException e) {
throw new RepoException(String.format("Error running ls-remote for '%s' and refs '%s': Exit code %s, Output:\n%s", url, refs, e.getOutput().getTerminationStatus().getExitCode(), e.getOutput().getStderr()), e);
} catch (CommandException e) {
throw new RepoException(String.format("Error running ls-remote for '%s' and refs '%s'", url, refs), e);
}
if (output.getTerminationStatus().success()) {
for (String line : Splitter.on('\n').split(output.getStdout())) {
if (line.isEmpty()) {
continue;
}
Matcher matcher = LS_REMOTE_OUTPUT_LINE.matcher(line);
if (!matcher.matches()) {
throw new RepoException("Unexpected format for ls-remote output: " + line);
}
result.put(matcher.group(2), matcher.group(1));
}
}
return result.build();
}
use of com.google.copybara.shell.CommandException 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);
}
}
use of com.google.copybara.shell.CommandException in project copybara by google.
the class BuildozerOptions method run.
/**
* Runs buildozer with the given commands.
*/
void run(Console console, Path checkoutDir, Iterable<BuildozerCommand> commands) throws ValidationException, TargetNotFoundException {
List<String> args = Lists.newArrayList(buildozerBin, "-buildifier=" + buildifierOptions.buildifierBin);
// We only use -k in keep going mode because it shows less errors (http://b/69386431)
if (workflowOptions.ignoreNoop) {
args.add("-k");
}
args.add("-f");
args.add("-");
try {
Command cmd = new Command(args.toArray(new String[0]), /*environmentVariables*/
null, checkoutDir.toFile());
CommandOutputWithStatus output = generalOptions.newCommandRunner(cmd).withVerbose(generalOptions.isVerbose()).withInput(Joiner.on('\n').join(commands).getBytes(UTF_8)).execute();
if (!output.getStdout().isEmpty()) {
logger.atInfo().log("buildozer stdout: %s", output.getStdout());
}
if (!output.getStderr().isEmpty()) {
logger.atInfo().log("buildozer stderr: %s", output.getStderr());
}
} catch (BadExitStatusWithOutputException e) {
// Don't print the output for common/known errors.
if (generalOptions.isVerbose()) {
logError(console, e.getOutput());
}
if (e.getResult().getTerminationStatus().getExitCode() == 3) {
// :%java_library
throw new TargetNotFoundException(commandsMessage("Buildozer could not find a target for", commands));
}
if (e.getResult().getTerminationStatus().getExitCode() == 2) {
ImmutableList<String> errors = Splitter.on('\n').splitToList(e.getOutput().getStderr()).stream().filter(s -> !(s.isEmpty() || s.startsWith("fixed "))).collect(ImmutableList.toImmutableList());
ImmutableList.Builder<String> notFoundMsg = ImmutableList.builder();
boolean allNotFound = true;
for (String error : errors) {
Matcher matcher = targetNotFound.matcher(error);
if (matcher.matches()) {
notFoundMsg.add(String.format("Buildozer could not find a target for %s", matcher.group(1)));
} else if (error.contains("no such file or directory") || error.contains("not a directory")) {
notFoundMsg.add("Buildozer could not find build file: " + error);
} else {
allNotFound = false;
}
}
if (allNotFound) {
throw new TargetNotFoundException(Joiner.on("\n").join(notFoundMsg.build()));
}
}
// Otherwise we have already printed above.
if (!generalOptions.isVerbose()) {
logError(console, e.getOutput());
}
throw new ValidationException(String.format("%s\nCommand stderr:%s", commandsMessage("Failed to execute buildozer with args", commands), e.getOutput().getStderr()), e);
} catch (CommandException e) {
String message = String.format("Error '%s' running buildozer command: %s", e.getMessage(), e.getCommand().toDebugString());
console.error(message);
throw new ValidationException(message, e);
}
}
use of com.google.copybara.shell.CommandException in project copybara by google.
the class BuildifierFormat method run.
/**
* Runs buildifier with the given arguments.
*/
private void run(Console console, Path checkoutDir, Iterable<String> args) throws IOException, ValidationException {
ImmutableList.Builder<String> argBuilder = new Builder<String>().add(buildifierOptions.buildifierBin);
if (type != null) {
argBuilder.add("-type=" + type);
}
if (lintMode != LintMode.OFF) {
argBuilder.add("-lint=" + lintMode.toString().toLowerCase());
if (!warnings.isEmpty()) {
argBuilder.add("-warnings=" + Joiner.on(",").join(warnings));
}
}
String[] argv = argBuilder.addAll(args).build().toArray(new String[0]);
try {
Command cmd = new Command(argv, /*environmentVariables*/
null, checkoutDir.toFile());
CommandOutputWithStatus output = generalOptions.newCommandRunner(cmd).withVerbose(generalOptions.isVerbose()).execute();
if (!output.getStdout().isEmpty()) {
logger.atInfo().log("buildifier stdout: %s", output.getStdout());
}
if (!output.getStderr().isEmpty()) {
logger.atInfo().log("buildifier stderr: %s", output.getStderr());
}
} catch (BadExitStatusWithOutputException e) {
log(console, e.getOutput());
checkCondition(e.getResult().getTerminationStatus().getExitCode() != 1, "Build file(s) couldn't be formatted because there was a syntax error");
throw new IOException("Failed to execute buildifier with args: " + args, e);
} catch (CommandException e) {
throw new IOException("Failed to execute buildifier with args: " + args, e);
}
}
Aggregations