From 26d24f47cd7b72657339082771a0e001c47bfd3e Mon Sep 17 00:00:00 2001 From: Sora Morimoto Date: Tue, 6 Aug 2024 04:49:56 +0900 Subject: [PATCH] Fix global Git ignore for self-hosted runner Signed-off-by: Sora Morimoto --- CHANGELOG.md | 4 ++++ dist/index.js | 31 +++++++++++++++++++-------- dist/post/index.js | 8 +++---- packages/setup-ocaml/src/cache.ts | 8 +++++-- packages/setup-ocaml/src/constants.ts | 7 ++---- packages/setup-ocaml/src/windows.ts | 31 +++++++++++++++++++-------- 6 files changed, 59 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 97ed602e..029d6670 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ and this project adheres to ## [unreleased] +### Fixed + +- Fix global Git ignore for self-hosted runner. + ## [3.0.5] ### Changed diff --git a/dist/index.js b/dist/index.js index 32f6a1cf..ba373f88 100644 --- a/dist/index.js +++ b/dist/index.js @@ -108201,10 +108201,7 @@ const PLATFORM = (() => { })(); const CYGWIN_MIRROR = "https://cygwin.mirror.constant.com/"; const GITHUB_WORKSPACE = external_node_process_.env.GITHUB_WORKSPACE ?? external_node_process_.cwd(); -const CYGWIN_LOCAL_PACKAGE_DIRECTORY = (() => { - const cygwinMirrorEncodedUri = encodeURIComponent(CYGWIN_MIRROR).toLowerCase(); - return external_node_path_namespaceObject.join(GITHUB_WORKSPACE, cygwinMirrorEncodedUri); -})(); +const CYGWIN_MIRROR_ENCODED_URI = encodeURIComponent(CYGWIN_MIRROR).toLowerCase(); // [HACK] https://github.com/ocaml/setup-ocaml/pull/55 const CYGWIN_ROOT = external_node_path_namespaceObject.join("D:", "cygwin"); const CYGWIN_ROOT_BIN = external_node_path_namespaceObject.join(CYGWIN_ROOT, "bin"); @@ -125793,10 +125790,25 @@ async function setGitToIgnoreCygwinLocalPackageDirectory() { : external_node_path_namespaceObject.join(homeDir, ".config", "git"); await promises_namespaceObject.mkdir(globalGitConfigDir, { recursive: true }); const globalGitIgnorePath = external_node_path_namespaceObject.join(globalGitConfigDir, "ignore"); - await promises_namespaceObject.appendFile(globalGitIgnorePath, CYGWIN_LOCAL_PACKAGE_DIRECTORY, { - encoding: "utf8", - }); - await (0,lib_exec.exec)("git", ["config", "--add", "--global", "core.excludesfile", globalGitIgnorePath], { windowsVerbatimArguments: true }); + try { + await promises_namespaceObject.access(globalGitIgnorePath, promises_namespaceObject.constants.R_OK); + const contents = await promises_namespaceObject.readFile(globalGitIgnorePath, { + encoding: "utf8", + }); + if (!contents.includes(CYGWIN_MIRROR_ENCODED_URI)) { + await promises_namespaceObject.appendFile(globalGitIgnorePath, CYGWIN_MIRROR_ENCODED_URI, { + encoding: "utf8", + }); + } + } + catch { + await promises_namespaceObject.writeFile(globalGitIgnorePath, CYGWIN_MIRROR_ENCODED_URI, { + encoding: "utf8", + }); + } + finally { + await (0,lib_exec.exec)("git", ["config", "--add", "--local", "core.excludesfile", globalGitIgnorePath], { windowsVerbatimArguments: true }); + } } async function setupCygwin() { await lib_core.group("Prepare the Cygwin environment", async () => { @@ -125887,9 +125899,10 @@ async function composeOpamCacheKeys() { } function composeCygwinCachePaths() { const cygwinRootSymlinkPath = external_node_path_namespaceObject.posix.join("/cygdrive", "d", "cygwin"); + const cygwinLocalPackageDirectory = external_node_path_namespaceObject.join(GITHUB_WORKSPACE, CYGWIN_MIRROR_ENCODED_URI); const paths = [ - CYGWIN_LOCAL_PACKAGE_DIRECTORY, CYGWIN_ROOT, + cygwinLocalPackageDirectory, cygwinRootSymlinkPath, ]; return paths; diff --git a/dist/post/index.js b/dist/post/index.js index cf8bafaf..633f7e04 100644 --- a/dist/post/index.js +++ b/dist/post/index.js @@ -103540,10 +103540,7 @@ const constants_PLATFORM = (() => { })(); const CYGWIN_MIRROR = "https://cygwin.mirror.constant.com/"; const constants_GITHUB_WORKSPACE = external_node_process_.env.GITHUB_WORKSPACE ?? external_node_process_.cwd(); -const constants_CYGWIN_LOCAL_PACKAGE_DIRECTORY = (() => { - const cygwinMirrorEncodedUri = encodeURIComponent(CYGWIN_MIRROR).toLowerCase(); - return external_node_path_namespaceObject.join(constants_GITHUB_WORKSPACE, cygwinMirrorEncodedUri); -})(); +const constants_CYGWIN_MIRROR_ENCODED_URI = encodeURIComponent(CYGWIN_MIRROR).toLowerCase(); // [HACK] https://github.com/ocaml/setup-ocaml/pull/55 const constants_CYGWIN_ROOT = external_node_path_namespaceObject.join("D:", "cygwin"); const CYGWIN_ROOT_BIN = external_node_path_namespaceObject.join(constants_CYGWIN_ROOT, "bin"); @@ -103662,9 +103659,10 @@ async function composeOpamCacheKeys() { } function composeCygwinCachePaths() { const cygwinRootSymlinkPath = path.posix.join("/cygdrive", "d", "cygwin"); + const cygwinLocalPackageDirectory = path.join(GITHUB_WORKSPACE, CYGWIN_MIRROR_ENCODED_URI); const paths = [ - CYGWIN_LOCAL_PACKAGE_DIRECTORY, CYGWIN_ROOT, + cygwinLocalPackageDirectory, cygwinRootSymlinkPath, ]; return paths; diff --git a/packages/setup-ocaml/src/cache.ts b/packages/setup-ocaml/src/cache.ts index 10259daa..0fe466b8 100644 --- a/packages/setup-ocaml/src/cache.ts +++ b/packages/setup-ocaml/src/cache.ts @@ -8,7 +8,7 @@ import * as system from "systeminformation"; import { ARCHITECTURE, CACHE_PREFIX, - CYGWIN_LOCAL_PACKAGE_DIRECTORY, + CYGWIN_MIRROR_ENCODED_URI, CYGWIN_ROOT, DUNE_CACHE_ROOT, GITHUB_WORKSPACE, @@ -65,9 +65,13 @@ async function composeOpamCacheKeys() { function composeCygwinCachePaths() { const cygwinRootSymlinkPath = path.posix.join("/cygdrive", "d", "cygwin"); + const cygwinLocalPackageDirectory = path.join( + GITHUB_WORKSPACE, + CYGWIN_MIRROR_ENCODED_URI, + ); const paths = [ - CYGWIN_LOCAL_PACKAGE_DIRECTORY, CYGWIN_ROOT, + cygwinLocalPackageDirectory, cygwinRootSymlinkPath, ]; return paths; diff --git a/packages/setup-ocaml/src/constants.ts b/packages/setup-ocaml/src/constants.ts index c0c6fb52..3886f9cb 100644 --- a/packages/setup-ocaml/src/constants.ts +++ b/packages/setup-ocaml/src/constants.ts @@ -52,11 +52,8 @@ export const CYGWIN_MIRROR = "https://cygwin.mirror.constant.com/"; export const GITHUB_WORKSPACE = process.env.GITHUB_WORKSPACE ?? process.cwd(); -export const CYGWIN_LOCAL_PACKAGE_DIRECTORY = (() => { - const cygwinMirrorEncodedUri = - encodeURIComponent(CYGWIN_MIRROR).toLowerCase(); - return path.join(GITHUB_WORKSPACE, cygwinMirrorEncodedUri); -})(); +export const CYGWIN_MIRROR_ENCODED_URI = + encodeURIComponent(CYGWIN_MIRROR).toLowerCase(); // [HACK] https://github.com/ocaml/setup-ocaml/pull/55 export const CYGWIN_ROOT = path.join("D:", "cygwin"); diff --git a/packages/setup-ocaml/src/windows.ts b/packages/setup-ocaml/src/windows.ts index fd17fb07..a04f5089 100644 --- a/packages/setup-ocaml/src/windows.ts +++ b/packages/setup-ocaml/src/windows.ts @@ -9,8 +9,8 @@ import * as toolCache from "@actions/tool-cache"; import * as cheerio from "cheerio"; import * as semver from "semver"; import { - CYGWIN_LOCAL_PACKAGE_DIRECTORY, CYGWIN_MIRROR, + CYGWIN_MIRROR_ENCODED_URI, CYGWIN_ROOT, } from "./constants.js"; @@ -45,14 +45,27 @@ async function setGitToIgnoreCygwinLocalPackageDirectory() { : path.join(homeDir, ".config", "git"); await fs.mkdir(globalGitConfigDir, { recursive: true }); const globalGitIgnorePath = path.join(globalGitConfigDir, "ignore"); - await fs.appendFile(globalGitIgnorePath, CYGWIN_LOCAL_PACKAGE_DIRECTORY, { - encoding: "utf8", - }); - await exec( - "git", - ["config", "--add", "--global", "core.excludesfile", globalGitIgnorePath], - { windowsVerbatimArguments: true }, - ); + try { + await fs.access(globalGitIgnorePath, fs.constants.R_OK); + const contents = await fs.readFile(globalGitIgnorePath, { + encoding: "utf8", + }); + if (!contents.includes(CYGWIN_MIRROR_ENCODED_URI)) { + await fs.appendFile(globalGitIgnorePath, CYGWIN_MIRROR_ENCODED_URI, { + encoding: "utf8", + }); + } + } catch { + await fs.writeFile(globalGitIgnorePath, CYGWIN_MIRROR_ENCODED_URI, { + encoding: "utf8", + }); + } finally { + await exec( + "git", + ["config", "--add", "--local", "core.excludesfile", globalGitIgnorePath], + { windowsVerbatimArguments: true }, + ); + } } export async function setupCygwin() {