merge-translation-pairing.ts 2.1 KB

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