refactor: commandline parsing

* `--rebase-todo-editor` launches this app as a git `sequence.editor`
* `--rebase-message-editor` launches this app as a git `core.editor` which runs on background by reading rebasing jobs
* `--core-editor` launches this app as a git `core.editor`
* `--askpass` launches this app as a SSH askpass program
This commit is contained in:
leo 2024-07-09 10:16:15 +08:00
parent cbe4c36525
commit 6930b51c64
No known key found for this signature in database
14 changed files with 320 additions and 155 deletions

View file

@ -59,7 +59,7 @@ namespace SourceGit
typeof(GridLengthConverter), typeof(GridLengthConverter),
] ]
)] )]
[JsonSerializable(typeof(List<Models.InteractiveRebaseJob>))] [JsonSerializable(typeof(Models.InteractiveRebaseJobCollection))]
[JsonSerializable(typeof(Models.JetBrainsState))] [JsonSerializable(typeof(Models.JetBrainsState))]
[JsonSerializable(typeof(Models.ThemeOverrides))] [JsonSerializable(typeof(Models.ThemeOverrides))]
[JsonSerializable(typeof(Models.Version))] [JsonSerializable(typeof(Models.Version))]

View file

@ -47,8 +47,10 @@ namespace SourceGit
{ {
try try
{ {
if (args.Length > 1 && args[0].Equals("--rebase-editor", StringComparison.Ordinal)) if (TryLaunchedAsRebaseTodoEditor(args, out int exitTodo))
Environment.Exit(Models.InteractiveRebaseEditor.Process(args[1])); Environment.Exit(exitTodo);
else if (TryLaunchedAsRebaseMessageEditor(args, out int exitMessage))
Environment.Exit(exitMessage);
else else
BuildAvaloniaApp().StartWithClassicDesktopLifetime(args); BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
} }
@ -326,28 +328,14 @@ namespace SourceGit
{ {
BindingPlugins.DataValidators.RemoveAt(0); BindingPlugins.DataValidators.RemoveAt(0);
var commandlines = Environment.GetCommandLineArgs(); if (TryLaunchedAsCoreEditor(desktop))
if (TryParseAskpass(commandlines, out var keyname)) return;
{
desktop.MainWindow = new Views.Askpass(Path.GetFileName(keyname));
}
else
{
Native.OS.SetupEnternalTools();
_launcher = new ViewModels.Launcher(commandlines); if (TryLaunchedAsAskpass(desktop))
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher }; return;
var pref = ViewModels.Preference.Instance; TryLaunchedAsNormal(desktop);
if (pref.ShouldCheck4UpdateOnStartup)
{
pref.Save();
Check4Update();
}
}
} }
base.OnFrameworkInitializationCompleted();
} }
private static void ShowSelfUpdateResult(object data) private static void ShowSelfUpdateResult(object data)
@ -358,10 +346,7 @@ namespace SourceGit
{ {
var dialog = new Views.SelfUpdate() var dialog = new Views.SelfUpdate()
{ {
DataContext = new ViewModels.SelfUpdate DataContext = new ViewModels.SelfUpdate() { Data = data }
{
Data = data
}
}; };
dialog.Show(desktop.MainWindow); dialog.Show(desktop.MainWindow);
@ -369,18 +354,133 @@ namespace SourceGit
}); });
} }
private static bool TryParseAskpass(string[] args, out string keyname) private static bool TryLaunchedAsRebaseTodoEditor(string[] args, out int exitCode)
{ {
keyname = string.Empty; exitCode = -1;
if (args.Length != 2) if (args.Length <= 1 || !args[0].Equals("--rebase-todo-editor", StringComparison.Ordinal))
return false;
var file = args[1];
var filename = Path.GetFileName(file);
if (!filename.Equals("git-rebase-todo", StringComparison.OrdinalIgnoreCase))
return true;
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(file));
if (!dirInfo.Exists || !dirInfo.Name.Equals("rebase-merge", StringComparison.Ordinal))
return true;
var jobsFile = Path.Combine(dirInfo.Parent.FullName, "sourcegit_rebase_jobs.json");
if (!File.Exists(jobsFile))
return true;
var collection = JsonSerializer.Deserialize(File.ReadAllText(jobsFile), JsonCodeGen.Default.InteractiveRebaseJobCollection);
var lines = new List<string>();
foreach (var job in collection.Jobs)
{
switch (job.Action)
{
case Models.InteractiveRebaseAction.Pick:
lines.Add($"p {job.SHA}");
break;
case Models.InteractiveRebaseAction.Edit:
lines.Add($"e {job.SHA}");
break;
case Models.InteractiveRebaseAction.Reword:
lines.Add($"r {job.SHA}");
break;
case Models.InteractiveRebaseAction.Squash:
lines.Add($"s {job.SHA}");
break;
case Models.InteractiveRebaseAction.Fixup:
lines.Add($"f {job.SHA}");
break;
default:
lines.Add($"d {job.SHA}");
break;
}
}
File.WriteAllLines(file, lines);
exitCode = 0;
return true;
}
private static bool TryLaunchedAsRebaseMessageEditor(string[] args, out int exitCode)
{
exitCode = -1;
if (args.Length <= 1 || !args[0].Equals("--rebase-message-editor", StringComparison.Ordinal))
return false;
var file = args[1];
var filename = Path.GetFileName(file);
if (!filename.Equals("COMMIT_EDITMSG", StringComparison.OrdinalIgnoreCase))
return true;
var jobsFile = Path.Combine(Path.GetDirectoryName(file), "sourcegit_rebase_jobs.json");
if (!File.Exists(jobsFile))
return true;
var collection = JsonSerializer.Deserialize(File.ReadAllText(jobsFile), JsonCodeGen.Default.InteractiveRebaseJobCollection);
var doneFile = Path.Combine(Path.GetDirectoryName(file), "rebase-merge", "done");
if (!File.Exists(doneFile))
return true;
var done = File.ReadAllText(doneFile).Split(new char[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (done.Length > collection.Jobs.Count)
return true;
var job = collection.Jobs[done.Length - 1];
File.WriteAllText(file, job.Message);
exitCode = 0;
return true;
}
private bool TryLaunchedAsCoreEditor(IClassicDesktopStyleApplicationLifetime desktop)
{
var args = desktop.Args;
if (args.Length <= 1 || !args[0].Equals("--core-editor", StringComparison.Ordinal))
return false;
var file = args[1];
if (!File.Exists(file))
Environment.Exit(-1);
desktop.MainWindow = new Views.CodeEditor(file);
return true;
}
private bool TryLaunchedAsAskpass(IClassicDesktopStyleApplicationLifetime desktop)
{
var args = desktop.Args;
if (args.Length <= 1 || !args[0].Equals("--askpass", StringComparison.Ordinal))
return false; return false;
var match = REG_ASKPASS().Match(args[1]); var match = REG_ASKPASS().Match(args[1]);
if (match.Success) if (!match.Success)
keyname = match.Groups[1].Value; return false;
return match.Success; desktop.MainWindow = new Views.Askpass(Path.GetFileName(match.Groups[1].Value));
return true;
}
private void TryLaunchedAsNormal(IClassicDesktopStyleApplicationLifetime desktop)
{
Native.OS.SetupEnternalTools();
var startupRepo = desktop.Args.Length == 1 && Directory.Exists(desktop.Args[0]) ? desktop.Args[0] : null;
_launcher = new ViewModels.Launcher(startupRepo);
desktop.MainWindow = new Views.Launcher() { DataContext = _launcher };
var pref = ViewModels.Preference.Instance;
if (pref.ShouldCheck4UpdateOnStartup)
{
pref.Save();
Check4Update();
}
} }
[GeneratedRegex(@"Enter\s+passphrase\s*for\s*key\s*['""]([^'""]+)['""]\:\s*", RegexOptions.IgnoreCase)] [GeneratedRegex(@"Enter\s+passphrase\s*for\s*key\s*['""]([^'""]+)['""]\:\s*", RegexOptions.IgnoreCase)]

View file

@ -3,7 +3,6 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using Avalonia.Threading; using Avalonia.Threading;
namespace SourceGit.Commands namespace SourceGit.Commands
@ -22,9 +21,17 @@ namespace SourceGit.Commands
public string StdErr { get; set; } public string StdErr { get; set; }
} }
public enum EditorType
{
None,
CoreEditor,
RebaseEditor,
}
public string Context { get; set; } = string.Empty; public string Context { get; set; } = string.Empty;
public CancelToken Cancel { get; set; } = null; public CancelToken Cancel { get; set; } = null;
public string WorkingDirectory { get; set; } = null; public string WorkingDirectory { get; set; } = null;
public EditorType Editor { get; set; } = EditorType.CoreEditor; // Only used in Exec() mode
public string Args { get; set; } = string.Empty; public string Args { get; set; } = string.Empty;
public bool RaiseError { get; set; } = true; public bool RaiseError { get; set; } = true;
public bool TraitErrorAsOutput { get; set; } = false; public bool TraitErrorAsOutput { get; set; } = false;
@ -33,7 +40,7 @@ namespace SourceGit.Commands
public void UseSSHKey(string key) public void UseSSHKey(string key)
{ {
Envs.Add("DISPLAY", "required"); Envs.Add("DISPLAY", "required");
Envs.Add("SSH_ASKPASS", Process.GetCurrentProcess().MainModule.FileName); Envs.Add("SSH_ASKPASS", $"\"{Process.GetCurrentProcess().MainModule.FileName}\" --askpass");
Envs.Add("SSH_ASKPASS_REQUIRE", "prefer"); Envs.Add("SSH_ASKPASS_REQUIRE", "prefer");
Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{key}'"); Envs.Add("GIT_SSH_COMMAND", $"ssh -i '{key}'");
} }
@ -42,7 +49,7 @@ namespace SourceGit.Commands
{ {
var start = new ProcessStartInfo(); var start = new ProcessStartInfo();
start.FileName = Native.OS.GitExecutable; start.FileName = Native.OS.GitExecutable;
start.Arguments = "--no-pager -c core.quotepath=off " + Args; start.Arguments = "--no-pager -c core.quotepath=off ";
start.UseShellExecute = false; start.UseShellExecute = false;
start.CreateNoWindow = true; start.CreateNoWindow = true;
start.RedirectStandardOutput = true; start.RedirectStandardOutput = true;
@ -50,6 +57,24 @@ namespace SourceGit.Commands
start.StandardOutputEncoding = Encoding.UTF8; start.StandardOutputEncoding = Encoding.UTF8;
start.StandardErrorEncoding = Encoding.UTF8; start.StandardErrorEncoding = Encoding.UTF8;
// Editors
var editorProgram = $"\\\"{Process.GetCurrentProcess().MainModule.FileName}\\\"";
switch (Editor)
{
case EditorType.CoreEditor:
start.Arguments += $"-c core.editor=\"{editorProgram} --core-editor\" ";
break;
case EditorType.RebaseEditor:
start.Arguments += $"-c core.editor=\"{editorProgram} --rebase-message-editor\" -c sequence.editor=\"{editorProgram} --rebase-todo-editor\" -c rebase.abbreviateCommands=true ";
break;
default:
start.Arguments += "-c core.editor=true ";
break;
}
// Append command args
start.Arguments += Args;
// User environment overrides. // User environment overrides.
foreach (var kv in Envs) foreach (var kv in Envs)
start.Environment.Add(kv.Key, kv.Value); start.Environment.Add(kv.Key, kv.Value);

View file

@ -1,6 +1,4 @@
using System.Diagnostics; namespace SourceGit.Commands
namespace SourceGit.Commands
{ {
public class Rebase : Command public class Rebase : Command
{ {
@ -19,12 +17,10 @@ namespace SourceGit.Commands
{ {
public InteractiveRebase(string repo, string basedOn) public InteractiveRebase(string repo, string basedOn)
{ {
var exec = Process.GetCurrentProcess().MainModule.FileName;
var editor = $"\\\"{exec}\\\" --rebase-editor";
WorkingDirectory = repo; WorkingDirectory = repo;
Context = repo; Context = repo;
Args = $"-c core.editor=\"{editor}\" -c sequence.editor=\"{editor}\" -c rebase.abbreviateCommands=true rebase -i --autosquash {basedOn}"; Editor = EditorType.RebaseEditor;
Args = $"rebase -i --autosquash {basedOn}";
} }
} }
} }

View file

@ -0,0 +1,26 @@
using System.Collections.Generic;
namespace SourceGit.Models
{
public enum InteractiveRebaseAction
{
Pick,
Edit,
Reword,
Squash,
Fixup,
Drop,
}
public class InteractiveRebaseJob
{
public string SHA { get; set; } = string.Empty;
public InteractiveRebaseAction Action { get; set; } = InteractiveRebaseAction.Pick;
public string Message { get; set; } = string.Empty;
}
public class InteractiveRebaseJobCollection
{
public List<InteractiveRebaseJob> Jobs { get; set; } = new List<InteractiveRebaseJob>();
}
}

View file

@ -1,98 +0,0 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace SourceGit.Models
{
public enum InteractiveRebaseAction
{
Pick,
Edit,
Reword,
Squash,
Fixup,
Drop,
}
public class InteractiveRebaseJob
{
public string SHA { get; set; } = string.Empty;
public InteractiveRebaseAction Action { get; set; } = InteractiveRebaseAction.Pick;
public string Message { get; set; } = string.Empty;
}
public static class InteractiveRebaseEditor
{
public static int Process(string file)
{
try
{
var filename = Path.GetFileName(file);
if (filename.Equals("git-rebase-todo", StringComparison.OrdinalIgnoreCase))
{
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(file));
if (!dirInfo.Exists || !dirInfo.Name.Equals("rebase-merge", StringComparison.Ordinal))
return -1;
var jobsFile = Path.Combine(dirInfo.Parent.FullName, "sourcegit_rebase_jobs.json");
if (!File.Exists(jobsFile))
return -1;
var jobs = JsonSerializer.Deserialize(File.ReadAllText(jobsFile), JsonCodeGen.Default.ListInteractiveRebaseJob);
var lines = new List<string>();
foreach (var job in jobs)
{
switch (job.Action)
{
case InteractiveRebaseAction.Pick:
lines.Add($"p {job.SHA}");
break;
case InteractiveRebaseAction.Edit:
lines.Add($"e {job.SHA}");
break;
case InteractiveRebaseAction.Reword:
lines.Add($"r {job.SHA}");
break;
case InteractiveRebaseAction.Squash:
lines.Add($"s {job.SHA}");
break;
case InteractiveRebaseAction.Fixup:
lines.Add($"f {job.SHA}");
break;
default:
lines.Add($"d {job.SHA}");
break;
}
}
File.WriteAllLines(file, lines);
}
else if (filename.Equals("COMMIT_EDITMSG", StringComparison.OrdinalIgnoreCase))
{
var jobsFile = Path.Combine(Path.GetDirectoryName(file), "sourcegit_rebase_jobs.json");
if (!File.Exists(jobsFile))
return 0;
var jobs = JsonSerializer.Deserialize(File.ReadAllText(jobsFile), JsonCodeGen.Default.ListInteractiveRebaseJob);
var doneFile = Path.Combine(Path.GetDirectoryName(file), "rebase-merge", "done");
if (!File.Exists(doneFile))
return -1;
var done = File.ReadAllText(doneFile).Split(new char[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries);
if (done.Length > jobs.Count)
return -1;
var job = jobs[done.Length - 1];
File.WriteAllText(file, job.Message);
}
return 0;
}
catch
{
return -1;
}
}
}
}

View file

@ -91,6 +91,7 @@
<x:String x:Key="Text.Clone.ParentFolder" xml:space="preserve">Parent Folder:</x:String> <x:String x:Key="Text.Clone.ParentFolder" xml:space="preserve">Parent Folder:</x:String>
<x:String x:Key="Text.Clone.RemoteURL" xml:space="preserve">Repository URL:</x:String> <x:String x:Key="Text.Clone.RemoteURL" xml:space="preserve">Repository URL:</x:String>
<x:String x:Key="Text.Close" xml:space="preserve">CLOSE</x:String> <x:String x:Key="Text.Close" xml:space="preserve">CLOSE</x:String>
<x:String x:Key="Text.CodeEditor" xml:space="preserve">Editor</x:String>
<x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">Cherry-Pick This Commit</x:String> <x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">Cherry-Pick This Commit</x:String>
<x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">Checkout Commit</x:String> <x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">Checkout Commit</x:String>
<x:String x:Key="Text.CommitCM.CompareWithHead" xml:space="preserve">Compare with HEAD</x:String> <x:String x:Key="Text.CommitCM.CompareWithHead" xml:space="preserve">Compare with HEAD</x:String>

View file

@ -94,6 +94,7 @@
<x:String x:Key="Text.Clone.ParentFolder" xml:space="preserve">父级目录 </x:String> <x:String x:Key="Text.Clone.ParentFolder" xml:space="preserve">父级目录 </x:String>
<x:String x:Key="Text.Clone.RemoteURL" xml:space="preserve">远程仓库 </x:String> <x:String x:Key="Text.Clone.RemoteURL" xml:space="preserve">远程仓库 </x:String>
<x:String x:Key="Text.Close" xml:space="preserve">关闭</x:String> <x:String x:Key="Text.Close" xml:space="preserve">关闭</x:String>
<x:String x:Key="Text.CodeEditor" xml:space="preserve">提交信息编辑器</x:String>
<x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">挑选(cherry-pick)此提交</x:String> <x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">挑选(cherry-pick)此提交</x:String>
<x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">检出此提交</x:String> <x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">检出此提交</x:String>
<x:String x:Key="Text.CommitCM.CompareWithHead" xml:space="preserve">与当前HEAD比较</x:String> <x:String x:Key="Text.CommitCM.CompareWithHead" xml:space="preserve">与当前HEAD比较</x:String>

View file

@ -94,6 +94,7 @@
<x:String x:Key="Text.Clone.ParentFolder" xml:space="preserve">父級目錄 </x:String> <x:String x:Key="Text.Clone.ParentFolder" xml:space="preserve">父級目錄 </x:String>
<x:String x:Key="Text.Clone.RemoteURL" xml:space="preserve">遠端倉庫 </x:String> <x:String x:Key="Text.Clone.RemoteURL" xml:space="preserve">遠端倉庫 </x:String>
<x:String x:Key="Text.Close" xml:space="preserve">關閉</x:String> <x:String x:Key="Text.Close" xml:space="preserve">關閉</x:String>
<x:String x:Key="Text.CodeEditor" xml:space="preserve">提交資訊編輯器</x:String>
<x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">挑選(cherry-pick)此提交</x:String> <x:String x:Key="Text.CommitCM.CherryPick" xml:space="preserve">挑選(cherry-pick)此提交</x:String>
<x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">檢出此提交</x:String> <x:String x:Key="Text.CommitCM.Checkout" xml:space="preserve">檢出此提交</x:String>
<x:String x:Key="Text.CommitCM.CompareWithHead" xml:space="preserve">與當前HEAD比較</x:String> <x:String x:Key="Text.CommitCM.CompareWithHead" xml:space="preserve">與當前HEAD比較</x:String>

View file

@ -1,5 +1,4 @@
using System.Diagnostics; using System.IO;
using System.IO;
namespace SourceGit.ViewModels namespace SourceGit.ViewModels
{ {
@ -39,7 +38,8 @@ namespace SourceGit.ViewModels
{ {
WorkingDirectory = Repository, WorkingDirectory = Repository,
Context = Repository, Context = Repository,
Args = $"-c core.editor=true {Cmd} --continue", Editor = Commands.Command.EditorType.None,
Args = $"{Cmd} --continue",
}.Exec(); }.Exec();
} }
} }
@ -58,14 +58,12 @@ namespace SourceGit.ViewModels
public override bool Continue() public override bool Continue()
{ {
var exec = Process.GetCurrentProcess().MainModule.FileName;
var editor = $"\\\"{exec}\\\" --rebase-editor";
var succ = new Commands.Command() var succ = new Commands.Command()
{ {
WorkingDirectory = Repository, WorkingDirectory = Repository,
Context = Repository, Context = Repository,
Args = $"-c core.editor=\"{editor}\" rebase --continue", Editor = Commands.Command.EditorType.RebaseEditor,
Args = $"rebase --continue",
}.Exec(); }.Exec();
if (succ) if (succ)

View file

@ -168,18 +168,18 @@ namespace SourceGit.ViewModels
_repo.SetWatcherEnabled(false); _repo.SetWatcherEnabled(false);
var saveFile = Path.Combine(_repo.GitDir, "sourcegit_rebase_jobs.json"); var saveFile = Path.Combine(_repo.GitDir, "sourcegit_rebase_jobs.json");
var jobs = new List<Models.InteractiveRebaseJob>(); var collection = new Models.InteractiveRebaseJobCollection();
for (int i = Items.Count - 1; i >= 0; i--) for (int i = Items.Count - 1; i >= 0; i--)
{ {
var item = Items[i]; var item = Items[i];
jobs.Add(new Models.InteractiveRebaseJob() collection.Jobs.Add(new Models.InteractiveRebaseJob()
{ {
SHA = item.Commit.SHA, SHA = item.Commit.SHA,
Action = item.Action, Action = item.Action,
Message = item.FullMessage, Message = item.FullMessage,
}); });
} }
File.WriteAllText(saveFile, JsonSerializer.Serialize(jobs, JsonCodeGen.Default.ListInteractiveRebaseJob)); File.WriteAllText(saveFile, JsonSerializer.Serialize(collection, JsonCodeGen.Default.InteractiveRebaseJobCollection));
return Task.Run(() => return Task.Run(() =>
{ {

View file

@ -28,21 +28,20 @@ namespace SourceGit.ViewModels
} }
} }
public Launcher(string[] commandlines) public Launcher(string startupRepo)
{ {
Pages = new AvaloniaList<LauncherPage>(); Pages = new AvaloniaList<LauncherPage>();
AddNewTab(); AddNewTab();
if (commandlines.Length == 2) if (!string.IsNullOrEmpty(startupRepo))
{ {
var path = commandlines[1]; var root = new Commands.QueryRepositoryRootPath(startupRepo).Result();
var root = new Commands.QueryRepositoryRootPath(path).Result();
if (string.IsNullOrEmpty(root)) if (string.IsNullOrEmpty(root))
{ {
Pages[0].Notifications.Add(new Notification Pages[0].Notifications.Add(new Notification
{ {
IsError = true, IsError = true,
Message = $"Given path: '{path}' is NOT a valid repository!" Message = $"Given path: '{startupRepo}' is NOT a valid repository!"
}); });
return; return;
} }

View file

@ -0,0 +1,62 @@
<v:ChromelessWindow xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:SourceGit.ViewModels"
xmlns:v="using:SourceGit.Views"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.CodeEditor"
Icon="/App.ico"
Title="{DynamicResource Text.CodeEditor}"
Width="800"
SizeToContent="Height"
CanResize="False"
WindowStartupLocation="CenterScreen">
<Grid RowDefinitions="Auto,*">
<!-- TitleBar -->
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto" Height="30">
<Border Grid.Column="0" Grid.ColumnSpan="3"
Background="{DynamicResource Brush.TitleBar}"
BorderThickness="0,0,0,1" BorderBrush="{DynamicResource Brush.Border0}"
PointerPressed="BeginMoveWindow"/>
<Path Grid.Column="0"
Width="14" Height="14"
Margin="10,0,0,0"
Data="{StaticResource Icons.Edit}"
IsVisible="{OnPlatform True, macOS=False}"/>
<Grid Grid.Column="0" Classes="caption_button_box" Margin="2,4,0,0" IsVisible="{OnPlatform False, macOS=True}">
<Button Classes="caption_button_macos" Click="CloseWindow">
<Grid>
<Ellipse Fill="{DynamicResource Brush.MacOS.Close}"/>
<Path Height="6" Width="6" Stretch="Fill" Fill="#404040" Stroke="#404040" StrokeThickness="1" Data="{StaticResource Icons.Window.Close}"/>
</Grid>
</Button>
</Grid>
<TextBlock Grid.Column="0" Grid.ColumnSpan="3"
Classes="bold"
Text="{DynamicResource Text.CodeEditor}"
HorizontalAlignment="Center" VerticalAlignment="Center"
IsHitTestVisible="False"/>
<Button Grid.Column="2"
Classes="caption_button"
Click="CloseWindow"
IsVisible="{OnPlatform True, macOS=False}">
<Path Data="{StaticResource Icons.Window.Close}"/>
</Button>
</Grid>
<StackPanel Grid.Row="1" Orientation="Vertical" Margin="8">
<v:CommitMessageTextBox x:Name="Editor" Height="400" Text=""/>
<Button Classes="flat primary"
Width="80"
Margin="0,8,0,4"
Content="{DynamicResource Text.Sure}"
Click="SaveAndClose"
HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
</v:ChromelessWindow>

View file

@ -0,0 +1,54 @@
using System;
using System.IO;
using System.Text;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace SourceGit.Views
{
public partial class CodeEditor : ChromelessWindow
{
public CodeEditor()
{
DataContext = this;
InitializeComponent();
}
public CodeEditor(string file)
{
_file = file;
DataContext = this;
InitializeComponent();
var content = File.ReadAllText(file).ReplaceLineEndings("\n");
var firstLineEnd = content.IndexOf('\n');
if (firstLineEnd == -1)
{
Editor.SubjectEditor.Text = content;
}
else
{
Editor.SubjectEditor.Text = content.Substring(0, firstLineEnd);
Editor.DescriptionEditor.Text = content.Substring(firstLineEnd + 1);
}
}
private void BeginMoveWindow(object sender, PointerPressedEventArgs e)
{
BeginMoveDrag(e);
}
private void CloseWindow(object sender, RoutedEventArgs e)
{
Environment.Exit(-1);
}
private void SaveAndClose(object sender, RoutedEventArgs e)
{
File.WriteAllText(_file, Editor.Text);
Environment.Exit(0);
}
private string _file = string.Empty;
}
}