Skip to content

[code-simplifier] refactor: extract shared sanitizeForFilename helper in generate_git_patch.cjs#19980

Merged
pelikhan merged 1 commit intomainfrom
code-simplifier/2026-03-07-8821f9c9ec826462
Mar 7, 2026
Merged

[code-simplifier] refactor: extract shared sanitizeForFilename helper in generate_git_patch.cjs#19980
pelikhan merged 1 commit intomainfrom
code-simplifier/2026-03-07-8821f9c9ec826462

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Mar 7, 2026

Summary

Simplification of recently modified generate_git_patch.cjs (changed in #19963 which merged 2026-03-07).

Files Simplified

  • actions/setup/js/generate_git_patch.cjs — extracted duplicate sanitization logic and fixed string concatenation

Improvements Made

1. Eliminated Duplicate Sanitization Logic

sanitizeBranchNameForPatch and sanitizeRepoSlugForPatch shared an identical four-step .replace() chain, differing only in their fallback value for empty/null input. Extracted into a private sanitizeForFilename(value, fallback) helper:

// Before: two functions with identical replace chains
function sanitizeBranchNameForPatch(branchName) {
  if (!branchName) return "unknown";
  return branchName.replace(...).replace(...).replace(...).toLowerCase();
}
function sanitizeRepoSlugForPatch(repoSlug) {
  if (!repoSlug) return "";
  return repoSlug.replace(...).replace(...).replace(...).toLowerCase();
}

// After: single shared implementation
function sanitizeForFilename(value, fallback) {
  if (!value) return fallback;
  return value.replace(...).replace(...).replace(...).toLowerCase();
}
function sanitizeBranchNameForPatch(branchName) { return sanitizeForFilename(branchName, "unknown"); }
function sanitizeRepoSlugForPatch(repoSlug)     { return sanitizeForFilename(repoSlug, ""); }

2. Fixed String Concatenation

Replaced a three-fragment string concatenation with a single template literal:

// Before
errorMessage = `Cannot generate incremental patch: failed to fetch origin/\$\{branchName}. ` +
  `This typically happens when...` +
  `Error: \$\{getErrorMessage(fetchError)}`;

// After
errorMessage = `Cannot generate incremental patch: failed to fetch origin/\$\{branchName}. This typically happens when... Error: \$\{getErrorMessage(fetchError)}`;

Changes Based On

Recent changes from #19963 - fix: pass git auth via environment variables instead of writing to .git/config

Testing

  • ✅ Manual smoke test — all sanitizeBranchNameForPatch / sanitizeRepoSlugForPatch assertions pass
  • ✅ Linting passes (make lint-cjs)
  • ✅ Formatting passes (make fmt-cjs)
  • ✅ No functional changes — exported API and behavior are identical

Review Focus

Please verify:

  • Exported function signatures and behavior remain unchanged
  • sanitizeForFilename is not exported (it's a private implementation detail)
  • No unintended side effects

References: §22805044981

Generated by Code Simplifier ·

  • expires on Mar 8, 2026, 6:56 PM UTC

…ncatenation

- Extract duplicate sanitization logic from sanitizeBranchNameForPatch
  and sanitizeRepoSlugForPatch into a shared private sanitizeForFilename
  helper that accepts a fallback parameter
- Fix multi-part string concatenation on error message to use a single
  template literal instead of three concatenated fragments

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant