Bug 532617 - Replace headstream instead of branch

Change-Id: I637835cc4ef37f95fa7e1a665ad2eb2d2bc27b5e
Signed-off-by: Kalyan Prasad Tatavarthi <kalyan_prasad@in.ibm.com>
diff --git a/bundles/org.eclipse.team.cvs.core/META-INF/MANIFEST.MF b/bundles/org.eclipse.team.cvs.core/META-INF/MANIFEST.MF
index 7189a4f..ce77745 100644
--- a/bundles/org.eclipse.team.cvs.core/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.team.cvs.core/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.team.cvs.core; singleton:=true
-Bundle-Version: 3.3.700.qualifier
+Bundle-Version: 3.3.701.qualifier
 Bundle-Activator: org.eclipse.team.internal.ccvs.core.CVSProviderPlugin
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/bundles/org.eclipse.team.cvs.core/pom.xml b/bundles/org.eclipse.team.cvs.core/pom.xml
index 92b1d96..1b894ef 100644
--- a/bundles/org.eclipse.team.cvs.core/pom.xml
+++ b/bundles/org.eclipse.team.cvs.core/pom.xml
@@ -19,6 +19,6 @@
   </parent>
   <groupId>org.eclipse.team</groupId>
   <artifactId>org.eclipse.team.cvs.core</artifactId>
-  <version>3.3.700-SNAPSHOT</version>
+  <version>3.3.701-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/util/Util.java b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/util/Util.java
index 937df4d..3a3d00b 100644
--- a/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/util/Util.java
+++ b/bundles/org.eclipse.team.cvs.core/src/org/eclipse/team/internal/ccvs/core/util/Util.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -15,8 +15,7 @@
 import java.io.InterruptedIOException;
 import java.net.Socket;
 import java.net.UnknownHostException;
-import java.util.ArrayList;
-import java.util.List;
+import java.util.*;
 
 import org.eclipse.core.resources.*;
 import org.eclipse.core.runtime.*;
@@ -439,6 +438,48 @@
 	}
 
 	/**
+	 * Workaround for CVS "bug" where CVS ENTRIES file does not contain correct
+	 * Branch vs. Version info.  Entries files always record a Tv1 so all entries would
+	 * appear as branches.
+	 * 	
+	 * By comparing the revision number to the tag name and to the existing tags
+	 * you can determine if the tag is a branch or version.
+	 * 
+	 * @param cvsResource the resource to test.  Must nut be null.
+	 * @return the correct cVSTag.  May be null.
+	 */
+	public static CVSTag getAccurateFileTag(ICVSResource cvsResource, CVSTag[] existing) throws CVSException {
+		List tags= Arrays.asList(existing);
+		CVSTag tag = null;
+		ResourceSyncInfo info = cvsResource.getSyncInfo();
+		if(info != null) {
+			tag = info.getTag();
+		}
+
+		FolderSyncInfo parentInfo = cvsResource.getParent().getFolderSyncInfo();
+		CVSTag parentTag = null;
+		if(parentInfo != null) {
+			parentTag = parentInfo.getTag();
+		}
+
+		if(tag != null) {
+			if(tag.getName().equals(info.getRevision())) {
+				tag = new CVSTag(tag.getName(), CVSTag.VERSION);
+			} else if(parentTag != null){
+				tag = new CVSTag(tag.getName(), parentTag.getType());
+			} else if (!tags.contains(tag)) {
+				if (!tags.contains(tag)) {
+					CVSTag newTag= new CVSTag(tag.getName(), CVSTag.VERSION);
+					if (tags.contains(newTag)) {
+						tag = newTag;
+					}
+				}
+			}
+		}
+		return tag;
+	}
+	
+	/**
 	 * Return the fullest path that we can obtain for the given resource
 	 * @param resource
 	 * @return
diff --git a/bundles/org.eclipse.team.cvs.ui/META-INF/MANIFEST.MF b/bundles/org.eclipse.team.cvs.ui/META-INF/MANIFEST.MF
index 36b85ce..594f117 100644
--- a/bundles/org.eclipse.team.cvs.ui/META-INF/MANIFEST.MF
+++ b/bundles/org.eclipse.team.cvs.ui/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@
 Bundle-ManifestVersion: 2
 Bundle-Name: %pluginName
 Bundle-SymbolicName: org.eclipse.team.cvs.ui; singleton:=true
-Bundle-Version: 3.3.700.qualifier
+Bundle-Version: 3.3.701.qualifier
 Bundle-Activator: org.eclipse.team.internal.ccvs.ui.CVSUIPlugin
 Bundle-Vendor: %providerName
 Bundle-Localization: plugin
diff --git a/bundles/org.eclipse.team.cvs.ui/pom.xml b/bundles/org.eclipse.team.cvs.ui/pom.xml
index 228d6a5..4217545 100644
--- a/bundles/org.eclipse.team.cvs.ui/pom.xml
+++ b/bundles/org.eclipse.team.cvs.ui/pom.xml
@@ -19,6 +19,6 @@
   </parent>
   <groupId>org.eclipse.team</groupId>
   <artifactId>org.eclipse.team.cvs.ui</artifactId>
-  <version>3.3.700-SNAPSHOT</version>
+  <version>3.3.701-SNAPSHOT</version>
   <packaging>eclipse-plugin</packaging>
 </project>
diff --git a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/CVSFilePropertiesPage.java b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/CVSFilePropertiesPage.java
index c7d18d8..f448349 100644
--- a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/CVSFilePropertiesPage.java
+++ b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/CVSFilePropertiesPage.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2012 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -25,7 +25,7 @@
 import org.eclipse.team.internal.ccvs.core.ICVSFile;
 import org.eclipse.team.internal.ccvs.core.resources.CVSWorkspaceRoot;
 import org.eclipse.team.internal.ccvs.core.syncinfo.ResourceSyncInfo;
-import org.eclipse.team.internal.ccvs.core.util.Util;
+import org.eclipse.team.internal.ccvs.ui.actions.CVSAction;
 import org.eclipse.ui.PlatformUI;
 
 import com.ibm.icu.text.DateFormat;
@@ -83,7 +83,7 @@
 			
 			// Tag
 			createLabel(composite, CVSUIMessages.CVSFilePropertiesPage_tag); 
-			CVSTag tag = Util.getAccurateFileTag(cvsResource);
+			CVSTag tag = CVSAction.getAccurateFileTag(cvsResource);
 			createReadOnlyText(composite, getTagLabel(tag));
 		} catch (TeamException e) {
 			// Display error text
diff --git a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/CVSAction.java b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/CVSAction.java
index a2fd881..cae90fd 100644
--- a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/CVSAction.java
+++ b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/CVSAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2014 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -33,8 +33,11 @@
 import org.eclipse.team.internal.ccvs.core.*;
 import org.eclipse.team.internal.ccvs.core.filehistory.CVSFileRevision;
 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.*;
 import org.eclipse.team.internal.ccvs.ui.repo.RepositoryManager;
+import org.eclipse.team.internal.ccvs.ui.tags.TagSource;
+import org.eclipse.team.internal.ccvs.ui.tags.TagSourceWorkbenchAdapter;
 import org.eclipse.team.internal.ui.*;
 import org.eclipse.team.internal.ui.actions.TeamAction;
 import org.eclipse.team.internal.ui.dialogs.IPromptCondition;
@@ -644,4 +647,18 @@
         }
         return props.getCVSResourceFor(resource);
     }
+
+    public static CVSTag getAccurateFileTag(ICVSResource cvsResource) throws CVSException {
+		CVSTag tag = null;
+		if (cvsResource != null) {
+			return Util.getAccurateFileTag(cvsResource,  getTags(cvsResource));
+		}
+		return tag;
+	}
+	
+	public static CVSTag[] getTags(ICVSResource cvsResource) {
+		TagSource tagSource= TagSource.create(new ICVSResource[] { cvsResource });
+		return tagSource.getTags(TagSource.convertIncludeFlaqsToTagTypes(TagSourceWorkbenchAdapter.INCLUDE_ALL_TAGS));
+		
+	}
 }
diff --git a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/ReplaceWithRemoteAction.java b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/ReplaceWithRemoteAction.java
index c9a79db..a6b8118 100644
--- a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/ReplaceWithRemoteAction.java
+++ b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/ReplaceWithRemoteAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2007 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -23,7 +23,7 @@
     
 	public void execute(IAction action)  throws InvocationTargetException, InterruptedException {
 		
-		final ReplaceOperation replaceOperation = new ReplaceOperation(getTargetPart(), getCVSResourceMappings(), null);
+		final ReplaceOperation replaceOperation = new ReplaceOperation(getTargetPart(), getCVSResourceMappings(), resourceCommonTag);
 		if (hasOutgoingChanges(replaceOperation)) {
 			final boolean[] keepGoing = new boolean[] { true };
 			Display.getDefault().syncExec(new Runnable() {
diff --git a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/WorkspaceAction.java b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/WorkspaceAction.java
index 2b538d3..cca2ace 100644
--- a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/WorkspaceAction.java
+++ b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/actions/WorkspaceAction.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2010 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -45,6 +45,8 @@
  */
 public abstract class WorkspaceAction extends CVSAction {
 
+	protected CVSTag resourceCommonTag = null;
+	
 	public interface IProviderAction {
 		public IStatus execute(CVSTeamProvider provider, IResource[] resources, IProgressMonitor monitor) throws CVSException;
 	}
@@ -459,6 +461,7 @@
 	 */
 	protected String calculateActionTagValue() {
 		try {
+			resourceCommonTag = null;
 			IResource[] resources = getSelectedResources();
 			CVSTag commonTag = null;
 			boolean sameTagType = true;
@@ -476,7 +479,7 @@
 						tag = Util.getAccurateFolderTag(resources[i], tag);
 					}
 				} else {
-					tag = Util.getAccurateFileTag(cvsResource);
+					tag = CVSAction.getAccurateFileTag(cvsResource);
 				}
 				if(tag == null) {
 					tag = new CVSTag();
@@ -498,6 +501,9 @@
 			if(commonTag != null) {
 				int tagType = commonTag.getType();
 				String tagName = commonTag.getName();
+				if(tagType != CVSTag.HEAD) {
+					resourceCommonTag = commonTag;
+				}
 				// multiple tag names but of the same type
 				if(sameTagType && !multipleSameNames) {
 					if(tagType == CVSTag.BRANCH) {
diff --git a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/subscriber/LogEntryCacheUpdateHandler.java b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/subscriber/LogEntryCacheUpdateHandler.java
index ff7d93b..cc676af 100644
--- a/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/subscriber/LogEntryCacheUpdateHandler.java
+++ b/bundles/org.eclipse.team.cvs.ui/src/org/eclipse/team/internal/ccvs/ui/subscriber/LogEntryCacheUpdateHandler.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * Copyright (c) 2000, 2018 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -31,6 +31,7 @@
 import org.eclipse.team.internal.ccvs.core.util.Util;
 import org.eclipse.team.internal.ccvs.ui.*;
 import org.eclipse.team.internal.ccvs.ui.Policy;
+import org.eclipse.team.internal.ccvs.ui.actions.CVSAction;
 import org.eclipse.team.internal.ccvs.ui.mappings.ModelCompareParticipant;
 import org.eclipse.team.internal.ccvs.ui.operations.RemoteLogOperation;
 import org.eclipse.team.internal.ccvs.ui.operations.RemoteLogOperation.LogEntryCache;
@@ -604,7 +605,7 @@
             		tag = Util.getAccurateFolderTag(local, tag);
             	}
             } else {
-            	tag = Util.getAccurateFileTag(cvsResource);
+            	tag = CVSAction.getAccurateFileTag(cvsResource);
             }
             if(tag == null) {
             	tag = new CVSTag();