merge-translation-pairing.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /** Git merge-driver and explicit conflict-resolver entrypoint for pairing records. */
  2. import { execFileSync } from 'node:child_process'
  3. import { readFileSync, writeFileSync } from 'node:fs'
  4. import {
  5. mergeTranslationPairingRecords,
  6. repositoryTranslationPairSource,
  7. resolveTranslationPairingConflicts,
  8. } from './translation-pairing-merge.ts'
  9. const args = process.argv.slice(2)
  10. try {
  11. if (args[0] === '--probe') {
  12. if (args.length !== 1) throw new Error('--probe takes no other arguments')
  13. } else {
  14. const root = execFileSync('git', ['rev-parse', '--show-toplevel'], { encoding: 'utf8' }).trim()
  15. if (args[0] === '--resolve') {
  16. if (args.length !== 1) throw new Error('--resolve takes no paths; it inspects the unmerged index')
  17. const resolved = resolveTranslationPairingConflicts(root, repositoryTranslationPairSource(root))
  18. if (resolved.length === 0) {
  19. console.log('merge-translation-pairing: no unresolved pairing records')
  20. } else {
  21. for (const path of resolved) console.log(`merge-translation-pairing: resolved ${path}`)
  22. }
  23. } else {
  24. if (args.length !== 4) {
  25. throw new Error('merge-driver mode requires <ancestor> <current> <other> <repository-path>')
  26. }
  27. const [ancestorPath, currentPath, otherPath, metaPath] = args
  28. if (ancestorPath === undefined || currentPath === undefined || otherPath === undefined || metaPath === undefined) {
  29. throw new Error('merge-driver arguments are incomplete')
  30. }
  31. const result = mergeTranslationPairingRecords(
  32. root,
  33. metaPath,
  34. readFileSync(ancestorPath, 'utf8'),
  35. readFileSync(currentPath, 'utf8'),
  36. readFileSync(otherPath, 'utf8'),
  37. repositoryTranslationPairSource(root),
  38. )
  39. writeFileSync(currentPath, result.record)
  40. }
  41. }
  42. } catch (error) {
  43. console.error(`merge-translation-pairing: ${error instanceof Error ? error.message : String(error)}`)
  44. console.error(
  45. 'merge-translation-pairing: resolve owner conflicts, then confirm the pair with '
  46. + '`pnpm run verify-translation-pairing --write <pair>`; rerun '
  47. + '`pnpm run resolve-translation-pairing-conflicts` for other safe records',
  48. )
  49. process.exitCode = 1
  50. }