commitMessageFileScope.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { isAbsolute, relative } from "node:path";
  2. import type { GitFileChange } from "@zcode/shared";
  3. import { normalizeGitPath, normalizeWorkspaceInRepoPath } from "./config.js";
  4. function normalizeCommitMessageScopePath(path: string): string {
  5. return normalizeGitPath(path.trim())
  6. .replace(/^\.?\//, "")
  7. .replace(/\/+$/, "");
  8. }
  9. function isRelativePathInsideScope(path: string): boolean {
  10. const normalizedPath = normalizeGitPath(path);
  11. return (
  12. normalizedPath.length > 0 &&
  13. normalizedPath !== ".." &&
  14. !normalizedPath.startsWith("../") &&
  15. !isAbsolute(normalizedPath)
  16. );
  17. }
  18. function addCommitMessageScopeCandidate(scope: Set<string>, path: string): void {
  19. const normalizedPath = normalizeCommitMessageScopePath(path);
  20. if (!normalizedPath) {
  21. return;
  22. }
  23. scope.add(normalizedPath);
  24. }
  25. function buildCommitMessageFileScope(params: {
  26. workspacePath: string;
  27. repoRoot: string;
  28. workspaceInRepoPath: string;
  29. currentSessionFilePaths?: readonly string[];
  30. }): Set<string> | null {
  31. const sourcePaths = params.currentSessionFilePaths
  32. ?.map((path) => path.trim())
  33. .filter((path) => path.length > 0);
  34. if (!sourcePaths || sourcePaths.length === 0) {
  35. return null;
  36. }
  37. const scope = new Set<string>();
  38. const normalizedWorkspaceInRepoPath = normalizeWorkspaceInRepoPath(params.workspaceInRepoPath);
  39. for (const path of sourcePaths) {
  40. addCommitMessageScopeCandidate(scope, path);
  41. if (isAbsolute(path)) {
  42. const repoRelativePath = relative(params.repoRoot, path);
  43. if (isRelativePathInsideScope(repoRelativePath)) {
  44. addCommitMessageScopeCandidate(scope, repoRelativePath);
  45. }
  46. const workspaceRelativePath = relative(params.workspacePath, path);
  47. if (isRelativePathInsideScope(workspaceRelativePath)) {
  48. addCommitMessageScopeCandidate(scope, workspaceRelativePath);
  49. }
  50. continue;
  51. }
  52. const normalizedPath = normalizeCommitMessageScopePath(path);
  53. if (
  54. normalizedWorkspaceInRepoPath !== "." &&
  55. !normalizedPath.startsWith(`${normalizedWorkspaceInRepoPath}/`)
  56. ) {
  57. addCommitMessageScopeCandidate(scope, `${normalizedWorkspaceInRepoPath}/${normalizedPath}`);
  58. }
  59. }
  60. return scope.size > 0 ? scope : null;
  61. }
  62. function isCommitMessageFileInScope(file: GitFileChange, scope: Set<string> | null): boolean {
  63. if (!scope) {
  64. return true;
  65. }
  66. return [file.path, file.repoRelativePath, file.workspaceRelativePath].some((path) =>
  67. scope.has(normalizeCommitMessageScopePath(path)),
  68. );
  69. }
  70. export function filterCommitMessageFilesByCurrentSession(params: {
  71. files: readonly GitFileChange[];
  72. workspacePath: string;
  73. repoRoot: string;
  74. workspaceInRepoPath: string;
  75. currentSessionFilePaths?: readonly string[];
  76. }): GitFileChange[] {
  77. const scope = buildCommitMessageFileScope({
  78. workspacePath: params.workspacePath,
  79. repoRoot: params.repoRoot,
  80. workspaceInRepoPath: params.workspaceInRepoPath,
  81. currentSessionFilePaths: params.currentSessionFilePaths,
  82. });
  83. return params.files.filter((file) => isCommitMessageFileInScope(file, scope));
  84. }