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

Commit b2bb6ce

Browse files
Using the cache objects to keep file Status
1 parent 31127b0 commit b2bb6ce

File tree

2 files changed

+62
-80
lines changed

2 files changed

+62
-80
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/FileHistoryWindow.cs

Lines changed: 30 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -39,53 +39,27 @@ private static bool GitFileHistoryValidation()
3939
[SerializeField] private FileHistoryView fileHistoryView = new FileHistoryView();
4040
[SerializeField] private UnityEngine.Object selectedObject;
4141
[SerializeField] private NPath selectedObjectAssetPath;
42-
[SerializeField] private string selectedObjectAssetPathStr;
4342

4443
public void SetSelectedPath(NPath assetPath)
4544
{
46-
var fullPath = Application.dataPath.ToNPath().Parent.Combine(assetPath);
47-
this.fileHistoryView.SetFullPath(fullPath);
45+
NPath repositoryPath = NPath.Default;
4846

49-
selectedObjectAssetPathStr = assetPath;
5047
selectedObjectAssetPath = assetPath;
5148
selectedObject = null;
5249

5350
if (selectedObjectAssetPath != NPath.Default)
5451
{
5552
selectedObject = AssetDatabase.LoadMainAssetAtPath(selectedObjectAssetPath.ToString());
53+
54+
repositoryPath = Environment.GetRepositoryPath(assetPath);
5655
}
5756

58-
InitializeAssetIcon();
57+
LoadSelectedIcon();
5958

60-
// If we use selectedObjectAssetPath then this will break if the Unity project isn't located at the root
61-
// of the git repository.
62-
Repository.UpdateFileLog(fullPath)
59+
Repository.UpdateFileLog(repositoryPath)
6360
.Start();
6461
}
6562

66-
private void InitializeAssetIcon()
67-
{
68-
Texture nodeIcon = null;
69-
70-
if (selectedObjectAssetPath != NPath.Default)
71-
{
72-
selectedObject = AssetDatabase.LoadMainAssetAtPath(selectedObjectAssetPath.ToString());
73-
74-
if (selectedObjectAssetPath.DirectoryExists())
75-
{
76-
nodeIcon = Styles.FolderIcon;
77-
}
78-
else
79-
{
80-
nodeIcon = UnityEditorInternal.InternalEditorUtility.GetIconForFile(selectedObjectAssetPath.ToString());
81-
}
82-
83-
nodeIcon.hideFlags = HideFlags.HideAndDontSave;
84-
}
85-
86-
selectedIcon = nodeIcon;
87-
}
88-
8963
public override void Initialize(IApplicationManager applicationManager)
9064
{
9165
base.Initialize(applicationManager);
@@ -102,6 +76,8 @@ public override void OnEnable()
10276
{
10377
base.OnEnable();
10478

79+
LoadSelectedIcon();
80+
10581
if (fileHistoryView != null)
10682
fileHistoryView.OnEnable();
10783
}
@@ -153,9 +129,9 @@ public override void OnSelectionChange()
153129
{
154130
selectedObjectAssetPath = AssetDatabase.GetAssetPath(selectedObject)
155131
.ToNPath();
156-
}
157132

158-
SetSelectedPath(selectedObjectAssetPath);
133+
SetSelectedPath(selectedObjectAssetPath);
134+
}
159135
}
160136
}
161137

@@ -168,21 +144,10 @@ public override void Refresh()
168144
Redraw();
169145
}
170146

171-
// Ideally we'd just call this in 'Initialize()' but that is too early in the domain reload and causes exceptions
172-
private void RestoreFromDomainReload()
173-
{
174-
if (selectedObjectAssetPathStr != selectedObjectAssetPath && !string.IsNullOrEmpty(selectedObjectAssetPathStr))
175-
{
176-
this.SetSelectedPath(selectedObjectAssetPathStr.ToNPath());
177-
}
178-
}
179-
180147
public override void OnUI()
181148
{
182149
base.OnUI();
183150

184-
RestoreFromDomainReload();
185-
186151
if (selectedObject != null)
187152
{
188153
GUILayout.BeginVertical(Styles.HeaderStyle);
@@ -216,6 +181,27 @@ private void DetachHandlers(IRepository repository)
216181
return;
217182
}
218183

184+
private void LoadSelectedIcon()
185+
{
186+
Texture nodeIcon = null;
187+
188+
if (selectedObjectAssetPath != NPath.Default)
189+
{
190+
if (selectedObjectAssetPath.DirectoryExists())
191+
{
192+
nodeIcon = Styles.FolderIcon;
193+
}
194+
else
195+
{
196+
nodeIcon = UnityEditorInternal.InternalEditorUtility.GetIconForFile(selectedObjectAssetPath.ToString());
197+
}
198+
199+
nodeIcon.hideFlags = HideFlags.HideAndDontSave;
200+
}
201+
202+
selectedIcon = nodeIcon;
203+
}
204+
219205
private void ShowButton(Rect rect)
220206
{
221207
EditorGUI.BeginChangeCheck();

src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs

Lines changed: 32 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -651,23 +651,19 @@ private GenericMenu CreateChangesTreeContextMenu(ChangesTreeNode node)
651651
class FileHistoryView : HistoryBase
652652
{
653653
[SerializeField] private bool currentFileLogHasUpdate;
654+
[SerializeField] private bool currentStatusEntriesHasUpdate;
654655

655656
[SerializeField] private GitFileLog gitFileLog;
656657

657658
[SerializeField] private HistoryControl historyControl;
658659
[SerializeField] private GitLogEntry selectedEntry = GitLogEntry.Default;
659660
[SerializeField] private ChangesTree treeChanges = new ChangesTree { DisplayRootNode = false };
660661
[SerializeField] private Vector2 detailsScroll;
661-
[SerializeField] private NPath fullPath;
662+
[SerializeField] private List<GitStatusEntry> gitStatusEntries = new List<GitStatusEntry>();
662663

664+
[SerializeField] private CacheUpdateEvent lastStatusEntriesChangedEvent;
663665
[SerializeField] private CacheUpdateEvent lastFileLogChangedEvent;
664666

665-
public void SetFullPath(NPath inFullPath)
666-
{
667-
this.fullPath = inFullPath;
668-
669-
}
670-
671667
public override void Refresh()
672668
{
673669
base.Refresh();
@@ -686,6 +682,17 @@ private void RepositoryOnFileLogChanged(CacheUpdateEvent cacheUpdateEvent)
686682
}
687683
}
688684

685+
private void RepositoryOnStatusEntriesChanged(CacheUpdateEvent cacheUpdateEvent)
686+
{
687+
if (!lastStatusEntriesChangedEvent.Equals(cacheUpdateEvent))
688+
{
689+
ReceivedEvent(cacheUpdateEvent.cacheType);
690+
lastStatusEntriesChangedEvent = cacheUpdateEvent;
691+
currentStatusEntriesHasUpdate = true;
692+
Redraw();
693+
}
694+
}
695+
689696
protected override void AttachHandlers(IRepository repository)
690697
{
691698
if (repository == null)
@@ -694,6 +701,7 @@ protected override void AttachHandlers(IRepository repository)
694701
}
695702

696703
repository.FileLogChanged += RepositoryOnFileLogChanged;
704+
repository.StatusEntriesChanged += RepositoryOnStatusEntriesChanged;
697705
}
698706

699707
protected override void DetachHandlers(IRepository repository)
@@ -704,6 +712,7 @@ protected override void DetachHandlers(IRepository repository)
704712
}
705713

706714
repository.FileLogChanged -= RepositoryOnFileLogChanged;
715+
repository.FileLogChanged -= RepositoryOnStatusEntriesChanged;
707716
}
708717

709718
protected override void ValidateCachedData(IRepository repository)
@@ -726,6 +735,13 @@ protected override void MaybeUpdateData()
726735

727736
BuildHistoryControl(0, gitFileLog.LogEntries);
728737
}
738+
739+
if (currentStatusEntriesHasUpdate)
740+
{
741+
currentStatusEntriesHasUpdate = false;
742+
743+
gitStatusEntries = Repository.CurrentChanges;
744+
}
729745
}
730746

731747
public override void OnGUI()
@@ -734,7 +750,7 @@ public override void OnGUI()
734750
DoHistoryGui(lastRect, entry => {
735751
GenericMenu menu = new GenericMenu();
736752
string checkoutPrompt = string.Format("Checkout revision {0}", entry.ShortID);
737-
menu.AddItem(new GUIContent(checkoutPrompt), false, () => { Checkout(entry); });
753+
menu.AddItem(new GUIContent(checkoutPrompt), false, () => Checkout(entry.commitID));
738754
menu.ShowAsContext();
739755
}, node => {
740756
});
@@ -769,36 +785,16 @@ protected override Vector2 DetailsScroll
769785
private const string ConfirmCheckoutOK = "Overwrite";
770786
private const string ConfirmCheckoutCancel = "Cancel";
771787

772-
protected void Checkout(GitLogEntry entry)
788+
protected void Checkout(string commitId)
773789
{
774-
GitClient.Status().ThenInUI((success, status) =>
775-
{
776-
if (success)
777-
{
778-
bool promptUser = false;
779-
780-
foreach (var e in status.Entries)
781-
{
782-
if (e.FullPath == this.fullPath)
783-
{
784-
// locally modified file; prompt user
785-
promptUser = true;
786-
break;
787-
}
788-
}
790+
var promptUser = gitStatusEntries.Count > 0 && gitStatusEntries.Any(statusEntry => gitFileLog.Path.Equals(statusEntry.Path.ToNPath()));
789791

790-
if (!promptUser || EditorUtility.DisplayDialog(ConfirmCheckoutTitle, string.Format(ConfirmCheckoutMessage, this.fullPath), ConfirmCheckoutOK, ConfirmCheckoutCancel))
791-
{
792-
Repository.CheckoutVersion(entry.commitID, new string[] { fullPath })
793-
.ThenInUI(AssetDatabase.Refresh)
794-
.Start();
795-
}
796-
}
797-
else
798-
{
799-
EditorUtility.DisplayDialog("Oops", "There was an error checking out this version of the file. Try again!", "OK");
800-
}
801-
}).Start();
792+
if (!promptUser || EditorUtility.DisplayDialog(ConfirmCheckoutTitle, string.Format(ConfirmCheckoutMessage, gitFileLog.Path), ConfirmCheckoutOK, ConfirmCheckoutCancel))
793+
{
794+
Repository.CheckoutVersion(commitId, new string[] { gitFileLog.Path })
795+
.ThenInUI(AssetDatabase.Refresh)
796+
.Start();
797+
}
802798
}
803799
}
804800
}

0 commit comments

Comments
 (0)