private static final byte[] LINE_BEGIN = "add changeset ".getBytes();
private static final byte[] REMOTE_ERROR = "remote: error: ".getBytes();
public static List<Changeset> parseStream(AbstractCommand command,
HgInputStream stream) throws IOException {
Repository repo = command.getRepository();
List<Changeset> result;
// Pulling locally the full nodes are written after
// "list of changesets:"
// Over HTTP this isn't written.
// In either case 12 character node prefixes are printed after
// "adding changesets".
// If possible use first case, otherwise fall back to node prefixes.
// Make a copy
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteStreams.copy(stream, bos);
stream.consumeAll();
stream = new HgInputStream(new ByteArrayInputStream(bos.toByteArray()), repo.newDecoder());
if (stream.find(BEGIN)) {
result = Lists.newArrayList();
while (!stream.isEof() && !stream.match(BEGIN_FALLBACK)) {
result.add(repo.changeset(stream.textUpTo('\n')));
}
} else {
stream = new HgInputStream(new ByteArrayInputStream(bos.toByteArray()), repo.newDecoder());
List<String> nodePrefixes = Lists.newArrayList();
if (stream.find(BEGIN_FALLBACK)) {
while (stream.find(LINE_BEGIN)) {
// 12 Character node prefix
nodePrefixes.add(stream.textUpTo('\n'));
}
}
if (nodePrefixes.isEmpty()) {
result = Collections.emptyList();
} else {
result = LogCommand
.on(repo)
.rev(nodePrefixes.toArray(new String[nodePrefixes
.size()])).execute();
}
}
stream = new HgInputStream(new ByteArrayInputStream(bos.toByteArray()), repo.newDecoder());
if (stream.find(REMOTE_ERROR)) {
throw new ExecutionException(command, stream.textUpTo('\n'));
}