Search in sources :

Example 1 with CommandOutput

use of com.google.copybara.util.CommandOutput in project copybara by google.

the class GitRepository method status.

public List<StatusFile> status() throws RepoException {
    CommandOutput output = git(getCwd(), addGitDirAndWorkTreeParams(ImmutableList.of("status", "--porcelain")));
    ImmutableList.Builder<StatusFile> builder = ImmutableList.builder();
    for (String line : Splitter.on('\n').split(output.getStdout())) {
        if (line.isEmpty()) {
            continue;
        }
        // Format 'XY file (-> file)?'
        List<String> split = Splitter.on(" -> ").limit(2).splitToList(line.substring(3));
        String fileName;
        String newFileName;
        if (split.size() == 1) {
            fileName = split.get(0);
            newFileName = null;
        } else {
            fileName = split.get(0);
            newFileName = split.get(1);
        }
        builder.add(new StatusFile(fileName, newFileName, toStatusCode(line.charAt(0)), toStatusCode(line.charAt(1))));
    }
    return builder.build();
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) CommandOutput(com.google.copybara.util.CommandOutput)

Example 2 with CommandOutput

use of com.google.copybara.util.CommandOutput in project copybara by google.

the class GitRepository method showRef.

/**
 * Execute show-ref git command in the local repository and returns a map from reference name to
 * GitReference(SHA-1).
 */
protected ImmutableMap<String, GitRevision> showRef(Iterable<String> refs) throws RepoException {
    ImmutableMap.Builder<String, GitRevision> result = ImmutableMap.builder();
    CommandOutput commandOutput = gitAllowNonZeroExit(CommandRunner.NO_INPUT, ImmutableList.<String>builder().add("show-ref").addAll(refs).build());
    if (!commandOutput.getStderr().isEmpty()) {
        throw new RepoException(String.format("Error executing show-ref on %s git repo:\n%s", getGitDir(), commandOutput.getStderr()));
    }
    for (String line : Splitter.on('\n').split(commandOutput.getStdout())) {
        if (line.isEmpty()) {
            continue;
        }
        List<String> strings = Splitter.on(' ').splitToList(line);
        Preconditions.checkState(strings.size() == 2 && SHA1_PATTERN.matcher(strings.get(0)).matches(), "Cannot parse line: '%s'", line);
        // Ref -> SHA1
        result.put(strings.get(1), new GitRevision(this, strings.get(0)));
    }
    return result.build();
}
Also used : CommandOutput(com.google.copybara.util.CommandOutput) RepoException(com.google.copybara.exception.RepoException) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

CommandOutput (com.google.copybara.util.CommandOutput)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 RepoException (com.google.copybara.exception.RepoException)1