tikhomirov@181: /* tikhomirov@181: * Copyright (c) 2011 TMate Software Ltd tikhomirov@181: * tikhomirov@181: * This program is free software; you can redistribute it and/or modify tikhomirov@181: * it under the terms of the GNU General Public License as published by tikhomirov@181: * the Free Software Foundation; version 2 of the License. tikhomirov@181: * tikhomirov@181: * This program is distributed in the hope that it will be useful, tikhomirov@181: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@181: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@181: * GNU General Public License for more details. tikhomirov@181: * tikhomirov@181: * For information on how to redistribute this software under tikhomirov@181: * the terms of a license other than GNU General Public License tikhomirov@181: * contact TMate Software at support@hg4j.com tikhomirov@181: */ tikhomirov@181: package org.tmatesoft.hg.core; tikhomirov@181: tikhomirov@192: import java.io.IOException; tikhomirov@192: import java.util.ArrayList; tikhomirov@202: import java.util.HashSet; tikhomirov@202: import java.util.Iterator; tikhomirov@192: import java.util.LinkedHashSet; tikhomirov@192: import java.util.LinkedList; tikhomirov@181: import java.util.List; tikhomirov@194: import java.util.Set; tikhomirov@194: import java.util.TreeSet; tikhomirov@181: tikhomirov@192: import org.tmatesoft.hg.internal.RepositoryComparator; tikhomirov@192: import org.tmatesoft.hg.internal.RepositoryComparator.BranchChain; tikhomirov@192: import org.tmatesoft.hg.repo.HgBundle; tikhomirov@192: import org.tmatesoft.hg.repo.HgChangelog; tikhomirov@192: import org.tmatesoft.hg.repo.HgChangelog.RawChangeset; tikhomirov@192: import org.tmatesoft.hg.repo.HgRemoteRepository; tikhomirov@181: import org.tmatesoft.hg.repo.HgRepository; tikhomirov@181: import org.tmatesoft.hg.util.CancelledException; tikhomirov@181: tikhomirov@181: /** tikhomirov@192: * Command to find out changes available in a remote repository, missing locally. tikhomirov@192: * tikhomirov@181: * @author Artem Tikhomirov tikhomirov@181: * @author TMate Software Ltd. tikhomirov@181: */ tikhomirov@181: public class HgIncomingCommand { tikhomirov@181: tikhomirov@192: private final HgRepository localRepo; tikhomirov@192: private HgRemoteRepository remoteRepo; tikhomirov@202: @SuppressWarnings("unused") tikhomirov@181: private boolean includeSubrepo; tikhomirov@192: private RepositoryComparator comparator; tikhomirov@192: private List missingBranches; tikhomirov@192: private HgChangelog.ParentWalker parentHelper; tikhomirov@194: private Set branches; tikhomirov@181: tikhomirov@181: public HgIncomingCommand(HgRepository hgRepo) { tikhomirov@192: localRepo = hgRepo; tikhomirov@192: } tikhomirov@192: tikhomirov@192: public HgIncomingCommand against(HgRemoteRepository hgRemote) { tikhomirov@192: remoteRepo = hgRemote; tikhomirov@192: comparator = null; tikhomirov@192: missingBranches = null; tikhomirov@192: return this; tikhomirov@181: } tikhomirov@181: tikhomirov@181: /** tikhomirov@194: * Select specific branch to push. tikhomirov@194: * Multiple branch specification possible (changeset from any of these would be included in result). tikhomirov@194: * Note, {@link #executeLite(Object)} does not respect this setting. tikhomirov@192: * tikhomirov@194: * @param branch - branch name, case-sensitive, non-null. tikhomirov@181: * @return this for convenience tikhomirov@194: * @throws IllegalArgumentException when branch argument is null tikhomirov@181: */ tikhomirov@181: public HgIncomingCommand branch(String branch) { tikhomirov@194: if (branch == null) { tikhomirov@194: throw new IllegalArgumentException(); tikhomirov@194: } tikhomirov@194: if (branches == null) { tikhomirov@194: branches = new TreeSet(); tikhomirov@194: } tikhomirov@194: branches.add(branch); tikhomirov@194: return this; tikhomirov@181: } tikhomirov@181: tikhomirov@181: /** tikhomirov@192: * PLACEHOLDER, NOT IMPLEMENTED YET. tikhomirov@192: * tikhomirov@181: * Whether to include sub-repositories when collecting changes, default is true XXX or false? tikhomirov@181: * @return this for convenience tikhomirov@181: */ tikhomirov@181: public HgIncomingCommand subrepo(boolean include) { tikhomirov@181: includeSubrepo = include; tikhomirov@181: throw HgRepository.notImplemented(); tikhomirov@181: } tikhomirov@181: tikhomirov@181: /** tikhomirov@194: * Lightweight check for incoming changes, gives only list of revisions to pull. tikhomirov@194: * Reported changes are from any branch (limits set by {@link #branch(String)} are not taken into account. tikhomirov@181: * tikhomirov@181: * @param context anything hg4j can use to get progress and/or cancel support tikhomirov@181: * @return list of nodes present at remote and missing locally tikhomirov@181: * @throws HgException tikhomirov@181: * @throws CancelledException tikhomirov@181: */ tikhomirov@181: public List executeLite(Object context) throws HgException, CancelledException { tikhomirov@192: LinkedHashSet result = new LinkedHashSet(); tikhomirov@192: RepositoryComparator repoCompare = getComparator(context); tikhomirov@192: for (BranchChain bc : getMissingBranches(context)) { tikhomirov@192: List missing = repoCompare.visitBranches(bc); tikhomirov@202: HashSet common = new HashSet(); // ordering is irrelevant tikhomirov@202: repoCompare.collectKnownRoots(bc, common); tikhomirov@202: // missing could only start with common elements. Once non-common, rest is just distinct branch revision trails. tikhomirov@202: for (Iterator it = missing.iterator(); it.hasNext() && common.contains(it.next()); it.remove()) ; tikhomirov@192: result.addAll(missing); tikhomirov@192: } tikhomirov@192: ArrayList rv = new ArrayList(result); tikhomirov@192: return rv; tikhomirov@181: } tikhomirov@181: tikhomirov@181: /** tikhomirov@181: * Full information about incoming changes tikhomirov@181: * tikhomirov@181: * @throws HgException tikhomirov@181: * @throws CancelledException tikhomirov@181: */ tikhomirov@192: public void executeFull(final HgLogCommand.Handler handler) throws HgException, CancelledException { tikhomirov@192: if (handler == null) { tikhomirov@192: throw new IllegalArgumentException("Delegate can't be null"); tikhomirov@192: } tikhomirov@192: final List common = getCommon(handler); tikhomirov@202: HgBundle changegroup = remoteRepo.getChanges(common); tikhomirov@192: try { tikhomirov@192: changegroup.changes(localRepo, new HgChangelog.Inspector() { tikhomirov@202: private int localIndex = -1; // in case we start with empty repo and localIndex would not get initialized in regular way tikhomirov@192: private final HgChangelog.ParentWalker parentHelper; tikhomirov@192: private final ChangesetTransformer transformer; tikhomirov@192: private final HgChangelog changelog; tikhomirov@192: tikhomirov@192: { tikhomirov@195: transformer = new ChangesetTransformer(localRepo, handler, getParentHelper()); tikhomirov@194: transformer.limitBranches(branches); tikhomirov@192: parentHelper = getParentHelper(); tikhomirov@192: changelog = localRepo.getChangelog(); tikhomirov@192: } tikhomirov@192: tikhomirov@192: public void next(int revisionNumber, Nodeid nodeid, RawChangeset cset) { tikhomirov@192: if (parentHelper.knownNode(nodeid)) { tikhomirov@192: if (!common.contains(nodeid)) { tikhomirov@192: throw new HgBadStateException("Bundle shall not report known nodes other than roots we've supplied"); tikhomirov@192: } tikhomirov@192: localIndex = changelog.getLocalRevision(nodeid); tikhomirov@192: return; tikhomirov@192: } tikhomirov@192: transformer.next(++localIndex, nodeid, cset); tikhomirov@192: } tikhomirov@192: }); tikhomirov@192: } catch (IOException ex) { tikhomirov@192: throw new HgException(ex); tikhomirov@192: } tikhomirov@192: } tikhomirov@192: tikhomirov@192: private RepositoryComparator getComparator(Object context) throws HgException, CancelledException { tikhomirov@192: if (remoteRepo == null) { tikhomirov@192: throw new HgBadArgumentException("Shall specify remote repository to compare against", null); tikhomirov@192: } tikhomirov@192: if (comparator == null) { tikhomirov@192: comparator = new RepositoryComparator(getParentHelper(), remoteRepo); tikhomirov@202: // comparator.compare(context); // XXX meanwhile we use distinct path to calculate common tikhomirov@192: } tikhomirov@192: return comparator; tikhomirov@192: } tikhomirov@192: tikhomirov@192: private HgChangelog.ParentWalker getParentHelper() { tikhomirov@192: if (parentHelper == null) { tikhomirov@192: parentHelper = localRepo.getChangelog().new ParentWalker(); tikhomirov@192: parentHelper.init(); tikhomirov@192: } tikhomirov@192: return parentHelper; tikhomirov@192: } tikhomirov@192: tikhomirov@192: private List getMissingBranches(Object context) throws HgException, CancelledException { tikhomirov@192: if (missingBranches == null) { tikhomirov@192: missingBranches = getComparator(context).calculateMissingBranches(); tikhomirov@192: } tikhomirov@192: return missingBranches; tikhomirov@192: } tikhomirov@192: tikhomirov@192: private List getCommon(Object context) throws HgException, CancelledException { tikhomirov@202: // return getComparator(context).getCommon(); tikhomirov@192: final LinkedHashSet common = new LinkedHashSet(); tikhomirov@192: // XXX common can be obtained from repoCompare, but at the moment it would almost duplicate work of calculateMissingBranches tikhomirov@192: // once I refactor latter, common shall be taken from repoCompare. tikhomirov@202: RepositoryComparator repoCompare = getComparator(context); tikhomirov@192: for (BranchChain bc : getMissingBranches(context)) { tikhomirov@202: repoCompare.collectKnownRoots(bc, common); tikhomirov@192: } tikhomirov@192: return new LinkedList(common); tikhomirov@181: } tikhomirov@181: }