Skip to content

Commit 81b141c

Browse files
Copilotalexr00
andcommitted
Address PR review feedback - move to static method and fix parameter naming
Co-authored-by: alexr00 <38270282+alexr00@users.noreply.github.com>
1 parent 96c0c5b commit 81b141c

File tree

5 files changed

+69
-86
lines changed

5 files changed

+69
-86
lines changed

src/commands.ts

Lines changed: 15 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import * as pathLib from 'path';
88
import * as vscode from 'vscode';
9+
import { Repository } from './api/api';
910
import { GitErrorCodes } from './api/api1';
1011
import { CommentReply, findActiveHandler, resolveCommentHandler } from './commentHandlerResolver';
1112
import { COPILOT_LOGINS } from './common/copilot';
@@ -55,7 +56,7 @@ const DISCARD_CHANGES = vscode.l10n.t('Discard changes');
5556
* @param repository The git repository with uncommitted changes
5657
* @returns Promise<boolean> true if user chose to proceed (after staging/discarding), false if cancelled
5758
*/
58-
async function handleUncommittedChanges(repository: any): Promise<boolean> {
59+
async function handleUncommittedChanges(repository: Repository): Promise<boolean> {
5960
const hasWorkingTreeChanges = repository.state.workingTreeChanges.length > 0;
6061
const hasIndexChanges = repository.state.indexChanges.length > 0;
6162

@@ -520,19 +521,19 @@ export function registerCommands(
520521
);
521522
}
522523

523-
function isSourceControl(x: any): x is any {
524+
function isSourceControl(x: any): x is Repository {
524525
return !!x?.rootUri;
525526
}
526527

527528
context.subscriptions.push(
528529
vscode.commands.registerCommand(
529530
'pr.create',
530-
async (args?: { repoPath: string; compareBranch: string } | any) => {
531+
async (args?: { repoPath: string; compareBranch: string } | Repository) => {
531532
// The arguments this is called with are either from the SCM view, or manually passed.
532533
if (isSourceControl(args)) {
533534
(await chooseReviewManager(args.rootUri.fsPath))?.createPullRequest();
534535
} else {
535-
(await chooseReviewManager((args as any)?.repoPath))?.createPullRequest((args as any)?.compareBranch);
536+
(await chooseReviewManager(args?.repoPath))?.createPullRequest(args?.compareBranch);
536537
}
537538
},
538539
),
@@ -541,7 +542,7 @@ export function registerCommands(
541542
context.subscriptions.push(
542543
vscode.commands.registerCommand(
543544
'pr.pushAndCreate',
544-
async (args?: any) => {
545+
async (args?: any | Repository) => {
545546
if (isSourceControl(args)) {
546547
const reviewManager = await chooseReviewManager(args.rootUri.fsPath);
547548
const folderManager = reposManager.getManagerForFile(args.rootUri);
@@ -576,7 +577,7 @@ export function registerCommands(
576577
}
577578

578579
let pullRequestModel: PullRequestModel;
579-
let repository: any | undefined;
580+
let repository: Repository | undefined;
580581

581582
if (pr instanceof PRNode || pr instanceof RepositoryChangesNode) {
582583
pullRequestModel = pr.pullRequestModel;
@@ -654,75 +655,15 @@ export function registerCommands(
654655
);
655656

656657
context.subscriptions.push(
657-
vscode.commands.registerCommand('pr.openCommitChanges', async (commitShaOrUrl: string, folderManager?: FolderRepositoryManager) => {
658-
try {
659-
// Extract commit SHA from GitHub URL if needed
660-
let commitSha: string;
661-
if (commitShaOrUrl.includes('github.com') && commitShaOrUrl.includes('/commit/')) {
662-
const match = commitShaOrUrl.match(/\/commit\/([a-f0-9]{7,40})/);
663-
if (!match) {
664-
vscode.window.showErrorMessage(vscode.l10n.t('Invalid commit URL: {0}', commitShaOrUrl));
665-
return;
666-
}
667-
commitSha = match[1];
668-
} else {
669-
commitSha = commitShaOrUrl;
670-
}
671-
672-
// Use provided folder manager or try to find one for the active workspace
673-
if (!folderManager) {
674-
folderManager = reposManager.folderManagers.find(fm => fm.repository.rootUri.fsPath === vscode.workspace.workspaceFolders?.[0]?.uri.fsPath);
675-
if (!folderManager) {
676-
vscode.window.showErrorMessage(vscode.l10n.t('No repository found to show commit changes.'));
677-
return;
678-
}
679-
}
680-
681-
const repository = folderManager.repository;
682-
683-
// Get the changes for this commit
684-
const changes = await repository.diffBetween(commitSha + '^', commitSha);
685-
686-
if (changes.length === 0) {
687-
vscode.window.showInformationMessage(vscode.l10n.t('No file changes found in commit {0}', commitSha.substring(0, 7)));
688-
return;
689-
}
690-
691-
// Build arguments for vscode.changes command
692-
const args: [vscode.Uri, vscode.Uri | undefined, vscode.Uri | undefined][] = [];
693-
for (const change of changes) {
694-
// For each changed file, create URIs for before and after
695-
const parentCommit = commitSha + '^';
696-
let beforeUri: vscode.Uri | undefined;
697-
let afterUri: vscode.Uri | undefined;
698-
699-
if (change.status === 7) { // DELETED
700-
// File was deleted, show old version vs empty
701-
beforeUri = change.uri.with({ scheme: 'git', authority: parentCommit });
702-
afterUri = undefined;
703-
} else if (change.status === 1 || change.status === 11) { // INDEX_ADDED || ADDED_BY_US
704-
// File was added, show empty vs new version
705-
beforeUri = undefined;
706-
afterUri = change.uri.with({ scheme: 'git', authority: commitSha });
707-
} else {
708-
// File was modified, show old vs new
709-
beforeUri = change.uri.with({ scheme: 'git', authority: parentCommit });
710-
afterUri = change.uri.with({ scheme: 'git', authority: commitSha });
711-
}
712-
713-
args.push([change.uri, beforeUri, afterUri]);
714-
}
715-
716-
// Send telemetry
717-
folderManager.telemetry.sendTelemetryEvent('pr.openCommitChanges');
718-
719-
// Open multi diff editor
720-
return vscode.commands.executeCommand('vscode.changes', vscode.l10n.t('Changes in Commit {0}', commitSha.substring(0, 7)), args);
721-
722-
} catch (error) {
723-
Logger.error(`Failed to open commit changes: ${formatError(error)}`, 'Commands');
724-
vscode.window.showErrorMessage(vscode.l10n.t('Failed to open commit changes: {0}', formatError(error)));
658+
vscode.commands.registerCommand('pr.openCommitChanges', async (commitSha: string, folderManager?: FolderRepositoryManager) => {
659+
if (!folderManager) {
660+
folderManager = reposManager.folderManagers[0];
661+
}
662+
if (!folderManager) {
663+
vscode.window.showErrorMessage(vscode.l10n.t('No repository found'));
664+
return;
725665
}
666+
return PullRequestModel.openCommitChanges(folderManager, commitSha);
726667
}),
727668
);
728669

src/github/pullRequestModel.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,53 @@ export class PullRequestModel extends IssueModel<PullRequest> implements IPullRe
11901190
return vscode.commands.executeCommand('vscode.changes', vscode.l10n.t('Changes in Pull Request #{0}', pullRequestModel.number), args);
11911191
}
11921192

1193+
static async openCommitChanges(folderManager: FolderRepositoryManager, commitSha: string): Promise<void> {
1194+
try {
1195+
// Get the repository from the folder manager
1196+
const repository = folderManager.repository;
1197+
if (!repository) {
1198+
vscode.window.showErrorMessage(vscode.l10n.t('No repository found'));
1199+
return;
1200+
}
1201+
1202+
// Get the changes between the commit and its parent
1203+
const changes = await repository.diffBetween(commitSha + '~1', commitSha);
1204+
if (!changes || changes.length === 0) {
1205+
vscode.window.showInformationMessage(vscode.l10n.t('No changes found in commit {0}', commitSha.substring(0, 7)));
1206+
return;
1207+
}
1208+
1209+
// Create URI pairs for the multi diff editor
1210+
const args: [vscode.Uri, vscode.Uri | undefined, vscode.Uri | undefined][] = [];
1211+
for (const change of changes) {
1212+
const workspaceUri = vscode.Uri.file(path.resolve(repository.rootUri.fsPath, change.uri.fsPath));
1213+
1214+
if (change.status === GitChangeType.ADD) {
1215+
// For added files, show against empty
1216+
args.push([workspaceUri, undefined, workspaceUri]);
1217+
} else if (change.status === GitChangeType.DELETE) {
1218+
// For deleted files, show old version against empty
1219+
const beforeUri = change.originalUri || change.uri;
1220+
args.push([workspaceUri, beforeUri, undefined]);
1221+
} else {
1222+
// For modified files, show before and after
1223+
const beforeUri = change.originalUri || change.uri;
1224+
args.push([workspaceUri, beforeUri, workspaceUri]);
1225+
}
1226+
}
1227+
1228+
/* __GDPR__
1229+
"pr.openCommitChanges" : {}
1230+
*/
1231+
folderManager.telemetry.sendTelemetryEvent('pr.openCommitChanges');
1232+
1233+
return vscode.commands.executeCommand('vscode.changes', vscode.l10n.t('Changes in Commit {0}', commitSha.substring(0, 7)), args);
1234+
} catch (error) {
1235+
const errorMessage = error instanceof Error ? error.message : String(error);
1236+
vscode.window.showErrorMessage(vscode.l10n.t('Failed to open commit changes: {0}', errorMessage));
1237+
}
1238+
}
1239+
11931240
static async openDiffFromComment(
11941241
folderManager: FolderRepositoryManager,
11951242
pullRequestModel: PullRequestModel,

src/github/pullRequestOverview.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -527,10 +527,10 @@ export class PullRequestOverviewPanel extends IssueOverviewPanel<PullRequestMode
527527
}
528528
}
529529

530-
private async openCommitChanges(message: IRequestMessage<{ commitShaOrUrl: string }>): Promise<void> {
530+
private async openCommitChanges(message: IRequestMessage<{ commitSha: string }>): Promise<void> {
531531
try {
532-
const { commitShaOrUrl } = message.args;
533-
await vscode.commands.executeCommand('pr.openCommitChanges', commitShaOrUrl, this._folderRepositoryManager);
532+
const { commitSha } = message.args;
533+
await vscode.commands.executeCommand('pr.openCommitChanges', commitSha, this._folderRepositoryManager);
534534
} catch (error) {
535535
Logger.error(`Failed to open commit changes: ${formatError(error)}`, PullRequestOverviewPanel.ID);
536536
vscode.window.showErrorMessage(vscode.l10n.t('Failed to open commit changes: {0}', formatError(error)));

webviews/common/context.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export class PRContext {
254254

255255
public openSessionLog = (link: SessionLinkInfo, openToTheSide?: boolean) => this.postMessage({ command: 'pr.open-session-log', args: { link, openToTheSide } });
256256

257-
public openCommitChanges = (commitShaOrUrl: string) => this.postMessage({ command: 'pr.openCommitChanges', args: { commitShaOrUrl } });
257+
public openCommitChanges = (commitSha: string) => this.postMessage({ command: 'pr.openCommitChanges', args: { commitSha } });
258258

259259
setPR = (pr: PullRequest) => {
260260
this.pr = pr;

webviews/components/timeline.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ const CommitEventView = (event: CommitEvent) => {
110110

111111
const handleCommitClick = (e: React.MouseEvent) => {
112112
e.preventDefault();
113-
context.openCommitChanges(event.htmlUrl);
113+
context.openCommitChanges(event.sha);
114114
};
115115

116116
return (
@@ -309,12 +309,7 @@ function AddReviewSummaryComment() {
309309
const CommentEventView = (event: CommentEvent) => <CommentView headerInEditMode comment={event} />;
310310

311311
const MergedEventView = (event: MergedEvent) => {
312-
const { revert, pr, openCommitChanges } = useContext(PullRequestContext);
313-
314-
const handleCommitClick = (e: React.MouseEvent) => {
315-
e.preventDefault();
316-
openCommitChanges(event.commitUrl);
317-
};
312+
const { revert, pr } = useContext(PullRequestContext);
318313

319314
return (
320315
<div className="comment-container commit">
@@ -327,7 +322,7 @@ const MergedEventView = (event: MergedEvent) => {
327322
<AuthorLink for={event.user} />
328323
<div className="message">
329324
merged commit{nbsp}
330-
<a className="sha" onClick={handleCommitClick} style={{ cursor: 'pointer' }} title={event.commitUrl}>
325+
<a className="sha" href={event.commitUrl} title={event.commitUrl}>
331326
{event.sha.substr(0, 7)}
332327
</a>
333328
{nbsp}

0 commit comments

Comments
 (0)