diff --git a/generators/app/__snapshots__/generator.spec.ts.snap b/generators/app/__snapshots__/generator.spec.ts.snap index 654289992775..46a4d1aca321 100644 --- a/generators/app/__snapshots__/generator.spec.ts.snap +++ b/generators/app/__snapshots__/generator.spec.ts.snap @@ -63,6 +63,7 @@ Options: --with-admin-ui Generate administrative user interface --client-root-dir Client root --skip-git Skip git repository initialization + --force-git Force commit to git repository --cypress-coverage Enable Cypress code coverage report generation --cypress-audit Enable cypress-audit/lighthouse report generation --enable-translation Enable translation diff --git a/generators/git/command.ts b/generators/git/command.ts index 744aa7cfe437..c24349d91371 100644 --- a/generators/git/command.ts +++ b/generators/git/command.ts @@ -19,14 +19,21 @@ import { JHipsterCommandDefinition } from '../base/api.js'; const command: JHipsterCommandDefinition = { - options: { + configs: { skipGit: { description: 'Skip git repository initialization', - type: Boolean, + cli: { + type: Boolean, + }, + scope: 'generator', + }, + forceGit: { + description: 'Force commit to git repository', + cli: { + type: Boolean, + }, scope: 'generator', }, - }, - configs: { monorepository: { description: 'Use monorepository', cli: { diff --git a/generators/git/generator.spec.ts b/generators/git/generator.spec.ts index 671b894575a6..96deb327a74e 100644 --- a/generators/git/generator.spec.ts +++ b/generators/git/generator.spec.ts @@ -50,7 +50,7 @@ describe(`generator - ${generator}`, () => { describe('with default option', () => { let runResult; before(async () => { - runResult = await helpers.run(generatorPath); + runResult = await helpers.run(generatorPath).withOptions({ skipGit: false }); }); it('should create .git', async () => { await expect(access(resolve(runResult.cwd, '.git'))).resolves.toBeUndefined(); @@ -75,13 +75,24 @@ describe(`generator - ${generator}`, () => { describe('regenerating', () => { let runResult; before(async () => { - runResult = await helpers.run(generatorPath); - runResult = await runResult.create(generatorPath).withOptions({ baseName: 'changed' }).run(); + runResult = await helpers.run(generatorPath).withOptions({ skipGit: false }); + runResult = await runResult.create(generatorPath).withOptions({ skipGit: false, baseName: 'changed' }).run(); }); - it('should have 1 commit', async () => { + it('should create a single commit', async () => { const git = runResult.generator.createGit(); await expect(git.log()).resolves.toMatchObject({ total: 1 }); }); }); + describe('regenerating with --force-git', () => { + let runResult; + before(async () => { + runResult = await helpers.run(generatorPath).withOptions({ skipGit: false }); + runResult = await runResult.create(generatorPath).withOptions({ skipGit: false, forceGit: true, baseName: 'changed' }).run(); + }); + it('should create 2 commits', async () => { + const git = runResult.generator.createGit(); + await expect(git.log()).resolves.toMatchObject({ total: 2 }); + }); + }); }); }); diff --git a/generators/git/generator.ts b/generators/git/generator.ts index 5995340df61c..a1445d66b439 100644 --- a/generators/git/generator.ts +++ b/generators/git/generator.ts @@ -27,10 +27,11 @@ import { files } from './files.js'; * @class * @extends {BaseGenerator} */ -export default class InitGenerator extends BaseGenerator { - gitInstalled; +export default class GitGenerator extends BaseGenerator { gitInitialized; skipGit; + forceGit; + existingRepository; async beforeQueue() { if (!this.fromBlueprint) { @@ -42,8 +43,8 @@ export default class InitGenerator extends BaseGenerator { return this.asInitializingTaskGroup({ async checkGit() { if (!this.skipGit) { - this.gitInstalled = (await this.createGit().version()).installed; - if (!this.gitInstalled) { + const gitInstalled = (await this.createGit().version()).installed; + if (!gitInstalled) { this.log.warn('Git repository will not be created, as Git is not installed on your system'); this.skipGit = true; } @@ -61,6 +62,21 @@ export default class InitGenerator extends BaseGenerator { return this.delegateTasksToBlueprint(() => this.initializing); } + get preparing() { + return this.asPreparingTaskGroup({ + async preparing() { + if (!this.skipGit) { + // Force write .yo-rc.json to disk, it's used to check if the application is regenerated + this.jhipsterConfig.skipGit = undefined; + } + }, + }); + } + + get [BaseGenerator.PREPARING]() { + return this.delegateTasksToBlueprint(() => this.preparing); + } + get writing() { return this.asWritingTaskGroup({ async writeFiles() { @@ -102,25 +118,30 @@ export default class InitGenerator extends BaseGenerator { this.debug('Committing files to git'); const git = this.createGit(); const repositoryRoot = await git.revparse(['--show-toplevel']); - const result = await git.log(['-n', '1', '--', '.yo-rc.json']).catch(() => {}); - if (result && result.total > 0) { + const result = await git.log(['-n', '1', '--', '.yo-rc.json']).catch(() => ({ total: 0 })); + const existingApplication = result.total > 0; + if (existingApplication && !this.forceGit) { this.log.info( `Found .yo-rc.json in Git from ${repositoryRoot}. So we assume this is application regeneration. Therefore automatic Git commit is not done. You can do Git commit manually.`, ); return; } - try { + if (!this.forceGit) { const statusResult = await git.status(); if (statusResult.staged.length > 0) { this.log.verboseInfo(`The repository ${repositoryRoot} has staged files, skipping commit.`); return; } - let commitMsg = `Initial version of ${this.jhipsterConfig.baseName} generated by generator-jhipster@${this.jhipsterConfig.jhipsterVersion}`; + } + try { + let commitMsg = existingApplication + ? `Regenerated ${this.jhipsterConfig.baseName} using generator-jhipster@${this.jhipsterConfig.jhipsterVersion}` + : `Initial version of ${this.jhipsterConfig.baseName} generated by generator-jhipster@${this.jhipsterConfig.jhipsterVersion}`; if (this.jhipsterConfig.blueprints && this.jhipsterConfig.blueprints.length > 0) { const bpInfo = this.jhipsterConfig.blueprints.map(bp => `${bp.name}@${bp.version}`).join(', '); commitMsg += ` with blueprints ${bpInfo}`; } - await git.add(['.']).commit(commitMsg); + await git.add(['.']).commit(commitMsg, { '--allow-empty': null }); this.log.ok(`Application successfully committed to Git from ${repositoryRoot}.`); } catch (e) { this.log.warn( @@ -146,10 +167,13 @@ export default class InitGenerator extends BaseGenerator { async initializeGitRepository() { try { const git = this.createGit(); - if (await git.checkIsRepo('root' as any)) { - this.log.info('Using existing git repository.'); - } else if (await git.checkIsRepo()) { - this.log.info('Using existing git repository at parent folder.'); + if (await git.checkIsRepo()) { + if (await git.checkIsRepo('root' as any)) { + this.log.info('Using existing git repository.'); + } else { + this.log.info('Using existing git repository at parent folder.'); + } + this.existingRepository = true; } else if (await git.init()) { this.log.ok('Git repository initialized.'); } diff --git a/generators/jdl/__snapshots__/generator.spec.ts.snap b/generators/jdl/__snapshots__/generator.spec.ts.snap index 491f271792a8..7cfce6986726 100644 --- a/generators/jdl/__snapshots__/generator.spec.ts.snap +++ b/generators/jdl/__snapshots__/generator.spec.ts.snap @@ -173,6 +173,7 @@ Options: --workspaces-folders Folders to use as monorepository workspace --workspaces Generate workspaces for multiples applications --skip-git Skip git repository initialization + --force-git Force commit to git repository --monorepository Use monorepository --defaults Execute jhipster with default config --skip-client Skip the client-side application generation diff --git a/generators/upgrade/upgrade.spec.ts b/generators/upgrade/upgrade.spec.ts index fd4bb2160fca..d3f7381420e8 100644 --- a/generators/upgrade/upgrade.spec.ts +++ b/generators/upgrade/upgrade.spec.ts @@ -23,7 +23,7 @@ describe('generator - upgrade', function () { skipServer: true, baseName: 'upgradeTest', }) - .withOptions({ useVersionPlaceholders: false }); + .withOptions({ skipGit: false, useVersionPlaceholders: false }); await runResult .create(getGenerator(GENERATOR_UPGRADE)) .withSpawnMock() @@ -80,6 +80,7 @@ Initial version of upgradeTest generated by generator-jhipster@undefined" baseName: 'upgradeTest', }) .withOptions({ + skipGit: false, blueprints: blueprintName, }) .run() diff --git a/testing/helpers.ts b/testing/helpers.ts index bce30cef8a60..542fe7b87cec 100644 --- a/testing/helpers.ts +++ b/testing/helpers.ts @@ -423,6 +423,7 @@ const commonTestOptions = { noInsight: true, useVersionPlaceholders: true, fakeKeytool: true, + skipGit: true, }; export const basicHelpers = createTestHelpers({ generatorOptions: { ...commonTestOptions } });