Skip to content

Commit

Permalink
Fixed Repository Initialization
Browse files Browse the repository at this point in the history
* all injections now happen regardless of repository's validity
* save file only saves if repository is valid
  • Loading branch information
simeonradivoev committed Jan 31, 2018
1 parent 124df94 commit 580b10c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 45 deletions.
34 changes: 32 additions & 2 deletions Assets/Plugins/UniGit/Editor/GitFileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,49 @@ public class GitFileWatcher : IDisposable
private readonly GitManager gitManager;
private readonly GitSettingsJson gitSettings;
private readonly GitCallbacks gitCallbacks;
private readonly string repoPath;
private Regex ignoreFoldersRegex;

[UniGitInject]
public GitFileWatcher(string repoPath,
GitManager gitManager,
GitCallbacks gitCallbacks,
GitSettingsJson gitSettings,
GitInitializer initializer,
[UniGitInjectOptional] bool trackAssetsPath)
{
this.gitManager = gitManager;
this.gitSettings = gitSettings;
this.gitCallbacks = gitCallbacks;
this.repoPath = repoPath;
fileWatchers = new List<FileSystemWatcher>();

string regexPattern = @".*.git$";
if (!trackAssetsPath) regexPattern += "|.*Assets$";
ignoreFoldersRegex = new Regex(regexPattern);

gitCallbacks.OnSettingsChange += OnSettingsChange;
gitCallbacks.RepositoryCreate += OnRepositoryCreate;

if (initializer.IsValidRepo)
{
CreateWatchers();
}
}

private void ClearWatchers()
{
foreach (var fileWatcher in fileWatchers)
{
Unsubscribe(fileWatcher);
fileWatcher.Dispose();
}

fileWatchers.Clear();
}

private void CreateWatchers()
{
var mainFileWatcher = new FileSystemWatcher(repoPath)
{
InternalBufferSize = 4,
Expand Down Expand Up @@ -57,8 +82,6 @@ public GitFileWatcher(string repoPath,
Subscribe(fileWatcher);
}
}

gitCallbacks.OnSettingsChange += OnSettingsChange;
}

private void OnSettingsChange()
Expand All @@ -69,6 +92,12 @@ private void OnSettingsChange()
}
}

private void OnRepositoryCreate()
{
ClearWatchers();
CreateWatchers();
}

private bool ShouldTrackDirectory(DirectoryInfo directory)
{
return !directory.Attributes.HasFlag(FileAttributes.Hidden) && !ignoreFoldersRegex.IsMatch(directory.FullName);
Expand Down Expand Up @@ -130,6 +159,7 @@ public bool WaitForChange(out WaitForChangedResult result,WatcherChangeTypes typ
public void Dispose()
{
gitCallbacks.OnSettingsChange -= OnSettingsChange;
gitCallbacks.RepositoryCreate -= OnRepositoryCreate;

foreach (var fileWatcher in fileWatchers)
{
Expand Down
4 changes: 3 additions & 1 deletion Assets/Plugins/UniGit/Editor/GitInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public class GitInitializer
private readonly GitCallbacks callbacks;

[UniGitInject]
public GitInitializer(string repoPath,ILogger logger,GitCallbacks callbacks)
public GitInitializer(string repoPath,
ILogger logger,
GitCallbacks callbacks)
{
this.repoPath = repoPath;
this.logger = logger;
Expand Down
22 changes: 13 additions & 9 deletions Assets/Plugins/UniGit/Editor/GitManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ public GitManager(string repoPath,

private void Initialize()
{
if (!initializer.IsValidRepo)
{
return;
}
callbacks.EditorUpdate += OnEditorUpdate;
callbacks.DelayCall += OnDelayedCall;
//asset postprocessing
Expand All @@ -76,8 +72,7 @@ private void Initialize()
callbacks.OnPostprocessDeletedAssets += OnPostprocessDeletedAssets;
callbacks.OnPostprocessMovedAssets += OnPostprocessMovedAssets;
callbacks.OnPlayModeStateChange += OnPlayModeStateChange;

CheckNullRepository();
callbacks.RepositoryCreate += OnRepositoryCreate;
}

private void OnPlayModeStateChange(PlayModeStateChange stateChange)
Expand All @@ -95,6 +90,11 @@ private void OnDelayedCall()
MarkDirty();
}

private void OnRepositoryCreate()
{
Update(true);
}

public void DeleteRepository()
{
if(string.IsNullOrEmpty(repoPath)) return;
Expand Down Expand Up @@ -125,7 +125,6 @@ internal void OnEditorUpdate()
var updateStatus = GetUpdateStatus();
if (updateStatus == UpdateStatusEnum.Ready)
{
CheckNullRepository();
if (CanUpdate())
{
if (repositoryDirty || !gitData.Initialized)
Expand Down Expand Up @@ -265,7 +264,7 @@ private void PostprocessUnstage(string[] paths)

private void CheckNullRepository()
{
if (initializer.IsValidRepo && repository == null)
if (repository == null && initializer.IsValidRepo)
{
repository = new Repository(RepoPath);
callbacks.IssueOnRepositoryLoad(repository);
Expand Down Expand Up @@ -497,6 +496,7 @@ public void Dispose()
callbacks.OnPostprocessDeletedAssets -= OnPostprocessDeletedAssets;
callbacks.OnPostprocessMovedAssets -= OnPostprocessMovedAssets;
callbacks.OnPlayModeStateChange -= OnPlayModeStateChange;
callbacks.RepositoryCreate -= OnRepositoryCreate;
}
}

Expand Down Expand Up @@ -866,7 +866,11 @@ public Signature Signature

public Repository Repository
{
get { return repository; }
get
{
CheckNullRepository();
return repository;
}
}

public GitSettingsJson.ThreadingType Threading
Expand Down
6 changes: 5 additions & 1 deletion Assets/Plugins/UniGit/Editor/GitSettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ public class GitSettingsManager : IDisposable
private readonly GitCallbacks gitCallbacks;
private readonly string settingsPath;
private readonly ILogger logger;
private readonly GitInitializer initializer;

[UniGitInject]
public GitSettingsManager(GitSettingsJson settings,string settingsPath,GitCallbacks gitCallbacks,ILogger logger)
public GitSettingsManager(GitSettingsJson settings,string settingsPath,GitCallbacks gitCallbacks,ILogger logger,GitInitializer initializer)
{
this.settings = settings;
this.settingsPath = settingsPath;
this.gitCallbacks = gitCallbacks;
this.logger = logger;
this.initializer = initializer;

gitCallbacks.EditorUpdate += OnEditorUpdate;
}
Expand Down Expand Up @@ -77,6 +79,8 @@ public void LoadOldSettingsFile()

public void SaveSettingsToFile()
{
if(!initializer.IsValidRepo) return;

ValidateSettingsPath();
string settingsFilePath = settingsPath;

Expand Down
49 changes: 18 additions & 31 deletions Assets/Plugins/UniGit/Editor/UniGitLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,29 @@ static UniGitLoader()

private static void Rebuild(InjectionHelper injectionHelper)
{
injectionHelper.InjectStatic(typeof(GitUnityMenu));

if (Repository.IsValid(injectionHelper.GetInstance<string>("repoPath")))
{
var settingsManager = injectionHelper.GetInstance<GitSettingsManager>();
settingsManager.LoadGitSettings();
GitCallbacks = injectionHelper.GetInstance<GitCallbacks>();

//delayed called must be used for serialized properties to be loaded
EditorApplication.delayCall += () =>
{
settingsManager.LoadOldSettingsFile();
};
var settingsManager = injectionHelper.GetInstance<GitSettingsManager>();
settingsManager.LoadGitSettings();

GitManager = injectionHelper.GetInstance<GitManager>();
GitCallbacks = injectionHelper.GetInstance<GitCallbacks>();
ReflectionHelper = injectionHelper.GetInstance<GitReflectionHelper>();
GitSettings = injectionHelper.GetInstance<GitSettingsJson>();
//delayed called must be used for serialized properties to be loaded
EditorApplication.delayCall += () =>
{
settingsManager.LoadOldSettingsFile();
};

GitCallbacks.RepositoryCreate += OnRepositoryCreate;
GitCallbacks.OnLogEntry += OnLogEntry;
GitCallbacks.OnBeforeAssemblyReload += OnBeforeAssemblyReload;
GitManager = injectionHelper.GetInstance<GitManager>();

ReflectionHelper = injectionHelper.GetInstance<GitReflectionHelper>();
GitSettings = injectionHelper.GetInstance<GitSettingsJson>();

GitCallbacks.OnLogEntry += OnLogEntry;
GitCallbacks.OnBeforeAssemblyReload += OnBeforeAssemblyReload;

injectionHelper.CreateNonLazy();
injectionHelper.CreateNonLazy();

injectionHelper.InjectStatic(typeof(GitProjectContextMenus));
}
injectionHelper.InjectStatic(typeof(GitProjectContextMenus));
injectionHelper.InjectStatic(typeof(GitUnityMenu));
}

private static void OnWindowAdded(EditorWindow editorWindow)
Expand All @@ -118,16 +115,6 @@ private static void OnEditorUpdate()
if(GitCallbacks != null) GitCallbacks.IssueDelayCall(true);
}

private static void OnRepositoryCreate()
{
Rebuild(injectionHelper);
foreach (var window in GitWindows.Windows)
{
injectionHelper.Inject(window);
window.Repaint();
}
}

private static void OnBeforeAssemblyReload()
{
if(!(bool)ReflectionHelper.TestRunningField.GetValue(null))
Expand Down
1 change: 0 additions & 1 deletion Assets/Plugins/UniGit/Editor/Utils/GitAboutWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ private void OnGUI()
GUILayout.Label(GitGUI.GetTempContent("Created by: Simeon Radivoev"));
GUILayout.Label(GitGUI.GetTempContent("UniGit Version: " + GitManager.Version));
GUILayout.Label(GitGUI.GetTempContent("Git Version: " + gitVersion));
GUILayout.Label(GitGUI.GetTempContent("LibGit2Sharp Version: " + version.InformationalVersion));
GUILayout.Label(GitGUI.GetTempContent("LibGit2Sharp Features: " + version.Features));
GUILayout.Label(GitGUI.GetTempContent("License: GNU GENERAL PUBLIC LICENSE, Version 3, 29 June 2007"),EditorStyles.wordWrappedLabel);
GUILayout.EndVertical();
Expand Down

0 comments on commit 580b10c

Please sign in to comment.