|
| 1 | +const GIT_REV_PARSE = 'rev-parse'; |
| 2 | +const GIT_REF_NOT_FOUND_STATUS = 128; |
| 3 | + |
| 4 | +const MISSING_REF_MARKERS = Object.freeze({ |
| 5 | + ambiguousArgument: 'ambiguous argument', |
| 6 | + neededSingleRevision: 'needed a single revision', |
| 7 | + unknownRevision: 'unknown revision', |
| 8 | +}); |
| 9 | + |
| 10 | +/** |
| 11 | + * @param {unknown} err |
| 12 | + * @param {string} ref |
| 13 | + * @returns {boolean} |
| 14 | + */ |
| 15 | +export function isGitMissingRefError(err, ref) { |
| 16 | + return isStdoutOnlyRevParseMiss(errorDetails(err), ref) || |
| 17 | + isGitMissingRefMessage(errorDetailsText(err), ref); |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * @param {unknown} err |
| 22 | + * @returns {string} |
| 23 | + */ |
| 24 | +export function errorDetailsText(err) { |
| 25 | + if (!(err instanceof Error)) { |
| 26 | + return String(err); |
| 27 | + } |
| 28 | + const details = errorDetails(err); |
| 29 | + return [ |
| 30 | + err.message, |
| 31 | + typeof details.stderr === 'string' ? details.stderr : '', |
| 32 | + typeof details.stdout === 'string' ? details.stdout : '', |
| 33 | + ].join('\n'); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @param {unknown} err |
| 38 | + * @returns {Record<string, unknown>} |
| 39 | + */ |
| 40 | +function errorDetails(err) { |
| 41 | + return err instanceof Error && typeof err.details === 'object' && err.details |
| 42 | + ? err.details |
| 43 | + : {}; |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * @param {Record<string, unknown>} details |
| 48 | + * @param {string} ref |
| 49 | + * @returns {boolean} |
| 50 | + */ |
| 51 | +function isStdoutOnlyRevParseMiss(details, ref) { |
| 52 | + // Some plumbing runners surface a stdout-only `rev-parse <ref>` miss: Git |
| 53 | + // exits 128 and echoes the unresolved ref without emitting locale text. |
| 54 | + return details.code === GIT_REF_NOT_FOUND_STATUS && |
| 55 | + Array.isArray(details.args) && |
| 56 | + details.args[0] === GIT_REV_PARSE && |
| 57 | + details.args.at(-1) === ref && |
| 58 | + typeof details.stdout === 'string' && |
| 59 | + details.stdout.trim() === ref && |
| 60 | + `${details.stderr ?? ''}`.trim() === ''; |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * @param {string} message |
| 65 | + * @param {string} ref |
| 66 | + * @returns {boolean} |
| 67 | + */ |
| 68 | +function isGitMissingRefMessage(message, ref) { |
| 69 | + const normalized = message.toLowerCase(); |
| 70 | + const normalizedRef = ref.toLowerCase(); |
| 71 | + if (!normalized.includes(normalizedRef)) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + // C/English-locale missing-ref fallback: normal adapters should return |
| 75 | + // GIT_REF_NOT_FOUND. This best-effort fallback is only for third-party ports |
| 76 | + // that expose Git stderr without a structured code. |
| 77 | + return ( |
| 78 | + normalized.includes(MISSING_REF_MARKERS.neededSingleRevision) || |
| 79 | + ( |
| 80 | + normalized.includes(MISSING_REF_MARKERS.ambiguousArgument) && |
| 81 | + normalized.includes(MISSING_REF_MARKERS.unknownRevision) |
| 82 | + ) |
| 83 | + ); |
| 84 | +} |
0 commit comments