From 644f559ea55abf4fdaa32a00158cf5b855a9d52d Mon Sep 17 00:00:00 2001 From: Markus Schulz <msc@onesty-tech.de> Date: Fri, 6 Jan 2023 11:23:09 +0100 Subject: [PATCH] feat(git): more flexible git.hasDiff function (#19676) --- lib/util/git/index.spec.ts | 12 ++++++++++++ lib/util/git/index.ts | 9 ++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/util/git/index.spec.ts b/lib/util/git/index.spec.ts index 1d17075dae..459e4b9944 100644 --- a/lib/util/git/index.spec.ts +++ b/lib/util/git/index.spec.ts @@ -385,6 +385,18 @@ describe('util/git/index', () => { }); }); + describe('hasDiff(sourceRef, targetRef)', () => { + it('compare without changes', () => { + return expect(git.hasDiff('HEAD', 'HEAD')).resolves.toBeFalse(); + }); + + it('compare with changes', () => { + return expect( + git.hasDiff('origin/master', 'origin/renovate/future_branch') + ).resolves.toBeTrue(); + }); + }); + describe('commitFiles({branchName, files, message})', () => { it('creates file', async () => { const file: FileChange = { diff --git a/lib/util/git/index.ts b/lib/util/git/index.ts index 1b2c749cfb..4698779d97 100644 --- a/lib/util/git/index.ts +++ b/lib/util/git/index.ts @@ -857,10 +857,13 @@ export async function getFile( } } -export async function hasDiff(branchName: string): Promise<boolean> { +export async function hasDiff( + sourceRef: string, + targetRef: string +): Promise<boolean> { await syncGit(); try { - return (await gitRetry(() => git.diff(['HEAD', branchName]))) !== ''; + return (await gitRetry(() => git.diff([sourceRef, targetRef]))) !== ''; } catch (err) { return true; } @@ -987,7 +990,7 @@ export async function prepareCommit({ { deletedFiles, ignoredFiles, result: commitRes }, `git commit` ); - if (!force && !(await hasDiff(`origin/${branchName}`))) { + if (!force && !(await hasDiff('HEAD', `origin/${branchName}`))) { logger.debug( { branchName, deletedFiles, addedModifiedFiles, ignoredFiles }, 'No file changes detected. Skipping commit' -- GitLab