From 5bb2c3d97ab95065215ff94215de5ab9a760da3c Mon Sep 17 00:00:00 2001 From: dajiaohuang Date: Sun, 4 Oct 2026 10:17:44 +0800 Subject: [PATCH] Match CLI descriptors by path boundary --- src/CLI/RodelCommit/CommitService.cs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/CLI/RodelCommit/CommitService.cs b/src/CLI/RodelCommit/CommitService.cs index 42a329bd..0d722371 100644 --- a/src/CLI/RodelCommit/CommitService.cs +++ b/src/CLI/RodelCommit/CommitService.cs @@ -452,7 +452,9 @@ public static string RemoveMarkdownCodeBlockDelimiters(string input) return default; } - var currentDirectory = Environment.CurrentDirectory; + var currentDirectory = Path.GetFullPath(Environment.CurrentDirectory); + (string? name, string? desc) bestMatch = default; + var bestMatchLength = -1; foreach (var file in files) { var lines = await File.ReadAllLinesAsync(file); @@ -462,14 +464,22 @@ public static string RemoveMarkdownCodeBlockDelimiters(string input) continue; } - if (currentDirectory.Contains(firstLine!) || firstLine!.Contains(currentDirectory)) + var descriptorDirectory = Path.GetFullPath(firstLine); + var relativePath = Path.GetRelativePath(descriptorDirectory, currentDirectory); + var isSameOrChildDirectory = relativePath == "." + || (!Path.IsPathRooted(relativePath) + && relativePath != ".." + && !relativePath.StartsWith($"..{Path.DirectorySeparatorChar}", StringComparison.Ordinal) + && !relativePath.StartsWith($"..{Path.AltDirectorySeparatorChar}", StringComparison.Ordinal)); + if (isSameOrChildDirectory && descriptorDirectory.Length > bestMatchLength) { var otherContent = string.Join('\n', lines.Skip(1).Where(p => !string.IsNullOrWhiteSpace(p)).ToList()); - return (Path.GetFileNameWithoutExtension(file), otherContent.Trim()); + bestMatch = (Path.GetFileNameWithoutExtension(file), otherContent.Trim()); + bestMatchLength = descriptorDirectory.Length; } } - return default; + return bestMatch; } private static async Task GetDiffAsync()