From 807f124d399909d2bfc7bcc50281f60fb1c0a35d Mon Sep 17 00:00:00 2001 From: ylemkimon <y@ylem.kim> Date: Sat, 9 Oct 2021 14:44:33 +0900 Subject: [PATCH] fix(git): support setting file mode (#12081) --- lib/manager/npm/post-update/types.ts | 1 + lib/util/git/index.spec.ts | 17 +++++++++++++++++ lib/util/git/index.ts | 13 +++++++++++-- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/manager/npm/post-update/types.ts b/lib/manager/npm/post-update/types.ts index 035ac3d39d..2b780ddc89 100644 --- a/lib/manager/npm/post-update/types.ts +++ b/lib/manager/npm/post-update/types.ts @@ -19,6 +19,7 @@ export interface ArtifactError { export interface UpdatedArtifacts { name: string; contents: string | Buffer; + mode?: string | number; } export interface WriteExistingFilesResult { diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index d7412b3720..a3e6d78857 100644 --- a/lib/util/git/index.spec.ts +++ b/lib/util/git/index.spec.ts @@ -412,6 +412,23 @@ describe('util/git/index', () => { expect.objectContaining({ '--no-verify': null }) ); }); + + it('creates file with specified mode', async () => { + const file = { + name: 'some-executable', + contents: 'some new-contents', + mode: 0o755, + }; + const commit = await git.commitFiles({ + branchName: 'renovate/past_branch', + files: [file], + message: 'Create something', + }); + const { mode } = await fs.stat(tmpDir.path + '/some-executable'); + expect(commit).not.toBeNull(); + // eslint-disable-next-line no-bitwise + expect(mode & 0o777).toBe(0o755); + }); }); describe('getCommitMessages()', () => { diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index 3c7610ecb4..fa5998ada0 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -653,6 +653,11 @@ export interface File { * file contents */ contents: string | Buffer; + + /** + * file mode + */ + mode?: fs.Mode; } export type CommitFilesConfig = { @@ -698,7 +703,8 @@ export async function commitFiles({ ignoredFiles.push(fileName); } } else { - if (await isDirectory(join(localDir, fileName))) { + const path = join(localDir, fileName); + if (await isDirectory(path)) { // This is usually a git submodule update logger.trace({ fileName }, 'Adding directory commit'); } else { @@ -709,7 +715,10 @@ export async function commitFiles({ } else { contents = file.contents; } - await fs.outputFile(join(localDir, fileName), contents); + await fs.outputFile(path, contents); + if (file.mode) { + await fs.chmod(path, file.mode); + } } try { await git.add(fileName); -- GitLab