Skip to content

Commit

Permalink
Blame unsaved files correctly (#26)
Browse files Browse the repository at this point in the history
* Blame unsaved files correctly

* Display "Unsaved changes" on unsaved changes

* 0.4.0 -> 0.5.0

---------

Co-authored-by: Carl Thomé <carlthome@gmail.com>
  • Loading branch information
dereli and carlthome authored Dec 9, 2023
1 parent 20d84e8 commit 55579cc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
12 changes: 11 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
14 changes: 10 additions & 4 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ export function formatMessage(fields: Record<string, string>): 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, string>): string {
Expand Down

0 comments on commit 55579cc

Please sign in to comment.