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

Commit

Permalink
Using the cache objects to keep file Status
Browse files Browse the repository at this point in the history
  • Loading branch information
StanleyGoldman committed Feb 12, 2019
1 parent 31127b0 commit b2bb6ce
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,53 +39,27 @@ private static bool GitFileHistoryValidation()
[SerializeField] private FileHistoryView fileHistoryView = new FileHistoryView();
[SerializeField] private UnityEngine.Object selectedObject;
[SerializeField] private NPath selectedObjectAssetPath;
[SerializeField] private string selectedObjectAssetPathStr;

public void SetSelectedPath(NPath assetPath)
{
var fullPath = Application.dataPath.ToNPath().Parent.Combine(assetPath);
this.fileHistoryView.SetFullPath(fullPath);
NPath repositoryPath = NPath.Default;

selectedObjectAssetPathStr = assetPath;
selectedObjectAssetPath = assetPath;
selectedObject = null;

if (selectedObjectAssetPath != NPath.Default)
{
selectedObject = AssetDatabase.LoadMainAssetAtPath(selectedObjectAssetPath.ToString());

repositoryPath = Environment.GetRepositoryPath(assetPath);
}

InitializeAssetIcon();
LoadSelectedIcon();

// If we use selectedObjectAssetPath then this will break if the Unity project isn't located at the root
// of the git repository.
Repository.UpdateFileLog(fullPath)
Repository.UpdateFileLog(repositoryPath)
.Start();
}

private void InitializeAssetIcon()
{
Texture nodeIcon = null;

if (selectedObjectAssetPath != NPath.Default)
{
selectedObject = AssetDatabase.LoadMainAssetAtPath(selectedObjectAssetPath.ToString());

if (selectedObjectAssetPath.DirectoryExists())
{
nodeIcon = Styles.FolderIcon;
}
else
{
nodeIcon = UnityEditorInternal.InternalEditorUtility.GetIconForFile(selectedObjectAssetPath.ToString());
}

nodeIcon.hideFlags = HideFlags.HideAndDontSave;
}

selectedIcon = nodeIcon;
}

public override void Initialize(IApplicationManager applicationManager)
{
base.Initialize(applicationManager);
Expand All @@ -102,6 +76,8 @@ public override void OnEnable()
{
base.OnEnable();

LoadSelectedIcon();

if (fileHistoryView != null)
fileHistoryView.OnEnable();
}
Expand Down Expand Up @@ -153,9 +129,9 @@ public override void OnSelectionChange()
{
selectedObjectAssetPath = AssetDatabase.GetAssetPath(selectedObject)
.ToNPath();
}

SetSelectedPath(selectedObjectAssetPath);
SetSelectedPath(selectedObjectAssetPath);
}
}
}

Expand All @@ -168,21 +144,10 @@ public override void Refresh()
Redraw();
}

// Ideally we'd just call this in 'Initialize()' but that is too early in the domain reload and causes exceptions
private void RestoreFromDomainReload()
{
if (selectedObjectAssetPathStr != selectedObjectAssetPath && !string.IsNullOrEmpty(selectedObjectAssetPathStr))
{
this.SetSelectedPath(selectedObjectAssetPathStr.ToNPath());
}
}

public override void OnUI()
{
base.OnUI();

RestoreFromDomainReload();

if (selectedObject != null)
{
GUILayout.BeginVertical(Styles.HeaderStyle);
Expand Down Expand Up @@ -216,6 +181,27 @@ private void DetachHandlers(IRepository repository)
return;
}

private void LoadSelectedIcon()
{
Texture nodeIcon = null;

if (selectedObjectAssetPath != NPath.Default)
{
if (selectedObjectAssetPath.DirectoryExists())
{
nodeIcon = Styles.FolderIcon;
}
else
{
nodeIcon = UnityEditorInternal.InternalEditorUtility.GetIconForFile(selectedObjectAssetPath.ToString());
}

nodeIcon.hideFlags = HideFlags.HideAndDontSave;
}

selectedIcon = nodeIcon;
}

private void ShowButton(Rect rect)
{
EditorGUI.BeginChangeCheck();
Expand Down
68 changes: 32 additions & 36 deletions src/UnityExtension/Assets/Editor/GitHub.Unity/UI/HistoryView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,23 +651,19 @@ private GenericMenu CreateChangesTreeContextMenu(ChangesTreeNode node)
class FileHistoryView : HistoryBase
{
[SerializeField] private bool currentFileLogHasUpdate;
[SerializeField] private bool currentStatusEntriesHasUpdate;

[SerializeField] private GitFileLog gitFileLog;

[SerializeField] private HistoryControl historyControl;
[SerializeField] private GitLogEntry selectedEntry = GitLogEntry.Default;
[SerializeField] private ChangesTree treeChanges = new ChangesTree { DisplayRootNode = false };
[SerializeField] private Vector2 detailsScroll;
[SerializeField] private NPath fullPath;
[SerializeField] private List<GitStatusEntry> gitStatusEntries = new List<GitStatusEntry>();

[SerializeField] private CacheUpdateEvent lastStatusEntriesChangedEvent;
[SerializeField] private CacheUpdateEvent lastFileLogChangedEvent;

public void SetFullPath(NPath inFullPath)
{
this.fullPath = inFullPath;

}

public override void Refresh()
{
base.Refresh();
Expand All @@ -686,6 +682,17 @@ private void RepositoryOnFileLogChanged(CacheUpdateEvent cacheUpdateEvent)
}
}

private void RepositoryOnStatusEntriesChanged(CacheUpdateEvent cacheUpdateEvent)
{
if (!lastStatusEntriesChangedEvent.Equals(cacheUpdateEvent))
{
ReceivedEvent(cacheUpdateEvent.cacheType);
lastStatusEntriesChangedEvent = cacheUpdateEvent;
currentStatusEntriesHasUpdate = true;
Redraw();
}
}

protected override void AttachHandlers(IRepository repository)
{
if (repository == null)
Expand All @@ -694,6 +701,7 @@ protected override void AttachHandlers(IRepository repository)
}

repository.FileLogChanged += RepositoryOnFileLogChanged;
repository.StatusEntriesChanged += RepositoryOnStatusEntriesChanged;
}

protected override void DetachHandlers(IRepository repository)
Expand All @@ -704,6 +712,7 @@ protected override void DetachHandlers(IRepository repository)
}

repository.FileLogChanged -= RepositoryOnFileLogChanged;
repository.FileLogChanged -= RepositoryOnStatusEntriesChanged;
}

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

BuildHistoryControl(0, gitFileLog.LogEntries);
}

if (currentStatusEntriesHasUpdate)
{
currentStatusEntriesHasUpdate = false;

gitStatusEntries = Repository.CurrentChanges;
}
}

public override void OnGUI()
Expand All @@ -734,7 +750,7 @@ public override void OnGUI()
DoHistoryGui(lastRect, entry => {
GenericMenu menu = new GenericMenu();
string checkoutPrompt = string.Format("Checkout revision {0}", entry.ShortID);
menu.AddItem(new GUIContent(checkoutPrompt), false, () => { Checkout(entry); });
menu.AddItem(new GUIContent(checkoutPrompt), false, () => Checkout(entry.commitID));
menu.ShowAsContext();
}, node => {
});
Expand Down Expand Up @@ -769,36 +785,16 @@ protected override Vector2 DetailsScroll
private const string ConfirmCheckoutOK = "Overwrite";
private const string ConfirmCheckoutCancel = "Cancel";

protected void Checkout(GitLogEntry entry)
protected void Checkout(string commitId)
{
GitClient.Status().ThenInUI((success, status) =>
{
if (success)
{
bool promptUser = false;

foreach (var e in status.Entries)
{
if (e.FullPath == this.fullPath)
{
// locally modified file; prompt user
promptUser = true;
break;
}
}
var promptUser = gitStatusEntries.Count > 0 && gitStatusEntries.Any(statusEntry => gitFileLog.Path.Equals(statusEntry.Path.ToNPath()));

if (!promptUser || EditorUtility.DisplayDialog(ConfirmCheckoutTitle, string.Format(ConfirmCheckoutMessage, this.fullPath), ConfirmCheckoutOK, ConfirmCheckoutCancel))
{
Repository.CheckoutVersion(entry.commitID, new string[] { fullPath })
.ThenInUI(AssetDatabase.Refresh)
.Start();
}
}
else
{
EditorUtility.DisplayDialog("Oops", "There was an error checking out this version of the file. Try again!", "OK");
}
}).Start();
if (!promptUser || EditorUtility.DisplayDialog(ConfirmCheckoutTitle, string.Format(ConfirmCheckoutMessage, gitFileLog.Path), ConfirmCheckoutOK, ConfirmCheckoutCancel))
{
Repository.CheckoutVersion(commitId, new string[] { gitFileLog.Path })
.ThenInUI(AssetDatabase.Refresh)
.Start();
}
}
}
}

0 comments on commit b2bb6ce

Please sign in to comment.