Skip to content

Commit 1b0d23a

Browse files
committed
Fix dirname on Windows
1 parent 4033f09 commit 1b0d23a

File tree

5 files changed

+17
-16
lines changed

5 files changed

+17
-16
lines changed

src/diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function getDiagnostics(doc: vscode.TextDocument): vscode.Diagnostic[] {
3535
returnMe.push(...getSecondLineDiagnostic(secondLine));
3636
}
3737

38-
if (utils.basename(doc.fileName) === "COMMIT_EDITMSG") {
38+
if (doc.fileName.endsWith("COMMIT_EDITMSG")) {
3939
returnMe.push(...getNoDiffDiagnostic(doc));
4040
}
4141

src/messages.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import * as vscode from "vscode";
22
import * as verbosecommits from "./verbosecommits";
3-
import * as utils from "./utils";
43

54
/** Show informational toast about doing verbose Git commits */
65
export async function displayVerboseDiffMessage(doc: vscode.TextDocument) {
7-
if (utils.basename(doc.fileName) !== "COMMIT_EDITMSG") {
6+
if (!doc.fileName.endsWith("COMMIT_EDITMSG")) {
87
// We only like one kind of files
98
return;
109
}

src/quickfix.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default class GitCommitCodeActionProvider
2424
returnMe.push(...createBranchIssueIdFix(gitBranch, doc, range));
2525
returnMe.push(...createUpcaseJiraIdFix(doc, range));
2626

27-
if (utils.basename(doc.fileName) === "COMMIT_EDITMSG") {
27+
if (doc.fileName.endsWith("COMMIT_EDITMSG")) {
2828
returnMe.push(...(await createEnableGitVerboseCommitFix(doc, range)));
2929
}
3030

src/test/suite/utils.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,14 @@ suite("Utils", () => {
5050
undefined,
5151
);
5252
});
53+
54+
test("Our dirname works on Windows as well", () => {
55+
assert.strictEqual(utils.dirname("/foo/bar/baz.txt"), "/foo/bar");
56+
assert.strictEqual(utils.dirname("foo/bar/baz.txt"), "foo/bar");
57+
assert.strictEqual(utils.dirname("baz.txt"), "");
58+
assert.strictEqual(utils.dirname("/foo/bar/"), "/foo");
59+
assert.strictEqual(utils.dirname("C:/foo/bar/baz.txt"), "C:/foo/bar");
60+
assert.strictEqual(utils.dirname("C:/foo/bar/"), "C:/foo");
61+
assert.strictEqual(utils.dirname("baz.txt"), "");
62+
});
5363
});

src/utils.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -94,25 +94,17 @@ export function getJiraIssueIdFromBranchName(
9494
return undefined;
9595
}
9696

97-
/**
98-
* Returns the last portion of a path, handling both / and \ separators.
99-
* Equivalent to Node's path.basename, but works in browser and cross-platform.
100-
* @param filePath - The full file path.
101-
* @returns The file name portion of the path.
102-
*/
103-
export function basename(filePath: string): string {
104-
const parts = filePath.split(/[/]+/);
105-
return parts.pop() || "";
106-
}
107-
10897
/**
10998
* Returns the directory portion of a path, handling both / and \ separators.
11099
* Equivalent to Node's path.dirname, but works in browser and cross-platform.
111100
* @param filePath - The full file path.
112101
* @returns The directory portion of the path.
113102
*/
114103
export function dirname(filePath: string): string {
115-
const parts = filePath.split(/[/]+/);
104+
// First, remove any trailing / or \
105+
filePath = filePath.replace(/[\\/]+$/, "");
106+
107+
const parts = filePath.split(/[\\/]+/);
116108
parts.pop();
117109
return parts.join("/");
118110
}

0 commit comments

Comments
 (0)