git.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. const path = require('path')
  2. const fs = require('fs-extra')
  3. const parseDiff = require('../util/parse-diff')
  4. // Connectors
  5. const cwd = require('./cwd')
  6. // Utils
  7. const { execa, hasProjectGit } = require('@vue/cli-shared-utils')
  8. async function getNewFiles (context) {
  9. if (!hasProjectGit(cwd.get())) return []
  10. const { stdout } = await execa('git', [
  11. 'ls-files',
  12. '-o',
  13. '--exclude-standard',
  14. '--full-name'
  15. ], {
  16. cwd: cwd.get()
  17. })
  18. if (stdout.trim()) {
  19. return stdout.split(/\r?\n/g)
  20. }
  21. return []
  22. }
  23. async function getDiffs (context) {
  24. if (!hasProjectGit(cwd.get())) return []
  25. const { highlightCode } = require('../util/highlight')
  26. const newFiles = await getNewFiles(context)
  27. await execa('git', ['add', '-N', '*'], {
  28. cwd: cwd.get()
  29. })
  30. const { stdout } = await execa('git', ['diff'], {
  31. cwd: cwd.get()
  32. })
  33. await reset(context)
  34. const list = parseDiff(stdout)
  35. for (const n in list) {
  36. const fileDiff = list[n]
  37. const isNew = newFiles.includes(fileDiff.to)
  38. let fromContent
  39. if (!isNew) {
  40. const result = await execa('git', ['show', `HEAD:${fileDiff.from}`], {
  41. cwd: cwd.get()
  42. })
  43. fromContent = result.stdout
  44. }
  45. const highlightedContentFrom = fromContent && highlightCode(fileDiff.from, fromContent).split('\n')
  46. const highlightedContentTo = highlightCode(fileDiff.to, fs.readFileSync(path.resolve(cwd.get(), fileDiff.to), { encoding: 'utf8' })).split('\n')
  47. for (const chunk of fileDiff.chunks) {
  48. for (const change of chunk.changes) {
  49. const firstChar = change.content.charAt(0)
  50. let highlightedCode
  51. if (change.normal) {
  52. highlightedCode = highlightedContentFrom[change.ln1 - 1]
  53. } else if (change.type === 'del') {
  54. highlightedCode = highlightedContentFrom[change.ln - 1]
  55. } else if (change.type === 'add') {
  56. highlightedCode = highlightedContentTo[change.ln - 1]
  57. }
  58. if (highlightedCode) {
  59. change.content = firstChar + highlightedCode
  60. }
  61. }
  62. }
  63. list[n] = {
  64. id: fileDiff.index.join(' '),
  65. ...fileDiff,
  66. new: isNew
  67. }
  68. }
  69. return list
  70. }
  71. async function commit (message, context) {
  72. if (!hasProjectGit(cwd.get())) return false
  73. await execa('git', ['add', '*'], {
  74. cwd: cwd.get()
  75. })
  76. await execa('git', ['commit', '-m', message.replace(/"/, '\\"')], {
  77. cwd: cwd.get()
  78. })
  79. return true
  80. }
  81. async function reset (context) {
  82. if (!hasProjectGit(cwd.get())) return false
  83. await execa('git', ['reset'], {
  84. cwd: cwd.get()
  85. })
  86. return true
  87. }
  88. async function getRoot (context) {
  89. if (!hasProjectGit(cwd.get())) return cwd.get()
  90. const { stdout } = await execa('git', [
  91. 'rev-parse',
  92. '--show-toplevel'
  93. ], {
  94. cwd: cwd.get()
  95. })
  96. return stdout
  97. }
  98. async function resolveFile (file, context) {
  99. const root = await getRoot(context)
  100. return path.resolve(root, file)
  101. }
  102. module.exports = {
  103. getDiffs,
  104. commit,
  105. reset,
  106. getRoot,
  107. resolveFile
  108. }