Skip to content

Commit

Permalink
Merge pull request #27025 from mshima/force-git
Browse files Browse the repository at this point in the history
add force-git option
  • Loading branch information
DanielFran authored Aug 21, 2024
2 parents a54abd4 + 4e449e0 commit f3490e4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 22 deletions.
1 change: 1 addition & 0 deletions generators/app/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Options:
--with-admin-ui Generate administrative user interface
--client-root-dir <value> 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
Expand Down
15 changes: 11 additions & 4 deletions generators/git/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
19 changes: 15 additions & 4 deletions generators/git/generator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 });
});
});
});
});
50 changes: 37 additions & 13 deletions generators/git/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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() {
Expand Down Expand Up @@ -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(
Expand All @@ -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.');
}
Expand Down
1 change: 1 addition & 0 deletions generators/jdl/__snapshots__/generator.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Options:
--workspaces-folders <value...> 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
Expand Down
3 changes: 2 additions & 1 deletion generators/upgrade/upgrade.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -80,6 +80,7 @@ Initial version of upgradeTest generated by generator-jhipster@undefined"
baseName: 'upgradeTest',
})
.withOptions({
skipGit: false,
blueprints: blueprintName,
})
.run()
Expand Down
1 change: 1 addition & 0 deletions testing/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ const commonTestOptions = {
noInsight: true,
useVersionPlaceholders: true,
fakeKeytool: true,
skipGit: true,
};

export const basicHelpers = createTestHelpers({ generatorOptions: { ...commonTestOptions } });
Expand Down

0 comments on commit f3490e4

Please sign in to comment.