Skip to content

Commit

Permalink
Fix getting the username in the case of using multiple accounts
Browse files Browse the repository at this point in the history
Use the "cm profile list" command instead of "cm whoami" that only returns the user of the default server for now
  • Loading branch information
SRombautsU committed Jan 10, 2024
1 parent ba62d52 commit e1f8956
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,46 @@ namespace PlasticSourceControlParsers
#define FILE_STATUS_SEPARATOR TEXT(";")


/**
* Parse the output of the "cm profile list --format="{server};{user}" command
*
* Example:
localhost:8087|sebastien.rombauts
local|[email protected]
SRombautsU@cloud|[email protected]
*/
bool ParseProfileInfo(TArray<FString>& InResults, const FString& InServerUrl, FString& OutUserName)
{
for (const FString& Result : InResults)
{
TArray<FString> ProfileInfos;
Result.ParseIntoArray(ProfileInfos, FILE_STATUS_SEPARATOR, false); // Don't cull empty values
if (ProfileInfos.Num() == 2)
{
if (ProfileInfos[0] == InServerUrl)
{
OutUserName = ProfileInfos[1];
return true;
}
}
}

return false;
}

/**
* Parse workspace information, in the form "Branch /main@UE5PlasticPluginDev@localhost:8087"
* or "Branch /main@UE5PlasticPluginDev@test@cloud" (when connected to the cloud)
* or "Branch /main@rep:UE5OpenWorldPerfTest@repserver:test@cloud"
* or "Changeset 1234@UE5PlasticPluginDev@test@cloud" (when the workspace is switched on a changeset instead of a branch)
*/
bool ParseWorkspaceInfo(TArray<FString>& InResults, FString& OutBranchName, FString& OutRepositoryName, FString& OutServerUrl)
{
if (InResults.Num() == 0)
{
return false;
}

// Get workspace information, in the form "Branch /main@UE5PlasticPluginDev@localhost:8087"
// or "Branch /main@UE5PlasticPluginDev@test@cloud" (when connected to the cloud)
// or "Branch /main@rep:UE5OpenWorldPerfTest@repserver:test@cloud"
// or "Changeset 1234@UE5PlasticPluginDev@test@cloud" (when the workspace is switched on a changeset instead of a branch)
static const FString BranchPrefix(TEXT("Branch "));
static const FString ChangesetPrefix(TEXT("Changeset "));
static const FString LabelPrefix(TEXT("Label "));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ struct FRemoveRedundantErrors
FString Filter;
};

bool ParseProfileInfo(TArray<FString>& InResults, const FString& InServerUrl, FString& OutUserName);

bool ParseWorkspaceInfo(TArray<FString>& InResults, FString& OutBranchName, FString& OutRepositoryName, FString& OutServerUrl);

bool GetChangesetFromWorkspaceStatus(const TArray<FString>& InResults, int32& OutChangeset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,25 @@ void FPlasticSourceControlProvider::CheckPlasticAvailability()

bUsesLocalReadOnlyState = PlasticSourceControlUtils::GetConfigSetFilesAsReadOnly();

// Get user name (from the global Unity Version Control client config)
PlasticSourceControlUtils::GetUserName(UserName);

// Register Console Commands
PlasticSourceControlConsole.Register();

if (!bWorkspaceFound)
if (bWorkspaceFound)
{
TArray<FString> ErrorMessages;
PlasticSourceControlUtils::GetWorkspaceInfo(BranchName, RepositoryName, ServerUrl, ErrorMessages);
UserName = PlasticSourceControlUtils::GetProfileUserName(ServerUrl);
}
else
{
// This info message is only useful here, if bPlasticAvailable, for the Login window
FFormatNamedArguments Args;
Args.Add(TEXT("WorkspacePath"), FText::FromString(PathToWorkspaceRoot));
FMessageLog("SourceControl").Info(FText::Format(LOCTEXT("NotInAWorkspace", "{WorkspacePath} is not in a workspace."), Args));

// Get default server and user name (from the global client config)
ServerUrl = PlasticSourceControlUtils::GetConfigDefaultRepServer();
UserName = PlasticSourceControlUtils::GetDefaultUserName();
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions Source/PlasticSourceControl/Private/PlasticSourceControlUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,35 @@ FString GetConfigDefaultRepServer()
return ServerUrl;
}

void GetUserName(FString& OutUserName)
FString GetDefaultUserName()
{
FString UserName;
TArray<FString> Results;
TArray<FString> ErrorMessages;
const bool bResult = RunCommand(TEXT("whoami"), TArray<FString>(), TArray<FString>(), Results, ErrorMessages);
if (bResult && Results.Num() > 0)
{
OutUserName = MoveTemp(Results[0]);
UserName = MoveTemp(Results[0]);
}

return UserName;
}

FString GetProfileUserName(const FString& InServerUrl)
{
FString UserName;
TArray<FString> Results;
TArray<FString> Parameters;
TArray<FString> ErrorMessages;
Parameters.Add("list");
Parameters.Add(TEXT("--format=\"{server};{user}\""));
bool bResult = RunCommand(TEXT("profile"), Parameters, TArray<FString>(), Results, ErrorMessages);
if (bResult)
{
bResult = PlasticSourceControlParsers::ParseProfileInfo(Results, InServerUrl, UserName);
}

return UserName;
}

bool GetWorkspaceName(const FString& InWorkspaceRoot, FString& OutWorkspaceName, TArray<FString>& OutErrorMessages)
Expand Down
13 changes: 10 additions & 3 deletions Source/PlasticSourceControl/Private/PlasticSourceControlUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,17 @@ bool GetConfigSetFilesAsReadOnly();
FString GetConfigDefaultRepServer();

/**
* Get Unity Version Control current user
* @param OutUserName Name of the Unity Version Control user configured globally
* Get Unity Version Control user for the default server
* @returns Name of the Unity Version Control user configured globally
*/
void GetUserName(FString& OutUserName);
FString GetDefaultUserName();

/**
* Get Unity Version Control user for the specified server
* @param InServerUrl Name of the specified server
* @returns Name of the Unity Version Control user for the specified server
*/
FString GetProfileUserName(const FString& InServerUrl);

/**
* Get workspace name
Expand Down

0 comments on commit e1f8956

Please sign in to comment.