diff --git a/src/github/createPRViewProvider.ts b/src/github/createPRViewProvider.ts index ad24a61d7d..7a344a5979 100644 --- a/src/github/createPRViewProvider.ts +++ b/src/github/createPRViewProvider.ts @@ -112,6 +112,31 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs super.show(); } + private async getTotalCommits(): Promise { + const origin = await this._folderRepositoryManager.getOrigin(this.compareBranch); + + if (this.compareBranch?.upstream) { + const headRepo = this._folderRepositoryManager.findRepo(byRemoteName(this.compareBranch.upstream.remote)); + + if (headRepo) { + const headBranch = `${headRepo.remote.owner}:${this.compareBranch.name ?? ''}`; + const baseBranch = `${this._pullRequestDefaults.owner}:${this._pullRequestDefaults.base}`; + const { total_commits } = await origin.compareCommits(baseBranch, headBranch); + + return total_commits; + } + } else if (this.compareBranch?.commit) { + // We can use the git API instead of the GitHub API + const baseBranch = await this._folderRepositoryManager.repository.getBranch(this._pullRequestDefaults.base); + if (baseBranch.commit) { + const changes = await this._folderRepositoryManager.repository.diffBetween(baseBranch.commit, this.compareBranch.commit); + return changes.length; + } + } + + return 0; + } + private async getTitle(): Promise { // Use same default as GitHub, if there is only one commit, use the commit, otherwise use the branch name, as long as it is not the default branch. // By default, the base branch we use for comparison is the base branch of origin. Compare this to the @@ -119,20 +144,15 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs const origin = await this._folderRepositoryManager.getOrigin(this.compareBranch); let useBranchName = false; - if (this.compareBranch?.upstream) { - const headRepo = this._folderRepositoryManager.findRepo(byRemoteName(this.compareBranch.upstream.remote)); - if (headRepo) { - const headBranch = `${headRepo.remote.owner}:${this.compareBranch.name ?? ''}`; - try { - const commits = await origin.compareCommits(`${this._pullRequestDefaults.owner}:${this._pullRequestDefaults.base}`, headBranch); - if (commits.total_commits > 1) { - const defaultBranch = await origin.getDefaultBranch(); - useBranchName = defaultBranch !== this.compareBranch.name; - } - } catch (e) { - // Ignore and fall back to commit message - } + + try { + const totalCommits = await this.getTotalCommits(); + if (totalCommits > 1) { + const defaultBranch = await origin.getDefaultBranch(); + useBranchName = defaultBranch !== this.compareBranch?.name; } + } catch (e) { + // Ignore and fall back to commit message } if (useBranchName) { @@ -164,6 +184,19 @@ export class CreatePullRequestViewProvider extends WebviewViewBase implements vs // Try to match github's default, first look for template, then use commit body if available. const pullRequestTemplate = await this.getPullRequestTemplate(); if (pullRequestTemplate) { + try { + const totalCommits = await this.getTotalCommits(); + + // If there's just a single commit, we include it as well as the PR template + if (totalCommits === 1 && this.compareBranch?.name) { + const message = titleAndBodyFrom(await this._folderRepositoryManager.getTipCommitMessage(this.compareBranch.name)).body; + + return `${message}\n\n${pullRequestTemplate}`; + } + } catch (e) { + // Ignore and fall back to the template + } + return pullRequestTemplate; }