|
| 1 | +const { exec } = require("child_process"); |
| 2 | +const util = require("util"); |
| 3 | +const execPromisified = util.promisify(exec); |
| 4 | + |
| 5 | +async function gitBranchCompare(repoPath, baseBranch, compareBranch) { |
| 6 | + let formattedCommitLogs = []; |
| 7 | + |
| 8 | + const commitLogs = await execPromisified( |
| 9 | + `git log --oneline ${compareBranch}..${baseBranch} --pretty=format:"%h||%an||%ad||%s" --date=short`, |
| 10 | + { windowsHide: true, maxBuffer: 1024 * 1024, cwd: repoPath } |
| 11 | + ) |
| 12 | + .then((res) => { |
| 13 | + const { stdout, stderr } = res; |
| 14 | + |
| 15 | + if (stderr) { |
| 16 | + console.log("ERROR", stderr); |
| 17 | + return { |
| 18 | + error: "Error occurred while comparing branches", |
| 19 | + }; |
| 20 | + } |
| 21 | + |
| 22 | + if (stdout) { |
| 23 | + console.log(stdout.trim().split("\n")); |
| 24 | + formattedCommitLogs = [...commitLogModel(stdout.trim().split("\n"))]; |
| 25 | + |
| 26 | + let dateGroupModel = [ |
| 27 | + { |
| 28 | + date: "", |
| 29 | + commits: [ |
| 30 | + { |
| 31 | + hash: "", |
| 32 | + author: "", |
| 33 | + commitMessage: "", |
| 34 | + }, |
| 35 | + ], |
| 36 | + }, |
| 37 | + ]; |
| 38 | + |
| 39 | + let groupedCommits = []; |
| 40 | + |
| 41 | + for (let i = 0; i < formattedCommitLogs.length; i++) { |
| 42 | + let commitArray = []; |
| 43 | + let dateGroup = { |
| 44 | + date: formattedCommitLogs[i].commitDate, |
| 45 | + }; |
| 46 | + |
| 47 | + for (var j = i; j < formattedCommitLogs.length; j++) { |
| 48 | + let item = formattedCommitLogs[j]; |
| 49 | + if (formattedCommitLogs[i].commitDate === item.commitDate) { |
| 50 | + commitArray.push({ |
| 51 | + hash: item.hash, |
| 52 | + author: item.author, |
| 53 | + commitMessage: item.commitMessage, |
| 54 | + }); |
| 55 | + } else { |
| 56 | + break; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + i = j - 1; |
| 61 | + dateGroup.commits = commitArray; |
| 62 | + groupedCommits.push(dateGroup); |
| 63 | + } |
| 64 | + |
| 65 | + console.log(groupedCommits); |
| 66 | + |
| 67 | + return groupedCommits; |
| 68 | + } |
| 69 | + }) |
| 70 | + .catch((err) => { |
| 71 | + console.log("ERROR", err); |
| 72 | + return { |
| 73 | + error: "Error occurred while comparing branches", |
| 74 | + }; |
| 75 | + }); |
| 76 | + |
| 77 | + return commitLogs; |
| 78 | +} |
| 79 | + |
| 80 | +function commitLogModel(commit) { |
| 81 | + const formattedCommits = commit.map((item) => { |
| 82 | + const splitCommit = item.split("||"); |
| 83 | + |
| 84 | + let commitObject = { |
| 85 | + hash: "", |
| 86 | + author: "", |
| 87 | + commitDate: "", |
| 88 | + commitMessage: "", |
| 89 | + }; |
| 90 | + |
| 91 | + commitObject.hash = splitCommit[0]; |
| 92 | + commitObject.author = splitCommit[1]; |
| 93 | + commitObject.commitDate = splitCommit[2]; |
| 94 | + commitObject.commitMessage = splitCommit[3]; |
| 95 | + |
| 96 | + return commitObject; |
| 97 | + }); |
| 98 | + |
| 99 | + return formattedCommits; |
| 100 | +} |
| 101 | + |
| 102 | +module.exports.gitBranchCompare = gitBranchCompare; |
0 commit comments