-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitLFS.cs
116 lines (105 loc) · 5.16 KB
/
GitLFS.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System.Linq;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
namespace Abuksigun.UnityGitUI
{
public static class GitLFS
{
[MenuItem("Assets/Git/LFS", true)]
public static bool Check() => Utils.GetSelectedGitModules().Any();
[MenuItem("Assets/Git/LFS")]
public static void Invoke()
{
if (EditorWindow.GetWindow<GitLFSWindow>() is { } window && window)
{
window.titleContent = new GUIContent("Git LFS");
window.Show();
}
}
public class GitLFSWindow : DefaultWindow
{
const float TopPanelHeight = 60;
[SerializeField] string selectedModuleGuid;
LazyTreeView<string> patternsTreeView;
[SerializeField] TreeViewState patternsTreeViewState;
protected override void OnGUI()
{
var modules = Utils.GetSelectedGitModules().ToList();
bool lfsInstalled = modules.All(x => x.IsLfsAvailable.GetResultOrDefault());
if (!lfsInstalled)
{
GUILayout.Label("Git LFS is not available in the system");
return;
}
var unitializedModules = modules.Where(x => !x.IsLfsInstalled.GetResultOrDefault()).ToArray();
var module = GUIUtils.ModuleGuidToolbar(modules, selectedModuleGuid);
if (module == null)
return;
selectedModuleGuid = module.Guid;
using (new GUILayout.HorizontalScope())
{
if (!module.IsLfsInstalled.GetResultOrDefault())
{
if (GUILayout.Button($"Install LFS"))
foreach (var m in unitializedModules)
m.InstallLfs();
}
else
{
if ((module.LfsTrackedPaths.GetResultOrDefault()?.Any() ?? false) || (module.LfsFiles.GetResultOrDefault()?.Any() ?? false))
{
if (GUILayout.Button("Prune LFS"))
_ = GUIUtils.RunSafe(new[] { module }, (module) => module.PruneLfsObjects());
if (GUILayout.Button("Fetch LFS"))
_ = GUIUtils.RunSafe(new[] { module }, (module) => module.FetchLfsObjects());
}
if (GUILayout.Button("Add new pattern"))
_ = ShowAddTrackPatternWindow(module);
}
}
if (module.LfsTrackedPaths.GetResultOrDefault() is { } paths)
{
GUILayout.Label($"Tracked paths ({paths.Length}):");
patternsTreeView ??= new(paths => paths.Select(x => new TreeViewItem(x.GetHashCode(), 0, x)).ToList(), patternsTreeViewState ??= new(), true);
var selectedPaths = paths.Where(x => patternsTreeViewState.selectedIDs.Contains(x.GetHashCode())).ToArray();
patternsTreeView.Draw(
new Vector2(position.width, position.height - TopPanelHeight),
paths,
contextMenuCallback: id => {
var menu = new GenericMenu();
menu.AddItem(new GUIContent("Untrack"), false, () => _ = module.UntrackPathsWithLfs(selectedPaths));
menu.AddItem(new GUIContent("Migrate"), false, () => {
string msg = $"Rewrite every commit in history (with flag -- everything) (will require running git push --force)\n{selectedPaths.Join()}";
if (EditorUtility.DisplayDialog($"DANGER! REWRITE HISTORY IN ALL BRANCHES!", msg, "Rewrite histroy", "Cancel"))
_ = GUIUtils.RunSafe(new[] { module }, (module) => module.GitLfsMigrate(GitLfsMigrateMode.Import, selectedPaths, false));
});
menu.ShowAsContext();
},
doubleClickCallback: id => {
});
}
}
static async Task ShowAddTrackPatternWindow(Module module)
{
string newPattern = "";
await GUIUtils.ShowModalWindow("Set Value", new Vector2Int(300, 180), window => {
newPattern = GUILayout.TextField(newPattern);
using (new GUILayout.HorizontalScope())
{
if (GUILayout.Button("Close"))
{
window.Close();
}
if (GUILayout.Button("Apply"))
{
_ = _ = GUIUtils.RunSafe(new[] { module }, (module) => module.TrackPathsWithLfs(new[] { newPattern }));
window.Close();
}
}
});
}
}
}
}