From 55579cc4e6028d021f2833510d1bf3c82a9890e0 Mon Sep 17 00:00:00 2001 From: "Z. Dereli" Date: Sat, 9 Dec 2023 14:18:09 +0100 Subject: [PATCH] Blame unsaved files correctly (#26) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Blame unsaved files correctly * Display "Unsaved changes" on unsaved changes * 0.4.0 -> 0.5.0 --------- Co-authored-by: Carl Thomé --- package-lock.json | 4 ++-- package.json | 2 +- src/extension.ts | 12 +++++++++++- src/lib.ts | 14 ++++++++++---- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1be48d0..ae5ccd7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "git-line-blame", - "version": "0.4.0", + "version": "0.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "git-line-blame", - "version": "0.4.0", + "version": "0.5.0", "license": "AGPL-3.0-only", "devDependencies": { "@types/mocha": "^10.0.1", diff --git a/package.json b/package.json index 36e79ed..a2798c5 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "displayName": "Git Line Blame", "description": "Display inline information in the text editor about the latest commit that edited the currently selected line", "publisher": "carlthome", - "version": "0.4.0", + "version": "0.5.0", "license": "AGPL-3.0-only", "icon": "icon.png", "engines": { diff --git a/src/extension.ts b/src/extension.ts index 6744ac9..0671e8d 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -29,15 +29,25 @@ export function activate(context: vscode.ExtensionContext) { activeEditor.selection.active.line ); - const file = activeEditor.document.uri; + const { uri: file, isDirty } = activeEditor.document; const line = activeLine.lineNumber; const command = "git"; const args = ["blame", "--porcelain", `-L${line + 1},+1`, file.fsPath]; + + if (isDirty) { + args.push("--content", "-"); + } + const workspaceFolder = vscode.workspace.getWorkspaceFolder(file); const workspaceFolderPath = workspaceFolder?.uri.fsPath; const options = { cwd: workspaceFolderPath }; const cmd = cp.spawn(command, args, options); + if (isDirty) { + cmd.stdin.write(activeEditor.document.getText()); + cmd.stdin.end(); + } + cmd.stdout.on("data", (data) => { const blame = data.toString(); diff --git a/src/lib.ts b/src/lib.ts index 5d04651..bdf2e3a 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -55,10 +55,16 @@ export function formatMessage(fields: Record): string { parseInt(fields["author-time"]) * 1000 ); const isUncommitted = fields["author"] === "Not Committed Yet"; - const message = isUncommitted - ? "Not committed yet" - : `${fields.author}, ${elapsed} • ${fields.summary}`; - return message; + const isUnsaved = fields["author"] === "External file (--contents)"; + const defaultMessage = `${fields.author}, ${elapsed} • ${fields.summary}`; + + if (isUncommitted) { + return "Not committed yet"; + } else if (isUnsaved) { + return "Unsaved changes"; + } else { + return defaultMessage; + } } export function formatHoverMessage(fields: Record): string {