add git console

This commit is contained in:
2026-04-07 13:05:14 +02:00
parent 48134f2a03
commit 7bc0eee024

View File

@@ -23,6 +23,10 @@ public class GitPanel : EditorWindow
private string[] changedFiles = new string[0]; private string[] changedFiles = new string[0];
private Vector2 scrollPositionChanges; private Vector2 scrollPositionChanges;
private Vector2 scrollPositionHistory; private Vector2 scrollPositionHistory;
// STATISCH: Damit RunGitCommand darauf zugreifen kann und das Log beim Neuladen erhalten bleibt!
private static string gitLogOutput = "";
private Vector2 scrollPositionLog;
private int selectedTab = 0; private int selectedTab = 0;
private string[] tabNames = { "Changes", "History" }; private string[] tabNames = { "Changes", "History" };
@@ -38,7 +42,7 @@ public class GitPanel : EditorWindow
public static void ShowWindow() public static void ShowWindow()
{ {
GitPanel window = GetWindow<GitPanel>("GIT Version Control System"); GitPanel window = GetWindow<GitPanel>("GIT Version Control System");
window.minSize = new Vector2(380, 600); // Etwas breiter gemacht für den neuen Button window.minSize = new Vector2(380, 650);
} }
private void OnEnable() private void OnEnable()
@@ -80,12 +84,13 @@ public class GitPanel : EditorWindow
} catch { isGitInstalled = false; } } catch { isGitInstalled = false; }
} }
// FIX: Nutzt jetzt die aktuelle Unity-API (VersionControlSettings.mode)
private void CheckUnitySettings() private void CheckUnitySettings()
{ {
settingsCorrect = true; settingsCorrect = true;
settingsWarning = ""; settingsWarning = "";
if (EditorSettings.externalVersionControl != "Visible Meta Files") if (VersionControlSettings.mode != "Visible Meta Files")
{ {
settingsCorrect = false; settingsCorrect = false;
settingsWarning += "• Version Control Mode must be 'Visible Meta Files'\n"; settingsWarning += "• Version Control Mode must be 'Visible Meta Files'\n";
@@ -100,7 +105,7 @@ public class GitPanel : EditorWindow
private void FixUnitySettings() private void FixUnitySettings()
{ {
EditorSettings.externalVersionControl = "Visible Meta Files"; VersionControlSettings.mode = "Visible Meta Files";
EditorSettings.serializationMode = SerializationMode.ForceText; EditorSettings.serializationMode = SerializationMode.ForceText;
UnityEngine.Debug.Log("Git-Tool: Unity Project Settings updated for Git compatibility."); UnityEngine.Debug.Log("Git-Tool: Unity Project Settings updated for Git compatibility.");
RefreshData(); RefreshData();
@@ -166,17 +171,17 @@ public class GitPanel : EditorWindow
remoteUrlInput = EditorGUILayout.TextField("Remote URL:", remoteUrlInput); remoteUrlInput = EditorGUILayout.TextField("Remote URL:", remoteUrlInput);
if (GUILayout.Button("Initialize Repository", GUILayout.Height(30))) if (GUILayout.Button("Initialize Repository", GUILayout.Height(30)))
{ {
RunGitCommand("init"); RunGitCommand("init", true);
RunGitCommand("branch -M main"); RunGitCommand("branch -M main", true);
if (!string.IsNullOrWhiteSpace(remoteUrlInput)) { if (!string.IsNullOrWhiteSpace(remoteUrlInput)) {
RunGitCommand($"remote add origin \"{remoteUrlInput.Trim()}\""); RunGitCommand($"remote add origin \"{remoteUrlInput.Trim()}\"", true);
RunGitCommand("pull origin main --allow-unrelated-histories --no-edit"); RunGitCommand("pull origin main --allow-unrelated-histories --no-edit", true);
} }
GenerateUnityGitIgnore(); GenerateUnityGitIgnore();
AssetDatabase.Refresh(); AssetDatabase.Refresh();
RunGitCommand("add .gitignore"); RunGitCommand("add .gitignore", true);
RunGitCommand("commit -m \"Initial commit (GitIgnore)\""); RunGitCommand("commit -m \"Initial commit (GitIgnore)\"", true);
if (!string.IsNullOrWhiteSpace(remoteUrlInput)) RunGitCommand("push -u origin main"); if (!string.IsNullOrWhiteSpace(remoteUrlInput)) RunGitCommand("push -u origin main", true);
RefreshData(); RefreshData();
} }
} }
@@ -192,8 +197,8 @@ public class GitPanel : EditorWindow
int newIndex = EditorGUILayout.Popup("Switch Branch:", selectedBranchIndex, availableBranches); int newIndex = EditorGUILayout.Popup("Switch Branch:", selectedBranchIndex, availableBranches);
if (EditorGUI.EndChangeCheck() && newIndex != selectedBranchIndex) if (EditorGUI.EndChangeCheck() && newIndex != selectedBranchIndex)
{ {
RunGitCommand($"checkout \"{availableBranches[newIndex]}\""); RunGitCommand($"checkout \"{availableBranches[newIndex]}\"", true);
AssetDatabase.Refresh(); // Unity zwingen, den neuen Code vom anderen Branch zu laden AssetDatabase.Refresh();
RefreshData(); RefreshData();
return; return;
} }
@@ -206,7 +211,7 @@ public class GitPanel : EditorWindow
{ {
if (!string.IsNullOrWhiteSpace(newBranchName)) if (!string.IsNullOrWhiteSpace(newBranchName))
{ {
RunGitCommand($"checkout -b \"{newBranchName.Trim()}\""); RunGitCommand($"checkout -b \"{newBranchName.Trim()}\"", true);
newBranchName = ""; newBranchName = "";
RefreshData(); RefreshData();
GUI.FocusControl(null); GUI.FocusControl(null);
@@ -220,18 +225,20 @@ public class GitPanel : EditorWindow
GUILayout.Space(10); GUILayout.Space(10);
commitMessage = EditorGUILayout.TextField(commitMessage, GUILayout.Height(25)); commitMessage = EditorGUILayout.TextField(commitMessage, GUILayout.Height(25));
// --- HAUPTAKTIEN BEREICH ---
EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginHorizontal();
// 1. Commit & Push
GUI.backgroundColor = new Color(0.2f, 0.4f, 0.8f); GUI.backgroundColor = new Color(0.2f, 0.4f, 0.8f);
if (GUILayout.Button("✓ Push", GUILayout.Height(30))) if (GUILayout.Button("✓ Push", GUILayout.Height(30)))
{ {
UnityEngine.Debug.Log("Git-Tool: Saving Scenes and Assets before push...");
UnityEditor.SceneManagement.EditorSceneManager.SaveOpenScenes();
AssetDatabase.SaveAssets();
if (string.IsNullOrWhiteSpace(commitMessage)) SetDefaultCommitMessage(); if (string.IsNullOrWhiteSpace(commitMessage)) SetDefaultCommitMessage();
RunGitCommand("add ."); RunGitCommand("add .", true);
RunGitCommand($"commit -m \"{commitMessage}\""); RunGitCommand($"commit -m \"{commitMessage}\"", true);
string pushResult = RunGitCommand("push -u origin HEAD"); string pushResult = RunGitCommand("push -u origin HEAD", true);
if (pushResult.Contains("rejected") || pushResult.Contains("fetch first")) if (pushResult.Contains("rejected") || pushResult.Contains("fetch first"))
{ {
UnityEngine.Debug.LogError("Git-Tool: PUSH REJECTED! Jemand anderes hat Änderungen hochgeladen. Bitte klicke zuerst auf 'Pull'."); UnityEngine.Debug.LogError("Git-Tool: PUSH REJECTED! Jemand anderes hat Änderungen hochgeladen. Bitte klicke zuerst auf 'Pull'.");
@@ -244,35 +251,30 @@ public class GitPanel : EditorWindow
RefreshData(); RefreshData();
} }
// 2. NEU: PULL (Sync) GUI.backgroundColor = new Color(0.8f, 0.6f, 0.2f);
GUI.backgroundColor = new Color(0.8f, 0.6f, 0.2f); // Orange/Gelb
if (GUILayout.Button("⬇️ Pull", GUILayout.Width(80), GUILayout.Height(30))) if (GUILayout.Button("⬇️ Pull", GUILayout.Width(80), GUILayout.Height(30)))
{ {
UnityEngine.Debug.Log("Git-Tool: Lade Änderungen vom Server herunter..."); UnityEditor.SceneManagement.EditorSceneManager.SaveOpenScenes();
string pullResult = RunGitCommand("pull"); AssetDatabase.SaveAssets();
// Ganz wichtig für Collaboration: Unity zwingen, die fremden Dateien einzulesen! string pullResult = RunGitCommand("pull", true);
AssetDatabase.Refresh(); AssetDatabase.Refresh();
if (pullResult.Contains("CONFLICT")) if (pullResult.Contains("CONFLICT"))
{ {
UnityEngine.Debug.LogError("Git-Tool: MERGE CONFLICT! Du und ein Freund haben dieselbe Datei bearbeitet. Bitte in VS Code auflösen!"); UnityEngine.Debug.LogError("Git-Tool: MERGE CONFLICT! Bitte in VS Code auflösen!");
EditorUtility.DisplayDialog("Merge Conflict", "Es gibt Konflikte mit den Server-Daten!\n\nGit konnte die Änderungen nicht automatisch zusammenführen. Bitte öffne die roten Dateien in deinem Code-Editor und löse den Konflikt manuell auf.", "OK"); EditorUtility.DisplayDialog("Merge Conflict", "Es gibt Konflikte mit den Server-Daten!\n\nGit konnte die Änderungen nicht automatisch zusammenführen. Bitte öffne die roten Dateien in deinem Code-Editor und löse den Konflikt manuell auf.", "OK");
} }
else
{
UnityEngine.Debug.Log("Git-Tool: Repository erfolgreich synchronisiert.");
}
RefreshData(); RefreshData();
} }
// 3. Revert
GUI.backgroundColor = new Color(0.8f, 0.3f, 0.3f); GUI.backgroundColor = new Color(0.8f, 0.3f, 0.3f);
if (GUILayout.Button("⎌ Revert", GUILayout.Width(80), GUILayout.Height(30))) if (GUILayout.Button("⎌ Revert", GUILayout.Width(80), GUILayout.Height(30)))
{ {
if (EditorUtility.DisplayDialog("Revert Changes?", "Discard ALL uncommitted changes?", "Yes", "Cancel")) { if (EditorUtility.DisplayDialog("Revert Changes?", "Discard ALL uncommitted changes?", "Yes", "Cancel")) {
RunGitCommand("reset --hard HEAD"); RunGitCommand("clean -fd"); RunGitCommand("reset --hard HEAD", true);
AssetDatabase.Refresh(); // Unity die alten Daten zeigen RunGitCommand("clean -fd", true);
AssetDatabase.Refresh();
RefreshData(); RefreshData();
} }
} }
@@ -289,6 +291,17 @@ public class GitPanel : EditorWindow
if (changedFiles.Length == 0) GUILayout.Label("No changes."); if (changedFiles.Length == 0) GUILayout.Label("No changes.");
else RenderFileList(changedFiles); else RenderFileList(changedFiles);
EditorGUILayout.EndScrollView(); EditorGUILayout.EndScrollView();
GUILayout.Space(5);
EditorGUILayout.BeginHorizontal();
GUILayout.Label("GIT CONSOLE", EditorStyles.boldLabel);
if (GUILayout.Button("Clear", GUILayout.Width(50))) gitLogOutput = "";
EditorGUILayout.EndHorizontal();
scrollPositionLog = EditorGUILayout.BeginScrollView(scrollPositionLog, "box", GUILayout.Height(120));
GUIStyle logStyle = new GUIStyle(EditorStyles.label) { wordWrap = true, fontSize = 10 };
GUILayout.Label(string.IsNullOrEmpty(gitLogOutput) ? "Ready." : gitLogOutput, logStyle);
EditorGUILayout.EndScrollView();
} }
private void RenderHistoryUI() private void RenderHistoryUI()
@@ -396,14 +409,33 @@ public class GitPanel : EditorWindow
if (!File.Exists(path)) File.WriteAllText(path, ".idea\n.vs\nbin\nobj\n/Library\n/Temp\n/UserSettings\n/Configs\n/*.csproj\n/*.sln\n/Logs\n/Packages/*\n!/Packages/manifest.json\n!/Packages/packages-lock.json\n~UnityDirMonSyncFile~*"); if (!File.Exists(path)) File.WriteAllText(path, ".idea\n.vs\nbin\nobj\n/Library\n/Temp\n/UserSettings\n/Configs\n/*.csproj\n/*.sln\n/Logs\n/Packages/*\n!/Packages/manifest.json\n!/Packages/packages-lock.json\n~UnityDirMonSyncFile~*");
} }
public static string RunGitCommand(string args) { // FIX: Methode ist wieder static!
public static string RunGitCommand(string args, bool logAction = false) {
try { try {
ProcessStartInfo si = new ProcessStartInfo("git", args) { WorkingDirectory = Path.GetFullPath(Path.Combine(Application.dataPath, "..")), UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true }; ProcessStartInfo si = new ProcessStartInfo("git", args) {
WorkingDirectory = Path.GetFullPath(Path.Combine(Application.dataPath, "..")),
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (Process p = Process.Start(si)) { using (Process p = Process.Start(si)) {
string o = p.StandardOutput.ReadToEnd(); string o = p.StandardOutput.ReadToEnd();
string e = p.StandardError.ReadToEnd(); string e = p.StandardError.ReadToEnd();
p.WaitForExit(); p.WaitForExit();
return o + " " + e; string result = o + (string.IsNullOrWhiteSpace(e) ? "" : "\n" + e);
if (logAction) {
string time = System.DateTime.Now.ToString("HH:mm:ss");
string entry = $"[{time}] > git {args}\n";
if (!string.IsNullOrWhiteSpace(result)) entry += result.Trim() + "\n\n";
gitLogOutput = entry + gitLogOutput;
if (gitLogOutput.Length > 10000) gitLogOutput = gitLogOutput.Substring(0, 10000);
}
return result;
} }
} catch { return ""; } } catch { return ""; }
} }