Ben Rohlfs | 976ec7e | 2022-10-27 09:02:22 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @license |
| 3 | * Copyright 2022 Google LLC |
| 4 | * SPDX-License-Identifier: Apache-2.0 |
| 5 | */ |
| 6 | |
| 7 | /** See also Patch.java for the backend equivalent. */ |
| 8 | export enum FileMode { |
| 9 | /** Mode indicating an entry is a symbolic link. */ |
| 10 | SYMLINK = 0o120000, |
| 11 | |
| 12 | /** Mode indicating an entry is a non-executable file. */ |
| 13 | REGULAR_FILE = 0o100644, |
| 14 | |
| 15 | /** Mode indicating an entry is an executable file. */ |
| 16 | EXECUTABLE_FILE = 0o100755, |
| 17 | |
| 18 | /** Mode indicating an entry is a submodule commit in another repository. */ |
| 19 | GITLINK = 0o160000, |
| 20 | } |
| 21 | |
| 22 | export function fileModeToString(mode?: number, includeNumber = true): string { |
| 23 | const str = fileModeStr(mode); |
| 24 | const num = mode?.toString(8); |
| 25 | return `${str}${includeNumber && str ? ` (${num})` : ''}`; |
| 26 | } |
| 27 | |
| 28 | function fileModeStr(mode?: number): string { |
| 29 | if (mode === FileMode.SYMLINK) return 'symlink'; |
| 30 | if (mode === FileMode.REGULAR_FILE) return 'regular'; |
| 31 | if (mode === FileMode.EXECUTABLE_FILE) return 'executable'; |
| 32 | if (mode === FileMode.GITLINK) return 'gitlink'; |
| 33 | return ''; |
| 34 | } |
| 35 | |
| 36 | export function expandFileMode(input?: string) { |
| 37 | if (!input) return input; |
| 38 | for (const modeNum of Object.values(FileMode) as FileMode[]) { |
| 39 | const modeStr = modeNum?.toString(8); |
| 40 | if (input.includes(modeStr)) { |
| 41 | return input.replace(modeStr, `${fileModeToString(modeNum)}`); |
| 42 | } |
| 43 | } |
| 44 | return input; |
| 45 | } |