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

Commit 314d61e

Browse files
Adding LfsLocksModificationProcessor
1 parent b69605d commit 314d61e

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/ApplicationManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ protected override void InitializeUI()
3131
Logger.Trace("Restarted {0}", Environment.Repository);
3232
EnvironmentCache.Instance.Flush();
3333

34+
LfsLocksModificationProcessor.Initialize(Environment.Repository);
3435
ProjectWindowInterface.Initialize(Environment.Repository);
3536
var window = Window.GetWindow();
3637
if (window != null)

src/UnityExtension/Assets/Editor/GitHub.Unity/GitHub.Unity.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
<Compile Include="Misc\Installer.cs" />
9595
<Compile Include="UI\BaseWindow.cs" />
9696
<Compile Include="UI\ChangesTreeControl.cs" />
97+
<Compile Include="UI\LfsLocksModificationProcessor.cs" />
9798
<Compile Include="UI\PopupWindow.cs" />
9899
<Compile Include="UI\ProjectWindowInterface.cs" />
99100
<Compile Include="Misc\Styles.cs" />
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using GitHub.Logging;
4+
using UnityEditor;
5+
6+
namespace GitHub.Unity
7+
{
8+
class LfsLocksModificationProcessor : UnityEditor.AssetModificationProcessor
9+
{
10+
private static ILogging logger;
11+
private static ILogging Logger { get { return logger = logger ?? LogHelper.GetLogger<LfsLocksModificationProcessor>(); } }
12+
13+
private static IRepository repository;
14+
15+
private static List<GitLock> locks = new List<GitLock>();
16+
17+
private static CacheUpdateEvent lastLocksChangedEvent;
18+
19+
public static void Initialize(IRepository repo)
20+
{
21+
Logger.Trace("Initialize HasRepository:{0}", repo != null);
22+
23+
repository = repo;
24+
25+
if (repository != null)
26+
{
27+
repository.LocksChanged += RepositoryOnLocksChanged;
28+
repository.CheckLocksChangedEvent(lastLocksChangedEvent);
29+
}
30+
}
31+
32+
private static void RepositoryOnLocksChanged(CacheUpdateEvent cacheUpdateEvent)
33+
{
34+
if (!lastLocksChangedEvent.Equals(cacheUpdateEvent))
35+
{
36+
lastLocksChangedEvent = cacheUpdateEvent;
37+
locks = repository.CurrentLocks;
38+
}
39+
}
40+
41+
public static string[] OnWillSaveAssets(string[] paths)
42+
{
43+
Logger.Trace("OnWillSaveAssets: [{0}]", string.Join(", ", paths));
44+
return paths;
45+
}
46+
47+
public static AssetMoveResult OnWillMoveAsset(string oldPath, string newPath)
48+
{
49+
Logger.Trace("OnWillMoveAsset:{0}->{1}", oldPath, newPath);
50+
51+
var result = AssetMoveResult.DidNotMove;
52+
if (IsLocked(oldPath))
53+
{
54+
result = AssetMoveResult.FailedMove;
55+
}
56+
else if (IsLocked(newPath))
57+
{
58+
result = AssetMoveResult.FailedMove;
59+
}
60+
return result;
61+
}
62+
63+
public static AssetDeleteResult OnWillDeleteAsset(string assetPath, RemoveAssetOptions option)
64+
{
65+
Logger.Trace("OnWillDeleteAsset:{0}", assetPath);
66+
67+
if (IsLocked(assetPath))
68+
{
69+
return AssetDeleteResult.FailedDelete;
70+
}
71+
return AssetDeleteResult.DidNotDelete;
72+
}
73+
74+
public static bool IsOpenForEdit(string assetPath, out string message)
75+
{
76+
Logger.Trace("IsOpenForEdit:{0}", assetPath);
77+
78+
if (IsLocked(assetPath))
79+
{
80+
message = "File is locked for editing!";
81+
return false;
82+
}
83+
else
84+
{
85+
message = null;
86+
return true;
87+
}
88+
}
89+
90+
private static bool IsLocked(string assetPath)
91+
{
92+
if(repository != null)
93+
{
94+
var repositoryPath = EntryPoint.Environment.GetRepositoryPath(assetPath.ToNPath());
95+
var gitLock = locks.FirstOrDefault(@lock => @lock.Path == repositoryPath);
96+
if (!gitLock.Equals(GitLock.Default))
97+
{
98+
Logger.Trace("Lock found on: {0}", assetPath);
99+
100+
//TODO: Check user and return true
101+
}
102+
}
103+
104+
return false;
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)