Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 010704530e3a31f6d6f9613810a2316875fa4812 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*******************************************************************************
 * Copyright (c) 2010, 2013 SAP AG.
 * 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
 *
 * Contributors:
 *    Mathias Kinzler (SAP AG) - initial implementation
 *******************************************************************************/
package org.eclipse.egit.ui.internal.repository.tree;

import java.io.IOException;
import java.net.URISyntaxException;

import org.eclipse.egit.ui.internal.ResourcePropertyTester;
import org.eclipse.egit.ui.internal.expressions.AbstractPropertyTester;
import org.eclipse.egit.ui.internal.selection.SelectionRepositoryStateCache;
import org.eclipse.egit.ui.internal.trace.GitTraceLocation;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryState;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.util.LfsFactory;

/**
 * Property Tester used for enabling/disabling of context menus in the Git
 * Repositories View.
 */
public class RepositoriesViewPropertyTester extends AbstractPropertyTester {

	@Override
	public boolean test(Object receiver, String property, Object[] args,
			Object expectedValue) {
		if (!(receiver instanceof RepositoryTreeNode)) {
			return false;
		}
		RepositoryTreeNode node = (RepositoryTreeNode) receiver;

		Repository repository = node.getRepository();
		if (repository == null) {
			return false;
		}
		boolean value = internalTest(node, repository, property);
		boolean trace = GitTraceLocation.PROPERTIESTESTER.isActive();
		if (trace) {
			GitTraceLocation
					.getTrace()
					.trace(GitTraceLocation.PROPERTIESTESTER.getLocation(),
							"prop "	+ property + " of " + receiver + " = " + value + ", expected = " + expectedValue); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
		}
		return computeResult(expectedValue, value);
	}

	private boolean internalTest(RepositoryTreeNode node, Repository repository,
			String property) {
		if (property.equals("isBare")) { //$NON-NLS-1$
			return repository.isBare();
		}

		if (property.equals("containsHead")) {//$NON-NLS-1$
			return containsHead(repository);
		}

		if (ResourcePropertyTester.testRepositoryState(repository, property)) {
			return true;
		}

		if (property.equals("isRefCheckedOut")) { //$NON-NLS-1$
			if (node instanceof BranchHierarchyNode) {
				try {
					for (Ref ref : ((BranchHierarchyNode) node)
							.getChildRefsRecursive()) {
						if (isRefCheckedOut(repository, ref)) {
							return true;
						}
					}
				} catch (IOException e) {
					return false;
				}
			}
			if (!(node.getObject() instanceof Ref)) {
				return false;
			}
			Ref ref = (Ref) node.getObject();
			return isRefCheckedOut(repository, ref);
		}
		if (property.equals("isLocalBranch")) { //$NON-NLS-1$
			if (!(node.getObject() instanceof Ref)) {
				return false;
			}
			Ref ref = (Ref) node.getObject();
			return ref.getName().startsWith(Constants.R_HEADS);
		}
		if (property.equals("fetchExists")) { //$NON-NLS-1$
			if (node instanceof RemoteNode) {
				String configName = ((RemoteNode) node).getObject();

				RemoteConfig rconfig;
				try {
					rconfig = new RemoteConfig(
							SelectionRepositoryStateCache.INSTANCE.getConfig(repository),
							configName);
				} catch (URISyntaxException e2) {
					return false;
				}
				// we need to have a fetch ref spec and a fetch URI
				return !rconfig.getFetchRefSpecs().isEmpty()
						&& !rconfig.getURIs().isEmpty();
			}
		}
		if (property.equals("pushExists")) { //$NON-NLS-1$
			if (node instanceof RemoteNode) {
				String configName = ((RemoteNode) node).getObject();

				RemoteConfig rconfig;
				try {
					rconfig = new RemoteConfig(
							SelectionRepositoryStateCache.INSTANCE.getConfig(repository),
							configName);
				} catch (URISyntaxException e2) {
					return false;
				}
				// we need to have at least a push ref spec and any URI
				return !rconfig.getPushRefSpecs().isEmpty()
						&& (!rconfig.getPushURIs().isEmpty() || !rconfig
								.getURIs().isEmpty());
			}
		}
		if (property.equals("canStash")) { //$NON-NLS-1$
			RepositoryState state = SelectionRepositoryStateCache.INSTANCE
					.getRepositoryState(repository);
			return state.canCommit();
		}
		if (property.equals("canMerge")) { //$NON-NLS-1$
			RepositoryState state = SelectionRepositoryStateCache.INSTANCE
					.getRepositoryState(repository);
			if (state != RepositoryState.SAFE) {
				return false;
			}
			String branch = SelectionRepositoryStateCache.INSTANCE
					.getFullBranchName(repository);
			if (branch == null) {
				return false; // fail gracefully...
			}
			return branch.startsWith(Constants.R_HEADS);
		}

		if ("isSubmodule".equals(property)) { //$NON-NLS-1$
			RepositoryTreeNode<?> parent = node.getParent();
			return parent != null
					&& parent.getType() == RepositoryTreeNodeType.SUBMODULES;
		}

		if ("canEnableLfs".equals(property)) { //$NON-NLS-1$
			if (LfsFactory.getInstance().isAvailable()) {
				return !LfsFactory.getInstance().isEnabled(repository);
			}
		}

		return false;
	}

	private boolean isRefCheckedOut(Repository repository, Ref ref) {
		if (ref.getName().startsWith(Constants.R_REFS)) {
			return ref.getName().equals(
					SelectionRepositoryStateCache.INSTANCE.getFullBranchName(repository));
		} else if (ref.getName().equals(Constants.HEAD)) {
			return true;
		} else {
			String leafname = ref.getLeaf().getName();
			if (leafname.startsWith(Constants.R_REFS) && leafname.equals(
					SelectionRepositoryStateCache.INSTANCE.getFullBranchName(repository))) {
				return true;
			} else {
				ObjectId objectId = ref.getLeaf().getObjectId();
				return objectId != null && objectId
						.equals(SelectionRepositoryStateCache.INSTANCE.getHead(repository));
			}
		}
	}

	private boolean containsHead(Repository repository) {
		return SelectionRepositoryStateCache.INSTANCE.getHead(repository) != null;
	}

}

Back to the top