Skip to content

Commit

Permalink
Fixes line history w/ uncommitted & dirty changes
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Apr 28, 2019
1 parent 4658b80 commit 97456ad
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
8 changes: 8 additions & 0 deletions src/git/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1021,6 +1021,14 @@ export class GitService implements Disposable {
return this.getBlameForRangeSync(blame, uri, range);
}

@log()
async getBlameForRangeContents(uri: GitUri, range: Range, contents: string): Promise<GitBlameLines | undefined> {
const blame = await this.getBlameForFileContents(uri, contents);
if (blame === undefined) return undefined;

return this.getBlameForRangeSync(blame, uri, range);
}

@log({
args: { 0: blame => '<blame>' }
})
Expand Down
36 changes: 27 additions & 9 deletions src/views/nodes/lineHistoryNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import { insertDateMarkers } from './helpers';
import { ResourceType, SubscribeableViewNode, ViewNode } from './viewNode';

export class LineHistoryNode extends SubscribeableViewNode {
constructor(uri: GitUri, view: View, parent: ViewNode, public readonly selection: Selection) {
constructor(
uri: GitUri,
view: View,
parent: ViewNode,
public readonly selection: Selection,
private readonly _editorContents: string | undefined
) {
super(uri, view, parent);
}

Expand All @@ -29,10 +35,26 @@ export class LineHistoryNode extends SubscribeableViewNode {
CommitFileNodeDisplayAs.CommitLabel |
(this.view.config.avatars ? CommitFileNodeDisplayAs.Gravatar : CommitFileNodeDisplayAs.StatusIcon);

let selection = this.selection;

if (this.uri.sha === undefined) {
// Check for any uncommitted changes in the range
const blame = await Container.git.getBlameForRange(this.uri, this.selection);
const blame = this._editorContents
? await Container.git.getBlameForRangeContents(this.uri, selection, this._editorContents)
: await Container.git.getBlameForRange(this.uri, selection);
if (blame !== undefined) {
const firstLine = blame.lines[0];
const lastLine = blame.lines[blame.lines.length - 1];

// Since there could be a change in the line numbers, update the selection
const firstActive = selection.active.line === firstLine.line - 1;
selection = new Selection(
(firstActive ? lastLine : firstLine).originalLine - 1,
selection.anchor.character,
(firstActive ? firstLine : lastLine).originalLine - 1,
selection.active.character
);

for (const commit of blame.commits.values()) {
if (!commit.isUncommitted) continue;

Expand Down Expand Up @@ -63,11 +85,7 @@ export class LineHistoryNode extends SubscribeableViewNode {
commit.originalFileName || commit.fileName
);

children.splice(
0,
0,
new CommitFileNode(this.view, this, file, uncommitted, displayAs, this.selection)
);
children.splice(0, 0, new CommitFileNode(this.view, this, file, uncommitted, displayAs, selection));

break;
}
Expand All @@ -76,14 +94,14 @@ export class LineHistoryNode extends SubscribeableViewNode {

const log = await Container.git.getLogForFile(this.uri.repoPath, this.uri.fsPath, {
ref: this.uri.sha,
range: this.selection
range: selection
});
if (log !== undefined) {
children.push(
...insertDateMarkers(
Iterables.filterMap(
log.commits.values(),
c => new CommitFileNode(this.view, this, c.files[0], c, displayAs, this.selection)
c => new CommitFileNode(this.view, this, c.files[0], c, displayAs, selection)
),
this
)
Expand Down
6 changes: 5 additions & 1 deletion src/views/nodes/lineHistoryTrackerNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ResourceType, SubscribeableViewNode, unknownGitUri, ViewNode } from './
export class LineHistoryTrackerNode extends SubscribeableViewNode<LineHistoryView> {
private _base: string | undefined;
private _child: LineHistoryNode | undefined;
private _editorContents: string | undefined;
private _selection: Selection | undefined;

constructor(view: LineHistoryView) {
Expand Down Expand Up @@ -54,7 +55,7 @@ export class LineHistoryTrackerNode extends SubscribeableViewNode<LineHistoryVie
sha: this.uri.sha || this._base
};
const fileUri = new GitUri(this.uri, commitish);
this._child = new LineHistoryNode(fileUri, this.view, this, this._selection!);
this._child = new LineHistoryNode(fileUri, this.view, this, this._selection!, this._editorContents);
}

return [this._child];
Expand Down Expand Up @@ -95,6 +96,7 @@ export class LineHistoryTrackerNode extends SubscribeableViewNode<LineHistoryVie

if (reset) {
this._uri = unknownGitUri;
this._editorContents = undefined;
this._selection = undefined;
this.resetChild();
}
Expand All @@ -110,6 +112,7 @@ export class LineHistoryTrackerNode extends SubscribeableViewNode<LineHistoryVie
}

this._uri = unknownGitUri;
this._editorContents = undefined;
this._selection = undefined;
this.resetChild();

Expand Down Expand Up @@ -140,6 +143,7 @@ export class LineHistoryTrackerNode extends SubscribeableViewNode<LineHistoryVie
}

this._uri = gitUri;
this._editorContents = editor.document.isDirty ? editor.document.getText() : undefined;
this._selection = editor.selection;
this.resetChild();

Expand Down

0 comments on commit 97456ad

Please sign in to comment.