Skip to content

Commit 3f8c4b0

Browse files
committed
Windows compatibility fix
1 parent e7c93d7 commit 3f8c4b0

File tree

1 file changed

+103
-49
lines changed

1 file changed

+103
-49
lines changed

git/gitRepoStatus.js

Lines changed: 103 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,43 @@ const getGitStatus = async (repoPath) => {
4141
});
4242

4343
// Module to get git remote repo URL
44-
await execPromised(
45-
`${currentDir} if [ ! -z "\`git remote\`" ]; then git remote | xargs -L 1 git remote get-url; else echo "NO_REMOTE"; fi`
46-
)
47-
.then((res) => {
48-
const { stdout, stderr } = res;
49-
if (stderr !== "") {
50-
console.log(stderr);
51-
gitRemoteData = "NO_REMOTE";
52-
} else {
53-
gitRemoteData = stdout.trim();
54-
console.log("REMOTE : " + gitRemoteData);
55-
if (gitRemoteData.split("\n").length > 0) {
56-
const splitRemote = gitRemoteData.split("\n");
57-
gitRemoteData = splitRemote.join("||");
58-
console.log(gitRemoteData);
44+
45+
let gitRemotePromise =
46+
isGitLogAvailable &&
47+
(await execPromised(`${currentDir} git remote`).then(
48+
({ stdout, stderr }) => {
49+
if (stdout && !stderr) {
50+
const localRemote = stdout.trim().split("\n");
51+
52+
const multiPromise = Promise.all(
53+
localRemote &&
54+
localRemote.map(async (remote) => {
55+
console.log("LOOP ::", remote);
56+
return await execPromised(
57+
`${currentDir} git remote get-url ${remote}`
58+
).then(({ stdout, stderr }) => {
59+
if (stdout && !stderr) {
60+
console.log("REMOTE :: ", stdout);
61+
return stdout.trim();
62+
} else {
63+
console.log(stderr);
64+
}
65+
});
66+
})
67+
);
68+
return multiPromise;
69+
} else {
70+
console.log(stderr);
71+
return null;
5972
}
6073
}
61-
})
62-
.catch((err) => {
63-
console.log("Error GIT : " + err);
64-
gitRemoteData = "NO_REMOTE";
65-
});
74+
));
75+
76+
if (gitRemotePromise) {
77+
gitRemoteData = gitRemotePromise.join("||");
78+
} else {
79+
gitRemoteData = "NO_REMOTE";
80+
}
6681

6782
// Module to get Git actual repo name
6883
if (gitRemoteData && gitRemoteData !== "NO_REMOTE") {
@@ -117,15 +132,24 @@ const getGitStatus = async (repoPath) => {
117132

118133
// Module to get total number of commits to current branch
119134
isGitLogAvailable &&
120-
(await execPromised(`${currentDir} git log --oneline | wc -l`)
135+
(await execPromised(`${currentDir} git log --oneline`)
121136
.then((res) => {
122137
const { stdout, stderr } = res;
123138
if (stderr) {
124139
console.log(stderr);
125140
}
126141
if (res && !res.stderr) {
127-
gitTotalCommits = res.stdout.trim();
142+
const gitLocalTotal = res.stdout.trim().split("\n");
143+
if (gitLocalTotal && gitLocalTotal.length > 0) {
144+
gitTotalCommits = gitLocalTotal.length;
145+
} else if (gitLocalTotal.length === 1) {
146+
gitTotalCommits = 1;
147+
}
148+
} else {
149+
gitTotalCommits = 0;
150+
console.log(stderr);
128151
}
152+
return gitTotalCommits;
129153
})
130154
.catch((err) => {
131155
gitTotalCommits = 0;
@@ -144,44 +168,74 @@ const getGitStatus = async (repoPath) => {
144168
//Module to get all git tracked files
145169
var gitTrackedFileDetails = [];
146170

147-
isGitLogAvailable &&
148-
(await execPromised(
149-
`${currentDir} for i in \`git ls-tree --name-status HEAD\`; do if [ -f $i ] || [ -d $i ] ; then file $i; fi; done`
150-
).then((res) => {
151-
const { stdout, stderr } = res;
152-
if (res && !stderr) {
153-
gitTrackedFiles = stdout.trim().split("\n");
154-
} else {
155-
console.log(stderr);
171+
gitTrackedFiles =
172+
isGitLogAvailable &&
173+
(await execPromised(`${currentDir} git ls-tree --name-status HEAD`).then(
174+
({ stdout, stderr }) => {
175+
if (stdout && !stderr) {
176+
const fileList = stdout.trim().split("\n");
177+
178+
const localFiles = Promise.all(
179+
fileList.map(async (item) => {
180+
gitTrackedFileDetails.push(item);
181+
182+
return await fs.promises
183+
.stat(`${item}`)
184+
.then((fileType) => {
185+
if (fileType.isFile()) {
186+
return `${item}: File`;
187+
} else if (fileType.isDirectory()) {
188+
return `${item}: directory`;
189+
} else {
190+
return `${item}: File`;
191+
}
192+
})
193+
.catch((err) => {
194+
console.log(err);
195+
return `${item}: File`;
196+
});
197+
})
198+
);
199+
return localFiles;
200+
} else {
201+
console.log(stderr);
202+
return [];
203+
}
156204
}
157-
}));
205+
));
158206

159207
//Module to fetch commit for each file and folder
160208

161209
var gitFileBasedCommit = [];
162210

163-
isGitLogAvailable &&
164-
(await execPromised(
165-
`${currentDir} for i in \`git ls-tree --name-status HEAD\`; do git log -1 --oneline $i; done 2> /dev/null`
166-
).then((res) => {
167-
const { stdout, stderr } = res;
168-
169-
if (res && !stderr) {
170-
gitFileBasedCommit = stdout
171-
.split("\n")
172-
.filter((elm) => (elm ? elm : null));
173-
} else {
174-
console.log(stderr);
175-
}
176-
}));
211+
gitFileBasedCommit =
212+
isGitLogAvailable &&
213+
(await Promise.all(
214+
gitTrackedFileDetails.map(async (gitFile) => {
215+
return await execPromised(
216+
`${currentDir} git log -1 --oneline ${gitFile}`
217+
).then(({ stdout, stderr }) => {
218+
if (stdout && !stderr) {
219+
return stdout.trim();
220+
} else {
221+
console.log(stderr);
222+
return "";
223+
}
224+
});
225+
})
226+
));
177227

178228
//Module to get totally tracked git artifacts
179229

180230
isGitLogAvailable &&
181-
(await execPromised(`${currentDir} git ls-files | wc -l`).then((res) => {
231+
(await execPromised(`${currentDir} git ls-files`).then((res) => {
182232
const { stdout, stderr } = res;
183-
if (res && !stderr) {
184-
gitTotalTrackedFiles = Number(stdout.trim());
233+
if (stdout && !stderr) {
234+
if (stdout.split("\n")) {
235+
gitTotalTrackedFiles = Number(stdout.trim().split("\n").length);
236+
} else {
237+
return 0;
238+
}
185239
} else {
186240
console.log(stderr);
187241
}

0 commit comments

Comments
 (0)