<commit_before>/*******************************************************************************
 * Copyright (c) 2008 VecTrace (<NAME>) 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:
 *     <NAME>              - implementation
 *     <NAME>           - added authentication to push
 *     <NAME> (Intland) - bug fixes
 *     <NAME> (Intland) 	 - bug fixes
 *******************************************************************************/
package com.vectrace.MercurialEclipse.commands;

import com.vectrace.MercurialEclipse.MercurialEclipsePlugin;
import com.vectrace.MercurialEclipse.exception.HgException;
import com.vectrace.MercurialEclipse.menu.UpdateJob;
import com.vectrace.MercurialEclipse.model.ChangeSet;
import com.vectrace.MercurialEclipse.model.HgRoot;
import com.vectrace.MercurialEclipse.model.IHgRepositoryLocation;
import com.vectrace.MercurialEclipse.preferences.MercurialPreferenceConstants;
import com.vectrace.MercurialEclipse.team.cache.RefreshRootJob;
import com.vectrace.MercurialEclipse.team.cache.RefreshWorkspaceStatusJob;

public class HgPushPullClient extends AbstractClient {

	public static String push(HgRoot hgRoot, IHgRepositoryLocation repo,
			boolean force, ChangeSet changeset, int timeout) throws HgException {
		return push(hgRoot, repo, force, changeset, timeout, null);
	}

	public static String push(HgRoot hgRoot, IHgRepositoryLocation repo,
			boolean force, ChangeSet changeset, int timeout, String branch) throws HgException {
		AbstractShellCommand command = new HgCommand("push", //$NON-NLS-1$
				makeDescription("Pushing", changeset, branch), hgRoot, true);
		command.setExecutionRule(new AbstractShellCommand.ExclusiveExecutionRule(hgRoot));
		command.setUsePreferenceTimeout(MercurialPreferenceConstants.PUSH_TIMEOUT);

		if (force) {
			command.addOptions("--force"); //$NON-NLS-1$
		}

		applyChangeset(command, changeset);

		boolean newBranch = MercurialEclipsePlugin.getDefault().getPreferenceStore()
				.getBoolean(MercurialPreferenceConstants.PREF_PUSH_NEW_BRANCH);

		if (newBranch) {
			command.addOptions("--new-branch");
		}

		if (branch != null) {
			command.addOptions("--branch", branch);
		}

		addRepoToHgCommand(repo, command);
		return new String(command.executeToBytes(timeout));
	}

	public static String pull(HgRoot hgRoot, ChangeSet changeset,
			IHgRepositoryLocation repo, boolean update, boolean rebase,
			boolean force, boolean timeout, boolean merge) throws HgException {
		return pull(hgRoot, changeset, repo, update, rebase, force, timeout, merge, null);
	}

	public static String pull(HgRoot hgRoot, ChangeSet changeset,
			IHgRepositoryLocation repo, boolean update, boolean rebase,
			boolean force, boolean timeout, boolean merge, String branch) throws HgException {

		HgCommand command = new HgCommand("pull", //$NON-NLS-1$
				makeDescription("Pulling", changeset, branch), hgRoot, true);
		command.setExecutionRule(new AbstractShellCommand.ExclusiveExecutionRule(hgRoot));

		if (update) {
			command.addOptions("--update"); //$NON-NLS-1$
			addMergeToolPreference(command);
		} else if (rebase) {
			command.addOptions("--config", "extensions.hgext.rebase="); //$NON-NLS-1$ //$NON-NLS-2$
			command.addOptions("--rebase"); //$NON-NLS-1$
			addMergeToolPreference(command);
		}

		if (force) {
			command.addOptions("--force"); //$NON-NLS-1$
		}

		applyChangeset(command, changeset);

		if (branch != null) {
			command.addOptions("--branch", branch);
		}

		addRepoToHgCommand(repo, command);

		String result = null;
		try {
			if (timeout) {
				command.setUsePreferenceTimeout(MercurialPreferenceConstants.PULL_TIMEOUT);
				result = new String(command.executeToBytes());
			} else {
				result = new String(command.executeToBytes(Integer.MAX_VALUE));
			}
		} finally {
			if (update && result != null && result.contains("not updating, since new heads added")
					&& !merge && !rebase) {
				// inform user about new heads and ask if he wants to merge or rebase
				UpdateJob.handleMultipleHeads(hgRoot, false);
			}

			// doesn't matter how far we were: we have to trigger update of caches in case
			// the pull was *partly* successful (e.g. pull was ok, but update not)
			refreshProjects(update, hgRoot);
		}
		return result;
	}

	private static String makeDescription(String op, ChangeSet changeset, String branch) {
		if (changeset == null) {
			return op + " all changes" + ((branch == null) ? "" : " in " + branch);
		}

		return op + " changeset " + changeset.getChangeset();
	}

	protected static void applyChangeset(AbstractShellCommand command, ChangeSet changeset) {
		if (changeset != null) {
			String cs = changeset.getChangeset();

			if (cs != null && (cs = cs.trim()).length() > 0) {
				command.addOptions("-r", cs); //$NON-NLS-1$
			}<commit_msg>#14255 Checking for message when pulling multiple heads for specific branch<commit_after>/*******************************************************************************
 * Copyright (c) 2008 VecTrace (<NAME>) 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:
 *     <NAME>              - implementation
 *     <NAME>           - added authentication to push
 *     <NAME> (Intland) - bug fixes
 *     <NAME> (Intland) 	 - bug fixes
 *******************************************************************************/
package com.vectrace.MercurialEclipse.commands;

import java.util.regex.Pattern;

import com.vectrace.MercurialEclipse.MercurialEclipsePlugin;
import com.vectrace.MercurialEclipse.exception.HgException;
import com.vectrace.MercurialEclipse.menu.UpdateJob;
import com.vectrace.MercurialEclipse.model.ChangeSet;
import com.vectrace.MercurialEclipse.model.HgRoot;
import com.vectrace.MercurialEclipse.model.IHgRepositoryLocation;
import com.vectrace.MercurialEclipse.preferences.MercurialPreferenceConstants;
import com.vectrace.MercurialEclipse.team.cache.RefreshRootJob;
import com.vectrace.MercurialEclipse.team.cache.RefreshWorkspaceStatusJob;

public class HgPushPullClient extends AbstractClient {

	public static String push(HgRoot hgRoot, IHgRepositoryLocation repo,
			boolean force, ChangeSet changeset, int timeout) throws HgException {
		return push(hgRoot, repo, force, changeset, timeout, null);
	}

	public static String push(HgRoot hgRoot, IHgRepositoryLocation repo,
			boolean force, ChangeSet changeset, int timeout, String branch) throws HgException {
		AbstractShellCommand command = new HgCommand("push", //$NON-NLS-1$
				makeDescription("Pushing", changeset, branch), hgRoot, true);
		command.setExecutionRule(new AbstractShellCommand.ExclusiveExecutionRule(hgRoot));
		command.setUsePreferenceTimeout(MercurialPreferenceConstants.PUSH_TIMEOUT);

		if (force) {
			command.addOptions("--force"); //$NON-NLS-1$
		}

		applyChangeset(command, changeset);

		boolean newBranch = MercurialEclipsePlugin.getDefault().getPreferenceStore()
				.getBoolean(MercurialPreferenceConstants.PREF_PUSH_NEW_BRANCH);

		if (newBranch) {
			command.addOptions("--new-branch");
		}

		if (branch != null) {
			command.addOptions("--branch", branch);
		}

		addRepoToHgCommand(repo, command);
		return new String(command.executeToBytes(timeout));
	}

	public static String pull(HgRoot hgRoot, ChangeSet changeset,
			IHgRepositoryLocation repo, boolean update, boolean rebase,
			boolean force, boolean timeout, boolean merge) throws HgException {
		return pull(hgRoot, changeset, repo, update, rebase, force, timeout, merge, null);
	}

	public static String pull(HgRoot hgRoot, ChangeSet changeset,
			IHgRepositoryLocation repo, boolean update, boolean rebase,
			boolean force, boolean timeout, boolean merge, String branch) throws HgException {

		HgCommand command = new HgCommand("pull", //$NON-NLS-1$
				makeDescription("Pulling", changeset, branch), hgRoot, true);
		command.setExecutionRule(new AbstractShellCommand.ExclusiveExecutionRule(hgRoot));

		if (update) {
			command.addOptions("--update"); //$NON-NLS-1$
			addMergeToolPreference(command);
		} else if (rebase) {
			command.addOptions("--config", "extensions.hgext.rebase="); //$NON-NLS-1$ //$NON-NLS-2$
			command.addOptions("--rebase"); //$NON-NLS-1$
			addMergeToolPreference(command);
		}

		if (force) {
			command.addOptions("--force"); //$NON-NLS-1$
		}

		applyChangeset(command, changeset);

		if (branch != null) {
			command.addOptions("--branch", branch);
		}

		addRepoToHgCommand(repo, command);

		String result = null;
		try {
			if (timeout) {
				command.setUsePreferenceTimeout(MercurialPreferenceConstants.PULL_TIMEOUT);
				result = new String(command.executeToBytes());
			} else {
				result = new String(command.executeToBytes(Integer.MAX_VALUE));
			}
		} finally {
			if (update && result != null && !merge && !rebase) {
				// different messages from hg depending on if branch was set or not
				if(result.contains("not updating, since new heads added") ||
						(branch != null &&
								Pattern.compile("\\(\\+\\d+\\sheads\\)").matcher(result).find())){

					// inform user about new heads and ask if he wants to merge or rebase
					UpdateJob.handleMultipleHeads(hgRoot, false);
				}
			}

			// doesn't matter how far we were: we have to trigger update of caches in case
			// the pull was *partly* successful (e.g. pull was ok, but update not)
			refreshProjects(update, hgRoot);
		}
		return result;
	}

	private static String makeDescription(String op, ChangeSet changeset, String branch) {
		if (changeset == null) {
			return op + " all changes" + ((branch == null) ? "" : " in " + branch);
		}

		return op + " changeset " + changeset.getChangeset();
	}

	protected static void applyChangeset(AbstractShellCommand command, ChangeSet changeset) {
		if (changeset != null) {
			String cs = changeset.getChangeset();

			if (cs != null && (cs = cs.trim()).length() > 0) {
				command.addOptions("-r", cs); //$NON-NLS-1$
			}
{'fraction_non_alphanumeric': 0.14519268197742313, 'fraction_numerical': 0.003989879330478786, 'mean_word_length': 4.5045527584359935, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6271785', 'n_tokens_mistral': 3426, 'n_tokens_neox': 3036, 'n_words': 763}