spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / hooks / git / rename-display-of / rename-display-of.ts
54 lines · 1.4 KBRaw
1/**
2 * A rename as `git diff --stat` prints it: the shared leading and trailing
3 * directories once, the differing middles in braces, `old => new` between.
4 *
5 * `docs/{notes => renamed}`, `{src => lib}/main`, or with nothing shared
6 * `old => new`; a prefix or suffix only counts up to a slash, as git counts
7 * it (pprint_rename).
8 *
9 * @param oldPath the path before the rename
10 * @param newPath the path after it
11 * @returns the one-line rename
12 */
13export function renameDisplayOf(oldPath: string, newPath: string): string {
14 let prefix = 0
15 let suffix = 0
16
17 for (let at = 0; at < oldPath.length && at < newPath.length; at++) {
18 if (oldPath[at] !== newPath[at]) {
19 break
20 }
21
22 if (oldPath[at] === '/') {
23 prefix = at + 1
24 }
25 }
26
27 const floor = prefix - (prefix > 0 ? 1 : 0)
28
29 for (
30 let back = 1;
31 oldPath.length - back >= floor && newPath.length - back >= floor;
32 back++
33 ) {
34 const character = oldPath[oldPath.length - back]
35
36 if (character !== newPath[newPath.length - back]) {
37 break
38 }
39
40 if (character === '/') {
41 suffix = back
42 }
43 }
44
45 const middleOf = (path: string) =>
46 path.slice(prefix, Math.max(prefix, path.length - suffix))
47
48 const middle = `${middleOf(oldPath)} => ${middleOf(newPath)}`
49 const isBraced = prefix + suffix > 0
50 const head = oldPath.slice(0, prefix)
51 const tail = oldPath.slice(oldPath.length - suffix)
52
53 return isBraced ? `${head}{${middle}}${tail}` : middle
54}