Skip to content

Commit 301db78

Browse files
committed
Commit compare API modules
1 parent 06c6c38 commit 301db78

File tree

6 files changed

+107
-0
lines changed

6 files changed

+107
-0
lines changed

API/commitCompareApi.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { gitCommitCompare } = require("../git/gitCommitCompareApi");
2+
const { getRepoPath } = require("../global/fetchGitRepoPath");
3+
4+
async function commitCompareApi(repoId, baseCommit, compareCommit) {
5+
const repoPath = getRepoPath(repoId);
6+
7+
if (baseCommit !== compareCommit) {
8+
return await gitCommitCompare(repoPath, baseCommit, compareCommit);
9+
} else {
10+
return {
11+
message: "Nothing to compare as the commits are the same",
12+
};
13+
}
14+
}
15+
16+
module.exports.commitCompareApi = commitCompareApi;

git/gitCommitCompareApi.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
const { exec } = require("child_process");
2+
const util = require("util");
3+
const execPromisified = util.promisify(exec);
4+
5+
async function gitCommitCompare(repoPath, baseCommit, compareCommit) {
6+
const diffFileItems = await execPromisified(
7+
`git diff ${baseCommit}..${compareCommit} --raw`,
8+
{ windowsHide: true, maxBuffer: 1024 * 1024, cwd: repoPath }
9+
)
10+
.then((res) => {
11+
const { stdout, stderr } = res;
12+
13+
if (stderr) {
14+
console.log(stderr);
15+
return {
16+
message: "Error occurred while fetching commit difference",
17+
};
18+
}
19+
20+
if (stdout) {
21+
const fileChanges = stdout.trim().split("\n");
22+
let diffArray = [];
23+
24+
if (fileChanges) {
25+
diffArray = fileChanges.map((change) => {
26+
const splitItem = change.split(/\s/gi);
27+
28+
let status = splitItem[4];
29+
let fileName = splitItem[splitItem.length - 1];
30+
31+
return {
32+
status,
33+
fileName,
34+
};
35+
});
36+
37+
return { difference: diffArray };
38+
} else {
39+
return {
40+
message: "No difference found",
41+
};
42+
}
43+
}
44+
})
45+
.catch((err) => {
46+
console.log("ERROR", err);
47+
return {
48+
message: "Error occurred while comparing branches",
49+
};
50+
});
51+
52+
console.log(diffFileItems);
53+
54+
return diffFileItems;
55+
}
56+
57+
module.exports.gitCommitCompare = gitCommitCompare;

global/globalAPIHandler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const {
1919
GIT_FOLDER_CONTENT,
2020
CODE_FILE_VIEW,
2121
BRANCH_COMPARE,
22+
COMMIT_COMPARE,
2223
} = require("./globalRouteStore");
2324

2425
const graphqlHTTP = require("express-graphql");
@@ -58,6 +59,7 @@ const {
5859
gitCommitLogSearchFunction,
5960
codeFileViewFunction,
6061
branchCompareApi,
62+
commitCompareApi,
6163
} = require("./globalFunctionStore");
6264

6365
app.use(
@@ -108,6 +110,8 @@ app.use(
108110
return codeFileViewFunction(parsedPayload);
109111
case BRANCH_COMPARE:
110112
return branchCompareApi(parsedPayload);
113+
case COMMIT_COMPARE:
114+
return commitCompareApi(parsedPayload);
111115
default:
112116
return { message: "Query Termination" };
113117
}

global/globalFunctionStore.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const { gitDeleteBranchApi } = require("../git/gitBranchDeleteApi");
1919
const { gitFetchFolderContentApi } = require("../git/gitFolderDetailsApi");
2020
const { codeFileViewApi } = require("../API/codeFileViewApi");
2121
const { branchCompareApi } = require("../API/branchCompareApi");
22+
const { commitCompareApi } = require("../API/commitCompareApi");
2223
const {
2324
fetchDatabaseFile,
2425
fetchRepoDetails,
@@ -462,9 +463,26 @@ module.exports.gitAddRemoteRepoApi = gitAddRemoteRepoApi = async (
462463
return await gitAddRemoteApi(repoId, remoteName, remoteUrl);
463464
};
464465

466+
/**
467+
* @param {String} payload - Stringified JSON
468+
* @returns {Object} branchCompare - list of all difference between two selected branches
469+
*/
470+
465471
module.exports.branchCompareApi = branchCompareFunction = async (payload) => {
466472
const { repoId, baseBranch, compareBranch } = JSON.parse(payload);
467473
return await {
468474
branchCompare: branchCompareApi(repoId, baseBranch, compareBranch),
469475
};
470476
};
477+
478+
/**
479+
* @param {String} payload - Stringified JSON
480+
* @returns {Object} branchCompare - list of all difference between two selected branches
481+
*/
482+
483+
module.exports.commitCompareApi = commitCompareFunction = async (payload) => {
484+
const { repoId, baseCommit, compareCommit } = JSON.parse(payload);
485+
return await {
486+
commitCompare: commitCompareApi(repoId, baseCommit, compareCommit),
487+
};
488+
};

global/globalRouteStore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ module.exports.SETTINGS_REPODETAILS = "SETTINGS_REPODETAILS";
1414
module.exports.GIT_FOLDER_CONTENT = "GIT_FOLDER_CONTENT";
1515
module.exports.CODE_FILE_VIEW = "CODE_FILE_VIEW";
1616
module.exports.BRANCH_COMPARE = "BRANCH_COMPARE";
17+
module.exports.COMMIT_COMPARE = "COMMIT_COMPARE";

global/gqlGlobalAPISchema.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const globalAPISchema = new buildSchema(
2222
settingsRepoDetails: [settingsFetchRepoResults]!
2323
codeFileDetails: langType!
2424
branchCompare: [branchCompareType]!
25+
commitCompare: commitCompareType!
2526
}
2627
2728
type healthCheckResults{
@@ -117,12 +118,22 @@ const globalAPISchema = new buildSchema(
117118
commits: [compareCommitType]
118119
}
119120
121+
type commitCompareType{
122+
message: String
123+
difference: [commitCompareFileType]!
124+
}
125+
120126
type compareCommitType{
121127
hash: String!
122128
author: String
123129
commitMessage: String!
124130
}
125131
132+
type commitCompareFileType{
133+
status: String!
134+
fileName: String!
135+
}
136+
126137
type GitConvexMutation{
127138
addRepo(repoName: String!, repoPath: String!, initSwitch: Boolean!, cloneSwitch: Boolean!, cloneUrl: String): addRepoStatus!
128139
setBranch(repoId: String!, branch: String!): String!

0 commit comments

Comments
 (0)