Skip to content

Commit

Permalink
Reverses refs for consistent comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Aug 6, 2019
1 parent 4fedcd8 commit c27681d
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 46 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

### Changed

- Reverses the order of comparisons in the _Compare_ view for consistent comparisons results

### Fixed

- Fixes [#812](https://github.com/eamodio/vscode-gitlens/issues/812) - Regression in 9.9.2: Clicking changed file in Repository Browser opens diff view between WorkingTree <-> WorkingTree, not index
Expand Down
5 changes: 2 additions & 3 deletions src/commands/diffDirectory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class DiffDirectoryCommand extends ActiveEditorCommand {
]);
}

protected preExecute(context: CommandContext, args: DiffDirectoryCommandArgs = {}) {
protected async preExecute(context: CommandContext, args: DiffDirectoryCommandArgs = {}) {
switch (context.command) {
case Commands.DiffDirectoryWithHead:
args.ref1 = 'HEAD';
Expand All @@ -41,8 +41,7 @@ export class DiffDirectoryCommand extends ActiveEditorCommand {

case Commands.ViewsOpenDirectoryDiff:
if (context.type === 'viewItem' && context.node instanceof CompareResultsNode) {
args.ref1 = context.node.ref1.ref;
args.ref2 = context.node.ref2.ref;
[args.ref1, args.ref2] = await context.node.getDiffRefs();
}
break;

Expand Down
2 changes: 1 addition & 1 deletion src/views/nodes/compareBranchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class CompareBranchNode extends ViewNode<RepositoriesView> {
if (this._compareWith === undefined) return [];

if (this._children === undefined) {
let ref1 = (this._compareWith && this._compareWith.ref) || 'HEAD';
let ref1 = this._compareWith.ref || 'HEAD';
if (this.comparisonNotation === '..') {
ref1 = (await Container.git.getMergeBase(this.branch.repoPath, ref1, this.branch.ref)) || ref1;
}
Expand Down
78 changes: 36 additions & 42 deletions src/views/nodes/compareResultsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
constructor(
view: CompareView,
public readonly repoPath: string,
private _ref1: NamedRef,
private _ref2: NamedRef,
private _ref: NamedRef,
private _compareWith: NamedRef,
private _pinned: boolean = false,
private _comparisonNotation?: '...' | '..'
) {
Expand All @@ -28,33 +28,16 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
}

get id(): string {
return `gitlens:repository(${this.repoPath}):compare(${this._ref1.ref}:${this._ref2.ref})|${this._instanceId}`;
}

get label() {
return `Comparing ${this._ref1.label ||
GitService.shortenSha(this._ref1.ref, { working: 'Working Tree' })} to ${this._ref2.label ||
GitService.shortenSha(this._ref2.ref, { working: 'Working Tree' })}`;
return `gitlens:repository(${this.repoPath}):compare(${this._ref.ref}:${this._compareWith.ref})|${this._instanceId}`;
}

get pinned(): boolean {
return this._pinned;
}

get ref1(): NamedRef {
return this._ref1;
}

get ref2(): NamedRef {
return this._ref2;
}

async getChildren(): Promise<ViewNode[]> {
if (this._children === undefined) {
let ref1 = this._ref1.ref;
if (this.comparisonNotation === '..') {
ref1 = (await Container.git.getMergeBase(this.repoPath, ref1, this._ref2.ref)) || ref1;
}
const [ref1, ref2] = await this.getDiffRefs();

this._children = [
new ResultsCommitsNode(
Expand All @@ -68,14 +51,7 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
includeDescription: true
}
),
new ResultsFilesNode(
this.view,
this,
this.uri.repoPath!,
ref1,
this._ref2.ref,
this.getFilesQuery.bind(this)
)
new ResultsFilesNode(this.view, this, this.uri.repoPath!, ref1, ref2, this.getFilesQuery.bind(this))
];
}
return this._children;
Expand All @@ -88,7 +64,11 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
description = (repo && repo.formattedName) || this.uri.repoPath;
}

const item = new TreeItem(this.label, this._state || TreeItemCollapsibleState.Collapsed);
const item = new TreeItem(
`Comparing ${this._ref.label || GitService.shortenSha(this._ref.ref, { working: 'Working Tree' })} to ${this
._compareWith.label || GitService.shortenSha(this._compareWith.ref, { working: 'Working Tree' })}`,
this._state || TreeItemCollapsibleState.Collapsed
);
item.contextValue = `${ResourceType.CompareResults}+${
this.comparisonNotation === '..' ? 'twodot' : 'threedot'
}`;
Expand All @@ -111,14 +91,28 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
return !this._pinned;
}

@gate()
@debug()
async getDiffRefs(): Promise<[string, string]> {
if (this.comparisonNotation === '..') {
return [
(await Container.git.getMergeBase(this.repoPath, this._compareWith.ref, this._ref.ref)) ||
this._compareWith.ref,
this._ref.ref
];
}

return [this._compareWith.ref, this._ref.ref];
}

@log()
async pin() {
if (this._pinned) return;

await this.view.updatePinnedComparison(this.getPinnableId(), {
path: this.repoPath,
ref1: this.ref1,
ref2: this.ref2,
ref1: this._ref,
ref2: this._compareWith,
notation: this._comparisonNotation
});

Expand All @@ -141,8 +135,8 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
if (this._pinned) {
await this.view.updatePinnedComparison(this.getPinnableId(), {
path: this.repoPath,
ref1: this.ref1,
ref2: this.ref2,
ref1: this._ref,
ref2: this._compareWith,
notation: this._comparisonNotation
});
}
Expand All @@ -156,17 +150,17 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
// Save the current id so we can update it later
const currentId = this.getPinnableId();

const ref1 = this._ref1;
this._ref1 = this._ref2;
this._ref2 = ref1;
const ref1 = this._ref;
this._ref = this._compareWith;
this._compareWith = ref1;

// If we were pinned, remove the existing pin and save a new one
if (this._pinned) {
await this.view.updatePinnedComparison(currentId);
await this.view.updatePinnedComparison(this.getPinnableId(), {
path: this.repoPath,
ref1: this.ref1,
ref2: this.ref2,
ref1: this._ref,
ref2: this._compareWith,
notation: this._comparisonNotation
});
}
Expand Down Expand Up @@ -203,7 +197,7 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
private async getCommitsQuery(maxCount: number | undefined): Promise<CommitsQueryResults> {
const log = await Container.git.getLog(this.uri.repoPath!, {
maxCount: maxCount,
ref: `${this._ref1.ref}${this.comparisonNotation}${this._ref2.ref || 'HEAD'}`
ref: `${this._compareWith.ref || 'HEAD'}${this.comparisonNotation}${this._ref.ref}`
});

const count = log !== undefined ? log.count : 0;
Expand All @@ -218,7 +212,7 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
private async getFilesQuery(): Promise<FilesQueryResults> {
const diff = await Container.git.getDiffStatus(
this.uri.repoPath!,
`${this._ref1.ref}${this.diffComparisonNotation}${this._ref2.ref || 'HEAD'}`
`${this._compareWith.ref || 'HEAD'}${this.diffComparisonNotation}${this._ref.ref || 'HEAD'}`
);

return {
Expand All @@ -228,6 +222,6 @@ export class CompareResultsNode extends SubscribeableViewNode<CompareView> {
}

private getPinnableId() {
return Strings.sha1(`${this.repoPath}|${this.ref1.ref}|${this.ref2.ref}`);
return Strings.sha1(`${this.repoPath}|${this._ref.ref}|${this._compareWith.ref}`);
}
}

0 comments on commit c27681d

Please sign in to comment.