DEV Community

Taylor Wang
Taylor Wang

Posted on

Prove Your OSS Patch Is Reversible Before the Pull Request

For a patch expected to support a rollback, record what an inverse commit restores. A patch that cannot be dropped is a one-way door for the maintainer who merges it.

The problem shows up in ordinary contributions. A contributor adds a lockfile update, a regenerated schema file, and a migration alongside a three-line fix. The diff looks correct. Nobody asks what happens when the change must be backed out under pressure.

Who Needs This

This check fits contributors who send patches to projects they do not own. It also fits maintainers who review AI-assisted patches with wide mechanical churn. The reader needs a local clone, a clean working tree, and the ability to run the project's code generation or build step.

The rehearsal assumes the patch lives in its own commit series on a branch. It assumes generated files are committed to the repository. Both assumptions are common but not universal.

Steps

  1. Separate mechanical churn from intent. Put lockfile updates, regenerated code, and formatting changes in their own commits. A reviewer can inspect the noisy changes separately. Keep a generator and its required output together where separating them would break an intermediate commit. This also makes the rehearsal readable.

  2. Record the base state. Note the base commit identifier. Record a hash of the tracked tree at that commit, so later comparisons are exact rather than visual.

  3. Rehearse the reverse direction on a scratch branch. Branch from the base commit, apply the patch commits, then reverse them. The reverse operation itself usually succeeds. The interesting part is what remains behind.

  4. Compare the reverted tree to the base tree. The tracked diff against base must be empty. Any residual file is a revert defect, not a formatting quirk. Line-ending changes, mode bits, and binary files are the usual culprits.

  5. Re-run code generation after reverting. Generation must be idempotent with respect to the base tree. Run the same generator on an untouched baseline first. A diff there points to the tool, environment or baseline itself. After the revert, inspect the working tree and index, since the committed tree ID cannot detect uncommitted generator output.

  6. List state that git cannot restore. Migrations, seeded data, caches, and installed packages live outside the repository. Each item needs a written undo note in the pull request, or an explicit statement that no undo exists. Reviewers can then price the risk.

A sketch of the gate logic, as illustrative pseudocode that was not executed:

# illustrative pseudocode, not executed
gate(patch):
  base_hash = tree_hash(base_commit)
  scratch = branch_from(base_commit)
  apply(patch.commits, scratch)
  reverse_apply(patch.commits, scratch)

  if tree_hash(scratch) != base_hash:
      fail("tracked state not restored", diff(scratch, base_commit))

  run_declared_codegen(scratch)
  if tracked_worktree_or_index_differs_from(base_commit) or unexpected_generated_paths():
      fail("inspect generated changes and baseline under the same toolchain")

  for item in patch.out_of_git_state:
      require undo_note(item)

  pass
Enter fullscreen mode Exit fullscreen mode

Test

The rehearsal is only trustworthy if it can fail. Build two controls before trusting a pass.

Positive control. Run the rehearsal on a patch already known to be clean. Expect an empty tracked diff against base after the reverse step. Expect the generator to reproduce the base tree exactly.

Negative control. After the clean revert, deliberately perturb one tracked generated output and compare working tree plus index to the base; that check should now fail. Separately, regenerate from a deliberately inconsistent source/output pair and verify the comparison detects the resulting change. Applying and completely reverting a bad patch can correctly restore a clean baseline.

Expected observations on a real patch. The tracked diff against base is empty. No untracked build outputs remain outside a declared ignore path. Every out-of-git item has an undo note or an explicit no-undo statement.

Record the evidence. Paste the base commit, the scratch branch result, and the generator output into the pull request. A reviewer can reproduce the rehearsal in a few minutes.

Failure Modes

  • The reverse step succeeds while untracked build outputs, caches, or installed dependencies survive. Git reports success; the environment stays dirty.
  • Generated files are committed but the generator is not re-run, so a stale artifact hides a real revert defect.
  • Line endings or file mode bits change during apply and reverse. The diff looks cosmetic but breaks the equality check.
  • Tracked binary blobs participate in the tree comparison. Submodule gitlinks and external large-file pointers may require checking that the corresponding external objects and working copies are available.
  • A deleted test is restored by the revert, but the associated fixture data lives outside version control.
  • A migration is reversed in the repository while the database keeps its new shape.

Tradeoffs And Limits

  • The rehearsal costs a few minutes per patch. Small documentation or comment-only changes rarely justify it.
  • Regenerating code needs a working toolchain. Projects with heavyweight generators may prefer a targeted comparison of the generated files instead.
  • The check proves local reversibility, not production safety. Deployed configuration and external services stay outside its reach.
  • For a squash merge, rehearse the inverse of the integration commit actually landed. When build outputs are untracked, compare artifacts independently. Git revert ยท Working-tree status
  • Patches that delete user data or rotate credentials need an incident plan, not a revert rehearsal.

A passing rehearsal is a claim about the repository state only. Treat it as one input to review, alongside tests and a human read of the diff.

This article was created with AI assistance. The pseudocode describes a proposed rehearsal.

Top comments (0)