Skip to content

Commit d6b6f6e

Browse files
committed
Refactored commit logs api to load log details based on demand
1 parent 5afc6df commit d6b6f6e

File tree

3 files changed

+56
-15
lines changed

3 files changed

+56
-15
lines changed

git/gitCommitLogsAPI.js

Lines changed: 52 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,49 @@ const { exec } = require("child_process");
33
const util = require("util");
44
const execPromisified = util.promisify(exec);
55

6-
async function gitCommitLogHandler(repoId) {
6+
async function gitCommitLogHandler(repoId, skipLimit = 0) {
77
const repoPath = fetchRepopath.getRepoPath(repoId);
8+
let commitLogLimit = 0;
9+
10+
const totalCommits = await execPromisified(`git log --oneline`, {
11+
cwd: repoPath,
12+
windowsHide: true,
13+
})
14+
.then((res) => {
15+
const { stdout, stderr } = res;
16+
if (stderr) {
17+
console.log(stderr);
18+
}
19+
if (res && !res.stderr) {
20+
const gitLocalTotal = stdout.trim().split("\n").length;
21+
return gitLocalTotal;
22+
} else {
23+
console.log(stderr);
24+
return 0;
25+
}
26+
})
27+
.catch((err) => {
28+
console.log(err);
29+
return 0;
30+
});
31+
32+
console.log("Total commits in the repo : ", totalCommits);
33+
34+
commitLogLimit = totalCommits < 10 ? totalCommits : 10;
35+
36+
if (!totalCommits) {
37+
return {
38+
hash: "",
39+
author: "",
40+
commitTime: "",
41+
commitMessage: "",
42+
commitRelativeTime: "",
43+
commitFilesCount: 0,
44+
};
45+
}
46+
847
return await execPromisified(
9-
`git log --pretty=format:"%h||%an||%ad||%s" --date=short`,
48+
`git log -n ${commitLogLimit} --skip ${skipLimit} --pretty=format:"%h||%an||%ad||%s" --date=short`,
1049
{
1150
cwd: repoPath,
1251
windowsHide: true,
@@ -19,7 +58,7 @@ async function gitCommitLogHandler(repoId) {
1958
let commits = stdout.trim();
2059

2160
let commitRelativeTime = await execPromisified(
22-
`git log --pretty=format:"%ad" --date=relative`,
61+
`git log -n ${commitLogLimit} --skip ${skipLimit} --pretty=format:"%ad" --date=relative`,
2362
{ cwd: repoPath, windowsHide: true }
2463
)
2564
.then(({ stdout, stderr }) => {
@@ -47,28 +86,30 @@ async function gitCommitLogHandler(repoId) {
4786
if (stdout) {
4887
return stdout.trim().split("\n").length;
4988
} else {
50-
console.log("Error occurred!");
89+
console.log(stderr);
5190
return 0;
5291
}
5392
})
5493
.catch((err) => {
55-
console.log("Error occurred!");
94+
console.log(err);
5695
return 0;
5796
});
5897
commit += "||" + commitFilesCount;
5998
return commitModel(commit);
6099
});
61100
return {
101+
totalCommits: totalCommits,
62102
commits: commitArray,
63103
};
64104
} else {
65105
return {
106+
totalCommits: 0,
66107
commits: [],
67108
};
68109
}
69110
})
70111
.catch((err) => {
71-
console.log("ERROR : Commit log collection Error!");
112+
console.log("ERROR : Commit log collection Error!", err);
72113
return {
73114
hash: "",
74115
author: "",
@@ -92,12 +133,11 @@ function commitModel(commit) {
92133

93134
let commitSplit = commit.split("||");
94135

95-
commitObject.hash = commitSplit[0];
96-
commitObject.author = commitSplit[1];
97-
commitObject.commitTime = commitSplit[2];
98-
commitObject.commitMessage = commitSplit[3];
99-
commitObject.commitRelativeTime = commitSplit[4];
100-
commitObject.commitFilesCount = commitSplit[5];
136+
const objKeys = Object.keys(commitObject);
137+
138+
for (let i = 0; i < objKeys.length; i++) {
139+
commitObject[objKeys[i]] = commitSplit[i];
140+
}
101141

102142
return commitObject;
103143
}

global/globalFunctionStore.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,17 @@ module.exports.addRepoFunction = addRepoFunction = async (
8686
module.exports.gitCommitLogsFunction = gitCommitLogsFunction = async (
8787
parsedPayload
8888
) => {
89-
const { repoId } = JSON.parse(parsedPayload);
89+
const { repoId, skipLimit } = JSON.parse(parsedPayload);
9090
if (repoId) {
91-
console.log(await gitCommitLogHandler(repoId));
9291
return {
9392
gitCommitLogs: {
94-
...(await gitCommitLogHandler(repoId)),
93+
...(await gitCommitLogHandler(repoId, skipLimit)),
9594
},
9695
};
9796
} else {
9897
return {
9998
gitCommitLogs: {
99+
totalCommits: 0,
100100
commits: [],
101101
},
102102
};

global/gqlGlobalAPISchema.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ const globalAPISchema = new buildSchema(
5353
}
5454
5555
type gitCommitLogResults{
56+
totalCommits: Int
5657
commits: [gitCommits]
5758
}
5859

0 commit comments

Comments
 (0)