Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 02697cdaa3c3cf928d58eddb4aa9ef6f2c499727 (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
/*******************************************************************************
 * Copyright (C) 2006, Robin Rosenberg <robin.rosenberg@dewire.com>
 * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
 * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
 * Copyright (C) 2015, 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.ui.internal.history;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.eclipse.core.resources.IResource;
import org.eclipse.egit.ui.Activator;
import org.eclipse.egit.ui.internal.UIText;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.osgi.util.NLS;

/**
 * Input for the {@link GitHistoryPage}
 **/
public class HistoryPageInput {
	private final List<IResource> list;

	private final List<File> files;

	private final Repository repo;

	private final Object singleFile;

	private final Object singleItem;

	/**
	 * @param repository
	 *            the {@link Repository}
	 * @param resourceItems
	 *            the resources
	 */
	public HistoryPageInput(final Repository repository,
			final IResource[] resourceItems) {
		this.repo = repository;
		list = Arrays.asList(resourceItems);
		if (resourceItems.length == 1) {
			singleItem = resourceItems[0];
			if (resourceItems[0].getType() == IResource.FILE)
				singleFile = resourceItems[0];
			else
				singleFile = null;
		} else {
			singleItem = null;
			singleFile = null;
		}
		files = null;
	}

	/**
	 * @param repository
	 *            the {@link Repository}
	 * @param fileItems
	 *            the files
	 */
	public HistoryPageInput(final Repository repository, final File[] fileItems) {
		this.repo = repository;
		list = null;
		if (fileItems.length == 1 && fileItems[0].isFile()
				&& !inGitDir(repository, fileItems[0])) {
			singleItem = fileItems[0];
			singleFile = fileItems[0];
			files = Arrays.asList(fileItems);
		} else {
			singleItem = null;
			singleFile = null;
			files = filterFilesInGitDir(repository, fileItems);
		}
	}

	/**
	 * @param repository
	 *            the {@link Repository}
	 */
	public HistoryPageInput(final Repository repository) {
		this.repo = repository;
		list = null;
		singleFile = null;
		singleItem = null;
		files = null;
	}

	/**
	 * @return the {@link Repository} provided to the constructor
	 */
	public Repository getRepository() {
		return repo;
	}

	/**
	 * @return the list provided to our constructor
	 */
	public IResource[] getItems() {
		return list == null ? null : list.toArray(new IResource[list.size()]);
	}

	/**
	 * @return the list provided to our constructor
	 */
	public File[] getFileList() {
		return files == null ? null : files.toArray(new File[files.size()]);
	}

	/**
	 * @return the single File, either a {@link IResource} or {@link File}, or
	 *         <code>null</code>
	 */
	public Object getSingleFile() {
		return singleFile;
	}

	/**
	 * @return the single Item, either a {@link IResource} or {@link File}, or
	 *         <code>null</code>
	 */
	public Object getSingleItem() {
		return singleItem;
	}

	/**
	 * @return <code>true</code> if this represents a single file (either as
	 *         {@link IResource} or as {@link File})
	 */
	public boolean isSingleFile() {
		return singleFile != null;
	}

	/**
	 * @return the HEAD Ref
	 */
	public Ref getHead() {
		try {
			Ref h = repo.exactRef(Constants.HEAD);
			if (h != null && h.isSymbolic())
				return h;
			return null;
		} catch (IOException e) {
			throw new IllegalStateException(NLS.bind(
					UIText.GitHistoryPage_errorParsingHead, Activator
							.getDefault().getRepositoryUtil()
							.getRepositoryName(repo)), e);
		}
	}

	/**
	 * Tests whether a file is in a repository's .git directory (or is that
	 * directory itself).
	 *
	 * @param repository
	 *            to test against
	 * @param file
	 *            to test
	 * @return {@code true} if the file is in the .git directory of the
	 *         repository, or is that directory itself.
	 */
	private boolean inGitDir(Repository repository, File file) {
		return file.getAbsoluteFile().toPath().startsWith(
				repository.getDirectory().getAbsoluteFile().toPath());
	}

	/**
	 * Filters all files that are inside a repository's .git directory from the
	 * given files.
	 *
	 * @param repository
	 *            to test against
	 * @param fileItems
	 *            to test
	 * @return a list of all files from {@code files} that are <em>not</em> in
	 *         the .git directory of the repository, or {@code null} if there
	 *         are none.
	 */
	private List<File> filterFilesInGitDir(Repository repository,
			File[] fileItems) {
		List<File> result = new ArrayList<>(fileItems.length);
		Path gitDirPath = repository.getDirectory().getAbsoluteFile().toPath();
		for (File f : fileItems) {
			if (!f.getAbsoluteFile().toPath().startsWith(gitDirPath)) {
				result.add(f);
			}
		}
		if (result.isEmpty()) {
			return null;
		}
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == this) {
			return true;
		}
		if (!(obj instanceof HistoryPageInput)) {
			return false;
		}
		HistoryPageInput other = (HistoryPageInput) obj;
		return repo == other.repo && singleFile == other.singleFile
				&& singleItem == other.singleItem
				&& listEquals(files, other.files)
				&& listEquals(list, other.list);
	}

	private <T> boolean listEquals(List<? extends T> a, List<? extends T> b) {
		if (a == b) {
			return true;
		}
		if (a == null || b == null) {
			return false;
		}
		return Arrays.equals(a.toArray(), b.toArray());
	}

	@Override
	public int hashCode() {
		return (repo == null ? 0 : repo.hashCode())
				^ (singleFile == null ? 0 : singleFile.hashCode())
				^ (singleItem == null ? 0 : singleItem.hashCode())
				^ (files == null ? 0 : Arrays.hashCode(files.toArray()))
				^ (list == null ? 0 : Arrays.hashCode(list.toArray()));
	}
}

Back to the top