refactor(git): share the info/exclude writer with the findings store
This commit is contained in:
@@ -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<SaveFindingOutcome> SaveAsync(string workingDir, FindingInput input, CancellationToken ct)
|
||||
public async Task<SaveFindingOutcome> 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);
|
||||
|
||||
|
||||
@@ -18,5 +18,5 @@ public sealed record SaveFindingOutcome(
|
||||
|
||||
public interface IFindingsStore
|
||||
{
|
||||
Task<SaveFindingOutcome> SaveAsync(string workingDir, FindingInput input, CancellationToken ct);
|
||||
Task<SaveFindingOutcome> SaveAsync(string workingDir, FindingInput input, CancellationToken ct, bool tracked = false);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace ClaudeDo.Worker.Git;
|
||||
|
||||
/// <summary>
|
||||
/// 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).
|
||||
/// </summary>
|
||||
public static class GitExcludeWriter
|
||||
{
|
||||
/// <summary>Idempotent — an already-present line is left alone.</summary>
|
||||
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<string> RunGitCaptureAsync(string workingDir, IEnumerable<string> 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');
|
||||
}
|
||||
}
|
||||
@@ -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<string> RunGitCaptureAsync(string workingDir, IEnumerable<string> 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');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user