js-git-service.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. var path = require('path');
  2. var whilst = require('async/whilst');
  3. var helper = require('../helper.js');
  4. var jsGitService = {};
  5. jsGitService.loadRepo = function (folder) {
  6. var repo = {};
  7. // Mixin the base DB operations using local git database on disk.
  8. require('git-node-fs/mixins/fs-db')(repo, path.join(folder, '.git'));
  9. // Mixin the walker helpers.
  10. require('js-git/mixins/walkers')(repo);
  11. return repo;
  12. };
  13. jsGitService.getHeadCommit = function (folder, remote, cb) {
  14. if (cb === undefined) {
  15. cb = remote;
  16. remote = null;
  17. }
  18. var repo = jsGitService.loadRepo(folder);
  19. // Look up the hash that master currently points to.
  20. // HEAD for local head
  21. // refs/remotes/origin/HEAD for remote head
  22. var ref = remote ? 'refs/remotes/' + remote + '/HEAD' : 'HEAD';
  23. jsGitService.getLastCommitByRef(repo, ref, cb);
  24. };
  25. jsGitService.getLastCommit = function (folder, branch, remote, cb) {
  26. if (cb === undefined) {
  27. cb = remote;
  28. remote = null;
  29. }
  30. var repo = jsGitService.loadRepo(folder);
  31. var ref = remote ? 'refs/remotes/origin/' + branch : 'refs/heads/' + branch;
  32. jsGitService.getLastCommitByRef(repo, ref, cb);
  33. };
  34. jsGitService.getLastCommitByRef = function (repo, ref, cb) {
  35. repo.readRef(ref, function (err, commitHash) {
  36. if (err) {
  37. return cb(err);
  38. }
  39. if (!commitHash) {
  40. return cb(null);
  41. }
  42. repo.logWalk(commitHash.replace(/ref: /g, ""), function (err, logStream) {
  43. if (err) {
  44. return cb(err);
  45. }
  46. if (!logStream) {
  47. return cb(null);
  48. }
  49. logStream.read(function (err, commit) {
  50. if (err) {
  51. return cb(err);
  52. }
  53. cb(null, commit);
  54. });
  55. });
  56. });
  57. };
  58. jsGitService.getCommitByHash = function (repo, hash, cb) {
  59. repo.loadAs("commit", hash, function (err, commit) {
  60. if (err) {
  61. return cb(err);
  62. }
  63. cb(null, commit);
  64. });
  65. };
  66. jsGitService.getCommitHistory = function (folder, n, branch, remote, cb) {
  67. var commitHistory = [];
  68. if (cb === undefined) {
  69. cb = remote;
  70. remote = null;
  71. }
  72. var repo = jsGitService.loadRepo(folder);
  73. // HEAD for local head
  74. // refs/remotes/origin/HEAD for remote head
  75. // refs/heads/my-branch for local branch
  76. // refs/remotes/origin/my-branch for remote branch
  77. var ref;
  78. if (branch === 'HEAD') {
  79. ref = remote ? 'refs/remotes/' + remote + '/HEAD' : 'HEAD';
  80. }
  81. else {
  82. ref = remote ? 'refs/remotes/origin/' + branch : 'refs/heads/' + branch;
  83. }
  84. jsGitService.getLastCommitByRef(repo, ref, function (err, commit) {
  85. if (err) {
  86. return cb(err);
  87. }
  88. if (!commit) {
  89. return cb(null, commitHistory);
  90. }
  91. commitHistory.push(commit);
  92. var count = 1;
  93. var parentCommitHash = helper.last(commit.parents); // last parent is the parent in the 'git log' meaning
  94. whilst(
  95. function () {
  96. return count < n && parentCommitHash;
  97. },
  98. function (callback) {
  99. jsGitService.getCommitByHash(repo, parentCommitHash, function (err, commit) {
  100. if (err) {
  101. return callback(err);
  102. }
  103. if (!commit) {
  104. parentCommitHash = null;
  105. return callback(null);
  106. }
  107. commit.hash = parentCommitHash; // add hash back to commit as not a property when loaded by hash
  108. count++;
  109. commitHistory.push(commit);
  110. parentCommitHash = helper.last(commit.parents);
  111. callback(null);
  112. });
  113. },
  114. function (err) {
  115. if (err) {
  116. return cb(err);
  117. }
  118. return cb(null, commitHistory);
  119. }
  120. );
  121. });
  122. };
  123. jsGitService.getRefHash = function (folder, branch, remote, cb) {
  124. var repo = jsGitService.loadRepo(folder);
  125. repo.readRef('refs/remotes/' + remote + '/' + branch, cb);
  126. };
  127. module.exports = jsGitService;