diff --git a/src/App.axaml.cs b/src/App.axaml.cs index a4c25193d..0fe1b59da 100644 --- a/src/App.axaml.cs +++ b/src/App.axaml.cs @@ -441,17 +441,25 @@ public override void OnFrameworkInitializationCompleted() _ipcChannel = new Models.IpcChannel(); if (!_ipcChannel.IsFirstInstance) { - var arg = desktop.Args is { Length: > 0 } ? desktop.Args[0].Trim() : string.Empty; - if (!string.IsNullOrEmpty(arg)) + var argsToSend = new List(); + if (desktop.Args != null) { - if (arg.StartsWith('"') && arg.EndsWith('"')) - arg = arg.Substring(1, arg.Length - 2).Trim(); + for (int i = 0; i < desktop.Args.Length; i++) + { + var arg = desktop.Args[i].Trim(); + if (i == 0 && !arg.StartsWith("--")) + { + if (arg.StartsWith('"') && arg.EndsWith('"')) + arg = arg.Substring(1, arg.Length - 2).Trim(); - if (arg.Length > 0 && !Path.IsPathFullyQualified(arg)) - arg = Path.GetFullPath(arg); + if (arg.Length > 0 && !Path.IsPathFullyQualified(arg)) + arg = Path.GetFullPath(arg); + } + argsToSend.Add(arg); + } } - _ipcChannel.SendToFirstInstance(arg); + _ipcChannel.SendToFirstInstance(string.Join("\n", argsToSend)); Environment.Exit(0); } else @@ -663,8 +671,8 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop) Models.AvatarManager.Instance.Start(); string startupRepo = null; - if (desktop.Args is { Length: 1 } && Directory.Exists(desktop.Args[0])) - startupRepo = desktop.Args[0]; + if (desktop.Args != null) + ParsedArgs = ParseArgs(desktop.Args, out startupRepo); var pref = ViewModels.Preferences.Instance; pref.SetCanModify(); @@ -679,8 +687,14 @@ private void TryLaunchAsNormal(IClassicDesktopStyleApplicationLifetime desktop) #endif } - private void TryOpenRepository(string repo) + private void TryOpenRepository(string message) { + if (string.IsNullOrWhiteSpace(message)) + return; + + string[] args = message.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + ParsedArgs = ParseArgs(args, out string repo); + if (!string.IsNullOrEmpty(repo) && Directory.Exists(repo)) { var test = new Commands.QueryRepositoryRootPath(repo).GetResult(); @@ -801,16 +815,48 @@ private string FixFontFamilyName(string input) } } - return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty; + return string.Join(",", trimmed); + } + + public class LaunchArguments + { + public bool IgnoreWorkspace { get; set; } + public bool NoSaveWorkspace { get; set; } + public int? StartupViewIndex { get; set; } + } + + public static LaunchArguments ParseArgs(string[] args, out string repoPath) + { + repoPath = null; + var parsed = new LaunchArguments(); + for (int i = 0; i < args.Length; i++) + { + var arg = args[i].Trim(); + if (arg.Equals("--ignore-workspace", StringComparison.OrdinalIgnoreCase)) + parsed.IgnoreWorkspace = true; + else if (arg.Equals("--no-save-workspace", StringComparison.OrdinalIgnoreCase)) + parsed.NoSaveWorkspace = true; + else if (arg.Equals("--commit", StringComparison.OrdinalIgnoreCase)) + parsed.StartupViewIndex = 1; + else if (arg.Equals("--history", StringComparison.OrdinalIgnoreCase)) + parsed.StartupViewIndex = 0; + else if (arg.Equals("--stashes", StringComparison.OrdinalIgnoreCase)) + parsed.StartupViewIndex = 2; + else if (i == 0 && !arg.StartsWith("--")) + repoPath = arg; + } + return parsed; } + public static LaunchArguments ParsedArgs { get; set; } = new LaunchArguments(); + [GeneratedRegex(@"^[a-z]+\s+([a-fA-F0-9]{4,40})(\s+.*)?$")] private static partial Regex REG_REBASE_TODO(); - private Models.IpcChannel _ipcChannel = null; - private ViewModels.Launcher _launcher = null; - private ResourceDictionary _activeLocale = null; - private ResourceDictionary _themeOverrides = null; - private ResourceDictionary _fontsOverrides = null; + private Models.IpcChannel _ipcChannel; + private ResourceDictionary _activeLocale; + private ResourceDictionary _themeOverrides; + private ResourceDictionary _fontsOverrides; + private ViewModels.Launcher _launcher; } } diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index ef34d4df5..fd00119f6 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -56,18 +56,21 @@ public Launcher(string startupRepo) ActiveWorkspace = pref.GetActiveWorkspace(); var repos = ActiveWorkspace.Repositories.ToArray(); - foreach (var repo in repos) + if (!App.ParsedArgs.IgnoreWorkspace) { - var node = pref.FindNode(repo) ?? - new RepositoryNode - { - Id = repo, - Name = Path.GetFileName(repo), - Bookmark = 0, - IsRepository = true, - }; + foreach (var repo in repos) + { + var node = pref.FindNode(repo) ?? + new RepositoryNode + { + Id = repo, + Name = Path.GetFileName(repo), + Bookmark = 0, + IsRepository = true, + }; - OpenRepositoryInTab(node, null); + OpenRepositoryInTab(node, null); + } } _ignoreIndexChange = false; diff --git a/src/ViewModels/Preferences.cs b/src/ViewModels/Preferences.cs index 95817cc1d..8d24e9e16 100644 --- a/src/ViewModels/Preferences.cs +++ b/src/ViewModels/Preferences.cs @@ -621,6 +621,9 @@ public void Save() if (_isLoading || _isReadonly) return; + if (App.ParsedArgs != null && App.ParsedArgs.NoSaveWorkspace) + return; + var file = Path.Combine(Native.OS.DataDir, "preference.json"); using var stream = File.Create(file); JsonSerializer.Serialize(stream, this, JsonCodeGen.Default.Preferences); diff --git a/src/ViewModels/Repository.cs b/src/ViewModels/Repository.cs index 7123a2e0c..5d2dc6cd9 100644 --- a/src/ViewModels/Repository.cs +++ b/src/ViewModels/Repository.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -465,7 +465,25 @@ public void Open() _stashesPage = new StashesPage(this); _searchCommitContext = new SearchCommitContext(this); - if (Preferences.Instance.ShowLocalChangesByDefault) + if (App.ParsedArgs != null && App.ParsedArgs.StartupViewIndex.HasValue) + { + switch (App.ParsedArgs.StartupViewIndex.Value) + { + case 1: + _selectedView = _workingCopy; + _selectedViewIndex = 1; + break; + case 2: + _selectedView = _stashesPage; + _selectedViewIndex = 2; + break; + default: + _selectedView = _histories; + _selectedViewIndex = 0; + break; + } + } + else if (Preferences.Instance.ShowLocalChangesByDefault) { _selectedView = _workingCopy; _selectedViewIndex = 1;