Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b6c537c3529eb4cc7bbf33adbad54b6a0a07b254 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*******************************************************************************
 * Copyright (c) 2000, 2018 IBM Corporation and others.
 *
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.team.internal.ccvs.core.filehistory;

import java.util.ArrayList;
import java.util.Iterator;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.*;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.RepositoryProvider;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.core.history.IFileHistoryProvider;
import org.eclipse.team.core.history.IFileRevision;
import org.eclipse.team.core.history.provider.FileHistory;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.client.Session;
import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.resources.RemoteFile;

public class CVSFileHistory extends FileHistory {

	public static final int REFRESH_LOCAL = 1;
	public static final int REFRESH_REMOTE = 2;
	
	public static final int REFRESH_ALL = REFRESH_LOCAL | REFRESH_REMOTE;
	
	private final ICVSFile cvsFile;
	//used to hold all revisions (changes based on filtering)
	protected IFileRevision[] revisions;
	//used to hold all of the remote revisions
	protected IFileRevision[] remoteRevisions;
	//used to hold all of the local revisions
	protected IFileRevision[] localRevisions;
	protected boolean includeLocalRevisions;
	protected boolean includeRemoteRevisions;
	protected boolean includesExists;
	protected boolean refetchRevisions;

	private int flag;

	/*
	 * Creates a new CVSFile history that will fetch remote revisions by default.
	 */
	public CVSFileHistory(ICVSFile file) {
		Assert.isNotNull(file);
		this.cvsFile = file;
		this.includeLocalRevisions = false;
		this.includeRemoteRevisions = true;
		this.refetchRevisions = true;
		this.flag = 0;
	}

	/*
	 * 
	 * Creates a new CVSFile history that will fetch remote revisions by default.
	 * The flag passed can be IFileHistoryProvider.SINGLE_REVISION or IFileHistoryProvider.SINGLE_LINE_OF_DESCENT
	 */
	public CVSFileHistory(ICVSFile file, int flag) {
		Assert.isNotNull(file);
		this.cvsFile = file;
		this.includeLocalRevisions = false;
		this.includeRemoteRevisions = true;
		this.refetchRevisions = true;
		this.flag = flag;
	}

	public IFileRevision[] getFileRevisions() {
		if (revisions == null)
			return new IFileRevision[0];
		return revisions;
	}

	/**
	 * Refreshes the revisions for this CVS file. It may or may not contact the server to get new revisions.
	 * 
	 * @param monitor	a progress monitor
	 */
	public void refresh(int flags, IProgressMonitor monitor) throws TeamException {
		if (flags == CVSFileHistory.REFRESH_LOCAL) {
			fetchLocalOnly(monitor);
			return;
		}
			
		if (refetchRevisions) {
			monitor.beginTask(NLS.bind(CVSMessages.CVSFileHistory_0, cvsFile.getRepositoryRelativePath()), 300);
			try {
				ILogEntry[] entries = cvsFile
						.getLogEntries(SubMonitor.convert(monitor, 200));
				
				if (entries.length == 0){
					//Get the parent folder
					ICVSFolder folder = cvsFile.getParent();
					if (folder.isManaged()){
						String remoteFolderLocation = folder.getRemoteLocation(folder);
						if (remoteFolderLocation != null) {
							String remoteFileName = remoteFolderLocation.concat(Session.SERVER_SEPARATOR + cvsFile.getName());
							//Create remote file
							CVSTeamProvider pro = (CVSTeamProvider) RepositoryProvider.getProvider(cvsFile.getIResource().getProject());
							if (pro != null){
								CVSWorkspaceRoot root = pro.getCVSWorkspaceRoot();
								CVSRepositoryLocation location = CVSRepositoryLocation.fromString(root.getRemoteLocation().getLocation(false));
								RemoteFile remFile = RemoteFile.create(remoteFileName, location);
								entries=remFile.getLogEntries(monitor);
							}
						}
					}
				}
				
				if (flag == IFileHistoryProvider.SINGLE_REVISION) {
					String revisionNumber = cvsFile.getSyncInfo().getRevision();
					for (int i = 0; i < entries.length; i++) {
						if (entries[i].getRevision().equals(revisionNumber)) {
							remoteRevisions = new IFileRevision[] {new CVSFileRevision(entries[i])};
							revisions = new IFileRevision[1];
							//copy over remote revisions
							System.arraycopy(remoteRevisions, 0, revisions, 0, remoteRevisions.length);
							break;
						}
					}

				} else if (flag == IFileHistoryProvider.SINGLE_LINE_OF_DESCENT) {
					CVSTag tempTag = cvsFile.getSyncInfo().getTag();
					ArrayList<ILogEntry> entriesOfInterest = new ArrayList<>();
					for (int i = 0; i < entries.length; i++) {
						CVSTag[] tags = entries[i].getTags();
						for (int j = 0; j < tags.length; j++) {
							if (tags[j].getType() == tempTag.getType()) {
								if (tempTag.getType() == CVSTag.BRANCH && tempTag.getName().equals(tags[j].getName())) {
									entriesOfInterest.add(entries[i]);
									break;
								} else {
									entriesOfInterest.add(entries[i]);
									break;
								}
							}

						}
					}

					//always fetch the remote revisions, just filter them out from the returned array
					remoteRevisions = new IFileRevision[entriesOfInterest.size()];
					Iterator iter = entriesOfInterest.iterator();
					int i = 0;
					while (iter.hasNext()) {
						remoteRevisions[i++] = new CVSFileRevision((ILogEntry) iter.next());
					}

					//copy over remote revisions
					revisions = new IFileRevision[remoteRevisions.length];
					System.arraycopy(remoteRevisions, 0, revisions, 0, remoteRevisions.length);

				} else {
					localRevisions = new IFileRevision[0];
					//always fetch the local revisions, just filter them out from the returned array if not wanted
					IResource localResource = cvsFile.getIResource();
					includesExists = false;
					if (localResource != null && localResource instanceof IFile) {
						//get the local revisions
						IFileState[] localHistoryState;
						try {
							localHistoryState = ((IFile) localResource).getHistory(SubMonitor.convert(monitor, 100));
							localRevisions = convertToFileRevision(localHistoryState, SubMonitor.convert(monitor, 100));
							includesExists = (localRevisions.length > 0);
						} catch (CoreException e) {
							TeamException.asTeamException(e);
						}
					}

					//always fetch the remote revisions, just filter them out from the returned array
					remoteRevisions = new IFileRevision[entries.length];
					for (int i = 0; i < entries.length; i++) {
						remoteRevisions[i] = new CVSFileRevision(entries[i]);
					}

					revisions = new IFileRevision[0];
					arrangeRevisions();
				}
			}  finally {
				monitor.done();
			}
		} else {
			//don't refetch revisions just return revisions with local revisions as requested
			arrangeRevisions();
		}
		
	}

	private void arrangeRevisions() {
		if (revisions != null) {
			if (includeLocalRevisions && includeRemoteRevisions) {
				//Local + Remote mode
				revisions = new IFileRevision[remoteRevisions.length + localRevisions.length];
				//copy over remote revisions
				System.arraycopy(remoteRevisions, 0, revisions, 0, remoteRevisions.length);
				//copy over local revisions
				System.arraycopy(localRevisions, 0, revisions, remoteRevisions.length, localRevisions.length);
			} else if (includeLocalRevisions) {
				//Local mode only
				revisions = new IFileRevision[localRevisions.length];
				//copy over local revisions
				System.arraycopy(localRevisions, 0, revisions, 0, localRevisions.length);
			} else if (includeRemoteRevisions) {
				//Remote mode and fall through for Local + Remote mode where no Locals exist
				revisions = new IFileRevision[remoteRevisions.length];
				//copy over remote revisions
				System.arraycopy(remoteRevisions, 0, revisions, 0, remoteRevisions.length);
			}
		}
	}

	public IFileRevision getFileRevision(String id) {
		IFileRevision[] revisions = getFileRevisions();
		for (int i = 0; i < revisions.length; i++) {
			if (revisions[i].getContentIdentifier().equals(id))
				return revisions[i];
		}
		return null;
	}

	public IFileRevision[] getContributors(IFileRevision revision) {

		IFileRevision[] revisions = getFileRevisions();

		//the predecessor is the file with a timestamp that is the largest timestamp
		//from the set of all timestamps smaller than the root file's timestamp
		IFileRevision fileRevision = null;
		for (int i = 0; i < revisions.length; i++) {
			if (((CVSFileRevision) revisions[i]).isPredecessorOf(revision)) {
				//no revision has been set as of yet
				if (fileRevision == null)
					fileRevision = revisions[i];
				//this revision is a predecessor - now check to see if it comes
				//after the current predecessor, if it does make it the current predecessor
				if (revisions[i].getTimestamp() > fileRevision.getTimestamp()) {
					fileRevision = revisions[i];
				}
			}
		}
		if (fileRevision == null)
			return new IFileRevision[0];
		return new IFileRevision[] {fileRevision};
	}

	public IFileRevision[] getTargets(IFileRevision revision) {
		IFileRevision[] revisions = getFileRevisions();

		//the predecessor is the file with a timestamp that is the largest timestamp
		//from the set of all timestamps smaller than the root file's timestamp
		ArrayList<IFileRevision> directDescendents = new ArrayList<>();

		for (int i = 0; i < revisions.length; i++) {
			if (((CVSFileRevision) revisions[i]).isDescendentOf(revision)) {
				directDescendents.add(revisions[i]);
			}
		}
		return (IFileRevision[]) directDescendents.toArray(new IFileRevision[directDescendents.size()]);
	}

	private IFileRevision[] convertToFileRevision(IFileState[] localRevisions, IProgressMonitor monitor) {
		boolean modified = false;
		try {
			modified = cvsFile.isModified(monitor);
		} catch (CVSException e) {
		}

		IFile localFile = (IFile) cvsFile.getIResource();
		boolean localFileExists = (localFile != null && localFile.exists());
		int arrayLength = 0;
		if (modified && localFileExists)
			arrayLength++;

		arrayLength += localRevisions.length;

		IFileRevision[] fileRevisions = new IFileRevision[arrayLength];
		for (int i = 0; i < localRevisions.length; i++) {
			IFileState localFileState = localRevisions[i];
			CVSLocalFileRevision localRevision = new CVSLocalFileRevision(localFileState);
			fileRevisions[i] = localRevision;
		}

		if (modified && localFileExists) {
			CVSLocalFileRevision currentFile = new CVSLocalFileRevision(localFile);
			CVSFileHistoryProvider provider = new CVSFileHistoryProvider();
			currentFile.setBaseRevision(provider.getWorkspaceFileRevision(localFile));
			fileRevisions[localRevisions.length] = currentFile;
		}

		return fileRevisions;
	}

	public void includeLocalRevisions(boolean flag) {
		this.includeLocalRevisions = flag;
	}

	public boolean getIncludesExists() {
		return includesExists;
	}

	public void setRefetchRevisions(boolean refetch) {
		this.refetchRevisions = refetch;
	}

	public void includeRemoteRevisions(boolean flag) {
		this.includeRemoteRevisions = flag;
	}

	public void fetchLocalOnly(IProgressMonitor monitor) {
		try{
		localRevisions = new IFileRevision[0];
		//always fetch the local revisions, just filter them out from the returned array if not wanted
		IResource localResource = cvsFile.getIResource();
		includesExists = false;
		if (localResource != null && localResource instanceof IFile) {
			//get the local revisions
			IFileState[] localHistoryState = ((IFile) localResource).getHistory(SubMonitor.convert(monitor, 100));
			localRevisions = convertToFileRevision(localHistoryState, SubMonitor.convert(monitor, 100));
			includesExists = (localRevisions.length > 0);
		}

		if (remoteRevisions == null)
			remoteRevisions = new IFileRevision[0];
		revisions = new IFileRevision[0];
		arrangeRevisions();
		} catch (CoreException ex){
			//nothing to do - calling getFileRevisions() when revisions is null will result in
			//revisions returning an empty array
		} finally {
			monitor.done();
		}
	}

	public boolean isInitialized() {
		return revisions != null;
	}

	public boolean isIncludeLocal() {
		return includeLocalRevisions;
	}		
}

Back to the top