| 1 | #nullable enable |
| 2 | |
| 3 | using System.Diagnostics; |
| 4 | using CascadeIDE.Models.Intercom; |
| 5 | using CascadeIDE.Services.Intercom; |
| 6 | |
| 7 | namespace CascadeIDE.Services.Intercom; |
| 8 | |
| 9 | /// <summary>Best-effort git/solution snapshot @ send (ADR 0128 §3.1).</summary> |
| 10 | public static class IntercomSenderWorkspaceContextCapture |
| 11 | { |
| 12 | public static SenderWorkspaceContext? TryCapture(string? workspaceRoot, string? solutionPath) |
| 13 | { |
| 14 | if (string.IsNullOrWhiteSpace(workspaceRoot)) |
| 15 | return null; |
| 16 | |
| 17 | var root = workspaceRoot.Trim(); |
| 18 | if (!Directory.Exists(root)) |
| 19 | return null; |
| 20 | |
| 21 | var branch = tryGit(root, "rev-parse --abbrev-ref HEAD"); |
| 22 | var commit = tryGit(root, "rev-parse --short HEAD"); |
| 23 | var relSolution = string.IsNullOrWhiteSpace(solutionPath) |
| 24 | ? null |
| 25 | : AttachmentAnchorPaths.ToWorkspaceRelative(solutionPath, root) ?? solutionPath; |
| 26 | |
| 27 | if (branch is null && commit is null && relSolution is null) |
| 28 | return null; |
| 29 | |
| 30 | return new SenderWorkspaceContext( |
| 31 | branch, |
| 32 | commit, |
| 33 | relSolution, |
| 34 | DateTimeOffset.UtcNow.ToString("O")); |
| 35 | } |
| 36 | |
| 37 | private static string? tryGit(string workspaceRoot, string args) |
| 38 | { |
| 39 | try |
| 40 | { |
| 41 | using var process = new Process |
| 42 | { |
| 43 | StartInfo = new ProcessStartInfo |
| 44 | { |
| 45 | FileName = "git", |
| 46 | Arguments = args, |
| 47 | WorkingDirectory = workspaceRoot, |
| 48 | RedirectStandardOutput = true, |
| 49 | RedirectStandardError = true, |
| 50 | UseShellExecute = false, |
| 51 | CreateNoWindow = true, |
| 52 | }, |
| 53 | }; |
| 54 | if (!process.Start()) |
| 55 | return null; |
| 56 | var output = process.StandardOutput.ReadToEnd().Trim(); |
| 57 | process.WaitForExit(2000); |
| 58 | return process.ExitCode == 0 && output.Length > 0 ? output : null; |
| 59 | } |
| 60 | catch |
| 61 | { |
| 62 | return null; |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |