diff --git a/scripts/ganerateCommitsHistory/index.js b/scripts/ganerateCommitsHistory/index.js index 8abbaf4a..ef54b9f3 100644 --- a/scripts/ganerateCommitsHistory/index.js +++ b/scripts/ganerateCommitsHistory/index.js @@ -23,7 +23,7 @@ import path from "path"; exportVariable("linguist-generated-ganerateCommitsHistory", JSON.stringify(["src/global/zh/MediaWiki:GHIAHistory.json"])); if (!isInMasterBranch) { - console.info("Not running in non-master branch, exit."); + console.info("Not running in master branch, exit."); process.exit(0); } console.info("Initialization done."); diff --git a/scripts/modules/createCommit.js b/scripts/modules/createCommit.js index e16f295a..9091df79 100644 --- a/scripts/modules/createCommit.js +++ b/scripts/modules/createCommit.js @@ -1,7 +1,8 @@ import console from "../modules/console.js"; import git from "../modules/git.js"; import { exportVariable } from "@actions/core"; -import { isInGithubActions, isPullRequest } from "../modules/octokit.js"; +import { isInGithubActions } from "../modules/octokit.js"; +import upstream from "../modules/getUpstream.js"; /** * @param {string} message @@ -12,8 +13,8 @@ export default async (message) => { console.info("Not running in github actions, exit."); return false; } - if (isPullRequest) { - console.info("Running in github actions, but in pull request event, skip creating commits..."); + if (!upstream) { + console.info("Running in github actions, but HEAD is not tracking any remote branch, exit."); return false; } console.info("[createCommit] Running in github actions, try to create commit."); diff --git a/scripts/modules/getUpstream.js b/scripts/modules/getUpstream.js new file mode 100644 index 00000000..b18ed62d --- /dev/null +++ b/scripts/modules/getUpstream.js @@ -0,0 +1,18 @@ +import console from "./console.js"; +import git from "./git.js"; +import { isInGithubActions } from "./octokit.js"; +if (!isInGithubActions) { + console.info("Not running in github actions, exit."); + process.exit(0); +} +/** + * @type { string | false } when false, it means the current branch is not tracking any remote branch. + */ +let result; +try { + result = await git.revparse(["--abbrev-ref", "--symbolic-full-name", "@{upstream}"]); +} catch (e) { + console.error(e); + result = false; +} +export default result; diff --git a/scripts/postCommit/push.js b/scripts/postCommit/push.js index ad0a03b8..672be2ba 100644 --- a/scripts/postCommit/push.js +++ b/scripts/postCommit/push.js @@ -4,10 +4,15 @@ import { startGroup, endGroup } from "@actions/core"; import git from "../modules/git.js"; import { isInGithubActions } from "../modules/octokit.js"; import readWorkflowEvent from "../modules/workflowEvent.js"; +import upstream from "../modules/getUpstream.js"; if (!isInGithubActions) { console.info("Not running in github actions, exit."); process.exit(0); } +if (!upstream) { + console.info("Running in github actions, but HEAD is not tracking any remote branch, exit."); + process.exit(0); +} const GITHUB_EVENT = await readWorkflowEvent(); const { before, after } = GITHUB_EVENT; const isBeforeExists = before && after && (await git.branch(["--contains", before]).catch(() => ({ current: "" }))).current.length > 0; @@ -22,7 +27,7 @@ startGroup("changedFiles:"); console.info(changedFiles); endGroup(); console.info("Running in github actions, start to check unpushed commits..."); -const unpushedCommits = (await git.raw(["cherry", "-v"])).trim(); +const unpushedCommits = (await git.raw(["cherry", "-v", upstream])).trim(); if (unpushedCommits.length === 0) { console.info("No unpushed commit."); process.exit(0);