Skip to content

Commit

Permalink
Adopt unset config (#6592)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexr00 authored Jan 9, 2025
1 parent 6f30673 commit d5de56b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/api/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ export interface Repository {
* The counterpart of `getConfig`
*/
setConfig(key: string, value: string): Promise<string>;
unsetConfig?(key: string): Promise<string>;
getGlobalConfig(key: string): Promise<string>;

getObjectDetails(treeish: string, path: string): Promise<{ mode: string; object: string; size: number }>;
Expand Down
13 changes: 8 additions & 5 deletions src/github/folderRepositoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1961,24 +1961,27 @@ export class FolderRepositoryManager extends Disposable {
progress.report({ message: vscode.l10n.t('Deleted {0} of {1} branches', deletedBranches, totalBranches) });
};

const hideConfig = async (branch: string) => {
const deleteConfig = async (branch: string) => {
await PullRequestGitHelper.associateBaseBranchWithBranch(this.repository, branch, undefined);
await PullRequestGitHelper.associateBranchWithPullRequest(this.repository, undefined, branch);
};

// delete configs first since that can't be parallelized
for (const pick of picks) {
await deleteConfig(pick.label);
}

// batch deleting the branches to avoid consuming all available resources
await batchPromiseAll(picks, 5, async (pick) => {
try {
await this.repository.deleteBranch(pick.label, true);
if ((await PullRequestGitHelper.getMatchingPullRequestMetadataForBranch(this.repository, pick.label))) {
await hideConfig(pick.label);
console.log(`Branch ${pick.label} was not deleted`);
}
reportProgress();
} catch (e) {
if (typeof e.stderr === 'string' && e.stderr.includes('not found')) {
// TODO: The git extension API doesn't support removing configs
// If that support is added we should remove the config as it is no longer useful.
nonExistantBranches.add(pick.label);
await hideConfig(pick.label);
reportProgress();
} else if (typeof e.stderr === 'string' && e.stderr.includes('unable to access') && needsRetry) {
// There is contention for the related git files
Expand Down
27 changes: 20 additions & 7 deletions src/github/pullRequestGitHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { IResolvedPullRequestModel, PullRequestModel } from './pullRequestModel'
const PullRequestRemoteMetadataKey = 'github-pr-remote';
export const PullRequestMetadataKey = 'github-pr-owner-number';
const BaseBranchMetadataKey = 'github-pr-base-branch';
const VscodeBaseBranchMetadataKey = 'vscode-merge-base';
const PullRequestBranchRegex = /branch\.(.+)\.github-pr-owner-number/;
const PullRequestRemoteRegex = /branch\.(.+)\.remote/;

Expand Down Expand Up @@ -389,7 +390,11 @@ export class PullRequestGitHelper {
Logger.appendLine(`associate ${branchName} with Pull Request #${pullRequest.number}`, PullRequestGitHelper.ID);
}
const prConfigKey = `branch.${branchName}.${PullRequestMetadataKey}`;
await repository.setConfig(prConfigKey, pullRequest ? PullRequestGitHelper.buildPullRequestMetadata(pullRequest) : ' ');
if (pullRequest) {
await repository.setConfig(prConfigKey, PullRequestGitHelper.buildPullRequestMetadata(pullRequest));
} else if (repository.unsetConfig) {
await repository.unsetConfig(prConfigKey);
}
} catch (e) {
if (pullRequest) {
Logger.error(`associate ${branchName} with Pull Request #${pullRequest.number} failed`, PullRequestGitHelper.ID);
Expand All @@ -400,16 +405,24 @@ export class PullRequestGitHelper {
static async associateBaseBranchWithBranch(
repository: Repository,
branch: string,
owner: string,
repo: string,
baseBranch: string
base: {
owner: string,
repo: string,
branch: string
} | undefined
) {
try {
Logger.appendLine(`associate ${branch} with base branch ${owner}/${repo}#${baseBranch}`, PullRequestGitHelper.ID);
const prConfigKey = `branch.${branch}.${BaseBranchMetadataKey}`;
await repository.setConfig(prConfigKey, PullRequestGitHelper.buildBaseBranchMetadata(owner, repo, baseBranch));
if (base) {
Logger.appendLine(`associate ${branch} with base branch ${base.owner}/${base.repo}#${base.branch}`, PullRequestGitHelper.ID);
await repository.setConfig(prConfigKey, PullRequestGitHelper.buildBaseBranchMetadata(base.owner, base.repo, base.branch));
} else if (repository.unsetConfig) {
await repository.unsetConfig(prConfigKey);
const vscodeBaseBranchConfigKey = `branch.${branch}.${VscodeBaseBranchMetadataKey}`;
await repository.unsetConfig(vscodeBaseBranchConfigKey);
}
} catch (e) {
Logger.error(`associate ${branch} with base branch ${owner}/${repo}#${baseBranch} failed`, PullRequestGitHelper.ID);
Logger.error(`associate ${branch} with base branch ${base?.owner}/${base?.repo}#${base?.branch} failed`, PullRequestGitHelper.ID);
}
}
}
2 changes: 1 addition & 1 deletion src/view/reviewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ export class ReviewManager extends Disposable {
// For forks, we use the upstream repo if it's available. Otherwise, fallback to the fork.
githubRepository = this._folderRepoManager.gitHubRepositories.find(repo => repo.remote.owner === metadata.parent?.owner?.login && repo.remote.repositoryName === metadata.parent?.name) ?? githubRepository;
}
return PullRequestGitHelper.associateBaseBranchWithBranch(this.repository, newHead.name, githubRepository.remote.owner, githubRepository.remote.repositoryName, oldHead.name);
return PullRequestGitHelper.associateBaseBranchWithBranch(this.repository, newHead.name, { owner: githubRepository.remote.owner, repo: githubRepository.remote.repositoryName, branch: oldHead.name });
}
}

Expand Down

0 comments on commit d5de56b

Please sign in to comment.