Skip to content

Commit e595f41

Browse files
committed
Added lock, unlock and unlock force menu items.
1 parent 4a3675b commit e595f41

File tree

2 files changed

+134
-6
lines changed

2 files changed

+134
-6
lines changed

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/Git.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Diagnostics;
7+
using System.IO;
78
using System.Threading.Tasks;
89
using UnityEngine;
10+
using Debug = UnityEngine.Debug;
911

1012
namespace CandyCoded.GitStatus
1113
{
@@ -23,6 +25,8 @@ public static class Git
2325
public static string GitLFSPath => "/usr/local/bin/git-lfs";
2426
#endif
2527

28+
public static string RepoPath => $"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}";
29+
2630
private static Task<Process> GenerateProcess(string path, string arguments)
2731
{
2832

@@ -111,6 +115,17 @@ public static async Task DiscardChanges(string path)
111115

112116
}
113117

118+
public static async Task ForceUnlockFile(string path)
119+
{
120+
121+
var process =
122+
await GenerateProcess(GitLFSPath,
123+
$@"unlock ""{path.Replace(RepoPath, "")}"" --force");
124+
125+
process?.WaitForExit();
126+
127+
}
128+
114129
public static async Task Init()
115130
{
116131

@@ -145,6 +160,23 @@ public static async Task<string[]> LockedFiles()
145160

146161
}
147162

163+
public static async Task LockFile(string path)
164+
{
165+
166+
var process = await GenerateProcess(GitLFSPath,
167+
$@"lock ""{path.Replace(RepoPath, "")}""");
168+
169+
if (process?.StandardError.ReadLine() is string line && line.StartsWith("Lock failed: missing protocol"))
170+
{
171+
172+
throw new Exception("Locking requires git repo has been pushed to a remote.");
173+
174+
}
175+
176+
process?.WaitForExit();
177+
178+
}
179+
148180
public static async Task<string> Status()
149181
{
150182

@@ -161,6 +193,16 @@ public static async Task<string> Status()
161193

162194
}
163195

196+
public static async Task UnlockFile(string path)
197+
{
198+
199+
var process = await GenerateProcess(GitLFSPath,
200+
$@"unlock ""{path.Replace(RepoPath, "")}""");
201+
202+
process?.WaitForExit();
203+
204+
}
205+
164206
public static async Task<string[]> UntrackedFiles()
165207
{
166208

Assets/Plugins/CandyCoded.GitStatus/Scripts/CustomEditor/GitMenuItems.cs

Lines changed: 92 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#if UNITY_EDITOR
44
using System;
55
using System.IO;
6+
using System.Linq;
67
using UnityEditor;
78

89
namespace CandyCoded.GitStatus
@@ -13,10 +14,17 @@ public static class GitMenuItems
1314

1415
private const int PRIORITY = 5000;
1516

16-
private static string GetSelectedPath()
17+
private static string GetSelectedAbsolutePath()
1718
{
1819

19-
return Path.Combine(Environment.CurrentDirectory, AssetDatabase.GetAssetPath(Selection.activeObject));
20+
return Path.Combine(Environment.CurrentDirectory, GetSelectedRelativePath());
21+
22+
}
23+
24+
private static string GetSelectedRelativePath()
25+
{
26+
27+
return AssetDatabase.GetAssetPath(Selection.activeObject);
2028

2129
}
2230

@@ -28,7 +36,7 @@ private static async void DiscardChanges()
2836
try
2937
{
3038

31-
await Git.DiscardChanges(GetSelectedPath());
39+
await Git.DiscardChanges(GetSelectedAbsolutePath());
3240

3341
}
3442
catch (Exception error)
@@ -45,7 +53,8 @@ private static async void DiscardChanges()
4553
private static bool ValidateDiscardChanges()
4654
{
4755

48-
return Selection.activeObject && File.Exists(GetSelectedPath());
56+
return Selection.activeObject && File.Exists(GetSelectedAbsolutePath()) &&
57+
GitStatus.changedFiles.Contains(GetSelectedRelativePath());
4958

5059
}
5160

@@ -63,7 +72,7 @@ private static async void DiscardAllChanges()
6372
try
6473
{
6574

66-
await Git.DiscardChanges(GetSelectedPath());
75+
await Git.DiscardChanges(GetSelectedAbsolutePath());
6776

6877
}
6978
catch (Exception error)
@@ -82,7 +91,84 @@ private static async void DiscardAllChanges()
8291
private static bool ValidateDiscardAllChanges()
8392
{
8493

85-
return Selection.activeObject && Directory.Exists(GetSelectedPath());
94+
return Selection.activeObject && Directory.Exists(GetSelectedAbsolutePath());
95+
96+
}
97+
98+
[MenuItem("Git/Lock File", true, PRIORITY)]
99+
[MenuItem("Assets/Lock File", true, PRIORITY)]
100+
private static bool ValidateLockFile()
101+
{
102+
103+
return Selection.activeObject && File.Exists(GetSelectedAbsolutePath()) &&
104+
!GitStatus.lockedFiles.Contains(GetSelectedRelativePath());
105+
106+
}
107+
108+
[MenuItem("Git/Lock File", false, PRIORITY)]
109+
[MenuItem("Assets/Lock File", false, PRIORITY)]
110+
private static async void LockFile()
111+
{
112+
113+
try
114+
{
115+
116+
await Git.LockFile(GetSelectedAbsolutePath());
117+
118+
}
119+
catch (Exception error)
120+
{
121+
122+
EditorUtility.DisplayDialog("Error", error.Message, "Ok");
123+
124+
}
125+
126+
}
127+
128+
[MenuItem("Git/Unlock File", true, PRIORITY)]
129+
[MenuItem("Assets/Unlock File", true, PRIORITY)]
130+
private static bool ValidateUnlockFile()
131+
{
132+
133+
return Selection.activeObject && File.Exists(GetSelectedAbsolutePath()) &&
134+
GitStatus.lockedFiles.Contains(GetSelectedRelativePath());
135+
136+
}
137+
138+
[MenuItem("Git/Unlock File", false, PRIORITY)]
139+
[MenuItem("Assets/Unlock File", false, PRIORITY)]
140+
private static async void UnlockFile()
141+
{
142+
143+
await Git.UnlockFile(GetSelectedAbsolutePath());
144+
145+
}
146+
147+
[MenuItem("Git/Force Unlock File", true, PRIORITY)]
148+
[MenuItem("Assets/Force Unlock File", true, PRIORITY)]
149+
private static bool ValidateForceUnlockFile()
150+
{
151+
152+
return Selection.activeObject && File.Exists(GetSelectedAbsolutePath()) &&
153+
GitStatus.lockedFiles.Contains(GetSelectedRelativePath());
154+
155+
}
156+
157+
[MenuItem("Git/Force Unlock File", false, PRIORITY)]
158+
[MenuItem("Assets/Force Unlock File", false, PRIORITY)]
159+
private static async void ForceUnlockFile()
160+
{
161+
162+
if (EditorUtility.DisplayDialog(
163+
"Force unlock file",
164+
$"Are you sure you want to force unlock {Selection.activeObject.name}?",
165+
"Yes",
166+
"Cancel"))
167+
{
168+
169+
await Git.ForceUnlockFile(GetSelectedAbsolutePath());
170+
171+
}
86172

87173
}
88174

0 commit comments

Comments
 (0)