fix(core): Set source control repository to track remote if ready (#9532)

This commit is contained in:
Iván Ovejero
2024-05-29 10:21:21 +02:00
committed by GitHub
parent ec0373f666
commit dbaac82f79
2 changed files with 65 additions and 0 deletions

View File

@@ -176,6 +176,7 @@ export class SourceControlGitService {
}
try {
await this.git.addRemote(SOURCE_CONTROL_ORIGIN, sourceControlPreferences.repositoryUrl);
this.logger.debug(`Git remote added: ${sourceControlPreferences.repositoryUrl}`);
} catch (error) {
if ((error as Error).message.includes('remote origin already exists')) {
this.logger.debug(`Git remote already exists: ${(error as Error).message}`);
@@ -189,6 +190,9 @@ export class SourceControlGitService {
: SOURCE_CONTROL_DEFAULT_NAME,
user.email ?? SOURCE_CONTROL_DEFAULT_EMAIL,
);
await this.trackRemoteIfReady(sourceControlPreferences.branchName);
if (sourceControlPreferences.initRepo) {
try {
const branches = await this.getBranches();
@@ -201,6 +205,28 @@ export class SourceControlGitService {
}
}
/**
* If this is a new local repository being set up after remote is ready,
* then set this local to start tracking remote's target branch.
*/
private async trackRemoteIfReady(targetBranch: string) {
if (!this.git) return;
await this.fetch();
const { currentBranch, branches: remoteBranches } = await this.getBranches();
if (!currentBranch && remoteBranches.some((b) => b === targetBranch)) {
await this.git.checkout(targetBranch);
const upstream = [SOURCE_CONTROL_ORIGIN, targetBranch].join('/');
await this.git.branch([`--set-upstream-to=${upstream}`, targetBranch]);
this.logger.info('Set local git repository to track remote', { upstream });
}
}
async setGitUserDetails(name: string, email: string): Promise<void> {
if (!this.git) {
throw new ApplicationError('Git is not initialized (setGitUserDetails)');