Skip to content

Commit

Permalink
fix(git): set --recurse-submodules flag for checkout (#26163)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
mtardy and viceice committed Dec 8, 2023
1 parent 2836301 commit 74014fd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
54 changes: 54 additions & 0 deletions lib/util/git/index.spec.ts
Expand Up @@ -190,6 +190,60 @@ describe('util/git/index', () => {
it('sets non-master base branch', async () => {
await expect(git.checkoutBranch('develop')).resolves.not.toThrow();
});

describe('submodules', () => {
beforeEach(async () => {
const repo = Git(base.path);

const submoduleBasePath = base.path + '/submodule';
await fs.mkdir(submoduleBasePath);
const submodule = Git(submoduleBasePath);
await submodule.init();
await submodule.addConfig('user.email', 'Jest@example.com');
await submodule.addConfig('user.name', 'Jest');

await fs.writeFile(submoduleBasePath + '/init_file', 'init');
await submodule.add('init_file');
await submodule.commit('init submodule');

await repo.submoduleAdd('./submodule', './submodule');
await repo.commit('add submodule');
await repo.branch(['stable']);

await fs.writeFile(submoduleBasePath + '/current_file', 'current');
await submodule.add('current_file');
await submodule.commit('update');
await repo.add('submodule');
await repo.commit('update submodule');
});

it('verifies that the --recurse-submodule flag is needed', async () => {
const repo = Git(base.path);
expect((await repo.status()).isClean()).toBeTrue();
await repo.checkout('stable');
expect((await repo.status()).isClean()).toBeFalse();
});

it('sets non-master base branch with submodule update', async () => {
await git.initRepo({
cloneSubmodules: true,
url: base.path,
});
expect((await git.getRepoStatus()).isClean()).toBeTrue();
await git.checkoutBranch('stable');
expect((await git.getRepoStatus()).isClean()).toBeTrue();
});

afterEach(async () => {
const repo = Git(base.path);
const defaultBranch =
(await repo.getConfig('init.defaultbranch')).value ?? 'master';
await repo.checkout(defaultBranch);
await repo.reset(['--hard', 'HEAD~2']);
await repo.branch(['-D', 'stable']);
await fs.rm(base.path + '/submodule', { recursive: true });
});
});
});

describe('getFileList()', () => {
Expand Down
8 changes: 7 additions & 1 deletion lib/util/git/index.ts
Expand Up @@ -520,7 +520,13 @@ export async function checkoutBranch(
logger.debug(`Setting current branch to ${branchName}`);
await syncGit();
try {
await gitRetry(() => git.checkout(['-f', branchName, '--']));
await gitRetry(() =>
git.checkout(
submodulesInitizialized
? ['-f', '--recurse-submodules', branchName, '--']
: ['-f', branchName, '--'],
),
);
config.currentBranch = branchName;
config.currentBranchSha = (
await git.raw(['rev-parse', 'HEAD'])
Expand Down

0 comments on commit 74014fd

Please sign in to comment.