Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9248a78d321304fe44d8b7d1a739344c563a616e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*******************************************************************************
 * Copyright (C) 2010, Dariusz Luksza <dariusz@luksza.org>
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package org.eclipse.egit.ui.internal.synchronize.model;

import static org.eclipse.compare.structuremergeviewer.Differencer.LEFT;
import static org.eclipse.compare.structuremergeviewer.Differencer.RIGHT;


/**
 * This helper class contains methods that determinate that operation can be
 * performed on given object
 */
public final class SupportedContextActionsHelper {

	private SupportedContextActionsHelper() {
		// non-instanceable helper class
	}

	/**
	 * @param object
	 * @return {@code true} if commit operation can be performed on
	 *         {@code object}
	 */
	public static boolean canCommit(GitModelObject object) {
		return object instanceof GitModelWorkingFile
				|| object instanceof GitModelCacheFile
				|| object instanceof GitModelCacheTree
				|| object instanceof GitModelCache;
	}

	/**
	 * @param object
	 * @return {@code true} if the merge tool can be used on
	 *         {@code object}
	 */
	public static boolean canUseMergeTool(GitModelObject object) {
		return object instanceof GitModelWorkingFile
				|| object instanceof GitModelCacheFile;
	}

	/**
	 * @param object
	 * @return {@code true} if stage/add operation can be performed on
	 *         {@code object}
	 */
	public static boolean canStage(GitModelObject object) {
		// stage action is not available on root node (object instanceof
		// GitModelWorkingTree) because in some cases we cannot determinate with
		// resource should be staged
		return object instanceof GitModelWorkingFile
				|| getRootObject(object) instanceof GitModelWorkingTree;
	}

	/**
	 * @param object
	 * @return {@code true} if 'assume unchanged' operation can be performed on
	 *         {@code object}
	 */
	public static boolean canAssumeUnchanged(GitModelObject object) {
		return canStage(object) || object instanceof GitModelCacheFile
				|| object instanceof GitModelCacheTree;
	}

	/**
	 * @param object
	 * @return {@code true} if reset operation can be performed on
	 *         {@code object}
	 */
	public static boolean canReset(GitModelObject object) {
		return isOutGoingCommit(object) || canCommit(object);
	}

	/**
	 * @param object
	 * @return {@code true} when push operation can be performed on
	 *         {@code object}
	 */
	public static boolean canPush(GitModelObject object) {
		return isOutGoingCommit(object);
	}

	/**
	 * @param object
	 * @return {@code true} when merge operation can be performed on
	 *         {@code object}
	 */
	public static boolean canMerge(GitModelObject object) {
		return getRootObject(object) instanceof GitModelCommit;
	}

	private static GitModelObject getRootObject(GitModelObject object) {
		GitModelObject root = object.getParent();

		while (root != null && root instanceof GitModelTree)
			root = root.getParent();

		return root;
	}

	private static boolean isOutGoingCommit(GitModelObject object) {
		int direction = LEFT;
		if (getRootObject(object) instanceof GitModelCommit)
			direction = ((GitModelCommit) object).getKind() & RIGHT;

		return direction == RIGHT;
	}

}

Back to the top