Skip to content

Commit 7db1442

Browse files
committed
feature: use <SOURCEGIT> --blame <FILE> to launch SourceGit as Blame viewer (#2047)
Signed-off-by: leo <[email protected]>
1 parent a546bec commit 7db1442

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/App.axaml.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,9 @@ public override void OnFrameworkInitializationCompleted()
390390
if (TryLaunchAsFileHistoryViewer(desktop))
391391
return;
392392

393+
if (TryLaunchAsBlameViewer(desktop))
394+
return;
395+
393396
if (TryLaunchAsCoreEditor(desktop))
394397
return;
395398

@@ -543,6 +546,41 @@ private bool TryLaunchAsFileHistoryViewer(IClassicDesktopStyleApplicationLifetim
543546
return true;
544547
}
545548

549+
private bool TryLaunchAsBlameViewer(IClassicDesktopStyleApplicationLifetime desktop)
550+
{
551+
var args = desktop.Args;
552+
if (args is not { Length: > 1 } || !args[0].Equals("--blame", StringComparison.Ordinal))
553+
return false;
554+
555+
var file = Path.GetFullPath(args[1]);
556+
var dir = Path.GetDirectoryName(file);
557+
558+
var test = new Commands.QueryRepositoryRootPath(dir).GetResult();
559+
if (!test.IsSuccess || string.IsNullOrEmpty(test.StdOut))
560+
{
561+
Console.Out.WriteLine($"'{args[1]}' is not in a valid git repository");
562+
desktop.Shutdown(-1);
563+
return true;
564+
}
565+
566+
var repo = test.StdOut.Trim();
567+
var head = new Commands.QuerySingleCommit(repo, "HEAD").GetResult();
568+
if (head == null)
569+
{
570+
Console.Out.WriteLine($"{repo} has no commits!");
571+
desktop.Shutdown(-1);
572+
return true;
573+
}
574+
575+
var relFile = Path.GetRelativePath(repo, file);
576+
var viewer = new Views.Blame()
577+
{
578+
DataContext = new ViewModels.Blame(repo, relFile, head)
579+
};
580+
desktop.MainWindow = viewer;
581+
return true;
582+
}
583+
546584
private bool TryLaunchAsCoreEditor(IClassicDesktopStyleApplicationLifetime desktop)
547585
{
548586
var args = desktop.Args;

0 commit comments

Comments
 (0)