Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Git - add more tracing to stage/unstage/revert commands #236409

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Git - add more tracing to stage/unstage/revert commands
  • Loading branch information
lszomoru committed Dec 17, 2024
commit ffcf623ad9cfa4d627c090a80a4ef103cb3c7047
13 changes: 5 additions & 8 deletions extensions/git/src/blame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { DecorationOptions, l10n, Position, Range, TextEditor, TextEditorChange, TextEditorDecorationType, TextEditorChangeKind, ThemeColor, Uri, window, workspace, EventEmitter, ConfigurationChangeEvent, StatusBarItem, StatusBarAlignment, Command, MarkdownString, TextEditorDiffInformation } from 'vscode';
import { DecorationOptions, l10n, Position, Range, TextEditor, TextEditorChange, TextEditorDecorationType, TextEditorChangeKind, ThemeColor, Uri, window, workspace, EventEmitter, ConfigurationChangeEvent, StatusBarItem, StatusBarAlignment, Command, MarkdownString } from 'vscode';
import { Model } from './model';
import { dispose, fromNow, IDisposable } from './util';
import { Repository } from './repository';
import { throttle } from './decorators';
import { BlameInformation } from './git';
import { fromGitUri, isGitUri } from './uri';
import { emojify, ensureEmojis } from './emoji';
import { getWorkingTreeAndIndexDiffInformation, getWorkingTreeDiffInformation } from './staging';

function lineRangesContainLine(changes: readonly TextEditorChange[], lineNumber: number): boolean {
return changes.some(c => c.modified.startLineNumber <= lineNumber && lineNumber < c.modified.endLineNumberExclusive);
Expand Down Expand Up @@ -277,10 +278,6 @@ export class GitBlameController {
return blameInformation;
}

private _findDiffInformation(textEditor: TextEditor, ref: string): TextEditorDiffInformation | undefined {
return textEditor.diffInformation?.find(diff => diff.original && isGitUri(diff.original) && fromGitUri(diff.original).ref === ref);
}

@throttle
private async _updateTextEditorBlameInformation(textEditor: TextEditor | undefined, showBlameInformationForPositionZero = false): Promise<void> {
if (!textEditor?.diffInformation || textEditor !== window.activeTextEditor) {
Expand Down Expand Up @@ -319,7 +316,7 @@ export class GitBlameController {
workingTreeAndIndexChanges = undefined;
} else if (ref === '') {
// Resource on the right-hand side of the diff editor when viewing a resource from the index.
const diffInformationWorkingTreeAndIndex = this._findDiffInformation(textEditor, 'HEAD');
const diffInformationWorkingTreeAndIndex = getWorkingTreeAndIndexDiffInformation(textEditor);

// Working tree + index diff information is present and it is stale
if (diffInformationWorkingTreeAndIndex && diffInformationWorkingTreeAndIndex.isStale) {
Expand All @@ -333,15 +330,15 @@ export class GitBlameController {
}
} else {
// Working tree diff information. Diff Editor (Working Tree) -> Text Editor
const diffInformationWorkingTree = this._findDiffInformation(textEditor, '~') ?? this._findDiffInformation(textEditor, '');
const diffInformationWorkingTree = getWorkingTreeDiffInformation(textEditor);

// Working tree diff information is not present or it is stale
if (!diffInformationWorkingTree || diffInformationWorkingTree.isStale) {
return;
}

// Working tree + index diff information
const diffInformationWorkingTreeAndIndex = this._findDiffInformation(textEditor, 'HEAD');
const diffInformationWorkingTreeAndIndex = getWorkingTreeAndIndexDiffInformation(textEditor);

// Working tree + index diff information is present and it is stale
if (diffInformationWorkingTreeAndIndex && diffInformationWorkingTreeAndIndex.isStale) {
Expand Down
26 changes: 25 additions & 1 deletion extensions/git/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ForcePushMode, GitErrorCodes, Ref, RefType, Status, CommitOptions, Remo
import { Git, Stash } from './git';
import { Model } from './model';
import { GitResourceGroup, Repository, Resource, ResourceGroupType } from './repository';
import { DiffEditorSelectionHunkToolbarContext, applyLineChanges, getModifiedRange, intersectDiffWithRange, invertLineChange, toLineRanges } from './staging';
import { DiffEditorSelectionHunkToolbarContext, applyLineChanges, getModifiedRange, getWorkingTreeAndIndexDiffInformation, getWorkingTreeDiffInformation, intersectDiffWithRange, invertLineChange, toLineChanges, toLineRanges } from './staging';
import { fromGitUri, toGitUri, isGitUri, toMergeUris, toMultiFileDiffEditorUris } from './uri';
import { dispose, grep, isDefined, isDescendant, pathEquals, relativePath, truncate } from './util';
import { GitTimelineItem } from './timelineProvider';
Expand Down Expand Up @@ -1565,6 +1565,12 @@ export class CommandCenter {

this.logger.trace(`[CommandCenter][stageSelectedChanges] changes: ${JSON.stringify(changes)}`);

const workingTreeDiffInformation = getWorkingTreeDiffInformation(textEditor);
if (workingTreeDiffInformation) {
this.logger.trace(`[CommandCenter][stageSelectedChanges] diffInformation: ${JSON.stringify(workingTreeDiffInformation)}`);
this.logger.trace(`[CommandCenter][stageSelectedChanges] diffInformation changes: ${JSON.stringify(toLineChanges(workingTreeDiffInformation))}`);
}

const modifiedDocument = textEditor.document;
const selectedLines = toLineRanges(textEditor.selections, modifiedDocument);
const selectedChanges = changes
Expand Down Expand Up @@ -1751,6 +1757,12 @@ export class CommandCenter {

this.logger.trace(`[CommandCenter][revertSelectedRanges] changes: ${JSON.stringify(changes)}`);

const workingTreeDiffInformation = getWorkingTreeDiffInformation(textEditor);
if (workingTreeDiffInformation) {
this.logger.trace(`[CommandCenter][revertSelectedRanges] diffInformation: ${JSON.stringify(workingTreeDiffInformation)}`);
this.logger.trace(`[CommandCenter][revertSelectedRanges] diffInformation changes: ${JSON.stringify(toLineChanges(workingTreeDiffInformation))}`);
}

const modifiedDocument = textEditor.document;
const selections = textEditor.selections;
const selectedChanges = changes.filter(change => {
Expand Down Expand Up @@ -1845,6 +1857,18 @@ export class CommandCenter {

this.logger.trace(`[CommandCenter][unstageSelectedRanges] changes: ${JSON.stringify(changes)}`);

const workingTreeDiffInformation = getWorkingTreeDiffInformation(textEditor);
if (workingTreeDiffInformation) {
this.logger.trace(`[CommandCenter][unstageSelectedRanges] diffInformation (working tree): ${JSON.stringify(workingTreeDiffInformation)}`);
this.logger.trace(`[CommandCenter][unstageSelectedRanges] diffInformation changes (working tree): ${JSON.stringify(toLineChanges(workingTreeDiffInformation))}`);
}

const workingTreeAndIndexDiffInformation = getWorkingTreeAndIndexDiffInformation(textEditor);
if (workingTreeAndIndexDiffInformation) {
this.logger.trace(`[CommandCenter][unstageSelectedRanges] diffInformation (working tree + index): ${JSON.stringify(workingTreeAndIndexDiffInformation)}`);
this.logger.trace(`[CommandCenter][unstageSelectedRanges] diffInformation changes (working tree + index): ${JSON.stringify(toLineChanges(workingTreeAndIndexDiffInformation))}`);
}

const originalUri = toGitUri(modifiedUri, 'HEAD');
const originalDocument = await workspace.openTextDocument(originalUri);
const selectedLines = toLineRanges(textEditor.selections, modifiedDocument);
Expand Down
50 changes: 49 additions & 1 deletion extensions/git/src/staging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/

import { TextDocument, Range, LineChange, Selection, Uri } from 'vscode';
import { TextDocument, Range, LineChange, Selection, Uri, TextEditor, TextEditorDiffInformation } from 'vscode';
import { fromGitUri, isGitUri } from './uri';

export function applyLineChanges(original: TextDocument, modified: TextDocument, diffs: LineChange[]): string {
const result: string[] = [];
Expand Down Expand Up @@ -143,6 +144,53 @@ export function invertLineChange(diff: LineChange): LineChange {
};
}

export function toLineChanges(diffInformation: TextEditorDiffInformation): LineChange[] {
return diffInformation.changes.map(x => {
let originalStartLineNumber: number;
let originalEndLineNumber: number;
let modifiedStartLineNumber: number;
let modifiedEndLineNumber: number;

if (x.original.startLineNumber === x.original.endLineNumberExclusive) {
// Insertion
originalStartLineNumber = x.original.startLineNumber - 1;
originalEndLineNumber = 0;
} else {
originalStartLineNumber = x.original.startLineNumber;
originalEndLineNumber = x.original.endLineNumberExclusive - 1;
}

if (x.modified.startLineNumber === x.modified.endLineNumberExclusive) {
// Deletion
modifiedStartLineNumber = x.modified.startLineNumber - 1;
modifiedEndLineNumber = 0;
} else {
modifiedStartLineNumber = x.modified.startLineNumber;
modifiedEndLineNumber = x.modified.endLineNumberExclusive - 1;
}

return {
originalStartLineNumber,
originalEndLineNumber,
modifiedStartLineNumber,
modifiedEndLineNumber
};
});
}

export function getWorkingTreeDiffInformation(textEditor: TextEditor): TextEditorDiffInformation | undefined {
// Working tree diff information. Diff Editor (Working Tree) -> Text Editor
return getDiffInformation(textEditor, '~') ?? getDiffInformation(textEditor, '');
}

export function getWorkingTreeAndIndexDiffInformation(textEditor: TextEditor): TextEditorDiffInformation | undefined {
return getDiffInformation(textEditor, 'HEAD');
}

function getDiffInformation(textEditor: TextEditor, ref: string): TextEditorDiffInformation | undefined {
return textEditor.diffInformation?.find(diff => diff.original && isGitUri(diff.original) && fromGitUri(diff.original).ref === ref);
}

export interface DiffEditorSelectionHunkToolbarContext {
mapping: unknown;
/**
Expand Down
Loading