Skip to content

Commit

Permalink
Refactors Git provider into sub-providers
Browse files Browse the repository at this point in the history
 - Commit operations
  • Loading branch information
eamodio committed Jan 21, 2025
1 parent 1f93828 commit 3566927
Show file tree
Hide file tree
Showing 62 changed files with 1,899 additions and 2,046 deletions.
2 changes: 1 addition & 1 deletion src/ai/aiProviderService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ export class AIProviderService implements Disposable {

const commit = isCommit(commitOrRevision)
? commitOrRevision
: await this.container.git.getCommit(commitOrRevision.repoPath, commitOrRevision.ref);
: await this.container.git.commits(commitOrRevision.repoPath).getCommit(commitOrRevision.ref);
if (commit == null) throw new Error('Unable to find commit');

if (!commit.hasFullDetails()) {
Expand Down
26 changes: 6 additions & 20 deletions src/annotations/gutterChangesAnnotationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,19 +107,14 @@ export class GutterChangesAnnotationProvider extends AnnotationProviderBase<Chan

let commit: GitCommit | undefined;

const commitsProvider = this.container.git.commits(this.trackedDocument.uri.repoPath!);

let localChanges = ref1 == null && ref2 == null;
if (localChanges) {
let ref = await this.container.git.getOldestUnpushedRefForFile(
this.trackedDocument.uri.repoPath!,
this.trackedDocument.uri,
);
let ref = await commitsProvider.getOldestUnpushedRefForFile(this.trackedDocument.uri);
if (ref != null) {
ref = `${ref}^`;
commit = await this.container.git.getCommitForFile(
this.trackedDocument.uri.repoPath,
this.trackedDocument.uri,
{ ref: ref },
);
commit = await commitsProvider.getCommitForFile(this.trackedDocument.uri, { ref: ref });
if (commit != null) {
if (ref2 != null) {
ref2 = ref;
Expand All @@ -139,10 +134,7 @@ export class GutterChangesAnnotationProvider extends AnnotationProviderBase<Chan
await this.container.git.getCurrentUser(this.trackedDocument.uri.repoPath!),
);
if (commits?.length) {
commit = await this.container.git.getCommitForFile(
this.trackedDocument.uri.repoPath,
this.trackedDocument.uri,
);
commit = await commitsProvider.getCommitForFile(this.trackedDocument.uri);
ref1 = 'HEAD';
} else if (this.trackedDocument.dirty) {
ref1 = 'HEAD';
Expand All @@ -153,13 +145,7 @@ export class GutterChangesAnnotationProvider extends AnnotationProviderBase<Chan
}

if (!localChanges) {
commit = await this.container.git.getCommitForFile(
this.trackedDocument.uri.repoPath,
this.trackedDocument.uri,
{
ref: ref2 ?? ref1,
},
);
commit = await commitsProvider.getCommitForFile(this.trackedDocument.uri, { ref: ref2 ?? ref1 });

if (commit != null) {
if (ref2 != null) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/copyMessageToClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class CopyMessageToClipboardCommand extends ActiveEditorCommand {
repoPath = this.container.git.getBestRepository(editor)?.path;
if (!repoPath) return;

const log = await this.container.git.getLog(repoPath, { limit: 1 });
const log = await this.container.git.commits(repoPath).getLog({ limit: 1 });
if (log == null) return;

const commit = first(log.commits.values());
Expand Down
2 changes: 1 addition & 1 deletion src/commands/copyShaToClipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class CopyShaToClipboardCommand extends ActiveEditorCommand {
const repoPath = this.container.git.getBestRepository(editor)?.path;
if (!repoPath) return;

const log = await this.container.git.getLog(repoPath, { limit: 1 });
const log = await this.container.git.commits(repoPath).getLog({ limit: 1 });
if (log == null) return;

args.sha = first(log.commits.values())?.sha;
Expand Down
9 changes: 4 additions & 5 deletions src/commands/diffFolderWithRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ export class DiffFolderWithRevisionCommand extends ActiveEditorCommand {
?.path;
if (!repoPath) return;

const log = this.container.git
.getLogForFile(gitUri.repoPath, gitUri.fsPath)
const commitsProvider = this.container.git.commits(repoPath);
const log = commitsProvider
.getLogForFile(gitUri.fsPath)
.then(
log =>
log ??
(gitUri.sha
? this.container.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, { ref: gitUri.sha })
: undefined),
(gitUri.sha ? commitsProvider.getLogForFile(gitUri.fsPath, { ref: gitUri.sha }) : undefined),
);

const relativePath = this.container.git.getRelativePath(uri, repoPath);
Expand Down
8 changes: 3 additions & 5 deletions src/commands/diffWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,9 @@ export class DiffWithCommand extends GlCommandBase {

if (args.rhs.sha && args.rhs.sha !== deletedOrMissing) {
// Ensure that the file still exists in this commit
const status = await this.container.git.getFileStatusForCommit(
args.repoPath,
args.rhs.uri,
args.rhs.sha,
);
const status = await this.container.git
.commits(args.repoPath)
.getFileStatusForCommit(args.rhs.uri, args.rhs.sha);
if (status?.status === 'D') {
args.rhs.sha = deletedOrMissing;
} else {
Expand Down
9 changes: 4 additions & 5 deletions src/commands/diffWithRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,13 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand {
}

try {
const log = this.container.git
.getLogForFile(gitUri.repoPath, gitUri.fsPath)
const commitsProvider = this.container.git.commits(gitUri.repoPath!);
const log = commitsProvider
.getLogForFile(gitUri.fsPath)
.then(
log =>
log ??
(gitUri.sha
? this.container.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, { ref: gitUri.sha })
: undefined),
(gitUri.sha ? commitsProvider.getLogForFile(gitUri.fsPath, { ref: gitUri.sha }) : undefined),
);

const title = `Open Changes with Revision${pad(GlyphChars.Dot, 2, 2)}`;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git/cherry-pick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ export class CherryPickGitCommand extends QuickCommand<State> {

let log = context.cache.get(ref);
if (log == null) {
log = this.container.git.getLog(state.repo.path, { ref: ref, merges: 'first-parent' });
log = state.repo.git.commits().getLog({ ref: ref, merges: 'first-parent' });
context.cache.set(ref, log);
}

Expand Down
6 changes: 3 additions & 3 deletions src/commands/git/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ export class LogGitCommand extends QuickCommand<State> {
if (log == null) {
log =
state.fileName != null
? this.container.git.getLogForFile(state.repo.path, state.fileName, { ref: ref })
: this.container.git.getLog(state.repo.path, { ref: ref });
? state.repo.git.commits().getLogForFile(state.fileName, { ref: ref })
: state.repo.git.commits().getLog({ ref: ref });
context.cache.set(ref, log);
}

Expand All @@ -187,7 +187,7 @@ export class LogGitCommand extends QuickCommand<State> {
}

if (!(state.reference instanceof GitCommit) || state.reference.file != null) {
state.reference = await this.container.git.getCommit(state.repo.path, state.reference.ref);
state.reference = await state.repo.git.commits().getCommit(state.reference.ref);
}

let result: StepResult<ReturnType<typeof getSteps>>;
Expand Down
9 changes: 4 additions & 5 deletions src/commands/git/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export class MergeGitCommand extends QuickCommand<State> {

let log = context.cache.get(ref);
if (log == null) {
log = this.container.git.getLog(state.repo.path, { ref: ref, merges: 'first-parent' });
log = state.repo.git.commits().getLog({ ref: ref, merges: 'first-parent' });
context.cache.set(ref, log);
}

Expand Down Expand Up @@ -211,10 +211,9 @@ export class MergeGitCommand extends QuickCommand<State> {
}

private async *confirmStep(state: MergeStepState, context: Context): AsyncStepResultGenerator<Flags[]> {
const counts = await this.container.git.getLeftRightCommitCount(
state.repo.path,
createRevisionRange(context.destination.ref, state.reference.ref, '...'),
);
const counts = await state.repo.git
.commits()
.getLeftRightCommitCount(createRevisionRange(context.destination.ref, state.reference.ref, '...'));

const title = `Merge ${getReferenceLabel(state.reference, {
icon: false,
Expand Down
12 changes: 6 additions & 6 deletions src/commands/git/rebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class RebaseGitCommand extends QuickCommand<State> {

let log = context.cache.get(ref);
if (log == null) {
log = this.container.git.getLog(state.repo.path, { ref: ref, merges: 'first-parent' });
log = state.repo.git.commits().getLog({ ref: ref, merges: 'first-parent' });
context.cache.set(ref, log);
}

Expand Down Expand Up @@ -221,11 +221,11 @@ export class RebaseGitCommand extends QuickCommand<State> {
}

private async *confirmStep(state: RebaseStepState, context: Context): AsyncStepResultGenerator<Flags[]> {
const counts = await this.container.git.getLeftRightCommitCount(
state.repo.path,
createRevisionRange(state.destination.ref, context.branch.ref, '...'),
{ excludeMerges: true },
);
const counts = await state.repo.git
.commits()
.getLeftRightCommitCount(createRevisionRange(state.destination.ref, context.branch.ref, '...'), {
excludeMerges: true,
});

const title = `${context.title} ${getReferenceLabel(state.destination, { icon: false, label: false })}`;
const ahead = counts != null ? counts.right : 0;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class ResetGitCommand extends QuickCommand<State> {

let log = context.cache.get(ref);
if (log == null) {
log = this.container.git.getLog(state.repo.path, { ref: ref, merges: 'first-parent' });
log = state.repo.git.commits().getLog({ ref: ref, merges: 'first-parent' });
context.cache.set(ref, log);
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/git/revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class RevertGitCommand extends QuickCommand<State> {

let log = context.cache.get(ref);
if (log == null) {
log = this.container.git.getLog(state.repo.path, { ref: ref, merges: 'first-parent' });
log = state.repo.git.commits().getLog({ ref: ref, merges: 'first-parent' });
context.cache.set(ref, log);
}

Expand Down
4 changes: 3 additions & 1 deletion src/commands/git/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ export class ShowGitCommand extends QuickCommand<State> {
state.reference.file != null
) {
if (state.reference != null && !isCommit(state.reference)) {
state.reference = await this.container.git.getCommit(state.reference.repoPath, state.reference.ref);
state.reference = await this.container.git
.commits(state.reference.repoPath)
.getCommit(state.reference.ref);
}

if (state.counter < 2 || state.reference == null) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/git/worktree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ export class WorktreeGitCommand extends QuickCommand<State> {
}

if (!isSha(state.changes.baseSha)) {
const commit = await this.container.git.getCommit(state.repo.uri, state.changes.baseSha);
const commit = await state.repo.git.commits().getCommit(state.changes.baseSha);
if (commit != null) {
state.changes.baseSha = commit.sha;
}
Expand Down
5 changes: 3 additions & 2 deletions src/commands/openFileAtRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,12 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand {

try {
if (args.revisionUri == null) {
const log = this.container.git.getLogForFile(gitUri.repoPath, gitUri.fsPath).then(
const commitsProvider = this.container.git.commits(gitUri.repoPath!);
const log = commitsProvider.getLogForFile(gitUri.fsPath).then(
log =>
log ??
(gitUri.sha
? this.container.git.getLogForFile(gitUri.repoPath, gitUri.fsPath, {
? commitsProvider.getLogForFile(gitUri.fsPath, {
ref: gitUri.sha,
})
: undefined),
Expand Down
2 changes: 1 addition & 1 deletion src/commands/openFileOnRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class OpenFileOnRemoteCommand extends ActiveEditorCommand {
const gitUri = await GitUri.fromUri(uri);
if (gitUri.repoPath) {
if (gitUri.sha == null) {
const commit = await this.container.git.getCommitForFile(gitUri.repoPath, gitUri, {
const commit = await this.container.git.commits(gitUri.repoPath).getCommitForFile(gitUri, {
firstIfNotFound: true,
});

Expand Down
2 changes: 1 addition & 1 deletion src/commands/openRevisionFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class OpenRevisionFileCommand extends ActiveEditorCommand {
try {
if (args.revisionUri == null) {
if (gitUri?.sha) {
const commit = await this.container.git.getCommit(gitUri.repoPath!, gitUri.sha);
const commit = await this.container.git.commits(gitUri.repoPath!).getCommit(gitUri.sha);

args.revisionUri =
commit?.file?.status === 'D'
Expand Down
20 changes: 9 additions & 11 deletions src/commands/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export class CreateCloudPatchCommand extends CreatePatchCommandBase {
return showPatchesView({ mode: 'create' });
}

const create = await createDraft(this.container, repo, args);
const create = await createDraft(repo, args);
if (create == null) {
return showPatchesView({ mode: 'create', create: { repositories: [repo] } });
}
Expand Down Expand Up @@ -371,11 +371,7 @@ export class OpenCloudPatchCommand extends GlCommandBase {
}
}

async function createDraft(
container: Container,
repository: Repository,
args: CreatePatchCommandArgs,
): Promise<CreateDraft | undefined> {
async function createDraft(repository: Repository, args: CreatePatchCommandArgs): Promise<CreateDraft | undefined> {
if (args.to == null) return undefined;

const to = args.to ?? 'HEAD';
Expand All @@ -393,31 +389,33 @@ async function createDraft(

const create: CreateDraft = { changes: [change], title: args.title, description: args.description };

const commit = await container.git.getCommit(repository.uri, to);
const commitsProvider = repository.git.commits();

const commit = await commitsProvider.getCommit(to);
if (commit == null) return undefined;

if (args.from == null) {
if (commit.files == null) return;

change.files = [...commit.files];
} else {
const diff = await container.git.getDiff(repository.uri, to, args.from);
const diff = await repository.git.getDiff(to, args.from);
if (diff == null) return;

const result = await container.git.getDiffFiles(repository.uri, diff.contents);
const result = await repository.git.getDiffFiles(diff.contents);
if (result?.files == null) return;

change.files = result.files;

if (!isSha(args.to)) {
const commit = await container.git.getCommit(repository.uri, args.to);
const commit = await commitsProvider.getCommit(args.to);
if (commit != null) {
change.revision.to = commit.sha;
}
}

if (!isSha(args.from)) {
const commit = await container.git.getCommit(repository.uri, args.from);
const commit = await commitsProvider.getCommit(args.from);
if (commit != null) {
change.revision.from = commit.sha;
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/quickCommand.steps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ export function getValidateGitReferenceFn(
}
}

const commit = await repos.git.getCommit(value);
const commit = await repos.git.commits().getCommit(value);
quickpick.items = [
await createCommitQuickPickItem(commit!, true, {
alwaysShow: true,
Expand Down
2 changes: 1 addition & 1 deletion src/commands/showQuickCommit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export class ShowQuickCommitCommand extends ActiveEditorCachedCommand {
}

if (args.repoLog == null) {
args.commit = await this.container.git.getCommit(repoPath, args.sha);
args.commit = await this.container.git.commits(repoPath).getCommit(args.sha);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/showQuickCommitFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export class ShowQuickCommitFileCommand extends ActiveEditorCachedCommand {

if (args.fileLog == null) {
const repoPath = args.commit?.repoPath ?? gitUri.repoPath;
args.commit = await this.container.git.getCommitForFile(repoPath, gitUri, {
args.commit = await this.container.git.commits(repoPath!).getCommitForFile(gitUri, {
ref: args.sha,
});
if (args.commit == null) {
Expand Down
Loading

0 comments on commit 3566927

Please sign in to comment.