Git - fix relativePath() edge case (#194653)

This commit is contained in:
Ladislau Szomoru 2023-10-05 15:37:52 +02:00 committed by GitHub
parent 8aced5c030
commit d4fb31c56d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -311,18 +311,19 @@ export function pathEquals(a: string, b: string): boolean {
* Given the `repository.root` compute the relative path while trying to preserve
* the casing of the resource URI. The `repository.root` segment of the path can
* have a casing mismatch if the folder/workspace is being opened with incorrect
* casing.
* casing which is why we attempt to use substring() before relative().
*/
export function relativePath(from: string, to: string): string {
// On Windows, there are cases in which `from` is a path that contains a trailing `\` character
// (ex: C:\, \\server\folder\) due to the implementation of `path.normalize()`. This behavior is
// by design as documented in https://github.com/nodejs/node/issues/1765.
if (isWindows) {
from = from.replace(/\\$/, '');
// There are cases in which the `from` path may contain a trailing separator at
// the end (ex: "C:\", "\\server\folder\" (Windows) or "/" (Linux/macOS)) which
// is by design as documented in https://github.com/nodejs/node/issues/1765. If
// the trailing separator is missing, we add it.
if (from.charAt(from.length - 1) !== sep) {
from += sep;
}
if (isDescendant(from, to) && from.length < to.length) {
return to.substring(from.length + 1);
return to.substring(from.length);
}
// Fallback to `path.relative`