Skip to content

Move files without losing the ratchet

At the end of this guide, a file or folder you moved keeps its counts in the baseline under its new name, and ashlar check passes.

The baseline holds counts by file path. A moved file has a new path that the baseline doesn't name, and a path the baseline doesn't name holds none, so every break the file carried counts as a rise.

  1. Move the file, and run the check:

    Terminal window
    git mv packages/money/src/total.ts packages/money/src/sum.ts
    Terminal window
    $ pnpm exec ashlar check
    Ashlar checked 9 files in 0.2 s: 1 count rose.
    packages/money/src/sum.ts moved from packages/money/src/total.ts: `ashlar move packages/money/src/total.ts packages/money/src/sum.ts` takes its counts with it.
    no-comment in packages/money/src/sum.ts: 1, up from 0
    packages/money/src/sum.ts:1:1
    Comments go stale and nothing checks them. Rename it, or write a test. Git leads from any line to the item or task that wrote it.

    The comment was there before the move. Ashlar sees that git renamed the file, and says which command takes its counts along.

  2. Take the counts to the new name with ashlar move:

    Terminal window
    $ pnpm exec ashlar move packages/money/src/total.ts packages/money/src/sum.ts
    packages/money/src/total.ts → packages/money/src/sum.ts: 1 break moved.
    "no-comment": {
    "apps/api/src/orders/index.ts": 1,
    "packages/money/src/total.ts": 1
    "packages/money/src/sum.ts": 1
    },
    Terminal window
    $ pnpm exec ashlar check
    Ashlar checked 9 files in 0.2 s: no count rose.

    Commit the baseline with the move. ashlar move works before or after you move the files, as long as exactly one of the two paths exists. Give it several pairs at once: ashlar move <from> <to> <from> <to>.

  3. Or let check --shrink take the renames git sees. On the same move, instead of ashlar move:

    Terminal window
    $ pnpm exec ashlar check --shrink
    Ashlar checked 9 files in 0.2 s: no count rose.
    The baseline now holds 6 breaks.

    --shrink gives each file git saw renamed the counts of its old name, then lowers counts as it always does. Git sees a rename between the branch's start on main and your working tree when the move is committed or staged (git mv stages it) and the file kept enough of its content. A file moved with a plain mv and not added, or changed too much on the way, looks like a file deleted and another added: use ashlar move for it.

  4. Move a folder the same way. Every file in it keeps its counts:

    Terminal window
    git mv apps/api/src/orders apps/api/src/sales
    Terminal window
    $ pnpm exec ashlar move apps/api/src/orders apps/api/src/sales
    apps/api/src/orders → apps/api/src/sales: 1 break moved.
  5. Fix a move that takes in new breaks. Each file keeps what it holds now, up to what the baseline held for it, so a moved file can't carry more breaks than before. If someone added one on the way, the move is refused, and the baseline stays as it was:

    Terminal window
    $ pnpm exec ashlar move packages/money/src/total.ts packages/money/src/sum.ts
    Ashlar moved no counts: the move would take in new breaks.
    packages/money/src/total.ts → packages/money/src/sum.ts: no-comment rose to 2, up from 1
    Fix the moved code, or give `--accept-rise` to take the rise into the baseline.

    It exits with 2. Take the new break out and move again. If a person accepts the rise, --accept-rise takes it in and says so:

    Terminal window
    $ pnpm exec ashlar move packages/money/src/total.ts packages/money/src/sum.ts --accept-rise
    packages/money/src/total.ts → packages/money/src/sum.ts: 2 breaks moved.
    no-comment rose to 2, up from 1: taken in with --accept-rise.
Ashlar says Why
There is no baseline to move counts in yet: take it first with ashlar check --shrink. The project has no baseline, so there are no counts to move.
Ashlar can't tell where packages/money/src/a.ts's counts go: exactly one of packages/money/src/a.ts and packages/money/src/b.ts must exist. Both paths are files on disk, or neither is on disk and the baseline holds no file under the first.
Ashlar moved no counts: the move would take in new breaks. The moved code holds more breaks than the baseline held for it.

ashlar move --json prints each move: its from, its to, its kind (file or folder), the breaks it moved and the rises it took in.