- Home /
VersionControl.Provider.CheckoutIsValid returns false, but Checkout succeeds?
I'm trying to get asset status before performing some operations (namely, checkout and add). This is my code:
if (UnityEditor.VersionControl.Provider.isActive)
{
UnityEditor.VersionControl.Asset asset = UnityEditor.VersionControl.Provider.GetAssetByPath(binaryFile);
if (UnityEditor.VersionControl.Provider.CheckoutIsValid(asset))
{
UnityEditor.VersionControl.Provider.Checkout(asset, UnityEditor.VersionControl.CheckoutMode.Asset).Wait();
}
else if (UnityEditor.VersionControl.Provider.AddIsValid(new UnityEditor.VersionControl.AssetList() { asset }))
{
UnityEditor.VersionControl.Provider.Add(asset, false).Wait();
}
}
CheckoutIsValid and AddIsValid both return false, even when those operations are indeed valid (i.e. asset is in source control, but is not checked out by anyone else or myself and asset is not in source control, respectively). However, if I remove the ifs for those specific cases, the actual Checkout and Add calls succeed. This is with perforce as the version control provider, btw.
Has anyone else run into this problem? Is this a bug or am I using the API incorrectly?
Thanks in advance.
Answer by $$anonymous$$ · Apr 19, 2016 at 08:36 AM
Sorry for the late response on this @Oncle Ben. The key is that you can't use the Asset reference that you got back from the GetAssetByPath() call - you need to do a separate Provider.Status() call on that reference and use the results of that task for the *IsValid calls. Here's an example for checkout:
// returns false if path is not in source control or source control is not active
// optionally, you can check out the file if it is in source control (defaults behavior is to check out)
public static bool IsFileInSourceControl(string path, bool checkout = true)
{
bool inVC = false;
// blocking checkout of versioned file, if necessary
if (UnityEditor.VersionControl.Provider.isActive)
{
UnityEditor.VersionControl.Asset asset = UnityEditor.VersionControl.Provider.GetAssetByPath(path.RelativeAssetPath());
if (asset != null)
{
UnityEditor.VersionControl.Task statusTask = UnityEditor.VersionControl.Provider.Status(asset);
statusTask.Wait();
if (UnityEditor.VersionControl.Provider.CheckoutIsValid(statusTask.assetList[0]))
{
inVC = true;
if (checkout)
{
UnityEditor.VersionControl.Task coTask = UnityEditor.VersionControl.Provider.Checkout(statusTask.assetList[0], UnityEditor.VersionControl.CheckoutMode.Both);
coTask.Wait();
}
}
}
}
return inVC;
}