Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to resolve the issue with JetBrains computer names in TFS workspaces #212

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
import com.microsoft.tfs.jni.NTLMEngine;
import com.microsoft.tfs.jni.NegotiateEngine;
import com.microsoft.tfs.jni.PlatformMiscUtils;
import com.microsoft.tfs.jni.helpers.LocalHost;
import com.microsoft.tfs.util.Check;
import com.microsoft.tfs.util.Closable;
import com.microsoft.tfs.util.NewlineUtils;
Expand Down Expand Up @@ -914,6 +915,61 @@ protected final WorkspaceInfo determineCachedWorkspace(final String[] pathFreeAr
return determineCachedWorkspace(pathFreeArguments, false);
}

protected final WorkspaceInfo determineCachedWorkspace(
final String[] pathFreeArguments,
final boolean ignoreWorkspaceOptionValue)
throws CannotFindWorkspaceException,
InvalidOptionValueException,
InvalidOptionException {

/*
* On all platforms but Mac OSX, we do not have issues with the computer
* names detected by CLC.
*/
if (!Platform.isCurrentPlatform(Platform.MAC_OS_X)) {
return determineCachedWorkspaceImpl(pathFreeArguments, ignoreWorkspaceOptionValue);
}

/*
* On the Mac OSX we should check two versions of the short computer
* name to ensure compatibility with the JetBrains' method of its
* compilation. The code should be removed when and if this
* compatibility is not needed anymore.
*/
WorkspaceInfo workspaceInfo = determineCachedWorkspaceImpl(pathFreeArguments, ignoreWorkspaceOptionValue);

if (!LocalHost.getShortName().equalsIgnoreCase(workspaceInfo.getComputer())) {
/*
* The workspace has been created not with CLC.
*/

log.info(
MessageFormat.format(
"The computer name {0} of the cached workspace {1} does not match to the current machine {2}", //$NON-NLS-1$
workspaceInfo.getComputer(),
workspaceInfo.getDisplayName(),
LocalHost.getShortName()));

/*
* Let's check if the workspace has been created not with the
* JetBrains plugin.
*/

final String jbComputerName = LocalHost.getShortNameJB();

if (jbComputerName != null && jbComputerName.equalsIgnoreCase(workspaceInfo.getComputer())) {
log.info(
MessageFormat.format(
"The workspace has been created with the JetBrains plugin. Use the JetBrains computer name {0} as a short host name.", //$NON-NLS-1$
jbComputerName));

System.setProperty(LocalHost.SHORT_NAME_OVERRIDE_PROPERTY, jbComputerName);
}
}

return workspaceInfo;
}

/**
* Determines which cached workspace (if any) matches the workspace command
* line option, or the free arguments, or the current directory (in that
Expand Down Expand Up @@ -944,7 +1000,7 @@ protected final WorkspaceInfo determineCachedWorkspace(final String[] pathFreeAr
* @throws InvalidOptionException
* if the server and collection options are both specified
*/
protected final WorkspaceInfo determineCachedWorkspace(
protected final WorkspaceInfo determineCachedWorkspaceImpl(
final String[] pathFreeArguments,
final boolean ignoreWorkspaceOptionValue)
throws CannotFindWorkspaceException,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public abstract class LocalHost {
* Where the cached computer name string goes.
*/
private static String computerName;
// JetBrains TFS plugin compatible name
private static String computerNameJB;

/**
* <p>
Expand Down Expand Up @@ -108,9 +110,53 @@ public synchronized static String getShortName() {
name.substring(0, (name.length() > MAX_COMPUTER_NAME_SIZE) ? MAX_COMPUTER_NAME_SIZE : name.length());
}

log.info("Short name resolved to: " + computerNameJB); //$NON-NLS-1$
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be computerName not computerNameJB?

return computerName;
}

/**
* <p>
* Gets the short name of the current computer compatible with JetBrains TFS
* plugin.
* <p>
* The only difference with the traditional TEE method
* {@link #getShortName()} is in the order of particular name detection
* sub-method calls. This method prefers {@link #getPureJavaShortName()}.
* </p>
* <p>
* The {@link #SHORT_NAME_OVERRIDE_PROPERTY} may be used to define the
* computer name exactly.
* </p>
*
* <p>
* We un-set the computerName variable, because we might need to recalculate
* it since the system property {@link #SHORT_NAME_OVERRIDE_PROPERTY} might
* change after call {@link #getShortNameJB()} in
* {@link com.microsoft.tfs.client.clc.commands.Command#determineCachedWorkspace()}.
* </p>
*
* @return a short identifier (name of this computer). Never
* <code>null</code> or empty.
*/
public synchronized static String getShortNameJB() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


computerName = null;

if (computerNameJB == null) {
String name = getPureJavaShortName();

if (name != null) {
// Cache the name forever. Truncate if too long.
computerNameJB = name.substring(
0,
(name.length() > MAX_COMPUTER_NAME_SIZE) ? MAX_COMPUTER_NAME_SIZE : name.length());
log.info("Short name resolved to: " + computerNameJB); //$NON-NLS-1$
}
}

return computerNameJB;
}

/**
* Gets the host name using environment variables. Many major versions of
* Java do not support reading environment variables, and on those platforms
Expand Down Expand Up @@ -224,9 +270,9 @@ private static String getMadeUpShortName() {
*/
private static String getPureJavaShortName() {
/*
* I have a feeling this may fail for some computers with unusual network
* configurations (lots of interfaces; no DNS; etc.). It may need to be
* scrapped and re-written.
* I have a feeling this may fail for some computers with unusual
* network configurations (lots of interfaces; no DNS; etc.). It may
* need to be scrapped and re-written.
*/
String name = null;

Expand Down