Skip to content

Commit 7dc7a97

Browse files
committed
git: avoid invoking the git process through a shell
Use the newly minted `execute` to directly execute the `git` process. This should be a slight bit faster as no new shell process needs to be setup.
1 parent d79e32f commit 7dc7a97

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

src/git.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const core = require("@actions/core");
22

3-
const { run } = require("./utils/action");
3+
const { execute } = require("./utils/action");
44

55
/**
66
* Fetches and checks out the remote Git branch (if it exists, the fork repository will be used)
@@ -10,27 +10,23 @@ function checkOutRemoteBranch(context) {
1010
if (context.repository.hasFork) {
1111
// Fork: Add fork repo as remote
1212
core.info(`Adding "${context.repository.forkName}" fork as remote with Git`);
13-
run(
14-
`git remote add fork https://${context.actor}:${context.token}@github.com/${context.repository.forkName}.git`,
15-
);
13+
execute("git", ["remote", "add", "fork", `https://${context.actor}:${context.token}@github.com/${context.repository.forkName}.git`]);
1614
} else {
1715
// No fork: Update remote URL to include auth information (so auto-fixes can be pushed)
1816
core.info(`Adding auth information to Git remote URL`);
19-
run(
20-
`git remote set-url origin https://${context.actor}:${context.token}@github.com/${context.repository.repoName}.git`,
21-
);
17+
execute("git", ["remote", "set-url", "origin", `https://${context.actor}:${context.token}@github.com/${context.repository.repoName}.git`]);
2218
}
2319

2420
const remote = context.repository.hasFork ? "fork" : "origin";
2521

2622
// Fetch remote branch
2723
core.info(`Fetching remote branch "${context.branch}"`);
28-
run(`git fetch --no-tags --depth=1 ${remote} ${context.branch}`);
24+
execute("git", ["fetch", "--no-tags", "--depth=1", remote, context.branch]);
2925

3026
// Switch to remote branch
3127
core.info(`Switching to the "${context.branch}" branch`);
32-
run(`git branch --force ${context.branch} --track ${remote}/${context.branch}`);
33-
run(`git checkout ${context.branch}`);
28+
execute("git", ["branch", "--force", context.branch, "--track", `${remote}/${context.branch}`]);
29+
execute("git", ["checkout", context.branch]);
3430
}
3531

3632
/**
@@ -39,15 +35,15 @@ function checkOutRemoteBranch(context) {
3935
*/
4036
function commitChanges(message) {
4137
core.info(`Committing changes`);
42-
run(`git commit -am "${message}"`);
38+
execute("git", ["commit", "-am", `"${message}"`]);
4339
}
4440

4541
/**
4642
* Returns the SHA of the head commit
4743
* @returns {string} - Head SHA
4844
*/
4945
function getHeadSha() {
50-
const sha = run("git rev-parse HEAD").stdout;
46+
const sha = execute("git", ["rev-parse", "HEAD"]).stdout;
5147
core.info(`SHA of last commit is "${sha}"`);
5248
return sha;
5349
}
@@ -57,7 +53,7 @@ function getHeadSha() {
5753
* @returns {boolean} - Boolean indicating whether changes exist
5854
*/
5955
function hasChanges() {
60-
const output = run("git diff-index --name-status --exit-code HEAD --", { ignoreErrors: true });
56+
const output = execute("git", ["diff-index", "--name-status", "--exit-code", "HEAD", "--"], { ignoreErrors: true });
6157
const hasChangedFiles = output.status === 1;
6258
core.info(`${hasChangedFiles ? "Changes" : "No changes"} found with Git`);
6359
return hasChangedFiles;
@@ -68,7 +64,7 @@ function hasChanges() {
6864
*/
6965
function pushChanges() {
7066
core.info("Pushing changes with Git");
71-
run("git push");
67+
execute("git", ["push"]);
7268
}
7369

7470
/**
@@ -78,8 +74,8 @@ function pushChanges() {
7874
*/
7975
function setUserInfo(name, email) {
8076
core.info(`Setting Git user information`);
81-
run(`git config --global user.name "${name}"`);
82-
run(`git config --global user.email "${email}"`);
77+
execute("git", ["config", "--global", "user.name", `"${name}"`]);
78+
execute("git", ["config", "--global", "user.email", `"${email}"`]);
8379
}
8480

8581
module.exports = {

0 commit comments

Comments
 (0)