3
3
#if UNITY_EDITOR
4
4
using System ;
5
5
using System . IO ;
6
+ using System . Linq ;
6
7
using UnityEditor ;
7
8
8
9
namespace CandyCoded . GitStatus
@@ -13,10 +14,17 @@ public static class GitMenuItems
13
14
14
15
private const int PRIORITY = 5000 ;
15
16
16
- private static string GetSelectedPath ( )
17
+ private static string GetSelectedAbsolutePath ( )
17
18
{
18
19
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 ) ;
20
28
21
29
}
22
30
@@ -28,7 +36,7 @@ private static async void DiscardChanges()
28
36
try
29
37
{
30
38
31
- await Git . DiscardChanges ( GetSelectedPath ( ) ) ;
39
+ await Git . DiscardChanges ( GetSelectedAbsolutePath ( ) ) ;
32
40
33
41
}
34
42
catch ( Exception error )
@@ -45,7 +53,8 @@ private static async void DiscardChanges()
45
53
private static bool ValidateDiscardChanges ( )
46
54
{
47
55
48
- return Selection . activeObject && File . Exists ( GetSelectedPath ( ) ) ;
56
+ return Selection . activeObject && File . Exists ( GetSelectedAbsolutePath ( ) ) &&
57
+ GitStatus . changedFiles . Contains ( GetSelectedRelativePath ( ) ) ;
49
58
50
59
}
51
60
@@ -63,7 +72,7 @@ private static async void DiscardAllChanges()
63
72
try
64
73
{
65
74
66
- await Git . DiscardChanges ( GetSelectedPath ( ) ) ;
75
+ await Git . DiscardChanges ( GetSelectedAbsolutePath ( ) ) ;
67
76
68
77
}
69
78
catch ( Exception error )
@@ -82,7 +91,84 @@ private static async void DiscardAllChanges()
82
91
private static bool ValidateDiscardAllChanges ( )
83
92
{
84
93
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
+ }
86
172
87
173
}
88
174
0 commit comments