Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e0a992412b4f2e50be75b5c37868b90b6e9e3da9 (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
/*******************************************************************************
 * Copyright (C) 2016, Stefan Dirix <sdirix@eclipsesource.com>
 *
 * 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.decorators;

import java.io.IOException;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.mapping.ResourceMapping;
import org.eclipse.egit.core.internal.indexdiff.IndexDiffData;
import org.eclipse.egit.core.internal.util.ResourceUtil;
import org.eclipse.egit.ui.internal.resources.ResourceStateFactory;
import org.eclipse.jgit.lib.Repository;

/**
 * Represents a decoratable resource mapping (i.e. a group of resources).
 */
public class DecoratableResourceMapping extends DecoratableResourceGroup {

	private static final String MULTIPLE = "*"; //$NON-NLS-1$

	/**
	 * Denotes the type of decoratable resource, used by the decoration helper.
	 */
	public static final int RESOURCE_MAPPING = 0x10;

	/**
	 * Creates a decoratable resource mapping.
	 *
	 * @param mapping
	 *            the resource mapping to decorate
	 * @throws IOException
	 * @see DecoratableWorkingSet
	 */
	public DecoratableResourceMapping(ResourceMapping mapping)
			throws IOException {
		super(mapping);

		Set<Repository> repositories = new HashSet<>();
		Set<StagingState> stagingStates = new HashSet<>();

		List<IResource> extractResourcesFromMapping = ResourceUtil
				.extractResourcesFromMapping(mapping);

		boolean anyIsTracked = false;
		boolean anyIsConflict = false;
		boolean anyIsDirty = false;

		for (IResource mappingResource : extractResourcesFromMapping) {
			if (mappingResource == null) {
				continue;
			}

			IndexDiffData indexDiffData = ResourceStateFactory.getInstance()
					.getIndexDiffDataOrNull(mappingResource);

			if (indexDiffData == null) {
				continue;
			}

			Repository repository = ResourceUtil.getRepository(mappingResource);
			repositories.add(repository);

			DecoratableResourceAdapter adapter = new DecoratableResourceAdapter(
					indexDiffData, mappingResource);
			if (adapter.isTracked()) {
				anyIsTracked = true;
			}
			if (adapter.hasConflicts()) {
				anyIsConflict = true;
			}
			if (adapter.isDirty()) {
				anyIsDirty = true;
			}
			stagingStates.add(adapter.getStagingState());
		}

		if (anyIsTracked) {
			setTracked(true);
		}
		if (anyIsConflict) {
			setConflicts(true);
		}
		if (anyIsDirty) {
			setDirty(true);
		}

		if (stagingStates.isEmpty()) {
			setStagingState(StagingState.NOT_STAGED);
		} else if (stagingStates.size() == 1) {
			StagingState state = stagingStates.iterator().next();
			if (state != null) {
				setStagingState(state);
			}
		} else {
			// handle mixed states by setting to modified
			setStagingState(StagingState.MODIFIED);
		}

		if (repositories.isEmpty()) {
			return;
		}
		someShared = true;
		decorateRepositoryInformation(this, repositories);
	}

	/**
	 * Decorate repositoryName, branch and branchStatus of
	 * {@link ResourceMapping}s.
	 *
	 * @param resource
	 *            the {@link DecoratableResource} to decorate.
	 * @param repositories
	 *            the collection of {@link Repository} which are affected by the
	 *            given {@link DecoratableResource}.
	 * @throws IOException
	 */
	protected static void decorateRepositoryInformation(
			DecoratableResource resource, Collection<Repository> repositories)
			throws IOException {
		// collect repository info for decoration (bug 369969)
		if (repositories.size() == 1) {
			// single repo, single branch --> [repo branch]
			Repository repository = repositories.iterator().next();
			resource.repositoryName = DecoratorRepositoryStateCache.INSTANCE
					.getRepositoryNameAndState(repository);
			resource.branch = DecoratorRepositoryStateCache.INSTANCE
					.getCurrentBranchLabel(repository);
			resource.branchStatus = DecoratorRepositoryStateCache.INSTANCE
					.getBranchStatus(repository);
		} else if (repositories.size() > 1) {
			// collect branch names but skip branch status (doesn't make sense)
			Set<String> branches = new HashSet<>(2);
			for (Repository repository : repositories) {
				branches.add(DecoratorRepositoryStateCache.INSTANCE
						.getCurrentBranchLabel(repository));
				if (branches.size() > 1)
					break;
			}

			// multiple repos, one branch --> [* branch]
			if (branches.size() == 1) {
				resource.repositoryName = MULTIPLE;
				resource.branch = branches.iterator().next();
			}

			// we set nothing in the following case:
			// multiple repos, multiple branches
		}
	}

	@Override
	public int getType() {
		return RESOURCE_MAPPING;
	}

	@Override
	public String getName() {
		// this value is not used by the GitLightweightDecorator, instead a
		// label provider provides the name and the decorator only provides
		// prefixes and suffixes
		return "<unknown>"; //$NON-NLS-1$
	}
}

Back to the top