33#if UNITY_EDITOR
44using System ;
55using System . IO ;
6+ using System . Linq ;
67using UnityEditor ;
78
89namespace 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,73 @@ 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+ await Git . LockFile ( GetSelectedAbsolutePath ( ) ) ;
114+
115+ }
116+
117+ [ MenuItem ( "Git/Unlock File" , true , PRIORITY ) ]
118+ [ MenuItem ( "Assets/Unlock File" , true , PRIORITY ) ]
119+ private static bool ValidateUnlockFile ( )
120+ {
121+
122+ return Selection . activeObject && File . Exists ( GetSelectedAbsolutePath ( ) ) &&
123+ GitStatus . lockedFiles . Contains ( GetSelectedRelativePath ( ) ) ;
124+
125+ }
126+
127+ [ MenuItem ( "Git/Unlock File" , false , PRIORITY ) ]
128+ [ MenuItem ( "Assets/Unlock File" , false , PRIORITY ) ]
129+ private static async void UnlockFile ( )
130+ {
131+
132+ await Git . UnlockFile ( GetSelectedAbsolutePath ( ) ) ;
133+
134+ }
135+
136+ [ MenuItem ( "Git/Force Unlock File" , true , PRIORITY ) ]
137+ [ MenuItem ( "Assets/Force Unlock File" , true , PRIORITY ) ]
138+ private static bool ValidateForceUnlockFile ( )
139+ {
140+
141+ return Selection . activeObject && File . Exists ( GetSelectedAbsolutePath ( ) ) &&
142+ GitStatus . lockedFiles . Contains ( GetSelectedRelativePath ( ) ) ;
143+
144+ }
145+
146+ [ MenuItem ( "Git/Force Unlock File" , false , PRIORITY ) ]
147+ [ MenuItem ( "Assets/Force Unlock File" , false , PRIORITY ) ]
148+ private static async void ForceUnlockFile ( )
149+ {
150+
151+ if ( EditorUtility . DisplayDialog (
152+ "Force unlock file" ,
153+ $ "Are you sure you want to force unlock { Selection . activeObject . name } ?",
154+ "Yes" ,
155+ "Cancel" ) )
156+ {
157+
158+ await Git . ForceUnlockFile ( GetSelectedAbsolutePath ( ) ) ;
159+
160+ }
86161
87162 }
88163
0 commit comments