Skip to content

Commit d324cc4

Browse files
committed
Commit log enhancement API changes
1 parent e57b775 commit d324cc4

File tree

7 files changed

+163
-23
lines changed

7 files changed

+163
-23
lines changed

git/gitCommitFilesApi.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const fetchRepopath = require("../global/fetchGitRepoPath");
2+
const { exec } = require("child_process");
3+
const util = require("util");
4+
const execPromisified = util.promisify(exec);
5+
6+
const gitCommitFileApi = async (repoId, commitHash) => {
7+
const repoPath = fetchRepopath.getRepoPath(repoId);
8+
return await execPromisified(
9+
`git diff-tree --no-commit-id --name-status -r ${commitHash}`,
10+
{ cwd: repoPath, windowsHide: true }
11+
)
12+
.then(({ stdout, stderr }) => {
13+
if (stdout) {
14+
const commitedFiles = stdout.trim().split("\n");
15+
return commitedFiles.map((entry) => {
16+
if (entry) {
17+
const splitEntry = entry.split(/\s/gi);
18+
return {
19+
type: splitEntry[0],
20+
fileName: splitEntry.slice(1, splitEntry.length).join(" "),
21+
};
22+
} else {
23+
return {
24+
type: "",
25+
fileName: "",
26+
};
27+
}
28+
});
29+
} else {
30+
console.log(stderr);
31+
return [
32+
{
33+
type: "",
34+
fileName: "",
35+
},
36+
];
37+
}
38+
})
39+
.catch((err) => {
40+
console.log(err);
41+
return [
42+
{
43+
type: "",
44+
fileName: "",
45+
},
46+
];
47+
});
48+
};
49+
50+
module.exports.gitCommitFileApi = gitCommitFileApi;

git/gitCommitLogsAPI.js

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,79 @@ const execPromisified = util.promisify(exec);
55

66
async function gitCommitLogHandler(repoId) {
77
const repoPath = fetchRepopath.getRepoPath(repoId);
8-
return await execPromisified(`git log --pretty=format:"%h||%an||%ad||%s"`, {
9-
cwd: repoPath,
10-
windowsHide: true,
11-
}).then((res) => {
12-
const { stdout, stderr } = res;
13-
14-
if (stdout && !stderr) {
15-
let commits = stdout.trim();
16-
let commitArray = commits.split("\n").map((commit) => {
17-
return commitModel(commit);
18-
});
19-
return {
20-
commits: commitArray,
21-
};
22-
} else {
8+
return await execPromisified(
9+
`git log --pretty=format:"%h||%an||%ad||%s" --date=short`,
10+
{
11+
cwd: repoPath,
12+
windowsHide: true,
13+
}
14+
)
15+
.then(async (res) => {
16+
const { stdout, stderr } = res;
17+
18+
if (stdout && !stderr) {
19+
let commits = stdout.trim();
20+
21+
let commitRelativeTime = await execPromisified(
22+
`git log --pretty=format:"%ad" --date=relative`,
23+
{ cwd: repoPath, windowsHide: true }
24+
)
25+
.then(({ stdout, stderr }) => {
26+
if (stdout) {
27+
return stdout.trim().split("\n");
28+
} else {
29+
console.log(stderr);
30+
return [];
31+
}
32+
})
33+
.catch((err) => {
34+
console.log(err);
35+
return [];
36+
});
37+
38+
let commitArray = commits.split("\n").map(async (commit, index) => {
39+
commit += "||" + commitRelativeTime[index];
40+
const commitFilesCount = await execPromisified(
41+
`git diff-tree --no-commit-id --name-only -r ${
42+
commit.split("||")[0]
43+
}`,
44+
{ cwd: repoPath, windowsHide: true }
45+
)
46+
.then(({ stdout, stderr }) => {
47+
if (stdout) {
48+
return stdout.trim().split("\n").length;
49+
} else {
50+
console.log("Error occurred!");
51+
return 0;
52+
}
53+
})
54+
.catch((err) => {
55+
console.log("Error occurred!");
56+
return 0;
57+
});
58+
commit += "||" + commitFilesCount;
59+
return commitModel(commit);
60+
});
61+
return {
62+
commits: commitArray,
63+
};
64+
} else {
65+
return {
66+
commits: [],
67+
};
68+
}
69+
})
70+
.catch((err) => {
71+
console.log("ERROR : Commit log collection Error!");
2372
return {
24-
commits: [],
73+
hash: "",
74+
author: "",
75+
commitTime: "",
76+
commitMessage: "",
77+
commitRelativeTime: "",
78+
commitFilesCount: 0,
2579
};
26-
}
27-
});
80+
});
2881
}
2982

3083
function commitModel(commit) {
@@ -33,6 +86,8 @@ function commitModel(commit) {
3386
author: "",
3487
commitTime: "",
3588
commitMessage: "",
89+
commitRelativeTime: "",
90+
commitFilesCount: 0,
3691
};
3792

3893
let commitSplit = commit.split("||");
@@ -41,6 +96,8 @@ function commitModel(commit) {
4196
commitObject.author = commitSplit[1];
4297
commitObject.commitTime = commitSplit[2];
4398
commitObject.commitMessage = commitSplit[3];
99+
commitObject.commitRelativeTime = commitSplit[4];
100+
commitObject.commitFilesCount = commitSplit[5];
44101

45102
return commitObject;
46103
}

git/gitRepoStatus.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const getGitStatus = async (repoPath) => {
3434
return isGitLogAvailable;
3535
})
3636
.catch((err) => {
37+
console.log(err);
3738
isGitLogAvailable = false;
3839
return isGitLogAvailable;
3940
});
@@ -190,11 +191,19 @@ const getGitStatus = async (repoPath) => {
190191
(await execPromised(`git log -1 --oneline --pretty=format:"%s"`, {
191192
cwd: repoPath,
192193
windowsHide: true,
193-
}).then((res) => {
194-
if (res && !res.stderr) {
195-
gitLatestCommit = res.stdout.trim();
196-
}
197-
}));
194+
})
195+
.then((res) => {
196+
if (res && !res.stderr) {
197+
gitLatestCommit = res.stdout.trim();
198+
} else {
199+
console.log(stderr);
200+
gitLatestCommit = "No Commits in the Current Branch";
201+
}
202+
})
203+
.catch((err) => {
204+
console.log(err);
205+
gitLatestCommit = "No Commits in the Current Branch";
206+
}));
198207

199208
//Module to get all git tracked files
200209
var gitTrackedFileDetails = [];

global/globalAPIHandler.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
REPO_TRACKED_DIFF,
1212
REPO_FILE_DIFF,
1313
COMMIT_LOGS,
14+
COMMIT_FILES,
1415
GIT_STAGED_FILES,
1516
GIT_UNPUSHED_COMMITS,
1617
SETTINGS_DBPATH,
@@ -30,6 +31,7 @@ const {
3031
healthCheckFunction,
3132
repoDetailsFunction,
3233
gitCommitLogsFunction,
34+
gitCommitFileFunction,
3335
gitGetStagedFiles,
3436
gitUnpushedCommits,
3537
gitSetBranch,
@@ -84,6 +86,8 @@ app.use(
8486
return gitFileDiffFunction(parsedPayload);
8587
case COMMIT_LOGS:
8688
return gitCommitLogsFunction(parsedPayload);
89+
case COMMIT_FILES:
90+
return gitCommitFileFunction(parsedPayload);
8791
case GIT_STAGED_FILES:
8892
return gitGetStagedFiles(parsedPayload);
8993
case GIT_UNPUSHED_COMMITS:

global/globalFunctionStore.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const { getGitRepoStatus } = require("../git/gitRepoAPI");
55
const { gitTrackedDiff } = require("../git/gitTrackedDiff");
66
const { gitFileDifferenceHandler } = require("../git/gitFileDifferenceAPI");
77
const { gitCommitLogHandler } = require("../git/gitCommitLogsAPI");
8+
const { gitCommitFileApi } = require("../git/gitCommitFilesApi");
89
const { getStagedFiles } = require("../git/gitGetStagedFilesAPI");
910
const { gitGetUnpushedCommits } = require("../git/gitGetUnpushedCommits");
1011
const { gitSetBranchApi } = require("../git/gitSetBranch.js");
@@ -93,6 +94,16 @@ module.exports.gitCommitLogsFunction = gitCommitLogsFunction = async (
9394
}
9495
};
9596

97+
module.exports.gitCommitFileFunction = gitCommitFileFunction = async (
98+
parsedPayload
99+
) => {
100+
const { repoId, commitHash } = JSON.parse(parsedPayload);
101+
console.log(await gitCommitFileApi(repoId, commitHash));
102+
return {
103+
gitCommitFiles: [...(await gitCommitFileApi(repoId, commitHash))],
104+
};
105+
};
106+
96107
module.exports.repoDetailsFunction = repoDetailsFunction = async (
97108
parsedPayload
98109
) => {

global/globalRouteStore.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports.HEALTH_CHECK = "HEALTH_CHECK";
22
module.exports.FETCH_REPO = "FETCH_REPO";
33
module.exports.ADD_REPO = "ADD_REPO";
44
module.exports.COMMIT_LOGS = "COMMIT_LOGS";
5+
module.exports.COMMIT_FILES = "COMMIT_FILES";
56
module.exports.REPO_DETAILS = "REPO_DETAILS";
67
module.exports.REPO_TRACKED_DIFF = "REPO_TRACKED_DIFF";
78
module.exports.REPO_FILE_DIFF = "REPO_FILE_DIFF";

global/gqlGlobalAPISchema.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const globalAPISchema = new buildSchema(
1414
gitChanges: gitChangeResults!
1515
gitFileLineChanges: gitFileLineChangeResults!
1616
gitCommitLogs: gitCommitLogResults!
17+
gitCommitFiles: [commitFileType]!
1718
gitStagedFiles: gitStagedFileResults!
1819
gitUnpushedCommits: gitUnpushedCommitResults!
1920
settingsDatabasePath: String!
@@ -43,6 +44,13 @@ const globalAPISchema = new buildSchema(
4344
author: String,
4445
commitTime: String,
4546
commitMessage: String,
47+
commitRelativeTime: String
48+
commitFilesCount: Int
49+
}
50+
51+
type commitFileType{
52+
type: String!
53+
fileName: String!
4654
}
4755
4856
type gitUnpushedCommitResults{

0 commit comments

Comments
 (0)