Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 61aa9b1c5258e91892bc29c41b8172fd171892e2 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/******************************************************************************
 *  Copyright (c) 2012 GitHub Inc.
 *  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:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *****************************************************************************/
package org.eclipse.egit.ui.internal.branch;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.egit.core.internal.util.ProjectUtil;
import org.eclipse.egit.core.internal.util.ResourceUtil;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.clone.ProjectRecord;
import org.eclipse.egit.ui.internal.clone.ProjectUtils;
import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.util.StringUtils;

/**
 * Class to track which projects are imported for each branch.
 * <p>
 * A unique preference is set for each repository/branch combination that is
 * persisted that includes information to be able to restore projects when the
 * branch is later checked out.
 * </p>
 * <p>
 * The workflow is as follows:
 * </p>
 * <ol>
 * <li>Call {@link #snapshot()} to get the current projects for the currently
 * checked out branch</li>
 * <li>Call {@link #save(ProjectTrackerMemento)} after the new branch has been
 * successfully checked out with the memento returned from step 1</li>
 * <li>Call {@link #restore(IProgressMonitor)} to restore the projects for the
 * newly checked out branch</li>
 * </ol>
 */
class BranchProjectTracker {

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

	private final Repository[] repositories;

	/**
	 * Create tracker for repositories
	 *
	 * @param repositories
	 */
	public BranchProjectTracker(final Repository[] repositories) {
		this.repositories = repositories;
	}

	/**
	 * Create tracker for repository
	 *
	 * @param repository
	 */
	public BranchProjectTracker(final Repository repository) {
		this.repositories = new Repository[] { repository };
	}

	private String getBranch(Repository repo) {
		try {
			return repo.getBranch();
		} catch (IOException e) {
			return null;
		}
	}

	/**
	 * Snapshot the projects currently associated with the repository
	 * <p>
	 * The memento returned can be later passed to
	 * {@link #save(ProjectTrackerMemento)} to persist it
	 *
	 * @see #save(ProjectTrackerMemento)
	 * @return memento
	 */
	public ProjectTrackerMemento snapshot() {
		ProjectTrackerMemento memento = new ProjectTrackerMemento();
		Stream.of(repositories).map(this::takeSnapshot).filter(Objects::nonNull)
				.forEach(x -> memento.addSnapshot(x));
		return memento;
	}

	private ProjectTrackerPreferenceSnapshot takeSnapshot(Repository repo) {
		String branch = getBranch(repo);
		if (StringUtils.isEmptyOrNull(branch)) {
			return null;
		}
		List<String> projectPaths = getAssociatedProjectsPaths(repo);
		if (projectPaths.isEmpty()) {
			return null;
		}
		return new ProjectTrackerPreferenceSnapshot(repo, branch,
				projectPaths);
	}

	@NonNull
	private List<String> getAssociatedProjectsPaths(Repository repo) {
		IProject[] projects = getValidOpenProjects(repo);
		if (projects == null) {
			return Collections.emptyList();
		}
		List<String> projectPaths = new ArrayList<>();

		final String workDir = repo.getWorkTree().getAbsolutePath();
		for (IProject project : projects) {
			IPath path = project.getLocation();
			if (path == null) {
				continue;
			}
			// Only remember mapped projects
			if (!ResourceUtil.isSharedWithGit(project)) {
				continue;
			}
			String fullPath = path.toOSString();
			if (fullPath.startsWith(workDir)) {
				String relative = fullPath.substring(workDir.length());
				if (relative.length() == 0) {
					relative = REPO_ROOT;
				}
				projectPaths.add(relative);
			}
		}
		return projectPaths;
	}

	private IProject[] getValidOpenProjects(Repository repo) {
		try {
			return ProjectUtil.getValidOpenProjects(repo);
		} catch (CoreException e) {
			return null;
		}
	}

	/**
	 * Associate projects with branch. The specified memento must the one
	 * previously returned from a call to {@link #snapshot()}.
	 *
	 * @see #snapshot()
	 * @param snapshot
	 * @return this tracker
	 */
	public BranchProjectTracker save(ProjectTrackerMemento snapshot) {
		snapshot.getSnapshots().stream()
				.forEach(BranchProjectTracker::savePreference);
		return this;
	}

	private static void savePreference(ProjectTrackerPreferenceSnapshot snapshot) {
		Repository repo = snapshot.getRepository();
		String branch = snapshot.getBranch();
		List<String> projects = snapshot.getAssociatedProjects();
		ProjectTrackerPreferenceHelper.saveToPreferences(repo, branch,
				projects);
	}

	/**
	 * Restore projects associated with the currently checked out branch to the
	 * workspace
	 *
	 * @param monitor
	 */
	public void restore(final IProgressMonitor monitor) {
		for (Repository repo : repositories) {
			String branch = getBranch(repo);
			if (branch != null) {
				restore(repo, branch, monitor);
			}
		}
	}

	/**
	 * Restore projects associated with the given branch to the workspace
	 *
	 * @param repo
	 * @param branch
	 * @param monitor
	 */
	public void restore(Repository repo, final String branch,
			final IProgressMonitor monitor) {
		List<String> paths = ProjectTrackerPreferenceHelper
				.restoreFromPreferences(repo, branch);
		if (paths.isEmpty()) {
			return;
		}
		Set<ProjectRecord> records = new LinkedHashSet<>();
		File parent = repo.getWorkTree();
		for (String path : paths) {
			File root;
			if (!REPO_ROOT.equals(path)) {
				root = new File(parent, path);
			} else {
				root = parent;
			}
			if (!root.isDirectory()) {
				continue;
			}
			File projectDescription = new File(root,
					IProjectDescription.DESCRIPTION_FILE_NAME);
			if (!projectDescription.isFile()) {
				continue;
			}
			ProjectRecord record = new ProjectRecord(projectDescription);
			if (record.getProjectDescription() == null) {
				continue;
			}
			records.add(record);
		}
		if (records.isEmpty()) {
			return;
		}
		try {
			ProjectUtils.createProjects(records, true, null, monitor);
		} catch (InvocationTargetException e) {
			Activator
					.logError("Error restoring branch-project associations", e); //$NON-NLS-1$
		} catch (InterruptedException ignored) {
			// Ignored
		}
	}
}

Back to the top