Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2c7ab5ea2cb716f1761cb0bf1afd965a75fe8992 (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 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.ui.operations;

import java.util.*;

import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.osgi.util.NLS;
import org.eclipse.team.core.TeamException;
import org.eclipse.team.internal.ccvs.core.*;
import org.eclipse.team.internal.ccvs.core.client.*;
import org.eclipse.team.internal.ccvs.core.client.listeners.ILogEntryListener;
import org.eclipse.team.internal.ccvs.core.client.listeners.LogListener;
import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
import org.eclipse.team.internal.ccvs.core.util.Util;
import org.eclipse.team.internal.ccvs.ui.CVSUIMessages;
import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin;
import org.eclipse.team.internal.ccvs.ui.Policy;
import org.eclipse.ui.IWorkbenchPart;

/**
 * Performs an rlog on the resources and caches the results.
 */
public class RemoteLogOperation extends RepositoryLocationOperation {
	
	private RLog rlog = new RLog();
	private CVSTag tag1;
	private CVSTag tag2;
	private LogEntryCache entryCache;
	
	/** 
	 * A log entry cache that can be shared by multiple instances of the
	 * remote log operation.
	 */
	public static class LogEntryCache implements ILogEntryListener {
	    
	    /*
	     * Cache of all log entries
	     */
		private Map<String, Map<String, ILogEntry>> entries = new HashMap<>(); /*
																				 * Map String:remoteFilePath->Map
																				 * (String:revision -> ILogEntry)
																				 */
		
		private Map<String, ILogEntry> internalGetLogEntries(String path) {
            return entries.get(path);
        }
        
        /**
         * Return all the log entries at the given path
         * @param path the file path
         * @return the log entries for the file
         */
        public ILogEntry[] getLogEntries(String path) {
			Map<String, ILogEntry> map = internalGetLogEntries(path);
            return map.values().toArray(new ILogEntry[map.values().size()]);
        }
        
        private ILogEntry internalGetLogEntry(String path, String revision) {
	        Map fileEntries = internalGetLogEntries(path);
	        if (fileEntries != null) {
	            return (ILogEntry)fileEntries.get(revision);
	        }
	        return null;
        }
        
        public String[] getCachedFilePaths() {
            return entries.keySet().toArray(new String[entries.size()]);
        }
        
		/**
		 * Return the log entry that for the given resource
		 * or <code>null</code> if no entry was fetched or the
		 * resource is not a file.
		 * @param getFullPath(resource) the resource
		 * @return the log entry or <code>null</code>
		 */
		public synchronized ILogEntry getLogEntry(ICVSRemoteResource resource) {
		    if (resource instanceof ICVSRemoteFile) {
		        try {
                    String path = getFullPath(resource);
                    String revision = ((ICVSRemoteFile)resource).getRevision();
                    return internalGetLogEntry(path, revision);
                } catch (TeamException e) {
                    // Log and return null
                    CVSUIPlugin.log(e);
                }
		    }
		    return null;
		}

        /**
		 * Return the log entries that were fetched for the given resource
		 * or an empty list if no entry was fetched.
		 * @param getFullPath(resource) the resource
		 * @return the fetched log entries or an empty list is none were found
		 */
		public synchronized ILogEntry[] getLogEntries(ICVSRemoteResource resource) {
			Map<String, ILogEntry> fileEntries = internalGetLogEntries(getFullPath(resource));
		    if (fileEntries != null) {
		        return fileEntries.values().toArray(new ILogEntry[fileEntries.size()]);
		    }
            return new ILogEntry[0];
		}
		
		/*
         * Return the full path that uniquely identifies the resource
         * accross repositories. This path include the repository and
         * resource path but does not include the revision so that 
         * all log entries for a file can be retrieved.
         */
        private String getFullPath(ICVSRemoteResource resource) {
            return Util.appendPath(resource.getRepository().getLocation(false), resource.getRepositoryRelativePath());
        }
		
		public synchronized void clearEntries() {
			entries.clear();
		}
	    
	    public synchronized ICVSRemoteFile getImmediatePredecessor(ICVSRemoteFile file) throws TeamException {
	        ILogEntry[] allLogs = getLogEntries(file);
            String revision = file.getRevision();
            // First decrement the last digit and see if that revision exists
            String predecessorRevision = getPredecessorRevision(revision);
            ICVSRemoteFile predecessor = findRevison(allLogs, predecessorRevision);
            // If nothing was found, try to fond the base of a branch
            if (predecessor == null && isBrancheRevision(revision)) {
                predecessorRevision = getBaseRevision(revision);
                predecessor = findRevison(allLogs, predecessorRevision);
            }
            // If that fails, it is still possible that there is a revision.
            // This can happen if the revision has been manually set.
            if (predecessor == null) {
                // We don't search in this case since this is costly and would be done
                // for any file that is new as well.
            }
            return predecessor;
	    }
        
	    /*
         * Find the given revision in the list of log entries.
         * Return null if the revision wasn't found.
         */
        private ICVSRemoteFile findRevison(ILogEntry[] allLogs, String predecessorRevision) throws TeamException {
            for (int i = 0; i < allLogs.length; i++) {
                ILogEntry entry = allLogs[i];
                ICVSRemoteFile file = entry.getRemoteFile();
                if (file.getRevision().equals(predecessorRevision)) {
                    return file;
                }
            }
            return null;
        }
        /*
	     * Decrement the trailing digit by one.
	     */
        private String getPredecessorRevision(String revision) {
            int digits[] = Util.convertToDigits(revision);
            digits[digits.length -1]--;
            StringBuffer buffer = new StringBuffer(revision.length());
            for (int i = 0; i < digits.length; i++) {
                buffer.append(Integer.toString(digits[i]));
                if (i < digits.length - 1) {
                    buffer.append('.');
                }
            }
            return buffer.toString();
        }
        
        /*
         * Return true if there are more than 2 digits in the revision number
         * (i.e. the revision is on a branch)
         */
        private boolean isBrancheRevision(String revision) {
            return Util.convertToDigits(revision).length > 2;
        }
        
        /*
         * Remove the trailing revision digits such that the
         * returned revision is shorter than the given revision 
         * and is an even number of digits long
         */
        private String getBaseRevision(String revision) {
            int digits[] = Util.convertToDigits(revision);
            int length = digits.length - 1;
            if (length % 2 == 1) {
                length--;
            }
            StringBuffer buffer = new StringBuffer(revision.length());
            for (int i = 0; i < length; i++) {
                buffer.append(Integer.toString(digits[i]));
                if (i < length - 1) {
                    buffer.append('.');
                }
            }
            return buffer.toString();
        }
        /**
         * Remove any entries for the remote resources
         * @param resource the remote resource
         */
        public synchronized void clearEntries(ICVSRemoteResource resource) {
            String remotePath = getFullPath(resource);
            entries.remove(remotePath);
        }

        /* (non-Javadoc)
         * @see org.eclipse.team.internal.ccvs.core.client.listeners.ILogEntryListener#addEntry(org.eclipse.team.internal.ccvs.core.client.listeners.LogEntry)
         */
        @Override
		public void handleLogEntryReceived(ILogEntry entry) {
    		ICVSRemoteFile file = entry.getRemoteFile();
    		String fullPath = getFullPath(file);
    		String revision = entry.getRevision();
			Map<String, ILogEntry> fileEntries = internalGetLogEntries(fullPath);
            if (fileEntries == null) {
				fileEntries = new HashMap<>();
                entries.put(fullPath, fileEntries);
            }
            fileEntries.put(revision, entry);
        }
	}
	
	public RemoteLogOperation(IWorkbenchPart part, ICVSRemoteResource[] remoteResources, CVSTag tag1, CVSTag tag2, LogEntryCache cache) {
		super(part, remoteResources);
		this.tag1 = tag1;
		this.tag2 = tag2;
		this.entryCache = cache;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.team.internal.ccvs.ui.operations.RepositoryLocationOperation#execute(org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation, org.eclipse.team.internal.ccvs.core.ICVSRemoteResource[], org.eclipse.core.runtime.IProgressMonitor)
	 */
	@Override
	protected void execute(ICVSRepositoryLocation location, ICVSRemoteResource[] remoteResources, IProgressMonitor monitor) throws CVSException {
		monitor.beginTask(NLS.bind(CVSUIMessages.RemoteLogOperation_0, new String[] { location.getHost() }), 100); 
		Session s = new Session(location, CVSWorkspaceRoot.getCVSFolderFor(ResourcesPlugin.getWorkspace().getRoot()), false /* do not output to console */);
		// Create a log listener that will update the cache as entries are received
		LogListener listener = new LogListener(entryCache);
		
		ICVSRemoteResource[] remotes = remoteResources;
		Command.LocalOption[] localOptions = getLocalOptions(tag1, tag2);
		if(tag1 == null || tag2 == null) {
			// Optimize the cases were we are only fetching the history for a single revision. If it is
			// already cached, don't fetch it again.
			ArrayList<ICVSRemoteResource> unCachedRemotes = new ArrayList<>();
			for (int i = 0; i < remoteResources.length; i++) {
				ICVSRemoteResource r = remoteResources[i];
				if(entryCache.getLogEntry(r) == null) {
					unCachedRemotes.add(r);
				}
			}
			remotes = unCachedRemotes.toArray(new ICVSRemoteResource[unCachedRemotes.size()]);
		}
		if (remotes.length > 0) {
			try {
				s.open(Policy.subMonitorFor(monitor, 10));
				IStatus status = rlog.execute(s, Command.NO_GLOBAL_OPTIONS, localOptions, remotes, listener, Policy.subMonitorFor(monitor, 90));
				collectStatus(status);
			} finally {
				s.close();
			}
		}
	}

    /* (non-Javadoc)
	 * @see org.eclipse.team.internal.ccvs.ui.operations.CVSOperation#getTaskName()
	 */
	@Override
	protected String getTaskName() {
		return CVSUIMessages.RemoteLogOperation_1; 
	}
	
	protected Command.LocalOption[] getLocalOptions(CVSTag tag1, CVSTag tag2) {
		if(tag1 != null && tag2 != null) {
			return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.makeTagOption(tag1, tag2)};
		} 
		else if (tag1 != null){
			if (tag1.getType() == CVSTag.HEAD ||
				tag1.getType() == CVSTag.VERSION)
				return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.getCurrentTag(tag1)};
			
			if (tag1.getType() == CVSTag.DATE)
				return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.REVISIONS_ON_DEFAULT_BRANCH, RLog.getCurrentTag(tag1)};
			//branch tag
			return new Command.LocalOption[] {RLog.getCurrentTag(tag1)};
		}
		else {
			return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES};
		}
	}
}

Back to the top