/git.js

https://github.com/tpurtell/changedna · JavaScript · 88 lines · 73 code · 7 blank · 8 comment · 4 complexity · 81e173aaa5f7d824aed27a587780f5fe MD5 · raw file

  1. //@author T.J. Purtell
  2. //MIT License, i.e. do whatever you want and don't bug me unless you like it :-)
  3. var spawn = require('child_process').spawn;
  4. var fs = require('fs');
  5. var path = require('path');
  6. var GIT_BINARY = 'C:\\program files (x86)\\git\\bin\\git.exe';
  7. //save this just in case some weird spawn code causes it to change
  8. var base_path = process.cwd();
  9. function encodeRepository(repository) {
  10. return encodeURIComponent(repository);
  11. }
  12. function getOutputBase(repository) {
  13. return path.join(base_path, 'processed', encodeRepository(repository));
  14. }
  15. function getOutputPath(repository) {
  16. return getOutputBase(repository) + '.json';
  17. }
  18. function getRepositoryPath(repository) {
  19. return path.join(base_path, 'git', encodeRepository(repository));
  20. }
  21. //callback invoked with first argument as code: success ==0 or fail !=0
  22. function checkoutRepository(repository, callback) {
  23. //if stat fails, the git folder doesn't exist so check it out
  24. try {
  25. fs.statSync(path.join(getRepositoryPath(repository), '.git'));
  26. //TODO: could pull to refresh it? but meh
  27. callback(0);
  28. } catch (err) {
  29. var child = spawn(GIT_BINARY, [ 'clone', repository, getRepositoryPath(repository)]);
  30. child.stdout.on('data', function(data){process.stdout.write(data)});
  31. child.stderr.on('data', function(data){process.stderr.write(data)});
  32. child.on('exit', callback);
  33. }
  34. }
  35. //callback invoked with first argument as code: success ==0 or fail !=0
  36. // second argument is revision list
  37. function getRevisionList(repository, branch, callback) {
  38. var child = spawn(GIT_BINARY, [ 'rev-list', branch ], { cwd:getRepositoryPath(repository) });
  39. var revisions_raw = "";
  40. child.stdout.on('data', function(data){revisions_raw += data});
  41. child.stderr.on('data', function(data){process.stderr.write(data)});
  42. child.on('exit', function(code) {
  43. var revisions = revisions_raw.split(/\W+/).filter(function(s){ return s.length > 0;}).reverse();
  44. callback(code, revisions);
  45. });
  46. }
  47. function getCommitDiff(repository, commit, callback) {
  48. var child = spawn(GIT_BINARY, [ 'show', '--format=format:%ct%n%s', commit ], { cwd:getRepositoryPath(repository) });
  49. var diff_raw = "";
  50. child.stdout.on('data', function(data){diff_raw += data});
  51. child.stderr.on('data', function(data){process.stderr.write(data)});
  52. child.on('exit', function(code) {
  53. var header = diff_raw.split('---', 1)[0];
  54. var date = header.split('\n', 1)[0] - 0;
  55. var note = header.split('\n').slice(1).join('\n').split('diff --git', 1)[0];
  56. var diff = diff_raw.substring(diff_raw.indexOf('---'));
  57. callback(code, date, note, diff);
  58. });
  59. }
  60. function getCommitUrl(repository, commit, callback) {
  61. }
  62. try {
  63. fs.mkdirSync(getOutputBase(''),0777);
  64. } catch (err) {
  65. if(err.code != "EEXIST") {
  66. throw err;
  67. }
  68. }
  69. try {
  70. fs.mkdirSync(getRepositoryPath(''), 0777);
  71. } catch (err) {
  72. if(err.code != "EEXIST") {
  73. throw err;
  74. }
  75. }
  76. exports.getOutputPath = getOutputPath;
  77. exports.getRepositoryPath = getRepositoryPath;
  78. exports.checkoutRepository = checkoutRepository;
  79. exports.getCommitDiff = getCommitDiff;
  80. exports.getCommitUrl = getCommitUrl;
  81. exports.getRevisionList = getRevisionList;