diff --git a/lib/platform/git/storage.js b/lib/platform/git/storage.js index f3456f2e466477b2988693ef9038f6f06d640a22..9be1f1ef3902067ffa6978b4dc905ef682377de9 100644 --- a/lib/platform/git/storage.js +++ b/lib/platform/git/storage.js @@ -304,15 +304,26 @@ class Storage { await git.reset('hard'); await git.raw(['clean', '-fd']); await git.checkout(['-B', branchName, 'origin/' + parentBranch]); + const fileNames = []; + const deleted = []; for (const file of files) { - await fs.writeFile(join(cwd, file.name), Buffer.from(file.contents)); + // istanbul ignore if + if (file.name === '|delete|') { + deleted.push(file.contents); + } else { + fileNames.push(file.name); + await fs.outputFile( + join(cwd, file.name), + Buffer.from(file.contents) + ); + } } - const fileNames = files.map(f => f.name); // istanbul ignore if if (fileNames.length === 1 && fileNames[0] === 'renovate.json') { fileNames.unshift('-f'); } - await git.add(fileNames); + if (fileNames.length) await git.add(fileNames); + if (deleted.length) await git.rm(deleted); await git.commit(message); await git.push([ 'origin', diff --git a/test/platform/git/__snapshots__/storage.spec.js.snap b/test/platform/git/__snapshots__/storage.spec.js.snap index 3c62c1e7bbf8de5d58da8f172a617cb047109500..e63f3686f2d7a9e833839e79678b88d9c0755501 100644 --- a/test/platform/git/__snapshots__/storage.spec.js.snap +++ b/test/platform/git/__snapshots__/storage.spec.js.snap @@ -11,6 +11,7 @@ exports[`platform/git/storage getFile(filePath, branchName) returns null for 404 exports[`platform/git/storage getFileList() should return the correct files 1`] = ` Array [ + "file_to_delete", "future_file", "master_file", "past_file", diff --git a/test/platform/git/storage.spec.js b/test/platform/git/storage.spec.js index 43b6d18463400bbaf4e2dd272d6c0db278dbc999..0d788f07be97e227af0976dff7a4df5533bc016f 100644 --- a/test/platform/git/storage.spec.js +++ b/test/platform/git/storage.spec.js @@ -24,7 +24,8 @@ describe('platform/git/storage', () => { await repo.checkout('master'); await fs.writeFile(base.path + '/master_file', 'master'); - await repo.add(['master_file']); + await fs.writeFile(base.path + '/file_to_delete', 'bye'); + await repo.add(['master_file', 'file_to_delete']); await repo.commit('master message', [ '--date=' + masterCommitDate.toISOString(), ]); @@ -182,6 +183,17 @@ describe('platform/git/storage', () => { 'Create something' ); }); + it('deletes file', async () => { + const file = { + name: '|delete|', + contents: 'file_to_delete', + }; + await git.commitFilesToBranch( + 'renovate/something', + [file], + 'Delete something' + ); + }); it('updates multiple files', async () => { const files = [ {