Search in sources :

Example 1 with BadExitStatusWithOutputException

use of com.google.copybara.util.BadExitStatusWithOutputException 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 2 with BadExitStatusWithOutputException

use of com.google.copybara.util.BadExitStatusWithOutputException 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();
}
Also used : BadExitStatusWithOutputException(com.google.copybara.util.BadExitStatusWithOutputException) CommandOutputWithStatus(com.google.copybara.util.CommandOutputWithStatus) Matcher(com.google.re2j.Matcher) RepoException(com.google.copybara.exception.RepoException) CommandException(com.google.copybara.shell.CommandException) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

RepoException (com.google.copybara.exception.RepoException)2 CommandException (com.google.copybara.shell.CommandException)2 BadExitStatusWithOutputException (com.google.copybara.util.BadExitStatusWithOutputException)2 CommandOutputWithStatus (com.google.copybara.util.CommandOutputWithStatus)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Command (com.google.copybara.shell.Command)1 CommandRunner (com.google.copybara.util.CommandRunner)1 Matcher (com.google.re2j.Matcher)1 ArrayList (java.util.ArrayList)1