use of com.google.copybara.shell.Command in project copybara by google.
the class CommandRunnerTest method testCommandWithVerboseLargeOutput.
@Test
public void testCommandWithVerboseLargeOutput() throws Exception {
Path largeFile = Files.createTempDirectory("test").resolve("large_file.txt");
String singleLine = Strings.repeat("A", 100);
try (BufferedWriter writer = Files.newBufferedWriter(largeFile)) {
for (int i = 0; i < LINES_SIZE; i++) {
writer.write(singleLine + "\n");
}
}
Command command = new Command(new String[] { "cat", largeFile.toAbsolutePath().toString() });
CommandOutputWithStatus result = runCommand(new CommandRunner(command).withVerbose(true));
assertThat(result.getTerminationStatus().success()).isTrue();
for (int i = 1; i < LINES_SIZE - 1; i++) {
assertThat(logLines.get(i)).endsWith(singleLine);
}
}
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();
}
use of com.google.copybara.shell.Command 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.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, Duration defaultTimeout) throws RepoException {
try {
List<String> allParams = new ArrayList<>();
allParams.add(gitEnv.resolveGitBinary());
allParams.addAll(addGitDirAndWorkTreeParams(params));
Command cmd = new Command(Iterables.toArray(allParams, String.class), gitEnv.getEnvironment(), getCwd().toFile());
return new CommandRunner(cmd, defaultTimeout).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 GitCredential method fill.
/**
* Execute 'git credential fill' for a url
*
* @param cwd the directory to execute the command. This is important if credential configuration
* is set in the local git config.
* @param url url to get the credentials from
* @return a username and password
* @throws RepoException If the url doesn't have protocol (For example https://), the url is
* not valid or username/password couldn't be found
*/
public UserPassword fill(Path cwd, String url) throws RepoException, ValidationException {
Map<String, String> env = gitEnv.withNoGitPrompt().getEnvironment();
URI uri;
try {
uri = URI.create(url);
} catch (IllegalArgumentException e) {
throw new ValidationException("Cannot get credentials for " + url, e);
}
String protocol = uri.getScheme();
checkCondition(!Strings.isNullOrEmpty(protocol), "Cannot find the protocol for %s", url);
String host = uri.getHost();
Command cmd = new Command(new String[] { gitBinary, "credential", "fill" }, env, cwd.toFile());
String request = format("protocol=%s\nhost=%s\n", protocol, host);
if (!Strings.isNullOrEmpty(uri.getPath())) {
request += format("path=%s\n", CharMatcher.is('/').trimLeadingFrom(uri.getPath()));
}
request += "\n";
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream err = new ByteArrayOutputStream();
try {
// DON'T REPLACE THIS WITH CommandRunner.execute(). WE DON'T WANT TO ACCIDENTALLY LOG THE
// PASSWORD!
CommandResult result = cmd.execute(new ByteArrayInputStream(request.getBytes(UTF_8)), new TimeoutKillableObserver(timeout), out, err);
if (!result.getTerminationStatus().success()) {
throw new RepoException("Error getting credentials:\n" + new String(err.toByteArray(), UTF_8));
}
} catch (BadExitStatusException e) {
String errStr = new String(err.toByteArray(), UTF_8);
checkCondition(!errStr.contains("could not read"), "Interactive prompting of passwords for git is disabled," + " use git credential store before calling Copybara.");
throw new RepoException("Error getting credentials:\n" + errStr, e);
} catch (CommandException e) {
throw new RepoException("Error getting credentials", e);
}
Map<String, String> map = Splitter.on(NEW_LINE).omitEmptyStrings().withKeyValueSeparator("=").split(new String(out.toByteArray(), UTF_8));
if (!map.containsKey("username")) {
throw new RepoException("git credentials for " + url + " didn't return a username");
}
if (!map.containsKey("password")) {
throw new RepoException("git credentials for " + url + " didn't return a password");
}
return new UserPassword(map.get("username"), map.get("password"));
}
Aggregations