Skip to content

Commit

Permalink
git: avoid invoking the git process through a shell
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
compnerd committed May 9, 2021
1 parent 99e32ae commit 252737f
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/git.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const core = require("@actions/core");

const { run } = require("./utils/action");
const { execute } = require("./utils/action");

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

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

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

// Switch to remote branch
core.info(`Switching to the "${context.branch}" branch`);
run(`git branch --force ${context.branch} --track ${remote}/${context.branch}`);
run(`git checkout ${context.branch}`);
execute("git", ["branch", "--force", context.branch, "--track", `${remote}/${context.branch}`]);
execute("git", ["checkout", context.branch]);
}

/**
Expand All @@ -39,15 +35,15 @@ function checkOutRemoteBranch(context) {
*/
function commitChanges(message) {
core.info(`Committing changes`);
run(`git commit -am "${message}"`);
execute("git", ["commit", "-am", `"${message}"`]);
}

/**
* Returns the SHA of the head commit
* @returns {string} - Head SHA
*/
function getHeadSha() {
const sha = run("git rev-parse HEAD").stdout;
const sha = execute("git", ["rev-parse", "HEAD"]).stdout;
core.info(`SHA of last commit is "${sha}"`);
return sha;
}
Expand All @@ -57,7 +53,7 @@ function getHeadSha() {
* @returns {boolean} - Boolean indicating whether changes exist
*/
function hasChanges() {
const output = run("git diff-index --name-status --exit-code HEAD --", { ignoreErrors: true });
const output = execute("git", ["diff-index", "--name-status", "--exit-code", "HEAD", "--"], { ignoreErrors: true });
const hasChangedFiles = output.status === 1;
core.info(`${hasChangedFiles ? "Changes" : "No changes"} found with Git`);
return hasChangedFiles;
Expand All @@ -68,7 +64,7 @@ function hasChanges() {
*/
function pushChanges() {
core.info("Pushing changes with Git");
run("git push");
execute("git", ["push"]);
}

/**
Expand All @@ -78,8 +74,8 @@ function pushChanges() {
*/
function setUserInfo(name, email) {
core.info(`Setting Git user information`);
run(`git config --global user.name "${name}"`);
run(`git config --global user.email "${email}"`);
execute("git", ["config", "--global", "user.name", `"${name}"`]);
execute("git", ["config", "--global", "user.email", `"${email}"`]);
}

module.exports = {
Expand Down

0 comments on commit 252737f

Please sign in to comment.