fix: normalize branch names in CLI and UI#6
Conversation
|
| Filename | Overview |
|---|---|
| lib/branch-name.ts | New utility: handles accent normalization, special char removal, and .. collapse correctly, but does not strip leading/trailing dots — git rejects such refs. |
| GIT Command Generator.html | Updated normalizeBranch to mirror lib/branch-name.ts, including the .. collapse, but shares the same missing leading/trailing dot trim. |
| scripts/cli.ts | Adds resolveBranchName wrapper for validation/logging; uses it for branch, merge, and switch commands. Correctness of results depends on the underlying sanitizeBranchName. |
| app/HomeClient.tsx | Replaces inline normalizeBranch with the shared sanitizeBranchName import — straightforward refactor with no logic change. |
| test/branch-name.test.ts | Good coverage of accent normalization, special chars, slash/dot collapsing, and empty input. Missing test cases for leading/trailing standalone dots (e.g., .feature, feature.). |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Raw user input] --> B["normalize NFD"]
B --> C["remove combining marks"]
C --> D["spaces/underscores to hyphen"]
D --> E["remove invalid chars"]
E --> F["collapse hyphens"]
F --> G["collapse slashes"]
G --> H["collapse dots"]
H --> I["strip leading/trailing hyphens"]
I --> J["strip leading/trailing slashes"]
J --> K{"leading/trailing dot?"}
K -- "not stripped - bug" --> L["Output starts/ends with dot"]
K -- "after fix" --> M["Clean branch name"]
L --> N["git fails: fatal invalid reference"]
M --> O["git checkout succeeds"]
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
A[Raw user input] --> B["normalize NFD"]
B --> C["remove combining marks"]
C --> D["spaces/underscores to hyphen"]
D --> E["remove invalid chars"]
E --> F["collapse hyphens"]
F --> G["collapse slashes"]
G --> H["collapse dots"]
H --> I["strip leading/trailing hyphens"]
I --> J["strip leading/trailing slashes"]
J --> K{"leading/trailing dot?"}
K -- "not stripped - bug" --> L["Output starts/ends with dot"]
K -- "after fix" --> M["Clean branch name"]
L --> N["git fails: fatal invalid reference"]
M --> O["git checkout succeeds"]
Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
lib/branch-name.ts:14-16
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, "");
}
```
Reviews (3): Last reviewed commit: "fix: handle multiple dots in path normal..." | Re-trigger Greptile
| .replace(/^-+|-+$/g, "") | ||
| .replace(/^\/+|\/+$/g, ""); | ||
| } No newline at end of file |
There was a problem hiding this 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.
| .replace(/^-+|-+$/g, "") | |
| .replace(/^\/+|\/+$/g, ""); | |
| } | |
| .replace(/^-+|-+$/g, "") | |
| .replace(/^\/+|\/+$/g, "") | |
| .replace(/^\.+|\.+$/g, ""); | |
| } |
Prompt To Fix With AI
This 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.
Summary
checkoutcommand to reflect newckaliassanitizeBranchNameutility to handle complex branch name transformationssanitizeBranchNameto ensure correctnessTest plan
npm testto execute the test suite forsanitizeBranchNameckalias in the CLI by switching to various branches, including those with special characters or non-standard namingckalias correctly resolves to the sanitized branch name