/*******************************************************************************
 * Copyright (c) 2005-2008 VecTrace (Zingo Andersen) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Bastian Doetsch           - implementation
 *     Andrei Loskutov (Intland) - bugfixes
 *     Philip Graf               - proxy support
 *******************************************************************************/
package com.vectrace.MercurialEclipse.commands;

import java.io.IOException;

import com.vectrace.MercurialEclipse.MercurialEclipsePlugin;
import com.vectrace.MercurialEclipse.exception.HgException;
import com.vectrace.MercurialEclipse.model.Branch;
import com.vectrace.MercurialEclipse.model.HgRoot;
import com.vectrace.MercurialEclipse.model.ChangeSet.Direction;
import com.vectrace.MercurialEclipse.preferences.MercurialPreferenceConstants;
import com.vectrace.MercurialEclipse.team.cache.RemoteData;
import com.vectrace.MercurialEclipse.team.cache.RemoteKey;

public class HgOutgoingClient extends AbstractParseChangesetClient {

	/**
	 * @return never return null
	 */
	public static RemoteData getOutgoing(RemoteKey key) throws HgException {
		AbstractShellCommand command = getCommand(key.getRoot(), key.getBranch());
		boolean computeFullStatus = MercurialEclipsePlugin.getDefault().getPreferenceStore().getBoolean(MercurialPreferenceConstants.SYNC_COMPUTE_FULL_REMOTE_FILE_STATUS);
		int style = computeFullStatus? AbstractParseChangesetClient.STYLE_WITH_FILES : AbstractParseChangesetClient.STYLE_WITH_FILES_FAST;
		try {
			command.addOptions("--style", AbstractParseChangesetClient //$NON-NLS-1$
					.getStyleFile(style).getCanonicalPath());
		} catch (IOException e) {
			throw new HgException(e.getLocalizedMessage(), e);
		}

		addRepoToHgCommand(key.getRepo(), command);

		String result = getResult(command);
		if (result == null) {
			return new RemoteData(key, Direction.OUTGOING);
		}

		RemoteData revisions = createRemoteRevisions(key, result, Direction.OUTGOING, null);
		return revisions;
	}

	private static String getResult(AbstractShellCommand command) throws HgException {
		try {
			String result = command.executeToString();
			if (result.endsWith("no changes found")) { //$NON-NLS-1$
				return null;
			}
			return result;
		} catch (HgException hg) {
			if (hg.getStatus().getCode() == 1) {
				return null;
			}
			throw hg;
		}
	}

	private static AbstractShellCommand getCommand(HgRoot hgRoot, String branch) {
		AbstractShellCommand command = new HgCommand("outgoing", hgRoot, //$NON-NLS-1$
				false);
		command.setExecutionRule(new AbstractShellCommand.ExclusiveExecutionRule(hgRoot));
		command.setUsePreferenceTimeout(MercurialPreferenceConstants.PULL_TIMEOUT);
		if (branch != null) {
			if (!Branch.isDefault(branch)) {
				command.addOptions("-r", branch);
			} else {
				// see issue 10495: there can be many "default" heads, so show all of them
				// otherwise if "-r default" is used, only unnamed at "tip" is shown, if any
			}
		}
		return command;
	}

}

@@ -1429,35 +1429,8 @@
 (key
-.getRoot(), key.getBranch()
 );%0A%09
@@ -2623,39 +2623,92 @@
 and(
-HgRoot hgRoot, String branch) %7B
+RemoteKey key) %7B%0A%09%09HgRoot hgRoot = key.getRoot();%0A%09%09String branch = key.getBranch();
 %0A%09%09A
@@ -3016,24 +3016,95 @@
 (branch)) %7B%0A
+%09%09%09%09if(HgBranchClient.isKnownRemote(hgRoot, key.getRepo(), branch)) %7B%0A%09
 %09%09%09%09command.
@@ -3129,16 +3129,22 @@
 ranch);%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D els

Fix for issue #10883: "Unknown revision" errors after creating a new branch, when syncing on it