1 11 package org.eclipse.team.internal.ccvs.ui.operations; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Map ; 16 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.core.runtime.IStatus; 20 import org.eclipse.osgi.util.NLS; 21 import org.eclipse.team.core.TeamException; 22 import org.eclipse.team.internal.ccvs.core.CVSException; 23 import org.eclipse.team.internal.ccvs.core.CVSTag; 24 import org.eclipse.team.internal.ccvs.core.ICVSRemoteFile; 25 import org.eclipse.team.internal.ccvs.core.ICVSRemoteResource; 26 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation; 27 import org.eclipse.team.internal.ccvs.core.ILogEntry; 28 import org.eclipse.team.internal.ccvs.core.client.Command; 29 import org.eclipse.team.internal.ccvs.core.client.RLog; 30 import org.eclipse.team.internal.ccvs.core.client.Session; 31 import org.eclipse.team.internal.ccvs.core.client.listeners.ILogEntryListener; 32 import org.eclipse.team.internal.ccvs.core.client.listeners.LogListener; 33 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot; 34 import org.eclipse.team.internal.ccvs.core.util.Util; 35 import org.eclipse.team.internal.ccvs.ui.CVSUIMessages; 36 import org.eclipse.team.internal.ccvs.ui.CVSUIPlugin; 37 import org.eclipse.team.internal.ccvs.ui.Policy; 38 import org.eclipse.ui.IWorkbenchPart; 39 40 43 public class RemoteLogOperation extends RepositoryLocationOperation { 44 45 private RLog rlog = new RLog(); 46 private CVSTag tag1; 47 private CVSTag tag2; 48 private LogEntryCache entryCache; 49 50 54 public static class LogEntryCache implements ILogEntryListener { 55 56 59 private Map entries = new HashMap (); 60 61 private Map internalGetLogEntries(String path) { 62 return (Map )entries.get(path); 63 } 64 65 70 public ILogEntry[] getLogEntries(String path) { 71 Map map = internalGetLogEntries(path); 72 return (ILogEntry[]) map.values().toArray(new ILogEntry[map.values().size()]); 73 } 74 75 private ILogEntry internalGetLogEntry(String path, String revision) { 76 Map fileEntries = internalGetLogEntries(path); 77 if (fileEntries != null) { 78 return (ILogEntry)fileEntries.get(revision); 79 } 80 return null; 81 } 82 83 public String [] getCachedFilePaths() { 84 return (String []) entries.keySet().toArray(new String [entries.size()]); 85 } 86 87 94 public synchronized ILogEntry getLogEntry(ICVSRemoteResource resource) { 95 if (resource instanceof ICVSRemoteFile) { 96 try { 97 String path = getFullPath(resource); 98 String revision = ((ICVSRemoteFile)resource).getRevision(); 99 return internalGetLogEntry(path, revision); 100 } catch (TeamException e) { 101 CVSUIPlugin.log(e); 103 } 104 } 105 return null; 106 } 107 108 114 public synchronized ILogEntry[] getLogEntries(ICVSRemoteResource resource) { 115 Map fileEntries = internalGetLogEntries(getFullPath(resource)); 116 if (fileEntries != null) { 117 return (ILogEntry[]) fileEntries.values().toArray(new ILogEntry[fileEntries.size()]); 118 } 119 return new ILogEntry[0]; 120 } 121 122 128 private String getFullPath(ICVSRemoteResource resource) { 129 return Util.appendPath(resource.getRepository().getLocation(false), resource.getRepositoryRelativePath()); 130 } 131 132 public synchronized void clearEntries() { 133 entries.clear(); 134 } 135 136 public synchronized ICVSRemoteFile getImmediatePredecessor(ICVSRemoteFile file) throws TeamException { 137 ILogEntry[] allLogs = getLogEntries(file); 138 String revision = file.getRevision(); 139 String predecessorRevision = getPredecessorRevision(revision); 141 ICVSRemoteFile predecessor = findRevison(allLogs, predecessorRevision); 142 if (predecessor == null && isBrancheRevision(revision)) { 144 predecessorRevision = getBaseRevision(revision); 145 predecessor = findRevison(allLogs, predecessorRevision); 146 } 147 if (predecessor == null) { 150 } 153 return predecessor; 154 } 155 156 160 private ICVSRemoteFile findRevison(ILogEntry[] allLogs, String predecessorRevision) throws TeamException { 161 for (int i = 0; i < allLogs.length; i++) { 162 ILogEntry entry = allLogs[i]; 163 ICVSRemoteFile file = entry.getRemoteFile(); 164 if (file.getRevision().equals(predecessorRevision)) { 165 return file; 166 } 167 } 168 return null; 169 } 170 173 private String getPredecessorRevision(String revision) { 174 int digits[] = Util.convertToDigits(revision); 175 digits[digits.length -1]--; 176 StringBuffer buffer = new StringBuffer (revision.length()); 177 for (int i = 0; i < digits.length; i++) { 178 buffer.append(Integer.toString(digits[i])); 179 if (i < digits.length - 1) { 180 buffer.append('.'); 181 } 182 } 183 return buffer.toString(); 184 } 185 186 190 private boolean isBrancheRevision(String revision) { 191 return Util.convertToDigits(revision).length > 2; 192 } 193 194 199 private String getBaseRevision(String revision) { 200 int digits[] = Util.convertToDigits(revision); 201 int length = digits.length - 1; 202 if (length % 2 == 1) { 203 length--; 204 } 205 StringBuffer buffer = new StringBuffer (revision.length()); 206 for (int i = 0; i < length; i++) { 207 buffer.append(Integer.toString(digits[i])); 208 if (i < length - 1) { 209 buffer.append('.'); 210 } 211 } 212 return buffer.toString(); 213 } 214 218 public synchronized void clearEntries(ICVSRemoteResource resource) { 219 String remotePath = getFullPath(resource); 220 entries.remove(remotePath); 221 } 222 223 226 public void handleLogEntryReceived(ILogEntry entry) { 227 ICVSRemoteFile file = entry.getRemoteFile(); 228 String fullPath = getFullPath(file); 229 String revision = entry.getRevision(); 230 Map fileEntries = internalGetLogEntries(fullPath); 231 if (fileEntries == null) { 232 fileEntries = new HashMap (); 233 entries.put(fullPath, fileEntries); 234 } 235 fileEntries.put(revision, entry); 236 } 237 } 238 239 public RemoteLogOperation(IWorkbenchPart part, ICVSRemoteResource[] remoteResources, CVSTag tag1, CVSTag tag2, LogEntryCache cache) { 240 super(part, remoteResources); 241 this.tag1 = tag1; 242 this.tag2 = tag2; 243 this.entryCache = cache; 244 } 245 246 249 protected void execute(ICVSRepositoryLocation location, ICVSRemoteResource[] remoteResources, IProgressMonitor monitor) throws CVSException { 250 monitor.beginTask(NLS.bind(CVSUIMessages.RemoteLogOperation_0, new String [] { location.getHost() }), 100); 251 Session s = new Session(location, CVSWorkspaceRoot.getCVSFolderFor(ResourcesPlugin.getWorkspace().getRoot()), false ); 252 LogListener listener = new LogListener(entryCache); 254 255 ICVSRemoteResource[] remotes = remoteResources; 256 Command.LocalOption[] localOptions = getLocalOptions(tag1, tag2); 257 if(tag1 == null || tag2 == null) { 258 ArrayList unCachedRemotes = new ArrayList (); 261 for (int i = 0; i < remoteResources.length; i++) { 262 ICVSRemoteResource r = remoteResources[i]; 263 if(entryCache.getLogEntry(r) == null) { 264 unCachedRemotes.add(r); 265 } 266 } 267 remotes = (ICVSRemoteResource[]) unCachedRemotes.toArray(new ICVSRemoteResource[unCachedRemotes.size()]); 268 } 269 if (remotes.length > 0) { 270 try { 271 s.open(Policy.subMonitorFor(monitor, 10)); 272 IStatus status = rlog.execute(s, Command.NO_GLOBAL_OPTIONS, localOptions, remotes, listener, Policy.subMonitorFor(monitor, 90)); 273 collectStatus(status); 274 } finally { 275 s.close(); 276 } 277 } 278 } 279 280 283 protected String getTaskName() { 284 return CVSUIMessages.RemoteLogOperation_1; 285 } 286 287 protected Command.LocalOption[] getLocalOptions(CVSTag tag1, CVSTag tag2) { 288 if(tag1 != null && tag2 != null) { 289 return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.makeTagOption(tag1, tag2)}; 290 } 291 else if (tag1 != null){ 292 if (tag1.getType() == CVSTag.HEAD || 293 tag1.getType() == CVSTag.VERSION) 294 return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.getCurrentTag(tag1)}; 295 296 if (tag1.getType() == CVSTag.DATE) 297 return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES, RLog.REVISIONS_ON_DEFAULT_BRANCH, RLog.getCurrentTag(tag1)}; 298 return new Command.LocalOption[] {RLog.getCurrentTag(tag1)}; 300 } 301 else { 302 return new Command.LocalOption[] {RLog.NO_TAGS, RLog.ONLY_INCLUDE_CHANGES}; 303 } 304 } 305 } 306 | Popular Tags |