Skip to content

Commit 0356def

Browse files
committed
partial compare feature API merge
1 parent 7ba8c38 commit 0356def

File tree

7 files changed

+152
-1
lines changed

7 files changed

+152
-1
lines changed

API/branchCompareApi.js

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

API/commitLogSearchApi.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const { gitCommitLogSearchHandler } = require("../git/gitCommitLogSearchApi");
44

55
async function gitCommitLogDbSerchApi(repoId, searchCategory, searchKey) {
66
let searchQuery = "";
7+
const db = new sqlite.Database(
8+
path.join(__dirname, "..", "/database/commitLogs.sqlite"),
9+
(err) => {
10+
if (err) {
11+
console.log("ERROR: Unable to open commit log SQLITE DB", err);
12+
}
13+
}
14+
);
715

816
switch (searchCategory) {
917
case "hash":

git/gitBranchCompare.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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;

global/globalAPIHandler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
SETTINGS_PORT,
1919
GIT_FOLDER_CONTENT,
2020
CODE_FILE_VIEW,
21+
BRANCH_COMPARE,
2122
} = require("./globalRouteStore");
2223

2324
const graphqlHTTP = require("express-graphql");
@@ -56,6 +57,7 @@ const {
5657
gitFolderContentApi,
5758
gitCommitLogSearchFunction,
5859
codeFileViewFunction,
60+
branchCompareApi,
5961
} = require("./globalFunctionStore");
6062

6163
app.use(
@@ -104,6 +106,8 @@ app.use(
104106
return gitFolderContentApi(parsedPayload);
105107
case CODE_FILE_VIEW:
106108
return codeFileViewFunction(parsedPayload);
109+
case BRANCH_COMPARE:
110+
return branchCompareApi(parsedPayload);
107111
default:
108112
return { message: "Query Termination" };
109113
}

global/globalFunctionStore.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const { gitAddRemoteApi } = require("../git/gitAddRemoteApi");
1818
const { gitDeleteBranchApi } = require("../git/gitBranchDeleteApi");
1919
const { gitFetchFolderContentApi } = require("../git/gitFolderDetailsApi");
2020
const { codeFileViewApi } = require("../API/codeFileViewApi");
21+
const { branchCompareApi } = require("../API/branchCompareApi");
2122
const {
2223
fetchDatabaseFile,
2324
fetchRepoDetails,
@@ -460,3 +461,10 @@ module.exports.gitAddRemoteRepoApi = gitAddRemoteRepoApi = async (
460461
) => {
461462
return await gitAddRemoteApi(repoId, remoteName, remoteUrl);
462463
};
464+
465+
module.exports.branchCompareApi = branchCompareFunction = async (payload) => {
466+
const { repoId, baseBranch, compareBranch } = JSON.parse(payload);
467+
return await {
468+
branchCompare: branchCompareApi(repoId, baseBranch, compareBranch),
469+
};
470+
};

global/globalRouteStore.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ module.exports.SETTINGS_DBPATH = "SETTINGS_DBPATH";
1212
module.exports.SETTINGS_PORT = "SETTINGS_PORT";
1313
module.exports.SETTINGS_REPODETAILS = "SETTINGS_REPODETAILS";
1414
module.exports.GIT_FOLDER_CONTENT = "GIT_FOLDER_CONTENT";
15-
module.exports.CODE_FILE_VIEW = "CODE_FILE_VIEW";
15+
module.exports.CODE_FILE_VIEW = "CODE_FILE_VIEW";
16+
module.exports.BRANCH_COMPARE = "BRANCH_COMPARE";

global/gqlGlobalAPISchema.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ const globalAPISchema = new buildSchema(
2121
settingsPortDetails: Int!
2222
settingsRepoDetails: [settingsFetchRepoResults]!
2323
codeFileDetails: langType!
24+
branchCompare: [branchCompareType]!
2425
}
2526
2627
type healthCheckResults{
@@ -111,6 +112,17 @@ const globalAPISchema = new buildSchema(
111112
prism: String
112113
}
113114
115+
type branchCompareType{
116+
date: String!
117+
commits: [compareCommitType]
118+
}
119+
120+
type compareCommitType{
121+
hash: String!
122+
author: String
123+
commitMessage: String!
124+
}
125+
114126
type GitConvexMutation{
115127
addRepo(repoName: String!, repoPath: String!, initSwitch: Boolean!, cloneSwitch: Boolean!, cloneUrl: String): addRepoStatus!
116128
setBranch(repoId: String!, branch: String!): String!

0 commit comments

Comments
 (0)