Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit 6248aff

Browse files
committed
Report the correct editing status to Unity. Fixes #934
There's two issues going on here - The `loggedInUser` field wasn't getting seeded with user information on load, so checking whether the current user owns a lock would always fail until the connections changed event got triggered somehow - `GetLock` didn't account for not having user information, so if the user was logged out, it would default to not allowing any edits. Since the info wasn't getting seeded, it would always report files as being locked by someone else. It now defaults to allowing edits if the user isn't signed in. Now, this isn't a bug, but if the user is not signed in, all bets are kinda off on validating locks. We should probably warn the user that we can't check whether they own the lock, but we should probably not lock down Unity entirely because of it.
1 parent 5ae520f commit 6248aff

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/LfsLocksModificationProcessor.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public static void Initialize(IEnvironment env, IPlatform plat)
2121
environment = env;
2222
platform = plat;
2323
platform.Keychain.ConnectionsChanged += UserMayHaveChanged;
24+
// we need to do this to get the initial user information up front
25+
UserMayHaveChanged();
2426

2527
repository = environment.Repository;
2628
if (repository != null)
@@ -37,19 +39,21 @@ public static string[] OnWillSaveAssets(string[] paths)
3739

3840
public static AssetMoveResult OnWillMoveAsset(string oldPath, string newPath)
3941
{
40-
return IsLocked(oldPath) || IsLocked(newPath) ? AssetMoveResult.FailedMove : AssetMoveResult.DidNotMove;
42+
return IsLockedBySomeoneElse(oldPath) || IsLockedBySomeoneElse(newPath) ? AssetMoveResult.FailedMove : AssetMoveResult.DidNotMove;
4143
}
4244

4345
public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
4446
{
45-
return IsLocked(assetPath) ? AssetDeleteResult.FailedDelete : AssetDeleteResult.DidNotDelete;
47+
return IsLockedBySomeoneElse(assetPath) ? AssetDeleteResult.FailedDelete : AssetDeleteResult.DidNotDelete;
4648
}
4749

50+
// Returns true if this file can be edited by this user
4851
public static bool IsOpenForEdit(string assetPath, out string message)
4952
{
5053
var lck = GetLock(assetPath);
51-
message = lck.HasValue ? "File is locked for editing by " + lck.Value.Owner : null;
52-
return !lck.HasValue;
54+
var canEdit = !IsLockedBySomeoneElse(lck);
55+
message = !canEdit ? "File is locked for editing by " + lck.Value.Owner : null;
56+
return canEdit;
5357
}
5458

5559
private static void RepositoryOnLocksChanged(CacheUpdateEvent cacheUpdateEvent)
@@ -66,9 +70,14 @@ private static void UserMayHaveChanged()
6670
loggedInUser = platform.Keychain.Connections.Select(x => x.Username).FirstOrDefault();
6771
}
6872

69-
private static bool IsLocked(string assetPath)
73+
private static bool IsLockedBySomeoneElse(GitLock? lck)
7074
{
71-
return GetLock(assetPath).HasValue;
75+
return lck.HasValue && !lck.Value.Owner.Name.Equals(loggedInUser);
76+
}
77+
78+
private static bool IsLockedBySomeoneElse(string assetPath)
79+
{
80+
return IsLockedBySomeoneElse(GetLock(assetPath));
7281
}
7382

7483
private static GitLock? GetLock(string assetPath)
@@ -78,9 +87,9 @@ private static bool IsLocked(string assetPath)
7887

7988
GitLock lck;
8089
var repositoryPath = environment.GetRepositoryPath(assetPath.ToNPath());
81-
if (!locks.TryGetValue(repositoryPath, out lck) || lck.Owner.Name.Equals(loggedInUser))
82-
return null;
83-
return lck;
90+
if (locks.TryGetValue(repositoryPath, out lck))
91+
return lck;
92+
return null;
8493
}
8594
}
8695
}

0 commit comments

Comments
 (0)