Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca2046029e8c71a09028cb48cc25f83c0035f9d0 (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
/*******************************************************************************
 * Copyright (C) 2011, Jens Baumgart <jens.baumgart@sap.com>
 * Copyright (C) 2012, Robin Stocker <robin@nibor.org>
 * Copyright (C) 2016, Thomas Wolf <thomas.wolf@paranor.ch>
 *
 * 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.core.internal.job;

import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IResourceRuleFactory;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.jobs.ISchedulingRule;
import org.eclipse.core.runtime.jobs.MultiRule;
import org.eclipse.egit.core.internal.util.ResourceUtil;
import org.eclipse.egit.core.project.RepositoryMapping;
import org.eclipse.jgit.lib.Repository;

/**
 * Utility class for scheduling rules
 *
 */
public class RuleUtil {

	/**
	 * Calculates an {@link ISchedulingRule} for Jobs working on the index of a
	 * repository. The rule consists of all projects belonging to the
	 * repository. If you schedule two jobs working on different resources of
	 * the same repository you have to ensure that these jobs cannot run in
	 * parallel because the Git index lock fails if another thread is already
	 * owning the lock. Thus it is a good practice to lock all projects
	 * belonging to a Git repository when executing a write operation on the
	 * index of a repository in a Job.
	 *
	 * @param repository
	 * @return scheduling rule
	 */
	public static ISchedulingRule getRule(Repository repository) {
		IProject[] projects = getProjects(repository);
		if (projects.length == 0)
			return null;
		return new MultiRule(projects);
	}

	/**
	 * Calculates a {@link ISchedulingRule} for Jobs working on the index of
	 * multiple repositories, see {@link #getRule(Repository)}.
	 *
	 * @param repositories
	 * @return scheduling rule
	 */
	public static ISchedulingRule getRuleForRepositories(Collection<Repository> repositories) {
		ISchedulingRule result = null;
		for (Repository repository : repositories) {
			ISchedulingRule rule = getRule(repository);
			result = MultiRule.combine(result, rule);
		}
		return result;
	}

	/**
	 * Calculates a {@link ISchedulingRule} for all repositories related to the
	 * given resources.
	 *
	 * @see RuleUtil#getRule(Repository)
	 * @param resources
	 * @return scheduling rule
	 */
	public static ISchedulingRule getRuleForRepositories(IResource[] resources) {
		ISchedulingRule result = null;
		Set<Repository> repositories = new HashSet<Repository>();
		for (IResource resource : resources) {
			RepositoryMapping mapping = RepositoryMapping.getMapping(resource);
			if (mapping != null)
				repositories.add(mapping.getRepository());
		}
		for (Repository repository : repositories) {
			ISchedulingRule rule = getRule(repository);
			result = MultiRule.combine(result, rule);
		}
		return result;
	}

	/**
	 * Calculates a {@link ISchedulingRule} for all containers of the paths that
	 * are in the workspace.
	 *
	 * @param paths
	 * @return scheduling rule
	 */
	public static ISchedulingRule getRuleForContainers(Collection<IPath> paths) {
		List<ISchedulingRule> rules = new ArrayList<ISchedulingRule>();
		IResourceRuleFactory ruleFactory = ResourcesPlugin.getWorkspace()
				.getRuleFactory();
		for (IPath path : paths) {
			IResource resource = ResourceUtil.getResourceForLocation(path, false);
			if (resource != null) {
				IContainer container = resource.getParent();
				ISchedulingRule rule = ruleFactory.modifyRule(container);
				if (rule != null)
					rules.add(rule);
			}
		}
		if (rules.size() == 0)
			return null;
		else
			return new MultiRule(rules.toArray(new ISchedulingRule[0]));
	}

	/**
	 * Determines the set of projects that are affected by a change in a
	 * repository.
	 *
	 * @param repository
	 *            to find the projects for
	 * @return an array of all {@link IProject}s that are affected by a change
	 *         in the given repository
	 */
	public static IProject[] getProjects(Repository repository) {
		return getProjects(repository.getWorkTree());
	}

	/**
	 * Determines the set of projects that are affected by a change in a
	 * repository.
	 *
	 * @param workTreeDirectory
	 *            working tree directory of the repository to find the projects
	 *            for
	 * @return an array of all {@link IProject}s that are affected by a change
	 *         in the given repository
	 */
	public static IProject[] getProjects(File workTreeDirectory) {
		final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot()
				.getProjects();
		List<IProject> result = new ArrayList<IProject>();
		final Path workTree = workTreeDirectory.getAbsoluteFile().toPath();
		for (IProject p : projects) {
			IPath projectLocation = p.getLocation();
			if (projectLocation == null) {
				continue;
			}
			Path projectPath = projectLocation.toFile().getAbsoluteFile()
					.toPath();
			if (projectPath.startsWith(workTree)
					|| workTree.startsWith(projectPath)) {
				result.add(p);
			}
		}
		return result.toArray(new IProject[0]);
	}

}

Back to the top