-
Notifications
You must be signed in to change notification settings - Fork 0
fix: normalize branch names in CLI and UI #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,16 @@ | ||||||||||||||||
| /** | ||||||||||||||||
| * Sanitize a git branch name: normalize accents (ç→c, á→a), spaces/underscores → | ||||||||||||||||
| * hyphens, remove invalid characters. Safe on Windows and macOS (Unicode NFD). | ||||||||||||||||
| */ | ||||||||||||||||
| export function sanitizeBranchName(input: string): string { | ||||||||||||||||
| return (input || "") | ||||||||||||||||
| .normalize("NFD") | ||||||||||||||||
| .replace(/[\u0300-\u036f]/g, "") | ||||||||||||||||
| .replace(/[_\s]+/g, "-") | ||||||||||||||||
| .replace(/[^a-zA-Z0-9\-/.]+/g, "") | ||||||||||||||||
| .replace(/-+/g, "-") | ||||||||||||||||
| .replace(/\/+/g, "/") | ||||||||||||||||
| .replace(/\.\.+/g, ".") | ||||||||||||||||
| .replace(/^-+|-+$/g, "") | ||||||||||||||||
|
greptile-apps[bot] marked this conversation as resolved.
|
||||||||||||||||
| .replace(/^\/+|\/+$/g, ""); | ||||||||||||||||
| } | ||||||||||||||||
|
Comment on lines
+14
to
+16
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: lib/branch-name.ts
Line: 14-16
Comment:
Leading and trailing dots survive sanitization, and git's `check-ref-format` unconditionally rejects any ref whose name starts or ends with `.`. For example, the input `.feature` passes every existing replace step unchanged and produces an invalid branch name. The same applies to `feature.` (e.g., from a user typing `feature.` or from the removal of a trailing non-alphanum that happened to follow a dot). Adding a dot-trim step after the existing `-` and `/` trims closes the gap.
```suggestion
.replace(/^-+|-+$/g, "")
.replace(/^\/+|\/+$/g, "")
.replace(/^\.+|\.+$/g, "");
}
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import { describe, it } from "node:test"; | ||
| import assert from "node:assert/strict"; | ||
| import { sanitizeBranchName } from "../lib/branch-name"; | ||
|
|
||
| describe("sanitizeBranchName", () => { | ||
| it("converts spaces and underscores to hyphens", () => { | ||
| assert.equal(sanitizeBranchName("feature login"), "feature-login"); | ||
| assert.equal(sanitizeBranchName("feature_login"), "feature-login"); | ||
| assert.equal(sanitizeBranchName("a b c"), "a-b-c"); | ||
| }); | ||
|
|
||
| it("normalizes accented characters", () => { | ||
| assert.equal(sanitizeBranchName("ação rápida"), "acao-rapida"); | ||
| assert.equal(sanitizeBranchName("configuração"), "configuracao"); | ||
| assert.equal(sanitizeBranchName("résumé"), "resume"); | ||
| assert.equal(sanitizeBranchName("niño"), "nino"); | ||
| }); | ||
|
|
||
| it("removes special characters", () => { | ||
| assert.equal(sanitizeBranchName("feature@login!"), "featurelogin"); | ||
| assert.equal(sanitizeBranchName("fix#123"), "fix123"); | ||
| assert.equal(sanitizeBranchName("test (wip)"), "test-wip"); | ||
| }); | ||
|
|
||
| it("keeps slashes and dots for hierarchical or versioned branches", () => { | ||
| assert.equal(sanitizeBranchName("feature/login"), "feature/login"); | ||
| assert.equal(sanitizeBranchName("release/1.0.0"), "release/1.0.0"); | ||
| }); | ||
|
|
||
| it("collapses repeated hyphens, slashes, and dots", () => { | ||
| assert.equal(sanitizeBranchName("feature--login"), "feature-login"); | ||
| assert.equal(sanitizeBranchName("feature//login"), "feature/login"); | ||
| assert.equal(sanitizeBranchName("feature..main"), "feature.main"); | ||
| assert.equal(sanitizeBranchName("a...b"), "a.b"); | ||
| }); | ||
|
|
||
| it("trims leading and trailing separators", () => { | ||
| assert.equal(sanitizeBranchName("-feature-"), "feature"); | ||
| assert.equal(sanitizeBranchName("/feature/"), "feature"); | ||
| assert.equal(sanitizeBranchName(" feature "), "feature"); | ||
| }); | ||
|
|
||
| it("returns empty for invalid-only input", () => { | ||
| assert.equal(sanitizeBranchName(""), ""); | ||
| assert.equal(sanitizeBranchName(" "), ""); | ||
| assert.equal(sanitizeBranchName("@#$"), ""); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.