From 55786a703094bd4da71f16f2d64b729e40a55f7e Mon Sep 17 00:00:00 2001 From: Vadim Lazuk Date: Sun, 15 Nov 2026 18:09:13 +0300 Subject: [PATCH] fix(vcs): the Git Log rename label names the original directory (#3546) The changed-file label trimmed a rename's old path to its base name, so a move between directories looked like a rename in place. A shared renameFrom() now says 'renamed from ' within one directory and 'moved from ' otherwise, as IDEA does; Git Log and the GitHub create / pull request change trees use it. Closes #3529 Co-authored-by: Inf1mum <8135225+Inf1mum@users.noreply.github.com> --- src/features/github/create/ChangesPanel.tsx | 4 +-- src/features/github/details/ChangesTree.tsx | 4 +-- src/features/vcs/GitLog.tsx | 4 +-- src/features/vcs/__tests__/logPanes.test.tsx | 31 ++++++++++++++++++++ src/features/vcs/model.ts | 20 +++++++++++++ src/platform/locales/text/en.json | 1 + src/platform/locales/text/zh.json | 1 + 7 files changed, 59 insertions(+), 6 deletions(-) diff --git a/src/features/github/create/ChangesPanel.tsx b/src/features/github/create/ChangesPanel.tsx index 5d03a01cc..a1b2e3212 100644 --- a/src/features/github/create/ChangesPanel.tsx +++ b/src/features/github/create/ChangesPanel.tsx @@ -7,7 +7,7 @@ import { useMemo, useRef, useState, type MouseEvent, type ReactNode } from "react"; import { buildFileTree, dirKeys, type FileTreeNode } from "@/features/vcs/fileTree"; import { openDiff, withChain } from "@/features/vcs/diffInput"; -import { KIND_COLOR, baseName, fileCount, type EffectiveKind } from "@/features/vcs/model"; +import { KIND_COLOR, fileCount, renameFrom, type EffectiveKind } from "@/features/vcs/model"; import type { Commit } from "@/platform/ipc"; import { tx } from "@/platform/i18n"; import { Icon, IconButton, Spinner, T, Tree, fileIconName, showContextMenu, type MenuItem } from "@/ui"; @@ -252,7 +252,7 @@ function ChangeRow({ node }: { node: FileTreeNode }) { {node.name} - {f.oldPath && {tx`renamed from ${baseName(f.oldPath)}`}} + {f.oldPath && {renameFrom(f.path, f.oldPath)}} ); } diff --git a/src/features/github/details/ChangesTree.tsx b/src/features/github/details/ChangesTree.tsx index 7da408e4b..eddfacd5c 100644 --- a/src/features/github/details/ChangesTree.tsx +++ b/src/features/github/details/ChangesTree.tsx @@ -10,7 +10,7 @@ import { useEffect, useLayoutEffect, useMemo, useRef, useState, type MouseEvent } from "react"; import clsx from "clsx"; import { dirKeys, type FileTreeNode } from "@/features/vcs/fileTree"; -import { baseName, dirName, fileCount, KIND_COLOR } from "@/features/vcs/model"; +import { dirName, fileCount, KIND_COLOR, renameFrom } from "@/features/vcs/model"; import { currentRepoRoot } from "@/features/vcs/store"; import { useTx } from "@/platform/i18n"; import { Icon, Tree, fileIconName, showContextMenu, T, type MenuItem, type TreeHandle } from "@/ui"; @@ -202,7 +202,7 @@ function ChangeRow(props: { {node.name} {flat && dirName(f.filename) && {dirName(f.filename)}} - {f.previousFilename && {tx`renamed from ${baseName(f.previousFilename)}`}} + {f.previousFilename && {renameFrom(f.filename, f.previousFilename)}} ) : ( <> diff --git a/src/features/vcs/GitLog.tsx b/src/features/vcs/GitLog.tsx index 33f0c1dcb..809233b56 100644 --- a/src/features/vcs/GitLog.tsx +++ b/src/features/vcs/GitLog.tsx @@ -86,7 +86,7 @@ import { PAGE, } from "./logStore"; import { logOptions, refLabels, useLogOptions, type LogOptions } from "./logOptions"; -import { KIND_COLOR, baseName, dirName, fileCount, formatTime, relTo, type EffectiveKind } from "./model"; +import { KIND_COLOR, dirName, fileCount, formatTime, relTo, renameFrom, type EffectiveKind } from "./model"; import { clearConsole, useVcsConsole } from "./ops"; import { currentRepo, currentRepoRoot, loadBranches, repoName, useVcs } from "./store"; import { canDeleteBranch, loadFavoriteBranches, useFavoriteBranches } from "./branchOps"; @@ -3070,7 +3070,7 @@ function ChangedFileRow({ node, flat = false }: { node: FileTreeNode {flat && dirName(f.path) && {dirName(f.path)}} - {f.oldPath && {tx`renamed from ${baseName(f.oldPath)}`}} + {f.oldPath && {renameFrom(f.path, f.oldPath)}} {stats && } ); diff --git a/src/features/vcs/__tests__/logPanes.test.tsx b/src/features/vcs/__tests__/logPanes.test.tsx index 0e8628470..a0ab87d93 100644 --- a/src/features/vcs/__tests__/logPanes.test.tsx +++ b/src/features/vcs/__tests__/logPanes.test.tsx @@ -44,6 +44,7 @@ import { canUpdateBranch, useBranchesPane } from "../logPaneActions"; import { BranchesPanel, CommitDetails, changesMenu, graphOptionsMenu } from "../GitLog"; import { LogSettingsPage } from "../LogSettingsPage"; import { DEFAULT_LOG_OPTIONS, useLogOptions } from "../logOptions"; +import { renameFrom } from "../model"; import { emptyFilters, useGitLog } from "../logStore"; import { useVcs } from "../store"; @@ -146,6 +147,20 @@ describe("changed files model", () => { }); }); +describe("rename label", () => { + it("names the original directory of a move, relative to the file's new directory (#3529)", () => { + // The issue's scenario: `git mv old/original.txt new/moved.txt`. + expect(renameFrom("new/moved.txt", "old/original.txt")).toBe("moved from ../old/original.txt"); + // A rename within one directory stays as written. + expect(renameFrom("old/renamed.txt", "old/original.txt")).toBe("renamed from original.txt"); + expect(renameFrom("b.ts", "a.ts")).toBe("renamed from a.ts"); + // Moves across the repository root climb with `..` or stay below it. + expect(renameFrom("moved.txt", "old/original.txt")).toBe("moved from old/original.txt"); + expect(renameFrom("new/moved.txt", "original.txt")).toBe("moved from ../original.txt"); + expect(renameFrom("a/b/c.txt", "a/c.txt")).toBe("moved from ../c.txt"); + }); +}); + describe("changed files pane", () => { it("shows the files flat or by directory and shares the selection with its actions", async () => { vcsCommitFiles.mockResolvedValue({ @@ -198,6 +213,22 @@ describe("changed files pane", () => { useLogOptions.setState({ showLineStats: false }); await waitFor(() => expect(screen.queryByLabelText("2 lines added")).not.toBeInTheDocument()); }); + + it("shows a moved file's original directory in its rename label", async () => { + vcsCommitFiles.mockResolvedValue({ + status: "ok", + data: [ + // The issue's scenario: `git mv old/original.txt new/moved.txt`. + { path: "new/moved.txt", kind: "renamed", oldPath: "old/original.txt" }, + { path: "old/renamed.txt", kind: "renamed", oldPath: "old/original.txt" }, + ], + }); + useGitLog.setState({ commits: [commit], selected: commit.id, selection: [commit.id] }); + render(); + expect(await screen.findByText("moved from ../old/original.txt")).toBeInTheDocument(); + // A rename within one directory keeps naming just the file. + expect(screen.getByText("renamed from original.txt")).toBeInTheDocument(); + }); }); describe("Branches pane", () => { diff --git a/src/features/vcs/model.ts b/src/features/vcs/model.ts index ce3d69052..dec54db39 100644 --- a/src/features/vcs/model.ts +++ b/src/features/vcs/model.ts @@ -112,6 +112,26 @@ export function dirName(path: string): string { return i < 0 ? "" : p.slice(0, i); } +/** + * Changed-file label for a rename, in IDEA's wording: `renamed from old.txt` when the + * file stays in its directory, `moved from ../old/old.txt` when it changed directories + * — the old path relative to the file's new directory, so the move is visible. + */ +export function renameFrom(newPath: string, oldPath: string): string { + const dir = dirName(newPath); + if (dirName(oldPath) === dir) return tx`renamed from ${baseName(oldPath)}`; + return tx`moved from ${relativePath(dir, oldPath)}`; +} + +/** `path` seen from the directory `from`, with `../` segments to climb out of it. */ +function relativePath(from: string, path: string): string { + const source = from ? from.split("/") : []; + const target = path.split("/"); + let common = 0; + while (common < source.length && common < target.length - 1 && source[common] === target[common]) common++; + return "../".repeat(source.length - common) + target.slice(common).join("/"); +} + /** List timestamp per Settings › System Settings › Date Formats. */ export function formatTime(ms: number | null | undefined, now = Date.now()): string { return ms ? formatTimestamp(ms, now) : ""; diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index cdf76c85b..28585a3ad 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1,4 +1,5 @@ { + "moved from {oldPath}": "moved from {oldPath}", "Symbolic link": "Symbolic link", "Copy as TSV": "Copy as TSV", "Copy as CSV": "Copy as CSV", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 055a74f45..4a7f0193a 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -5201,6 +5201,7 @@ "partial": "部分", "editable": "可编辑", "on {date}": "于 {date}", + "moved from {oldPath}": "已从 {oldPath} 移动", "renamed from {name}": "已从 {name} 重命名", "Label '{label}'": "标签“{label}”", "{n} file recorded before it": "在此之前记录了 {n} 个文件",