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

Commit 1b12912

Browse files
committed
added individual file revert; fixed domain reload bugs
1 parent 9a5d027 commit 1b12912

File tree

2 files changed

+94
-15
lines changed

2 files changed

+94
-15
lines changed

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

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,35 +38,52 @@ private static bool GitFileHistoryValidation()
3838
[SerializeField] private bool locked;
3939
[SerializeField] private FileHistoryView fileHistoryView = new FileHistoryView();
4040
[SerializeField] private UnityEngine.Object selectedObject;
41-
[SerializeField] private NPath selectedObjectPath;
41+
[SerializeField] private NPath selectedObjectAssetPath;
42+
[SerializeField] private string selectedObjectAssetPathStr;
4243

43-
public void SetSelectedPath(NPath path)
44+
public void SetSelectedPath(NPath assetPath)
4445
{
45-
selectedObjectPath = path;
46+
var fullPath = Application.dataPath.ToNPath().Parent.Combine(assetPath);
47+
this.fileHistoryView.SetFullPath(fullPath);
48+
49+
selectedObjectAssetPathStr = assetPath;
50+
selectedObjectAssetPath = assetPath;
4651
selectedObject = null;
4752

53+
if (selectedObjectAssetPath != NPath.Default)
54+
{
55+
selectedObject = AssetDatabase.LoadMainAssetAtPath(selectedObjectAssetPath.ToString());
56+
}
57+
58+
InitializeAssetIcon();
59+
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)
63+
.Start();
64+
}
65+
66+
private void InitializeAssetIcon()
67+
{
4868
Texture nodeIcon = null;
4969

50-
if (selectedObjectPath != NPath.Default)
70+
if (selectedObjectAssetPath != NPath.Default)
5171
{
52-
selectedObject = AssetDatabase.LoadMainAssetAtPath(path.ToString());
72+
selectedObject = AssetDatabase.LoadMainAssetAtPath(selectedObjectAssetPath.ToString());
5373

54-
if (selectedObjectPath.DirectoryExists())
74+
if (selectedObjectAssetPath.DirectoryExists())
5575
{
5676
nodeIcon = Styles.FolderIcon;
5777
}
5878
else
5979
{
60-
nodeIcon = UnityEditorInternal.InternalEditorUtility.GetIconForFile(selectedObjectPath.ToString());
80+
nodeIcon = UnityEditorInternal.InternalEditorUtility.GetIconForFile(selectedObjectAssetPath.ToString());
6181
}
6282

6383
nodeIcon.hideFlags = HideFlags.HideAndDontSave;
6484
}
6585

6686
selectedIcon = nodeIcon;
67-
68-
Repository.UpdateFileLog(selectedObjectPath)
69-
.Start();
7087
}
7188

7289
public override void Initialize(IApplicationManager applicationManager)
@@ -131,14 +148,14 @@ public override void OnSelectionChange()
131148
if (!locked)
132149
{
133150
selectedObject = Selection.activeObject;
134-
selectedObjectPath = NPath.Default;
151+
selectedObjectAssetPath = NPath.Default;
135152
if (selectedObject != null)
136153
{
137-
selectedObjectPath = AssetDatabase.GetAssetPath(selectedObject)
154+
selectedObjectAssetPath = AssetDatabase.GetAssetPath(selectedObject)
138155
.ToNPath();
139156
}
140157

141-
SetSelectedPath(selectedObjectPath);
158+
SetSelectedPath(selectedObjectAssetPath);
142159
}
143160
}
144161

@@ -151,10 +168,21 @@ public override void Refresh()
151168
Redraw();
152169
}
153170

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+
154180
public override void OnUI()
155181
{
156182
base.OnUI();
157183

184+
RestoreFromDomainReload();
185+
158186
if (selectedObject != null)
159187
{
160188
GUILayout.BeginVertical(Styles.HeaderStyle);
@@ -209,7 +237,7 @@ private void DoHeaderGUI()
209237

210238
GUILayout.Label(selectedIcon, GUILayout.Height(iconWidth), GUILayout.Width(iconHeight));
211239

212-
GUILayout.Label(selectedObjectPath, Styles.FileHistoryLogTitleStyle);
240+
GUILayout.Label(selectedObjectAssetPath, Styles.FileHistoryLogTitleStyle);
213241

214242
GUILayout.FlexibleSpace();
215243

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

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,16 @@ class FileHistoryView : HistoryBase
658658
[SerializeField] private GitLogEntry selectedEntry = GitLogEntry.Default;
659659
[SerializeField] private ChangesTree treeChanges = new ChangesTree { DisplayRootNode = false };
660660
[SerializeField] private Vector2 detailsScroll;
661+
[SerializeField] private NPath fullPath;
661662

662663
[SerializeField] private CacheUpdateEvent lastFileLogChangedEvent;
663664

665+
public void SetFullPath(NPath inFullPath)
666+
{
667+
this.fullPath = inFullPath;
668+
669+
}
670+
664671
public override void Refresh()
665672
{
666673
base.Refresh();
@@ -723,7 +730,14 @@ protected override void MaybeUpdateData()
723730

724731
public override void OnGUI()
725732
{
726-
DoHistoryGui(Rect.zero);
733+
var lastRect = GUILayoutUtility.GetLastRect();
734+
DoHistoryGui(lastRect, entry => {
735+
GenericMenu menu = new GenericMenu();
736+
string checkoutPrompt = string.Format("Checkout revision {0}", entry.ShortID);
737+
menu.AddItem(new GUIContent(checkoutPrompt), false, () => { Checkout(entry); });
738+
menu.ShowAsContext();
739+
}, node => {
740+
});
727741
}
728742

729743
protected override HistoryControl HistoryControl
@@ -749,5 +763,42 @@ protected override Vector2 DetailsScroll
749763
get { return detailsScroll; }
750764
set { detailsScroll = value; }
751765
}
766+
767+
private const string ConfirmCheckoutTitle = "Discard Changes?";
768+
private const string ConfirmCheckoutMessage = "You've made changes to file '{0}'. Overwrite these changes with the historical version?";
769+
private const string ConfirmCheckoutOK = "Overwrite";
770+
private const string ConfirmCheckoutCancel = "Cancel";
771+
772+
protected void Checkout(GitLogEntry entry)
773+
{
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+
}
789+
790+
if (!promptUser || EditorUtility.DisplayDialog(ConfirmCheckoutTitle, string.Format(ConfirmCheckoutMessage, this.fullPath), ConfirmCheckoutOK, ConfirmCheckoutCancel))
791+
{
792+
GitClient.CheckoutVersion(entry.commitID, new string[] { this.fullPath }).ThenInUI((checkoutSuccess, result) => {
793+
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();
802+
}
752803
}
753804
}

0 commit comments

Comments
 (0)