Package com.aragost.javahg.internals

Source Code of com.aragost.javahg.internals.PullPushHelper

/*
* #%L
* JavaHg
* %%
* Copyright (C) 2011 aragost Trifork ag
* %%
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* #L%
*/
package com.aragost.javahg.internals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

import com.aragost.javahg.Changeset;
import com.aragost.javahg.Repository;
import com.aragost.javahg.commands.ExecutionException;
import com.aragost.javahg.commands.LogCommand;
import com.google.common.collect.Lists;
import com.google.common.io.ByteStreams;

public class PullPushHelper {

    private static final byte[] BEGIN = "list of changesets:\n".getBytes();
    private static final byte[] BEGIN_FALLBACK = "adding changesets\n"
            .getBytes();
    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'));
        }

        return result;
    }
}
TOP

Related Classes of com.aragost.javahg.internals.PullPushHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.