Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. const node = {
  2. execSync: require('child_process').execSync,
  3. };
  4. class GitCommand {
  5. constructor ({cwd, sync = true}) {
  6. this.cache = {
  7. cwd,
  8. }
  9.  
  10. }
  11.  
  12. get cwd () {
  13. return this.cache.cwd;
  14. }
  15.  
  16. get branchName () {
  17. return this.execResult('git rev-parse --abbrev-ref HEAD');
  18. }
  19.  
  20. execResult(cmd, callback) {
  21. return node.execSync(cmd, {cwd: this.cwd}).toString();
  22. }
  23.  
  24. exec (cmd, callback) {
  25. let result = node.execSync(cmd, {cwd: this.cwd});
  26. callback && callback(result.toString());
  27. return this;
  28.  
  29. // return new Promise((resolve, reject) => {
  30. // node.exec(cmd, {cwd: this.cwd}, (error, stdout, stderr) => {
  31. // if (error) {
  32. // reject(error.toString());
  33. // } else {
  34. // resolve(stdout.toString(), stderr.toString());
  35. // }
  36. // });
  37. // });
  38. }
  39.  
  40. checkout (name, callback) {
  41. return this.exec(`git checkout ${name}`, callback);
  42. }
  43.  
  44. hash (name, callback) {
  45. return this.exec(`git rev-parse ${name}`, callback);
  46. }
  47.  
  48. merge (name, callback) {
  49. return this.exec(`git merge ${name}`, callback);
  50. }
  51.  
  52. push (callback = null) {
  53. let name = `origin ${this.branchName}`;
  54. return this.exec(`git push ${name}`, callback);
  55. }
  56.  
  57. listSHA () {
  58. return this.execResult(`git log --pretty=oneline`);
  59. }
  60.  
  61. diff (a, b) {
  62. return this.execResult(`git diff --name-status ${a} ${b}`);
  63. }
  64.  
  65. exportDiff({baseSHA, newSHA, path, exceStatus = null}) {
  66. this.checkout(newSHA, () => {
  67. let hasExce = Array.isArray(exceStatus);
  68. let files = this.diff(baseSHA, newSHA).split('\n')
  69. .filter(s => s)
  70. .map(s => ({
  71. status: s.substr(0, 1),
  72. path: s.substr(1).trim(),
  73. }))
  74. .filter(o => (! hasExce) || (exceStatus.indexOf(o.status) >= 0))
  75. .map(o => `"${o.path}"`)
  76. .join(' ');
  77. let result = this.execResult(`git archive --format zip -o "D:/test/${newSHA}.zip" ${newSHA} ${files}`);
  78. this.execResult(`compact /u D:/test/${newSHA}.zip`);
  79. console.info(result);
  80. });
  81. }
  82. test (sha) {
  83. this.execResult(`git archive --format zip -o "D:/test/file.zip" ${sha} "pub/6411.php"`);
  84. }
  85. }
  86.  
  87. module.exports = GitCommand;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement