| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { describe, expect |
| 3 | |
| 4 | import { isValidRefName, isWellFormedRef |
| 5 | |
| 6 | describe('isValidRefName', () |
| 7 | it('accepts names git accepts, including a slash', |
| 8 | for (const name |
| 9 | expect(isValidRefName('tags |
| 10 | expect(isValidRefName('heads |
| 11 | } |
| 12 | }); |
| 13 | |
| 14 | it('refuses flags, revision syntax, HEAD and empty names', |
| 15 | for (const name |
| 16 | '', |
| 17 | '-all', |
| 18 | '--delete', |
| 19 | 'HEAD', |
| 20 | 'v1..2', |
| 21 | 'v1^{tree}', |
| 22 | 'a b', |
| 23 | 'v1.lock', |
| 24 | 'x~1', |
| 25 | ]) { |
| 26 | expect(isValidRefName('heads |
| 27 | } |
| 28 | }); |
| 29 | }); |
| 30 | |
| 31 | describe('isWellFormedRef', () |
| 32 | const gitAccepts = |
| 33 | try { |
| 34 | execFileSync('git', |
| 35 | return true; |
| 36 | } catch { |
| 37 | return false; |
| 38 | } |
| 39 | }; |
| 40 | |
| 41 | it('agrees with git check-ref-format on every rule', |
| 42 | const corpus = |
| 43 | 'refs/heads/main', |
| 44 | 'refs/heads/feat/a-b_c.1', |
| 45 | 'refs/tags/v1.0.0', |
| 46 | 'refs/pull/1/head', |
| 47 | 'refs/heads/a@b', |
| 48 | 'refs/heads/é', |
| 49 | 'refs/heads/.hidden', |
| 50 | 'refs/heads/a/.b', |
| 51 | 'refs/heads/a.lock', |
| 52 | 'refs/heads/a.lock/b', |
| 53 | 'refs/heads/a..b', |
| 54 | 'refs/heads/a b', |
| 55 | 'refs/heads/a\tb', |
| 56 | 'refs/heads/a\x7fb', |
| 57 | 'refs/heads/a~1', |
| 58 | 'refs/heads/a^', |
| 59 | 'refs/heads/a:b', |
| 60 | 'refs/heads/a?', |
| 61 | 'refs/heads/a*', |
| 62 | 'refs/heads/a[b', |
| 63 | 'refs/heads/a\\b', |
| 64 | 'refs/heads/a@{1}', |
| 65 | 'refs/heads//a', |
| 66 | 'refs/heads/a/', |
| 67 | 'refs/heads/a.', |
| 68 | '/refs/heads/a', |
| 69 | 'refs/heads/a.b.', |
| 70 | 'refs/heads/@', |
| 71 | 'refs/heads/a/./b', |
| 72 | 'refs/heads/-dash', |
| 73 | ]; |
| 74 | for (const ref |
| 75 | expect([ref, isWellFormedRef |
| 76 | } |
| 77 | }); |
| 78 | |
| 79 | it('only accepts refs under refs/', |
| 80 | for (const ref |
| 81 | expect(isWellFormedRef(ref |
| 82 | } |
| 83 | }); |
| 84 | }); |