You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

80 lines
2.3 KiB

  1. const fs = require('fs');
  2. const { execSync } = require('child_process');
  3. const path = require('path');
  4. function getSubdirectories (directoryPath) {
  5. return fs.readdirSync(directoryPath, { withFileTypes: true })
  6. .filter(dirent => dirent.isDirectory())
  7. .map(dirent => dirent.name);
  8. }
  9. function isGitRepository (directoryPath) {
  10. const gitFolderPath = path.join(directoryPath, '.git');
  11. return fs.existsSync(gitFolderPath) && fs.statSync(gitFolderPath).isDirectory();
  12. }
  13. function doesReleaseBranchExist (directoryPath) {
  14. try {
  15. const branches = execSync('git branch -r', { cwd: directoryPath, encoding: 'utf-8' });
  16. return branches.includes('origin/release');
  17. } catch (error) {
  18. return false;
  19. }
  20. }
  21. function getCommitsInRange (directoryPath, range) {
  22. const command = `git log --pretty=format:"%s" --abbrev-commit ${range}`;
  23. try {
  24. const result = execSync(command, { cwd: directoryPath, encoding: 'utf-8' });
  25. return result.trim().split('\n');
  26. } catch (error) {
  27. return [];
  28. }
  29. }
  30. function getCommitCountBehind (directoryPath) {
  31. if (doesReleaseBranchExist(directoryPath)) {
  32. execSync('git fetch origin release', { cwd: directoryPath, encoding: 'utf-8' });
  33. const command = 'origin/release..origin/master';
  34. const commitMessages = getCommitsInRange(directoryPath, command);
  35. const commitCount = commitMessages.length;
  36. if (commitMessages.length === 1 && commitMessages[0] === '')
  37. return { count: 0, messages: [] };
  38. return { count: commitCount, messages: commitMessages };
  39. }
  40. return { count: 0, messages: [] };
  41. }
  42. const greenColor = '\x1b[32m';
  43. const grayColor = '\x1b[90m';
  44. const resetColor = '\x1b[0m';
  45. function displayCommitCountBehind (directoryPath) {
  46. if (isGitRepository(directoryPath)) {
  47. const { count, messages } = getCommitCountBehind(directoryPath);
  48. if (count > 0) {
  49. console.log(`${greenColor}${path.basename(directoryPath)}: ${count} commit(s)`);
  50. messages.forEach((message, index) => {
  51. console.log(`${grayColor}${index + 1}. ${message}`);
  52. });
  53. console.log('\n');
  54. }
  55. }
  56. }
  57. function processSubdirectories (subdirectories) {
  58. subdirectories.forEach(subdirectory => {
  59. const fullPath = path.join(process.cwd(), subdirectory);
  60. if (isGitRepository(fullPath))
  61. displayCommitCountBehind(fullPath);
  62. });
  63. }
  64. console.log('\n');
  65. const subdirectories = getSubdirectories(process.cwd());
  66. processSubdirectories(subdirectories);
  67. console.log(resetColor);