Skip to content

Instantly share code, notes, and snippets.

@kiprasmel
Created October 10, 2021 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiprasmel/bc4ed8df10079ce430d2861d26dea563 to your computer and use it in GitHub Desktop.
Save kiprasmel/bc4ed8df10079ce430d2861d26dea563 to your computer and use it in GitHub Desktop.
rename.js - experiments w/ `git mv` et al
#!/usr/bin/env node
const fs = require("fs");
const { spawn, exec } = require("child_process");
const path = require("path");
const { snakeCase, pascalCase } = require("/Users/kiprasmelnikovas/.local/share/yarn/global/node_modules/change-case");
// const {pipe} =require( "/Users/kiprasmelnikovas/.local/share/yarn/global/node_modules/fp-ts")
// // git status --short | grep '^[RAD]' > status
// const status = fs.readFileSync("./status", { encoding: 'utf-8' }).split("\n").map(line => {
// //
// })
// console.log(status)
// //
// const pipe = (...fns) => (x) => fns.reduce((h, f) => f(h), x);
// pipe(
// x => exec(x, (e, stdout) => )
// x => x.stdout.toString(),
// x => console.log(x)
// )
// ("git -c core.pager='' diff --staged --find-renames=0.01% | grep 'rename '")
// exec('git -c core.pager="" diff --staged --find-renames="0.01%" | grep "rename "', (e, stdout) => {
// console.log(stdout)
// // git -c core.pager="" diff --staged --find-renames="0.01%" | grep "rename " --color=always | sed -E 's@.*components(.*)@\1@g;'
// // git -c core.pager="" diff --staged --find-renames="0.01%" | grep "rename " | sed -E 's@(rename (to|from)).*components(.*)@\1 \3@g;' | sed -E 's/rename to/rename to /g'
// // git -c core.pager="" diff --staged --find-renames="0.01%" | grep "rename " | sed -E 's@rename to@rename to @g; s@src/components@@g'
// })
/**
*
* ```sh
* # NB! "sed" is OS-X specific
*
* MIN_TOLERANCE="0.01"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename to@rename to @g; s@src/components@@g' > $MIN_TOLERANCE
*
* # run again with 50, and then (assuming "diff-so-fancy" installed):
*
* diff -u ./0.01 ./50 | diff-so-fancy
* ```
*
* .
*
* ```sh
* MIN_TOLERANCE="20"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename to@rename to @g; s@src/components@@g' | while read -r from; do read -r to; echo "from $from; to $to"; done
* ```
*
* ```sh
* MIN_TOLERANCE="20"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename (to|from)@@g ' | while read -r from; do read -r to; echo "from $from to $to"; done
* ```
*
* ```sh
* MIN_TOLERANCE="20"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename (to|from)@@g ' | while read -r from; do read -r to; echo "from $from to $to"; git mv "$from" "$to"; done
* ```
*
* $from didn't exist (the source), but $to (destination) did, so I swapped $from $to, and then swapped again:
*
* ```sh
* # reverse
* MIN_TOLERANCE="20"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename (to|from)@@g ' | while read -r from; do read -r to; git mv "$to" "$from"; done
*
* # actual
* MIN_TOLERANCE="20"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename (to|from)@@g ' | while read -r from; do read -r to; git mv "$from" "$to"; done
* ```
*
* LMAO IT WORKED. 84 (72?) MODIFIED FILES XDDDD
*
* tho, $to $from, bad sources:
*
* ```
*
fatal: renaming 'src/components/table/components/body/bodyRows/bodyRow.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/bodyRows/bodyVirtualizedRow.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/bodyRows/infiniteScroll.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/rowActions/rowActionVirtualized.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/columnResizeHandle.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnBorder.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnIndicators.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnOverlay.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/types/dnd.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/getDropDirection.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/index.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDndColumnBorderStyle.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDraggingOverlay.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDroppingOverlay.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useHeadCellDndConditions.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useLastColumnRect.ts' failed: No such file or directory
* ```
*
* and $from $to, bad sources:
*
* ```
fatal: bad source, source=src/components/table/components/body/body-rows/body-row.js, destination=src/components/table/components/body/bodyRows/bodyRow.tsx
fatal: bad source, source=src/components/table/components/body/body-rows/body-virtualized-row.js, destination=src/components/table/components/body/bodyRows/bodyVirtualizedRow.tsx
fatal: bad source, source=src/components/table/components/body/body-rows/infinite-scroll.js, destination=src/components/table/components/body/bodyRows/infiniteScroll.tsx
fatal: bad source, source=src/components/table/components/body/row-actions/row-action-virtualized.js, destination=src/components/table/components/body/rowActions/rowActionVirtualized.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/column-resize-handle.js, destination=src/components/table/components/dnd/columnResizeHandle.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-border.js, destination=src/components/table/components/dnd/dndColumnBorder.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-indicators.js, destination=src/components/table/components/dnd/dndColumnIndicators.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-overlay.js, destination=src/components/table/components/dnd/dndColumnOverlay.tsx
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-head-cell-dnd-conditions.d.ts, destination=src/components/table/types/dnd.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/get-drop-direction.js, destination=src/components/table/utils/dnd/getDropDirection.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/index.js, destination=src/components/table/utils/dnd/index.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dnd-column-border-style.js, destination=src/components/table/utils/dnd/useDndColumnBorderStyle.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dragging-overlay.js, destination=src/components/table/utils/dnd/useDraggingOverlay.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dropping-overlay.js, destination=src/components/table/utils/dnd/useDroppingOverlay.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-head-cell-dnd-conditions.js, destination=src/components/table/utils/dnd/useHeadCellDndConditions.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-last-column-rect.js, destination=src/components/table/utils/dnd/useLastColumnRect.ts
* ```
*
*
* MIN_TOLERANCE=1
*
* from:
*
* fatal: renaming 'src/components/table/components/body/bodyRows/bodyRow.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/bodyRows/bodyVirtualizedRow.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/bodyRows/index.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/bodyRows/infiniteScroll.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/rowActions/index.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/rowActions/rowActionVirtualized.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/columnResizeHandle.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnBorder.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnIndicators.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnOverlay.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/types/columns.ts' failed: No such file or directory
fatal: renaming 'src/components/table/types/dnd.ts' failed: No such file or directory
fatal: renaming 'src/components/table/types/options.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/classname/useClassName.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/getDropDirection.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/index.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDndColumnBorderStyle.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDraggingOverlay.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDropDirection.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDroppingOverlay.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useHeadCellDndConditions.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useLastColumnRect.ts' failed: No such file or directory
*
*
* to:
*
fatal: bad source, source=src/components/table/components/body/body-rows/body-row.js, destination=src/components/table/components/body/bodyRows/bodyRow.tsx
fatal: bad source, source=src/components/table/components/body/body-rows/body-virtualized-row.js, destination=src/components/table/components/body/bodyRows/bodyVirtualizedRow.tsx
fatal: bad source, source=src/components/table/components/body/body-rows/index.js, destination=src/components/table/components/body/bodyRows/index.tsx
fatal: bad source, source=src/components/table/components/body/body-rows/infinite-scroll.js, destination=src/components/table/components/body/bodyRows/infiniteScroll.tsx
fatal: bad source, source=src/components/table/components/body/row-actions/index.js, destination=src/components/table/components/body/rowActions/index.tsx
fatal: bad source, source=src/components/table/components/body/row-actions/row-action-virtualized.js, destination=src/components/table/components/body/rowActions/rowActionVirtualized.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/column-resize-handle.js, destination=src/components/table/components/dnd/columnResizeHandle.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-border.js, destination=src/components/table/components/dnd/dndColumnBorder.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-indicators.js, destination=src/components/table/components/dnd/dndColumnIndicators.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-overlay.js, destination=src/components/table/components/dnd/dndColumnOverlay.tsx
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dropping-overlay.d.ts, destination=src/components/table/types/columns.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-head-cell-dnd-conditions.d.ts, destination=src/components/table/types/dnd.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dragging-overlay.d.ts, destination=src/components/table/types/options.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dnd-column-overlay-style.js, destination=src/components/table/utils/classname/useClassName.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/get-drop-direction.js, destination=src/components/table/utils/dnd/getDropDirection.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/index.js, destination=src/components/table/utils/dnd/index.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dnd-column-border-style.js, destination=src/components/table/utils/dnd/useDndColumnBorderStyle.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dragging-overlay.js, destination=src/components/table/utils/dnd/useDraggingOverlay.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-drop-direction.js, destination=src/components/table/utils/dnd/useDropDirection.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dropping-overlay.js, destination=src/components/table/utils/dnd/useDroppingOverlay.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-head-cell-dnd-conditions.js, destination=src/components/table/utils/dnd/useHeadCellDndConditions.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-last-column-rect.js, destination=src/components/table/utils/dnd/useLastColumnRect.ts
*
* Modified 86
*
*
*
*
* MIN_TOLERANCE=0.01
*
* from:
*
* ame (to|from)@@g ' | while read -r from; do read -r to; git mv "$to" "$from"; done
fatal: renaming 'src/components/table/components/body/bodyRows/bodyRow.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/bodyRows/bodyVirtualizedRow.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/bodyRows/index.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/bodyRows/infiniteScroll.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/rowActions/index.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/body/rowActions/rowActionVirtualized.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/columnResizeHandle.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnBorder.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnIndicators.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/components/dnd/dndColumnOverlay.tsx' failed: No such file or directory
fatal: renaming 'src/components/table/types/classnames.ts' failed: No such file or directory
fatal: renaming 'src/components/table/types/columns.ts' failed: No such file or directory
fatal: renaming 'src/components/table/types/dnd.ts' failed: No such file or directory
fatal: renaming 'src/components/table/types/options.ts' failed: No such file or directory
fatal: renaming 'src/components/table/types/ref.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/classname/useClassName.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/getDropDirection.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/index.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDndColumnBorderStyle.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDraggingOverlay.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDropDirection.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useDroppingOverlay.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useHeadCellDndConditions.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/dnd/useLastColumnRect.ts' failed: No such file or directory
fatal: renaming 'src/components/table/utils/ref/index.ts' failed: No such file or directory
*
*
* to:
*
* fatal: bad source, source=src/components/table/components/body/body-rows/body-row.js, destination=src/components/table/components/body/bodyRows/bodyRow.tsx
fatal: bad source, source=src/components/table/components/body/body-rows/body-virtualized-row.js, destination=src/components/table/components/body/bodyRows/bodyVirtualizedRow.tsx
fatal: bad source, source=src/components/table/components/body/body-rows/index.js, destination=src/components/table/components/body/bodyRows/index.tsx
fatal: bad source, source=src/components/table/components/body/body-rows/infinite-scroll.js, destination=src/components/table/components/body/bodyRows/infiniteScroll.tsx
fatal: bad source, source=src/components/table/components/body/row-actions/index.js, destination=src/components/table/components/body/rowActions/index.tsx
fatal: bad source, source=src/components/table/components/body/row-actions/row-action-virtualized.js, destination=src/components/table/components/body/rowActions/rowActionVirtualized.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/column-resize-handle.js, destination=src/components/table/components/dnd/columnResizeHandle.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-border.js, destination=src/components/table/components/dnd/dndColumnBorder.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-indicators.js, destination=src/components/table/components/dnd/dndColumnIndicators.tsx
fatal: bad source, source=src/components/table/components/drag-and-drop/dnd-column-overlay.js, destination=src/components/table/components/dnd/dndColumnOverlay.tsx
fatal: bad source, source=src/components/table/utils/drag-and-drop/index.d.ts, destination=src/components/table/types/classnames.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dropping-overlay.d.ts, destination=src/components/table/types/columns.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-head-cell-dnd-conditions.d.ts, destination=src/components/table/types/dnd.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dragging-overlay.d.ts, destination=src/components/table/types/options.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dnd-column-border-style.d.ts, destination=src/components/table/types/ref.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dnd-column-overlay-style.js, destination=src/components/table/utils/classname/useClassName.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/get-drop-direction.js, destination=src/components/table/utils/dnd/getDropDirection.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/index.js, destination=src/components/table/utils/dnd/index.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dnd-column-border-style.js, destination=src/components/table/utils/dnd/useDndColumnBorderStyle.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dragging-overlay.js, destination=src/components/table/utils/dnd/useDraggingOverlay.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-drop-direction.js, destination=src/components/table/utils/dnd/useDropDirection.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-dropping-overlay.js, destination=src/components/table/utils/dnd/useDroppingOverlay.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-head-cell-dnd-conditions.js, destination=src/components/table/utils/dnd/useHeadCellDndConditions.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-last-column-rect.js, destination=src/components/table/utils/dnd/useLastColumnRect.ts
fatal: bad source, source=src/components/table/utils/drag-and-drop/use-last-column-rect.d.ts, destination=src/components/table/utils/ref/index.ts
*
* still 86
*
*
*
* found fix: when doing reverse (first) (only that, not the second - actual), need to mkdir -p "path" && rm -r "$path" (will makedir but with the file name, and rm -r will remove the filename dir!)
*
*
*
* ```sh
* # reverse, then actual
MIN_TOLERANCE="20"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename (to|from)@@g ' | while read -r from; do read -r to; mkdir -p "$from" && rm -r "$from"; git mv "$to" "$from"; done
MIN_TOLERANCE="20"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename (to|from)@@g ' | while read -r from; do read -r to; mkdir -p "$to" && rm -r "$to"; git mv "$from" "$to"; done
* ```
*
*
* MIN_TOLERANCE=20
*
* 102 MODIFIED !!!!!
* 0 renamed! lemme see if that's good or bad :D
*
*
* HOLY SHIT DIFFS ARE GOOD!!!
*
* https://github.com/pipedrive/convention-ui-react/pull/634
*
*
*
*
* Now, let's combine the 2 commands into 1 (also new idea - one-liner to complete this whole process by just providing the repo name & original PR's branch LOL)
*
*
* ```sh
* # EDIT: BAD - DO NOT USE !!!
*
* MIN_TOLERANCE="20"; git -c core.pager="" diff --staged --find-renames="${MIN_TOLERANCE}%" | grep "rename " | sed -E 's@rename (to|from)@@g ' | while read -r from; do read -r to; mkdir -p "$from" && rm -r "$from" && git mv "$to" "$from" && mkdir -p "$to" && rm -r "$to" && git mv "$from" "$to"; done
* ```
*
* same as before:
*
git status --short | grep '^A' | wc -l
63
git status --short | grep '^D' | wc -l
14
git status --short | grep '^R' | wc -l
0
git status --short | grep '^M' | wc -l
102
*
*
* ```sh
* stats() { STATE="$1"; git status --short | grep "^$STATE" | wc -l }
* stats A && stats D && stats R && stats M
* ```
*
*
* MIN_TOLERANCE=10
*
71
22
0
94
*
*
* MIN_TOLERANCE=1
*
56
7
0
109
*
*
* MIN_TOLERANCE=0.1
*
52
3
0
113
*
*
* MIN_TOLERANCE=0.01
*
49
0
0
116
*
*
*
* holy shit. EDIT: BAD - false positives!!
*
*
*
*
*
* MIN_TOLERANCE=5
*
67
18
0
98
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment