Files
unity-git-auto-syncd/net.lenrui.git-auto-sync/Editor/GitAutoSyncSettings.cs
T
2026-11-06 11:42:58 -08:00

98 lines
4.2 KiB
C#

// GitAutoSyncSettings.cs - part of the Git Auto Sync package (net.lenrui.git-auto-sync).
//
// Project Settings > Git Auto Sync. Saved to ProjectSettings/GitAutoSyncSettings.json, so it gets committed
// along with the project and every computer that works on it uses the same SSH fallback.
using System;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Lenrui.GitSync
{
[Serializable]
internal class GitAutoSyncSettings
{
public bool sshFallback = true;
public string sshHost = "";
public string sshUser = "git";
public int sshPort = 22;
public string sshUrlOverride = "";
static string FilePath
{
get { return Path.GetFullPath(Path.Combine(Application.dataPath, "../ProjectSettings/GitAutoSyncSettings.json")); }
}
public static GitAutoSyncSettings Load()
{
var s = new GitAutoSyncSettings();
try { if (File.Exists(FilePath)) JsonUtility.FromJsonOverwrite(File.ReadAllText(FilePath), s); }
catch (Exception e) { Debug.LogWarning("[GitAutoSync] Couldn't read " + FilePath + ": " + e.Message); }
return s;
}
public void Save()
{
try { File.WriteAllText(FilePath, JsonUtility.ToJson(this, true)); }
catch (Exception e) { Debug.LogWarning("[GitAutoSync] Couldn't save " + FilePath + ": " + e.Message); }
}
}
public static class GitAutoSyncSettingsProvider
{
public const string SettingsPath = "Project/Git Auto Sync";
static GitAutoSyncSettings settings;
static string remoteUrl;
static bool lookedUpRemote;
[SettingsProvider]
public static SettingsProvider Create()
{
return new SettingsProvider(SettingsPath, SettingsScope.Project)
{
label = "Git Auto Sync",
keywords = new[] { "git", "ssh", "push", "413", "cloudflare", "sync" },
guiHandler = search => Draw(),
deactivateHandler = () => { settings = null; lookedUpRemote = false; },
};
}
static void Draw()
{
if (settings == null) settings = GitAutoSyncSettings.Load();
if (!lookedUpRemote) { remoteUrl = GitAutoSync.RemoteUrl(GitAutoSync.PushRemote()); lookedUpRemote = true; }
var s = settings;
EditorGUIUtility.labelWidth = 200;
EditorGUILayout.LabelField("SSH fallback for large pushes", EditorStyles.boldLabel);
EditorGUILayout.HelpBox("If the server rejects an HTTPS push as too large (HTTP 413, such as Cloudflare's 100 MB upload limit), " +
"the same push is retried over SSH. The SSH host can be different from the HTTPS one.", MessageType.Info);
EditorGUI.BeginChangeCheck();
s.sshFallback = EditorGUILayout.Toggle("Retry over SSH on HTTP 413", s.sshFallback);
using (new EditorGUI.DisabledScope(!s.sshFallback))
{
s.sshHost = EditorGUILayout.TextField(new GUIContent("SSH Host", "For example ssh.example.com. Leave blank to use the HTTPS remote's host."), s.sshHost);
s.sshUser = EditorGUILayout.TextField(new GUIContent("SSH User", "Usually git."), s.sshUser);
s.sshPort = Mathf.Clamp(EditorGUILayout.IntField("SSH Port", s.sshPort), 1, 65535);
s.sshUrlOverride = EditorGUILayout.TextField(new GUIContent("Full SSH URL (optional)",
"Use this exact address instead of working one out, for example ssh://[email protected]/owner/repo.git"), s.sshUrlOverride);
EditorGUILayout.Space();
string why;
string url = GitAutoSync.ResolveSshUrl(s, remoteUrl, out why);
EditorGUILayout.LabelField("Large pushes go to", url ?? "Nowhere: " + why, EditorStyles.wordWrappedLabel);
if (GUILayout.Button("Test SSH Connection", GUILayout.Width(180)))
{
var snapshot = s;
EditorApplication.delayCall += () => GitAutoSync.TestSshConnection(snapshot);
}
}
if (EditorGUI.EndChangeCheck()) s.Save();
}
}
}