Package com.aragost.javahg

Source Code of com.aragost.javahg.Changeset$Extra

/*
* #%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;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import com.aragost.javahg.commands.LogCommand;
import com.aragost.javahg.internals.GenericLogCommand;
import com.aragost.javahg.internals.HgInputStream;
import com.aragost.javahg.internals.RuntimeIOException;
import com.aragost.javahg.internals.Utils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
* Represent data for a single changeset.
* <p>
* A Changeset object can be created just with the node id. The
* actually data will be loaded on demand when it is accessed
*/
public class Changeset {

    /**
     * Character sequence that indicate the begin and end of the
     * changeset in the command output.
     * <p>
     * This pattern is written by the CHANGESET_STYLE_PATH
     */
    private static final byte[] CHANGESET_PATTERN = Utils.randomBytes();

    /**
     * Style file used among other with the log command to read
     * changesets.
     * <p>
     * The style is parsed by the createFromInputStream() method.
     */
    public static final String CHANGESET_STYLE_PATH = Utils.resourceAsFile("/styles/changesets.style",
            ImmutableMap.of("pattern", CHANGESET_PATTERN)).getPath();

    /**
     * The id for the null changeset
     */
    public static final String NULL_ID = "0000000000000000000000000000000000000000";

    private final String node;
    private final Repository repository;

    /**
     * The actual data for the Changeset
     */
    protected ChangesetData data;

    /**
     * Mercurial's extra data. Lazy loaded.
     */
    private Extra extra;

    /**
     * Use {@link Repository#changeset(String)} to create Changesets
     *
     * @param repository
     */
    public Changeset(Repository repository, String node) {
        this.repository = repository;
        this.node = node;
    }

    private static Changeset createFromInputStream(Repository repository, HgInputStream in) throws IOException {
        byte[] node = in.next(40);
        String nodeString = new String(node);
        int revision = in.revisionUpTo('\n');
        Changeset cset = repository.changeset(nodeString);
        String user = in.textUpTo('\n');
        DateTime timestamp = in.dateTimeUpTo('\n');
        String branch = in.textUpTo('\n');
        in.upTo(':');
        String p1 = in.nextAsText(40);
        in.upTo(':');
        String p2 = in.nextAsText(40);
        in.mustMatch(' '); // skip space part of {parents}
        Changeset parent1 = repository.changeset(p1);
        Changeset parent2 = repository.changeset(p2);
        String message = in.textUpTo('\0');

        ChangesetData data = cset.data;
        if (data == null) {
            data = new ChangesetData(revision, user, timestamp, branch, parent1, parent2, message);
            cset.data = data;
        } else if (revision != data.revision) {
            // Handle revision specially because revision is not part of the node
            data.revision = revision;
        }
        return cset;
    }

    /**
     * Read the rest of the content of the stream and return a list of
     * the Changeset found there.
     * <p>
     * Be aware the this method will read everything from the stream,
     * what is after the changesets will simply be discarded.
     *
     * @param repository
     * @param in
     * @return
     */
    public static List<Changeset> readListFromStream(Repository repository, HgInputStream in) {
        List<Changeset> changesets = Lists.newArrayList();
        try {
            boolean found = in.find(CHANGESET_PATTERN);
            if (found) {
                while (!in.match(CHANGESET_PATTERN)) {
                    Changeset cset = Changeset.createFromInputStream(repository, in);
                    changesets.add(cset);
                }
                Utils.consumeAll(in);
            }
            // If the pattern is not found there is no changsets
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
        return changesets;
    }

    public String getNode() {
        return this.node;
    }

    public int getRevision() {
        ensureAllDataLoaded();
        return this.data.revision;
    }

    public String getUser() {
        ensureAllDataLoaded();
        return this.data.user;
    }

    public DateTime getTimestamp() {
        ensureAllDataLoaded();
        return this.data.timestamp;
    }

    public String getBranch() {
        ensureAllDataLoaded();
        return this.data.branch;
    }

    public Changeset getParent1() {
        ensureAllDataLoaded();
        return this.data.parent1;
    }

    public Changeset getParent2() {
        ensureAllDataLoaded();
        return this.data.parent2;
    }

    public String getMessage() {
        ensureAllDataLoaded();
        return this.data.message;
    }

    private void ensureAllDataLoaded() {
        if (this.data != null) {
            return;
        }
        LogCommand.on(this.repository).rev(getNode()).execute();
        if (this.data == null) {
            throw new IllegalStateException("data was not loaded");
        }
    }

    @Deprecated
    public Phase readPhase() {
        return phase();
    }

    /**
     *
     * @return the phase for this changeset.
     */
    public Phase phase() {
        Map<Changeset, Phase> phases = getRepository().phases(getNode());
        return phases.get(this);
    }

    /**
     * Return tags that is pointing the this changeset
     */
    public List<String> tags() {
        GenericLogCommand cmd = new GenericLogCommand(getRepository()).style("tags");
        cmd.rev(getNode());
        HgInputStream stream = cmd.stream();
        List<String> result = Lists.newArrayList();
        try {
            while (!stream.isEof()) {
                String tag = stream.textUpTo(0);
                if (!"tip".equals(tag)) {
                    result.add(tag);
                }
            }
        } catch (IOException e) {
            throw new RuntimeIOException(e);
        }
        return result;
    }

    /**
     *
     * @return Mercurial's extra dictionary
     */
    public synchronized Extra getExtra() {
        if (this.extra == null) {
            GenericLogCommand cmd = new GenericLogCommand(getRepository()).style("extras");
            cmd.rev(getNode());
            this.extra = new Extra(cmd.stream());
        }
        return this.extra;
    }

    @Override
    public boolean equals(Object that) {
        if (that instanceof Changeset) {
            return equals((Changeset) that);
        } else {
            return false;
        }
    }

    public boolean equals(Changeset that) {
        if (this.repository != that.repository) {
            return false;
        }
        return this.getNode().equals(that.getNode());
    }

    @Override
    public int hashCode() {
        return getNode().hashCode();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder("changeset[");
        builder.append(this.data == null ? "?" : this.data.revision).append(':').append(this.node).append(']');
        return builder.toString();
    }

    @VisibleForTesting
    ChangesetData getData() {
        return this.data;
    }

    Repository getRepository() {
        return repository;
    }

    private String decodeBytes(byte[] bytes) {
        return Utils.decodeBytes(bytes, getRepository().getServer().newDecoder());
    }

    /**
     * Class representing the extra dictionary Mercurial has for each
     * changeset.
     * <p>
     * The values can be binary data, but is typically strings. For
     * this reason there is accessor methods to access the values as
     * both byte array and String.
     */
    public class Extra {

        private final Map<String, byte[]> map;

        private Extra(HgInputStream stream) {
            try {
                this.map = Maps.newHashMap();
                // The value is binary data, node is used as delimiter
                byte[] node = stream.upTo(0);
                while (!stream.isEof()) {
                    String key = stream.textUpTo(0);
                    byte[] value = stream.upTo(node);
                    this.map.put(key, value);
                }
            } catch (IOException e) {
                throw new RuntimeIOException(e);
            }
        }

        /**
         * @param key
         * @return The extra data for the key as a String
         */
        public String getString(String key) {
            byte[] bytes = getBytes(key);
            if (bytes == null) {
                return null;
            } else {
                return decodeBytes(bytes);
            }
        }

        /**
         * @param key
         * @return The extra data for the key as byte array
         */
        public byte[] getBytes(String key) {
            return map.get(key);
        }

        /**
         * @return a view on the extra data dictionary where values
         *         are Strings.
         */
        public Map<String, String> stringValuedMap() {
            Function<byte[], String> f = new Function<byte[], String>() {
                public String apply(byte[] input) {
                    return decodeBytes(input);
                }
            };
            return Maps.transformValues(this.map, f);
        }

        /**
         * @return a view on the extra data dictionary where values
         *         are byte arrays.
         */
        public Map<String, byte[]> byteArrayValuedMap() {
            return this.map;
        }

    }

}

class ChangesetData {
    public int revision;
    public String user;
    public DateTime timestamp;
    public String branch;
    public Changeset parent1;
    public Changeset parent2;
    public String message;

    public ChangesetData(int revision, String user, DateTime timestamp, String branch, Changeset parent1,
            Changeset parent2, String message) {
        this.revision = revision;
        this.user = user;
        this.timestamp = timestamp;
        this.branch = branch;
        this.parent1 = parent1;
        this.parent2 = parent2;
        this.message = message;
    }
}
TOP

Related Classes of com.aragost.javahg.Changeset$Extra

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.