git.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _path;
  6. function _load_path() {
  7. return _path = _interopRequireDefault(require('path'));
  8. }
  9. var _child_process;
  10. function _load_child_process() {
  11. return _child_process = _interopRequireDefault(require('child_process'));
  12. }
  13. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14. function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i]; return arr2; } else { return Array.from(arr); } }
  15. function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } /**
  16. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  17. *
  18. * This source code is licensed under the MIT license found in the
  19. * LICENSE file in the root directory of this source tree.
  20. *
  21. *
  22. */
  23. const findChangedFilesUsingCommand = (() => {
  24. var _ref = _asyncToGenerator(function* (args, cwd) {
  25. return new Promise(function (resolve, reject) {
  26. const child = (_child_process || _load_child_process()).default.spawn('git', args, { cwd });
  27. let stdout = '';
  28. let stderr = '';
  29. child.stdout.on('data', function (data) {
  30. return stdout += data;
  31. });
  32. child.stderr.on('data', function (data) {
  33. return stderr += data;
  34. });
  35. child.on('error', function (e) {
  36. return reject(e);
  37. });
  38. child.on('close', function (code) {
  39. if (code === 0) {
  40. stdout = stdout.trim();
  41. if (stdout === '') {
  42. resolve([]);
  43. } else {
  44. resolve(stdout.split('\n').filter(function (s) {
  45. return s !== '';
  46. }).map(function (changedPath) {
  47. return (_path || _load_path()).default.resolve(cwd, changedPath);
  48. }));
  49. }
  50. } else {
  51. reject(code + ': ' + stderr);
  52. }
  53. });
  54. });
  55. });
  56. return function findChangedFilesUsingCommand(_x, _x2) {
  57. return _ref.apply(this, arguments);
  58. };
  59. })();
  60. const adapter = {
  61. findChangedFiles: (() => {
  62. var _ref2 = _asyncToGenerator(function* (cwd, options) {
  63. const changedSince = options && (options.withAncestor ? 'HEAD^' : options.changedSince);
  64. if (options && options.lastCommit) {
  65. return yield findChangedFilesUsingCommand(['show', '--name-only', '--pretty=%b', 'HEAD'], cwd);
  66. } else if (changedSince) {
  67. const committed = yield findChangedFilesUsingCommand(['log', '--name-only', '--pretty=%b', 'HEAD', `^${changedSince}`], cwd);
  68. const staged = yield findChangedFilesUsingCommand(['diff', '--cached', '--name-only'], cwd);
  69. const unstaged = yield findChangedFilesUsingCommand(['ls-files', '--other', '--modified', '--exclude-standard'], cwd);
  70. return [].concat(_toConsumableArray(committed), _toConsumableArray(staged), _toConsumableArray(unstaged));
  71. } else {
  72. return yield findChangedFilesUsingCommand(['ls-files', '--other', '--modified', '--exclude-standard'], cwd);
  73. }
  74. });
  75. return function findChangedFiles(_x3, _x4) {
  76. return _ref2.apply(this, arguments);
  77. };
  78. })(),
  79. getRoot: (() => {
  80. var _ref3 = _asyncToGenerator(function* (cwd) {
  81. return new Promise(function (resolve) {
  82. try {
  83. let stdout = '';
  84. const options = ['rev-parse', '--show-toplevel'];
  85. const child = (_child_process || _load_child_process()).default.spawn('git', options, { cwd });
  86. child.stdout.on('data', function (data) {
  87. return stdout += data;
  88. });
  89. child.on('error', function () {
  90. return resolve(null);
  91. });
  92. child.on('close', function (code) {
  93. return resolve(code === 0 ? stdout.trim() : null);
  94. });
  95. } catch (e) {
  96. resolve(null);
  97. }
  98. });
  99. });
  100. return function getRoot(_x5) {
  101. return _ref3.apply(this, arguments);
  102. };
  103. })()
  104. };
  105. exports.default = adapter;