git.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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) {
  14. return obj && obj.__esModule ? obj : {default: obj};
  15. }
  16. function _toConsumableArray(arr) {
  17. if (Array.isArray(arr)) {
  18. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++)
  19. arr2[i] = arr[i];
  20. return arr2;
  21. } else {
  22. return Array.from(arr);
  23. }
  24. }
  25. function _asyncToGenerator(fn) {
  26. return function() {
  27. var gen = fn.apply(this, arguments);
  28. return new Promise(function(resolve, reject) {
  29. function step(key, arg) {
  30. try {
  31. var info = gen[key](arg);
  32. var value = info.value;
  33. } catch (error) {
  34. reject(error);
  35. return;
  36. }
  37. if (info.done) {
  38. resolve(value);
  39. } else {
  40. return Promise.resolve(value).then(
  41. function(value) {
  42. step('next', value);
  43. },
  44. function(err) {
  45. step('throw', err);
  46. }
  47. );
  48. }
  49. }
  50. return step('next');
  51. });
  52. };
  53. }
  54. /**
  55. * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
  56. *
  57. * This source code is licensed under the MIT license found in the
  58. * LICENSE file in the root directory of this source tree.
  59. *
  60. *
  61. */
  62. const findChangedFilesUsingCommand = (() => {
  63. var _ref = _asyncToGenerator(function*(args, cwd) {
  64. return new Promise(function(resolve, reject) {
  65. const child = (_child_process || _load_child_process()).default.spawn(
  66. 'git',
  67. args,
  68. {cwd: cwd}
  69. );
  70. let stdout = '';
  71. let stderr = '';
  72. child.stdout.on('data', function(data) {
  73. return (stdout += data);
  74. });
  75. child.stderr.on('data', function(data) {
  76. return (stderr += data);
  77. });
  78. child.on('error', function(e) {
  79. return reject(e);
  80. });
  81. child.on('close', function(code) {
  82. if (code === 0) {
  83. stdout = stdout.trim();
  84. if (stdout === '') {
  85. resolve([]);
  86. } else {
  87. resolve(
  88. stdout
  89. .split('\n')
  90. .filter(function(s) {
  91. return s !== '';
  92. })
  93. .map(function(changedPath) {
  94. return (_path || _load_path()).default.resolve(
  95. cwd,
  96. changedPath
  97. );
  98. })
  99. );
  100. }
  101. } else {
  102. reject(code + ': ' + stderr);
  103. }
  104. });
  105. });
  106. });
  107. return function findChangedFilesUsingCommand(_x, _x2) {
  108. return _ref.apply(this, arguments);
  109. };
  110. })();
  111. const adapter = {
  112. findChangedFiles: (() => {
  113. var _ref2 = _asyncToGenerator(function*(cwd, options) {
  114. const changedSince =
  115. options && (options.withAncestor ? 'HEAD^' : options.changedSince);
  116. const includePaths = (options && options.includePaths) || [];
  117. if (options && options.lastCommit) {
  118. return yield findChangedFilesUsingCommand(
  119. ['show', '--name-only', '--pretty=%b', 'HEAD'].concat(includePaths),
  120. cwd
  121. );
  122. } else if (changedSince) {
  123. const committed = yield findChangedFilesUsingCommand(
  124. [
  125. 'log',
  126. '--name-only',
  127. '--pretty=%b',
  128. 'HEAD',
  129. `^${changedSince}`
  130. ].concat(includePaths),
  131. cwd
  132. );
  133. const staged = yield findChangedFilesUsingCommand(
  134. ['diff', '--cached', '--name-only'].concat(includePaths),
  135. cwd
  136. );
  137. const unstaged = yield findChangedFilesUsingCommand(
  138. ['ls-files', '--other', '--modified', '--exclude-standard'].concat(
  139. includePaths
  140. ),
  141. cwd
  142. );
  143. return [].concat(
  144. _toConsumableArray(committed),
  145. _toConsumableArray(staged),
  146. _toConsumableArray(unstaged)
  147. );
  148. } else {
  149. return yield findChangedFilesUsingCommand(
  150. ['ls-files', '--other', '--modified', '--exclude-standard'].concat(
  151. includePaths
  152. ),
  153. cwd
  154. );
  155. }
  156. });
  157. return function findChangedFiles(_x3, _x4) {
  158. return _ref2.apply(this, arguments);
  159. };
  160. })(),
  161. getRoot: (() => {
  162. var _ref3 = _asyncToGenerator(function*(cwd) {
  163. return new Promise(function(resolve) {
  164. try {
  165. let stdout = '';
  166. const options = ['rev-parse', '--show-toplevel'];
  167. const child = (_child_process || _load_child_process()).default.spawn(
  168. 'git',
  169. options,
  170. {cwd: cwd}
  171. );
  172. child.stdout.on('data', function(data) {
  173. return (stdout += data);
  174. });
  175. child.on('error', function() {
  176. return resolve(null);
  177. });
  178. child.on('close', function(code) {
  179. return resolve(code === 0 ? stdout.trim() : null);
  180. });
  181. } catch (e) {
  182. resolve(null);
  183. }
  184. });
  185. });
  186. return function getRoot(_x5) {
  187. return _ref3.apply(this, arguments);
  188. };
  189. })()
  190. };
  191. exports.default = adapter;