|
1 | 1 | export function isValidGitBranchName(branch: string): boolean { |
2 | | - // Must not be empty |
3 | 2 | if (!branch) return false; |
4 | 3 |
|
5 | | - // Disallowed characters: space, ~, ^, :, ?, *, [, \ |
6 | 4 | if (/[ \~\^:\?\*\[\\]/.test(branch)) return false; |
7 | 5 |
|
8 | | - // Disallow ASCII control characters (0-31) and DEL (127) |
9 | 6 | for (let i = 0; i < branch.length; i++) { |
10 | 7 | const code = branch.charCodeAt(i); |
11 | 8 | if ((code >= 0 && code <= 31) || code === 127) return false; |
12 | 9 | } |
13 | 10 |
|
14 | | - // Cannot start or end with a slash |
15 | 11 | if (branch.startsWith("/") || branch.endsWith("/")) return false; |
16 | | - |
17 | | - // Cannot have consecutive slashes |
18 | 12 | if (branch.includes("//")) return false; |
19 | | - |
20 | | - // Cannot contain '..' |
21 | 13 | if (branch.includes("..")) return false; |
22 | | - |
23 | | - // Cannot contain '@{' |
24 | 14 | if (branch.includes("@{")) return false; |
25 | | - |
26 | | - // Cannot end with '.lock' |
27 | 15 | if (branch.endsWith(".lock")) return false; |
28 | 16 |
|
29 | 17 | return true; |
30 | 18 | } |
31 | 19 |
|
32 | | -export function sanitizeBranchName(ref: string | undefined): string | null { |
| 20 | +export function sanitizeBranchName(ref: string | null | undefined): string | null { |
33 | 21 | if (!ref) return null; |
34 | 22 | if (ref.startsWith("refs/heads/")) return ref.substring("refs/heads/".length); |
35 | 23 | if (ref.startsWith("refs/remotes/")) return ref.substring("refs/remotes/".length); |
36 | 24 | if (ref.startsWith("refs/tags/")) return ref.substring("refs/tags/".length); |
37 | 25 | if (ref.startsWith("refs/pull/")) return ref.substring("refs/pull/".length); |
38 | 26 | if (ref.startsWith("refs/merge/")) return ref.substring("refs/merge/".length); |
39 | 27 | if (ref.startsWith("refs/release/")) return ref.substring("refs/release/".length); |
40 | | - //unknown ref format, so reject |
41 | 28 | if (ref.startsWith("refs/")) return null; |
42 | 29 |
|
43 | 30 | return ref; |
|
0 commit comments