Skip to content

Commit

Permalink
fix: quick diff in commit/codereview pages (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
conwnet authored Jan 19, 2023
1 parent 07a5ca3 commit 042eb4a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion extensions/github1s/src/adapters/github1s/parse-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const parsePullUrl = async (path: string): Promise<RouterState> => {
return {
repo: `${owner}/${repo}`,
pageType: PageType.CodeReview,
ref: codeReview.base.commitSha,
ref: codeReview.head.commitSha,
codeReviewId,
};
};
Expand Down
4 changes: 3 additions & 1 deletion extensions/github1s/src/changes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import * as vscode from 'vscode';
import * as adapterTypes from '@/adapters/types';
import { GitHub1sQuickDiffProvider } from './quick-diff';
import { getChangedFileDiffCommand, getChangedFiles } from './files';
import adapterManager from '@/adapters/manager';

export const updateSourceControlChanges = (() => {
const sourceControl = vscode.scm.createSourceControl('github1s', 'GitHub1s');
const rootUri = vscode.Uri.parse('').with({ scheme: adapterManager.getCurrentScheme() });
const sourceControl = vscode.scm.createSourceControl('github1s', 'GitHub1s', rootUri);
const changesGroup = sourceControl.createResourceGroup('changes', 'Changes');
sourceControl.quickDiffProvider = new GitHub1sQuickDiffProvider();

Expand Down
45 changes: 26 additions & 19 deletions extensions/github1s/src/changes/quick-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@

import * as vscode from 'vscode';
import router from '@/router';
import { Repository } from '@/repository';
import { emptyFileUri } from '@/providers';
import adapterManager from '@/adapters/manager';
import * as adapterTypes from '@/adapters/types';

// get the original source uri when the `routerState.pageType` is `PageType.PULL`
const getOriginalResourceForPull = async (uri: vscode.Uri, codeReviewId: string): Promise<vscode.Uri | null> => {
const routeState = await router.getState();
const dataSource = await adapterManager.getCurrentAdapter().resolveDataSource();
const codeReview = await dataSource.provideCodeReview(routeState.repo, codeReviewId);
const changedFile = codeReview?.files?.find((changedFile) => changedFile.path === uri.path.slice(1));

// TODO: why removed
if (!changedFile || changedFile.status === adapterTypes.FileChangeStatus.Removed) {
const currentScheme = adapterManager.getCurrentScheme();
const repository = Repository.getInstance(currentScheme, routeState.repo);
const codeReviewFiles = await repository.getCodeReviewChangedFiles(codeReviewId);
const changedFile = codeReviewFiles?.find((changedFile) => changedFile.path === uri.path.slice(1));

if (
!changedFile ||
changedFile.status === adapterTypes.FileChangeStatus.Added ||
changedFile.status === adapterTypes.FileChangeStatus.Removed
) {
return null;
}

if (changedFile.status === adapterTypes.FileChangeStatus.Added) {
return emptyFileUri;
const codeReview = await repository.getCodeReviewItem(codeReviewId);
if (!codeReview?.base?.commitSha) {
return null;
}

const originalAuthority = `${routeState.repo}+${codeReview!.base.commitSha}`;
Expand All @@ -34,20 +40,21 @@ const getOriginalResourceForPull = async (uri: vscode.Uri, codeReviewId: string)
// get the original source uri when the `routerState.pageType` is `PageType.COMMIT`
const getOriginalResourceForCommit = async (uri: vscode.Uri, commitSha: string) => {
const routeState = await router.getState();
const dataSource = await adapterManager.getCurrentAdapter().resolveDataSource();
const commit = await dataSource.provideCommit(routeState.repo, commitSha);
const changedFile = commit?.files?.find((changedFile) => changedFile.path === uri.path.slice(1));

if (!changedFile || changedFile.status === adapterTypes.FileChangeStatus.Removed) {
const currentScheme = adapterManager.getCurrentScheme();
const repository = Repository.getInstance(currentScheme, routeState.repo);
const commitFiles = await repository.getCommitChangedFiles(commitSha);
const changedFile = commitFiles?.find((changedFile) => changedFile.path === uri.path.slice(1));

if (
!changedFile ||
changedFile.status === adapterTypes.FileChangeStatus.Added ||
changedFile.status === adapterTypes.FileChangeStatus.Removed
) {
return null;
}

if (changedFile.status === adapterTypes.FileChangeStatus.Added) {
return emptyFileUri;
}

const parentCommitSha = commit!.parents?.[0] || 'HEAD';

const commit = await repository.getCommitItem(commitSha);
const parentCommitSha = commit?.parents?.[0];
if (!parentCommitSha) {
return emptyFileUri;
}
Expand Down

0 comments on commit 042eb4a

Please sign in to comment.