Skip to content

Commit

Permalink
Refactors git provider naming and args consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Jan 22, 2025
1 parent 3566927 commit 6a5c158
Show file tree
Hide file tree
Showing 50 changed files with 407 additions and 427 deletions.
44 changes: 22 additions & 22 deletions src/annotations/gutterChangesAnnotationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,25 @@ export class GutterChangesAnnotationProvider extends AnnotationProviderBase<Chan
override async onProvideAnnotation(context?: ChangesAnnotationContext, state?: AnnotationState): Promise<boolean> {
const scope = getLogScope();

let ref1 = this.trackedDocument.uri.sha;
let ref2 = context?.sha != null && context.sha !== ref1 ? `${context.sha}^` : undefined;
let rev1 = this.trackedDocument.uri.sha;
let rev2 = context?.sha != null && context.sha !== rev1 ? `${context.sha}^` : undefined;

let commit: GitCommit | undefined;

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

let localChanges = ref1 == null && ref2 == null;
let localChanges = rev1 == null && rev2 == null;
if (localChanges) {
let ref = await commitsProvider.getOldestUnpushedRefForFile(this.trackedDocument.uri);
if (ref != null) {
ref = `${ref}^`;
commit = await commitsProvider.getCommitForFile(this.trackedDocument.uri, { ref: ref });
let rev = await commitsProvider.getOldestUnpushedShaForFile(this.trackedDocument.uri);
if (rev != null) {
rev = `${rev}^`;
commit = await commitsProvider.getCommitForFile(this.trackedDocument.uri, rev);
if (commit != null) {
if (ref2 != null) {
ref2 = ref;
if (rev2 != null) {
rev2 = rev;
} else {
ref1 = ref;
ref2 = '';
rev1 = rev;
rev2 = '';
}
} else {
localChanges = false;
Expand All @@ -135,40 +135,40 @@ export class GutterChangesAnnotationProvider extends AnnotationProviderBase<Chan
);
if (commits?.length) {
commit = await commitsProvider.getCommitForFile(this.trackedDocument.uri);
ref1 = 'HEAD';
rev1 = 'HEAD';
} else if (this.trackedDocument.dirty) {
ref1 = 'HEAD';
rev1 = 'HEAD';
} else {
localChanges = false;
}
}
}

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

if (commit != null) {
if (ref2 != null) {
ref2 = commit.ref;
if (rev2 != null) {
rev2 = commit.ref;
} else {
ref1 = `${commit.ref}^`;
ref2 = commit.ref;
rev1 = `${commit.ref}^`;
rev2 = commit.ref;
}
}
}

const diffs = (
await Promise.allSettled(
ref2 == null && this.editor.document.isDirty
rev2 == null && this.editor.document.isDirty
? [
this.container.git.getDiffForFileContents(
this.trackedDocument.uri,
ref1!,
rev1!,
this.editor.document.getText(),
),
this.container.git.getDiffForFile(this.trackedDocument.uri, ref1, ref2),
this.container.git.getDiffForFile(this.trackedDocument.uri, rev1, rev2),
]
: [this.container.git.getDiffForFile(this.trackedDocument.uri, ref1, ref2)],
: [this.container.git.getDiffForFile(this.trackedDocument.uri, rev1, rev2)],
)
)
.map(d => getSettledValue(d))
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.commits(repoPath).getLog({ limit: 1 });
const log = await this.container.git.commits(repoPath).getLog(undefined, { 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.commits(repoPath).getLog({ limit: 1 });
const log = await this.container.git.commits(repoPath).getLog(undefined, { limit: 1 });
if (log == null) return;

args.sha = first(log.commits.values())?.sha;
Expand Down
4 changes: 1 addition & 3 deletions src/commands/diffFolderWithRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ export class DiffFolderWithRevisionCommand extends ActiveEditorCommand {
const log = commitsProvider
.getLogForFile(gitUri.fsPath)
.then(
log =>
log ??
(gitUri.sha ? commitsProvider.getLogForFile(gitUri.fsPath, { ref: gitUri.sha }) : undefined),
log => log ?? (gitUri.sha ? commitsProvider.getLogForFile(gitUri.fsPath, gitUri.sha) : undefined),
);

const relativePath = this.container.git.getRelativePath(uri, repoPath);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/diffWith.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class DiffWithCommand extends GlCommandBase {
// Ensure that the file still exists in this commit
const status = await this.container.git
.commits(args.repoPath)
.getFileStatusForCommit(args.rhs.uri, args.rhs.sha);
.getCommitFileStatus(args.rhs.uri, args.rhs.sha);
if (status?.status === 'D') {
args.rhs.sha = deletedOrMissing;
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/commands/diffWithRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export class DiffWithRevisionCommand extends ActiveEditorCommand {
const log = commitsProvider
.getLogForFile(gitUri.fsPath)
.then(
log =>
log ??
(gitUri.sha ? commitsProvider.getLogForFile(gitUri.fsPath, { ref: gitUri.sha }) : undefined),
log => log ?? (gitUri.sha ? commitsProvider.getLogForFile(gitUri.fsPath, gitUri.sha) : undefined),
);

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

if (state.counter < 3 && context.selectedBranchOrTag != null) {
const ref = createRevisionRange(context.destination.ref, context.selectedBranchOrTag.ref, '..');
const rev = createRevisionRange(context.destination.ref, context.selectedBranchOrTag.ref, '..');

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

const result: StepResult<GitReference[]> = yield* pickCommitsStep(
state as CherryPickStepState,
context,
{
log: await log,
onDidLoadMore: log => context.cache.set(ref, Promise.resolve(log)),
onDidLoadMore: log => context.cache.set(rev, Promise.resolve(log)),
picked: state.references?.map(r => r.ref),
placeholder: (context, log) =>
log == null
Expand Down
12 changes: 6 additions & 6 deletions src/commands/git/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,21 +158,21 @@ export class LogGitCommand extends QuickCommand<State> {
}

if (state.counter < 3 && context.selectedBranchOrTag != null) {
const ref = context.selectedBranchOrTag.ref;
const rev = context.selectedBranchOrTag.ref;

let log = context.cache.get(ref);
let log = context.cache.get(rev);
if (log == null) {
log =
state.fileName != null
? state.repo.git.commits().getLogForFile(state.fileName, { ref: ref })
: state.repo.git.commits().getLog({ ref: ref });
context.cache.set(ref, log);
? state.repo.git.commits().getLogForFile(state.fileName, rev)
: state.repo.git.commits().getLog(rev);
context.cache.set(rev, log);
}

const result = yield* pickCommitStep(state, context, {
ignoreFocusOut: true,
log: await log,
onDidLoadMore: log => context.cache.set(ref, Promise.resolve(log)),
onDidLoadMore: log => context.cache.set(rev, Promise.resolve(log)),
placeholder: (context, log) =>
log == null
? `No commits found in ${getReferenceLabel(context.selectedBranchOrTag, {
Expand Down
10 changes: 5 additions & 5 deletions src/commands/git/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,18 @@ export class MergeGitCommand extends QuickCommand<State> {
context.selectedBranchOrTag != null &&
(context.pickCommit || context.pickCommitForItem || state.reference.ref === context.destination.ref)
) {
const ref = context.selectedBranchOrTag.ref;
const rev = context.selectedBranchOrTag.ref;

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

const result: StepResult<GitReference> = yield* pickCommitStep(state as MergeStepState, context, {
ignoreFocusOut: true,
log: await log,
onDidLoadMore: log => context.cache.set(ref, Promise.resolve(log)),
onDidLoadMore: log => context.cache.set(rev, Promise.resolve(log)),
placeholder: (context, log) =>
log == null
? `No commits found on ${getReferenceLabel(context.selectedBranchOrTag, {
Expand Down
10 changes: 5 additions & 5 deletions src/commands/git/rebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,18 +181,18 @@ export class RebaseGitCommand extends QuickCommand<State> {
context.selectedBranchOrTag != null &&
(context.pickCommit || context.pickCommitForItem || state.destination.ref === context.branch.ref)
) {
const ref = context.selectedBranchOrTag.ref;
const rev = context.selectedBranchOrTag.ref;

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

const result: StepResult<GitReference> = yield* pickCommitStep(state as RebaseStepState, context, {
ignoreFocusOut: true,
log: await log,
onDidLoadMore: log => context.cache.set(ref, Promise.resolve(log)),
onDidLoadMore: log => context.cache.set(rev, Promise.resolve(log)),
placeholder: (context, log) =>
log == null
? `No commits found on ${getReferenceLabel(context.selectedBranchOrTag, {
Expand Down
10 changes: 5 additions & 5 deletions src/commands/git/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,17 +132,17 @@ export class ResetGitCommand extends QuickCommand<State> {
context.title = `${this.title} ${getReferenceLabel(context.destination, { icon: false })}`;

if (state.counter < 2 || state.reference == null) {
const ref = context.destination.ref;
const rev = context.destination.ref;

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

const result: StepResult<GitReference> = yield* pickCommitStep(state as ResetStepState, context, {
log: await log,
onDidLoadMore: log => context.cache.set(ref, Promise.resolve(log)),
onDidLoadMore: log => context.cache.set(rev, Promise.resolve(log)),
placeholder: (context, log) =>
log == null
? `${context.destination.name} has no commits`
Expand Down
10 changes: 5 additions & 5 deletions src/commands/git/revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,20 @@ export class RevertGitCommand extends QuickCommand<State> {
}

if (state.counter < 2 || state.references == null || state.references.length === 0) {
const ref = context.destination.ref;
const rev = context.destination.ref;

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

const result: StepResult<GitRevisionReference[]> = yield* pickCommitsStep(
state as RevertStepState,
context,
{
log: await log,
onDidLoadMore: log => context.cache.set(ref, Promise.resolve(log)),
onDidLoadMore: log => context.cache.set(rev, Promise.resolve(log)),
placeholder: (context, log) =>
log == null ? `${context.destination.name} has no commits` : 'Choose commits to revert',
picked: state.references?.map(r => r.ref),
Expand Down
15 changes: 6 additions & 9 deletions src/commands/openFileAtRevision.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,12 @@ export class OpenFileAtRevisionCommand extends ActiveEditorCommand {
try {
if (args.revisionUri == null) {
const commitsProvider = this.container.git.commits(gitUri.repoPath!);
const log = commitsProvider.getLogForFile(gitUri.fsPath).then(
log =>
log ??
(gitUri.sha
? commitsProvider.getLogForFile(gitUri.fsPath, {
ref: gitUri.sha,
})
: undefined),
);
const log = commitsProvider
.getLogForFile(gitUri.fsPath)
.then(
log =>
log ?? (gitUri.sha ? commitsProvider.getLogForFile(gitUri.fsPath, gitUri.sha) : undefined),
);

const title = `Open ${args.annotationType === 'blame' ? 'Blame' : 'File'} at Revision${pad(
GlyphChars.Dot,
Expand Down
7 changes: 3 additions & 4 deletions src/commands/openFileOnRemote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ export class OpenFileOnRemoteCommand extends ActiveEditorCommand {
const gitUri = await GitUri.fromUri(uri);
if (gitUri.repoPath) {
if (gitUri.sha == null) {
const commit = await this.container.git.commits(gitUri.repoPath).getCommitForFile(gitUri, {
firstIfNotFound: true,
});

const commit = await this.container.git
.commits(gitUri.repoPath)
.getCommitForFile(gitUri, undefined, { firstIfNotFound: true });
if (commit != null) {
args.sha = commit.sha;
}
Expand Down
4 changes: 1 addition & 3 deletions src/commands/showQuickCommitFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ export class ShowQuickCommitFileCommand extends ActiveEditorCachedCommand {

if (args.fileLog == null) {
const repoPath = args.commit?.repoPath ?? gitUri.repoPath;
args.commit = await this.container.git.commits(repoPath!).getCommitForFile(gitUri, {
ref: args.sha,
});
args.commit = await this.container.git.commits(repoPath!).getCommitForFile(gitUri, args.sha);
if (args.commit == null) {
void showCommitNotFoundWarningMessage('Unable to show commit file details');

Expand Down
Loading

0 comments on commit 6a5c158

Please sign in to comment.