Skip to content

Commit

Permalink
Allow checked out PRs to show diffs only against PR head
Browse files Browse the repository at this point in the history
Fixes #4720
  • Loading branch information
alexr00 committed May 24, 2023
1 parent 32b95c9 commit a246afc
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
15 changes: 15 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,12 @@
"category": "%command.pull.request.category%",
"icon": "$(comment)"
},
{
"command": "review.diffWithPrHead",
"title": "%command.review.diffWithPrHead.title%",
"category": "%command.pull.request.category%",
"icon": "$(cloud)"
},
{
"command": "issue.createIssueFromSelection",
"title": "%command.issue.createIssueFromSelection.title%",
Expand Down Expand Up @@ -1469,6 +1475,10 @@
"command": "pr.addFileComment",
"when": "false"
},
{
"command": "review.diffWithPrHead",
"when": "false"
},
{
"command": "issue.copyGithubPermalink",
"when": "github:hasGitHubRemotes"
Expand Down Expand Up @@ -1758,6 +1768,11 @@
"group": "inline@0",
"when": "!openDiffOnClick && view == prStatus:github && viewItem =~ /filechange(?!:DELETE)/"
},
{
"command": "review.diffWithPrHead",
"group": "inline@1",
"when": "openDiffOnClick && view == prStatus:github && viewItem =~ /filechange(?!:DELETE)/"
},
{
"command": "pr.openOriginalFile",
"when": "view =~ /(pr|prStatus):github/ && viewItem =~ /filechange:MODIFY/"
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
"command.pr.applySuggestion.title": "Apply Suggestion",
"command.pr.addLabelsToNewPr.title": "Add Labels",
"command.pr.addFileComment.title": "Add File Comment",
"command.review.diffWithPrHead.title": "Open Diff With Pull Request Head (readonly)",
"command.issues.category": "GitHub Issues",
"command.issue.createIssueFromSelection.title": "Create Issue From Selection",
"command.issue.createIssueFromClipboard.title": "Create Issue From Clipboard",
Expand Down
24 changes: 23 additions & 1 deletion src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IComment } from './common/comment';
import Logger from './common/logger';
import { FILE_LIST_LAYOUT, PR_SETTINGS_NAMESPACE } from './common/settingKeys';
import { ITelemetry } from './common/telemetry';
import { asImageDataURI, fromReviewUri, Schemes } from './common/uri';
import { asImageDataURI, fromReviewUri, Schemes, toPRUri } from './common/uri';
import { formatError } from './common/utils';
import { EXTENSION_ID } from './constants';
import { FolderRepositoryManager } from './github/folderRepositoryManager';
Expand Down Expand Up @@ -1242,6 +1242,28 @@ ${contents}
return vscode.commands.executeCommand('workbench.action.addComment', { fileComment: true });
}));

context.subscriptions.push(
vscode.commands.registerCommand('review.diffWithPrHead', async (fileChangeNode: GitFileChangeNode) => {
const fileName = fileChangeNode.fileName;
let parentURI = toPRUri(
fileChangeNode.resourceUri,
fileChangeNode.pullRequest,
fileChangeNode.pullRequest.base.sha,
fileChangeNode.pullRequest.head.sha,
fileChangeNode.fileName,
true,
fileChangeNode.status);
let headURI = toPRUri(
fileChangeNode.resourceUri,
fileChangeNode.pullRequest,
fileChangeNode.pullRequest.base.sha,
fileChangeNode.pullRequest.head.sha,
fileChangeNode.fileName,
false,
fileChangeNode.status);
return vscode.commands.executeCommand('vscode.diff', parentURI, headURI, `${fileName} (Pull Request Diff with Head)`);
}));

function goToNextPrevDiff(diffs: vscode.LineChange[], next: boolean) {
const tab = vscode.window.tabGroups.activeTabGroup.activeTab;
const input = tab?.input;
Expand Down
51 changes: 50 additions & 1 deletion src/view/reviewManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import { GithubItemStateEnum } from '../github/interface';
import { PullRequestGitHelper, PullRequestMetadata } from '../github/pullRequestGitHelper';
import { IResolvedPullRequestModel, PullRequestModel } from '../github/pullRequestModel';
import { CreatePullRequestHelper } from './createPullRequestHelper';
import { GitFileChangeModel } from './fileChangeModel';
import { GitFileChangeModel, InMemFileChangeModel, RemoteFileChangeModel } from './fileChangeModel';
import { getGitHubFileContent } from './gitHubContentProvider';
import { getInMemPRFileSystemProvider, provideDocumentContentForChangeModel } from './inMemPRContentProvider';
import { PullRequestChangesTreeDataProvider } from './prChangesTreeDataProvider';
import { ProgressHelper } from './progress';
import { RemoteQuickPickItem } from './quickpick';
Expand All @@ -55,6 +56,7 @@ export class ReviewManager {
private _validateStatusInProgress?: Promise<void>;
private _reviewCommentController: ReviewCommentController | undefined;
private _quickDiffProvider: vscode.Disposable | undefined;
private _inMemGitHubContentProvider: vscode.Disposable | undefined;

private _statusBarItem: vscode.StatusBarItem;
private _prNumber?: number;
Expand Down Expand Up @@ -445,6 +447,7 @@ export class ReviewManager {
this._changesSinceLastReviewProgress.endProgress();
})
);
await this.registerGitHubInMemContentProvider();

this.statusBarItem.text = '$(git-pull-request) ' + vscode.l10n.t('Pull Request #{0}', pr.number);
this.statusBarItem.command = {
Expand Down Expand Up @@ -733,6 +736,50 @@ export class ReviewManager {
}
}

private async registerGitHubInMemContentProvider() {
this._inMemGitHubContentProvider?.dispose();
this._inMemGitHubContentProvider = undefined;

const pr = this._folderRepoManager.activePullRequest;
if (!pr) {
return;
}
const rawChanges = await pr.getFileChangesInfo();
const mergeBase = pr.mergeBase;
if (!mergeBase) {
return;
}
const changes = rawChanges.map(change => {
if (change instanceof SlimFileChange) {
return new RemoteFileChangeModel(this._folderRepoManager, change, pr);
}
return new InMemFileChangeModel(this._folderRepoManager,
pr as (PullRequestModel & IResolvedPullRequestModel),
change, true, mergeBase);
});

this._inMemGitHubContentProvider = getInMemPRFileSystemProvider()?.registerTextDocumentContentProvider(
pr.number,
async (uri: vscode.Uri): Promise<string> => {
const params = fromPRUri(uri);
if (!params) {
return '';
}
const fileChange = changes.find(
contentChange => contentChange.fileName === params.fileName,
);

if (!fileChange) {
Logger.error(`Cannot find content for document ${uri.toString()}`, 'PR');
return '';
}

return provideDocumentContentForChangeModel(this._folderRepoManager, pr, params, fileChange);

},
);
}

private async registerCommentController() {
if (this._folderRepoManager.activePullRequest?.reviewThreadsCacheReady && this._reviewModel.hasLocalFileChanges) {
await this.doRegisterCommentController();
Expand Down Expand Up @@ -1082,6 +1129,8 @@ export class ReviewManager {

this._reviewCommentController?.dispose();
this._reviewCommentController = undefined;
this._inMemGitHubContentProvider?.dispose();
this._inMemGitHubContentProvider = undefined;
}

async provideTextDocumentContent(uri: vscode.Uri): Promise<string | undefined> {
Expand Down

0 comments on commit a246afc

Please sign in to comment.