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 <name>' within one directory and 'moved from <old path relative to the new directory>' 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>
This commit is contained in:
Vadim Lazuk
2026-11-15 23:09:13 +08:00
committed by GitHub
co-authored by Inf1mum
parent b7adbbb17c
commit 55786a7030
7 changed files with 59 additions and 6 deletions
+2 -2
View File
@@ -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<CreateChange> }) {
<span className={styles.fileName} style={{ color: KIND_COLOR[kindOf(f)] }}>
{node.name}
</span>
{f.oldPath && <span className={styles.fileDetail}>{tx`renamed from ${baseName(f.oldPath)}`}</span>}
{f.oldPath && <span className={styles.fileDetail}>{renameFrom(f.path, f.oldPath)}</span>}
</span>
);
}
+2 -2
View File
@@ -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}
</span>
{flat && dirName(f.filename) && <span className={styles.fileDir}>{dirName(f.filename)}</span>}
{f.previousFilename && <span className={styles.fileDir}>{tx`renamed from ${baseName(f.previousFilename)}`}</span>}
{f.previousFilename && <span className={styles.fileDir}>{renameFrom(f.filename, f.previousFilename)}</span>}
</>
) : (
<>
+2 -2
View File
@@ -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<ChangedFile
{node.name}
</span>
{flat && dirName(f.path) && <span className={styles.fileDir}>{dirName(f.path)}</span>}
{f.oldPath && <span className={styles.fileDir}>{tx`renamed from ${baseName(f.oldPath)}`}</span>}
{f.oldPath && <span className={styles.fileDir}>{renameFrom(f.path, f.oldPath)}</span>}
{stats && <LineStats added={stats.added} removed={stats.removed} />}
</span>
);
@@ -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(<CommitDetails commits={[commit]} />);
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", () => {
+20
View File
@@ -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) : "";
+1
View File
@@ -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",
+1
View File
@@ -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} 个文件",