From 4d997a5f99a393ace270f9b5195ba3e27a4e2a0b Mon Sep 17 00:00:00 2001 From: mika kuns Date: Mon, 10 Aug 2026 09:59:42 +0200 Subject: [PATCH] refactor(git): share the info/exclude writer with the findings store --- src/ClaudeDo.Worker/Findings/FindingsStore.cs | 7 +- .../Findings/Interfaces/IFindingsStore.cs | 2 +- src/ClaudeDo.Worker/Git/GitExcludeWriter.cs | 74 +++++++++++++++++++ .../Skills/SessionSkillSeeder.cs | 65 +--------------- .../Findings/FindingsStoreGitTests.cs | 42 +++++++++++ .../Findings/FindingsStoreTests.cs | 22 +++--- 6 files changed, 136 insertions(+), 76 deletions(-) create mode 100644 src/ClaudeDo.Worker/Git/GitExcludeWriter.cs create mode 100644 tests/ClaudeDo.Worker.Tests/Findings/FindingsStoreGitTests.cs diff --git a/src/ClaudeDo.Worker/Findings/FindingsStore.cs b/src/ClaudeDo.Worker/Findings/FindingsStore.cs index 7354777c..df9b4c0c 100644 --- a/src/ClaudeDo.Worker/Findings/FindingsStore.cs +++ b/src/ClaudeDo.Worker/Findings/FindingsStore.cs @@ -1,5 +1,6 @@ using System.Text; using System.Text.RegularExpressions; +using ClaudeDo.Worker.Git; namespace ClaudeDo.Worker.Findings; @@ -21,7 +22,8 @@ public sealed class FindingsStore : IFindingsStore public static string TrapsDir(string workingDir) => Path.Combine(StoreDir(workingDir), "traps"); public static string IndexPath(string workingDir) => Path.Combine(StoreDir(workingDir), "INDEX.md"); - public async Task SaveAsync(string workingDir, FindingInput input, CancellationToken ct) + public async Task SaveAsync( + string workingDir, FindingInput input, CancellationToken ct, bool tracked = false) { if (string.IsNullOrWhiteSpace(input.Slug) || input.Slug.Length > 60 || !SlugPattern.IsMatch(input.Slug)) throw new ArgumentException( @@ -35,6 +37,9 @@ public sealed class FindingsStore : IFindingsStore var trapsDir = TrapsDir(workingDir); Directory.CreateDirectory(trapsDir); + if (!tracked) + await GitExcludeWriter.AppendLineAsync(workingDir, "/.claudedo/", ct); + var path = Path.Combine(trapsDir, input.Slug + ".md"); var created = !File.Exists(path); diff --git a/src/ClaudeDo.Worker/Findings/Interfaces/IFindingsStore.cs b/src/ClaudeDo.Worker/Findings/Interfaces/IFindingsStore.cs index eb015b52..fe766c8c 100644 --- a/src/ClaudeDo.Worker/Findings/Interfaces/IFindingsStore.cs +++ b/src/ClaudeDo.Worker/Findings/Interfaces/IFindingsStore.cs @@ -18,5 +18,5 @@ public sealed record SaveFindingOutcome( public interface IFindingsStore { - Task SaveAsync(string workingDir, FindingInput input, CancellationToken ct); + Task SaveAsync(string workingDir, FindingInput input, CancellationToken ct, bool tracked = false); } diff --git a/src/ClaudeDo.Worker/Git/GitExcludeWriter.cs b/src/ClaudeDo.Worker/Git/GitExcludeWriter.cs new file mode 100644 index 00000000..0d8fd6ae --- /dev/null +++ b/src/ClaudeDo.Worker/Git/GitExcludeWriter.cs @@ -0,0 +1,74 @@ +using System.Diagnostics; +using System.Text; + +namespace ClaudeDo.Worker.Git; + +/// +/// Appends paths to a repository's .git/info/exclude. Deliberately not .gitignore: the exclude +/// file is per-clone and invisible to the repository, so ClaudeDo never modifies a tracked file. +/// Shared by SessionSkillSeeder (seeded skills) and FindingsStore (untracked findings stores). +/// +public static class GitExcludeWriter +{ + /// Idempotent — an already-present line is left alone. + public static async Task AppendLineAsync(string workingDir, string excludeLine, CancellationToken ct) + { + var relativeExcludePath = await RunGitCaptureAsync(workingDir, ["rev-parse", "--git-path", "info/exclude"], ct); + var excludeFile = Path.IsPathRooted(relativeExcludePath) + ? relativeExcludePath + : Path.GetFullPath(Path.Combine(workingDir, relativeExcludePath)); + + var excludeDir = Path.GetDirectoryName(excludeFile); + if (!string.IsNullOrEmpty(excludeDir)) + Directory.CreateDirectory(excludeDir); + + if (File.Exists(excludeFile)) + { + var existingLines = await File.ReadAllLinesAsync(excludeFile, ct); + if (existingLines.Any(l => l.Trim() == excludeLine)) + return; + } + + await File.AppendAllTextAsync(excludeFile, excludeLine + Environment.NewLine, ct); + } + + private static async Task RunGitCaptureAsync(string workingDir, IEnumerable args, CancellationToken ct) + { + var psi = new ProcessStartInfo + { + FileName = "git", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true, + StandardOutputEncoding = Encoding.UTF8, + StandardErrorEncoding = Encoding.UTF8, + }; + psi.ArgumentList.Add("-C"); + psi.ArgumentList.Add(workingDir); + foreach (var a in args) psi.ArgumentList.Add(a); + + using var proc = new Process { StartInfo = psi }; + proc.Start(); + + await using var ctr = ct.Register(() => + { + try { proc.Kill(entireProcessTree: true); } + catch { /* already exited */ } + }); + + var stdoutTask = proc.StandardOutput.ReadToEndAsync(); + var stderrTask = proc.StandardError.ReadToEndAsync(); + + await proc.WaitForExitAsync(CancellationToken.None); + var stdout = await stdoutTask; + var stderr = await stderrTask; + + ct.ThrowIfCancellationRequested(); + + if (proc.ExitCode != 0) + throw new InvalidOperationException($"git rev-parse --git-path failed (exit {proc.ExitCode}): {stderr.TrimEnd()}"); + + return stdout.TrimEnd('\r', '\n'); + } +} diff --git a/src/ClaudeDo.Worker/Skills/SessionSkillSeeder.cs b/src/ClaudeDo.Worker/Skills/SessionSkillSeeder.cs index c3597385..da7ed2f9 100644 --- a/src/ClaudeDo.Worker/Skills/SessionSkillSeeder.cs +++ b/src/ClaudeDo.Worker/Skills/SessionSkillSeeder.cs @@ -1,6 +1,5 @@ -using System.Diagnostics; -using System.Text; using ClaudeDo.Data; +using ClaudeDo.Worker.Git; using Microsoft.Extensions.Logging; namespace ClaudeDo.Worker.Skills; @@ -35,7 +34,7 @@ public sealed class SessionSkillSeeder : ISessionSkillSeeder CopyDirectory(sourceDir, destDir); if (isWorktree) - await AppendExcludeLineAsync(workingDir, $"/.claude/skills/{name}/", ct); + await GitExcludeWriter.AppendLineAsync(workingDir, $"/.claude/skills/{name}/", ct); } } @@ -53,64 +52,4 @@ public sealed class SessionSkillSeeder : ISessionSkillSeeder } } - private static async Task AppendExcludeLineAsync(string workingDir, string excludeLine, CancellationToken ct) - { - var relativeExcludePath = await RunGitCaptureAsync(workingDir, ["rev-parse", "--git-path", "info/exclude"], ct); - var excludeFile = Path.IsPathRooted(relativeExcludePath) - ? relativeExcludePath - : Path.GetFullPath(Path.Combine(workingDir, relativeExcludePath)); - - var excludeDir = Path.GetDirectoryName(excludeFile); - if (!string.IsNullOrEmpty(excludeDir)) - Directory.CreateDirectory(excludeDir); - - if (File.Exists(excludeFile)) - { - var existingLines = await File.ReadAllLinesAsync(excludeFile, ct); - if (existingLines.Any(l => l.Trim() == excludeLine)) - return; - } - - await File.AppendAllTextAsync(excludeFile, excludeLine + Environment.NewLine, ct); - } - - private static async Task RunGitCaptureAsync(string workingDir, IEnumerable args, CancellationToken ct) - { - var psi = new ProcessStartInfo - { - FileName = "git", - RedirectStandardOutput = true, - RedirectStandardError = true, - UseShellExecute = false, - CreateNoWindow = true, - StandardOutputEncoding = Encoding.UTF8, - StandardErrorEncoding = Encoding.UTF8, - }; - psi.ArgumentList.Add("-C"); - psi.ArgumentList.Add(workingDir); - foreach (var a in args) psi.ArgumentList.Add(a); - - using var proc = new Process { StartInfo = psi }; - proc.Start(); - - await using var ctr = ct.Register(() => - { - try { proc.Kill(entireProcessTree: true); } - catch { /* already exited */ } - }); - - var stdoutTask = proc.StandardOutput.ReadToEndAsync(); - var stderrTask = proc.StandardError.ReadToEndAsync(); - - await proc.WaitForExitAsync(CancellationToken.None); - var stdout = await stdoutTask; - var stderr = await stderrTask; - - ct.ThrowIfCancellationRequested(); - - if (proc.ExitCode != 0) - throw new InvalidOperationException($"git rev-parse --git-path failed (exit {proc.ExitCode}): {stderr.TrimEnd()}"); - - return stdout.TrimEnd('\r', '\n'); - } } diff --git a/tests/ClaudeDo.Worker.Tests/Findings/FindingsStoreGitTests.cs b/tests/ClaudeDo.Worker.Tests/Findings/FindingsStoreGitTests.cs new file mode 100644 index 00000000..c0b8e5b2 --- /dev/null +++ b/tests/ClaudeDo.Worker.Tests/Findings/FindingsStoreGitTests.cs @@ -0,0 +1,42 @@ +using ClaudeDo.Worker.Findings; +using ClaudeDo.Worker.Tests.Infrastructure; + +namespace ClaudeDo.Worker.Tests.Findings; + +public sealed class FindingsStoreGitTests : IDisposable +{ + private readonly GitRepoFixture _repo = new(); + + public void Dispose() => _repo.Dispose(); + + private static FindingInput Input(string slug) + => new(slug, "Something is not what it looks like", "Body.", "src", "task-1", "abc1234"); + + [Fact] + public async Task SaveAsync_WhenNotTracked_ExcludesStoreFromGitOnce() + { + if (!GitRepoFixture.IsGitAvailable()) return; + + var store = new FindingsStore(); + + await store.SaveAsync(_repo.RepoDir, Input("first"), CancellationToken.None, tracked: false); + await store.SaveAsync(_repo.RepoDir, Input("second"), CancellationToken.None, tracked: false); + + var exclude = await File.ReadAllLinesAsync(Path.Combine(_repo.RepoDir, ".git", "info", "exclude")); + Assert.Single(exclude, l => l.Trim() == "/.claudedo/"); + } + + [Fact] + public async Task SaveAsync_WhenTracked_DoesNotTouchExclude() + { + if (!GitRepoFixture.IsGitAvailable()) return; + + var store = new FindingsStore(); + + await store.SaveAsync(_repo.RepoDir, Input("first"), CancellationToken.None, tracked: true); + + var excludePath = Path.Combine(_repo.RepoDir, ".git", "info", "exclude"); + var lines = File.Exists(excludePath) ? await File.ReadAllLinesAsync(excludePath) : Array.Empty(); + Assert.DoesNotContain(lines, l => l.Trim() == "/.claudedo/"); + } +} diff --git a/tests/ClaudeDo.Worker.Tests/Findings/FindingsStoreTests.cs b/tests/ClaudeDo.Worker.Tests/Findings/FindingsStoreTests.cs index 843aa492..f4eaa4f5 100644 --- a/tests/ClaudeDo.Worker.Tests/Findings/FindingsStoreTests.cs +++ b/tests/ClaudeDo.Worker.Tests/Findings/FindingsStoreTests.cs @@ -25,7 +25,7 @@ public sealed class FindingsStoreTests : IDisposable { var store = new FindingsStore(); - var outcome = await store.SaveAsync(_root, Input("conpty-arg-quoting"), CancellationToken.None); + var outcome = await store.SaveAsync(_root, Input("conpty-arg-quoting"), CancellationToken.None, tracked: true); Assert.True(outcome.Created); Assert.Equal("conpty-arg-quoting", outcome.Slug); @@ -44,9 +44,9 @@ public sealed class FindingsStoreTests : IDisposable public async Task SaveAsync_SameSlugOverwritesInsteadOfCreatingASecondFile() { var store = new FindingsStore(); - await store.SaveAsync(_root, Input("dup", "First title"), CancellationToken.None); + await store.SaveAsync(_root, Input("dup", "First title"), CancellationToken.None, tracked: true); - var outcome = await store.SaveAsync(_root, Input("dup", "Second title"), CancellationToken.None); + var outcome = await store.SaveAsync(_root, Input("dup", "Second title"), CancellationToken.None, tracked: true); Assert.False(outcome.Created); Assert.Equal(1, outcome.TotalFindings); @@ -61,8 +61,8 @@ public sealed class FindingsStoreTests : IDisposable public async Task SaveAsync_RebuildsIndexFromDiskSortedBySlug() { var store = new FindingsStore(); - await store.SaveAsync(_root, Input("zulu", "Zulu trap"), CancellationToken.None); - await store.SaveAsync(_root, Input("alpha", "Alpha trap"), CancellationToken.None); + await store.SaveAsync(_root, Input("zulu", "Zulu trap"), CancellationToken.None, tracked: true); + await store.SaveAsync(_root, Input("alpha", "Alpha trap"), CancellationToken.None, tracked: true); var index = await File.ReadAllTextAsync(Path.Combine(_root, ".claudedo", "INDEX.md")); var lines = index.Replace("\r\n", "\n").Split('\n'); @@ -78,11 +78,11 @@ public sealed class FindingsStoreTests : IDisposable public async Task SaveAsync_IndexDropsFindingsDeletedOnDisk() { var store = new FindingsStore(); - await store.SaveAsync(_root, Input("gone", "Gone trap"), CancellationToken.None); - await store.SaveAsync(_root, Input("stays", "Stays trap"), CancellationToken.None); + await store.SaveAsync(_root, Input("gone", "Gone trap"), CancellationToken.None, tracked: true); + await store.SaveAsync(_root, Input("stays", "Stays trap"), CancellationToken.None, tracked: true); File.Delete(Path.Combine(_root, ".claudedo", "traps", "gone.md")); - await store.SaveAsync(_root, Input("stays", "Stays trap"), CancellationToken.None); + await store.SaveAsync(_root, Input("stays", "Stays trap"), CancellationToken.None, tracked: true); var index = await File.ReadAllTextAsync(Path.Combine(_root, ".claudedo", "INDEX.md")); Assert.DoesNotContain("gone", index); @@ -100,7 +100,7 @@ public sealed class FindingsStoreTests : IDisposable var store = new FindingsStore(); await Assert.ThrowsAsync( - () => store.SaveAsync(_root, Input(slug), CancellationToken.None)); + () => store.SaveAsync(_root, Input(slug), CancellationToken.None, tracked: true)); } [Fact] @@ -109,7 +109,7 @@ public sealed class FindingsStoreTests : IDisposable var store = new FindingsStore(); SaveFindingOutcome outcome = null!; for (var i = 0; i < FindingsStore.WarnThreshold; i++) - outcome = await store.SaveAsync(_root, Input($"trap-{i:000}"), CancellationToken.None); + outcome = await store.SaveAsync(_root, Input($"trap-{i:000}"), CancellationToken.None, tracked: true); Assert.Equal(FindingsStore.WarnThreshold, outcome.TotalFindings); Assert.True(outcome.NearCapacity); @@ -120,7 +120,7 @@ public sealed class FindingsStoreTests : IDisposable { var store = new FindingsStore(); - var outcome = await store.SaveAsync(_root, Input("only-one"), CancellationToken.None); + var outcome = await store.SaveAsync(_root, Input("only-one"), CancellationToken.None, tracked: true); Assert.False(outcome.NearCapacity); }