Skip to content

Rewrite files in git to follow editorconfig, while preserving history and without force commits.

License

Notifications You must be signed in to change notification settings

dmeybohm/editorconfig-git-preserve-history

Repository files navigation

editorconfig-git-preserve-history

This tool is a script that runs on a git repository that contains editorconfig files. This rewrites all the text files in the repository to update the whitespace in accordance with the editorconfig, but preserves history by writing new commits with the original authorship and commit date.

The purpose of this is twofold: to maintain the original authorship of changes as reported by git blame, thus making the original authorship information more accessible even when using editorconfig, and also to avoid the hassle of having to handle large numbers of changes due to files that are drastically different from what's configured in an .editorconfig.

By rewriting all the whitespace at once with this tool, you can keep your editorconfig plugin in your editor enabled, avoid unnecessary whitespace changes in other, more important code changes, and at the same time preserve the usefulness of git blame on files that have drastically different whitespace from your editorconfig files.

Installation

Python 3.5 or later is required. On macOS, you can install homebrew first and then:

brew install python3

With pip

After installing Python 3

pip3 install EditorConfigGitPreserveHistory

Running

editorconfig-git-preserve-history

The script will write commits into your current branch that reformat according to the .editorconfig that applies to each file in your repository. It will also add "Impersonator:" and "Original-Commit:" lines to the commit message of each commit to reference back to the previous commit and inform whoever looks at the log who created this whitespace commit. It looks like this:

commit 0e5f0feb7f9453b2fc8595f3db7835dbfe57db0e
Author: Original Author <[email protected]>
Date:   Mon Feb 19 20:33:06 2018 -0500

Add editorconfig module to requirements.txt

Original-Commit: 0e9d1d4bd3fe4cb278ed785bdb229e519eccc857
Impersonator: New Author <[email protected]>

You can then put those commits up on github and issue a pull request to change all the whitespace in your project, but preserve the original authorship.

This way, if you do git blame you will still see the original author of each line, but the whitespace will also be updated.

Developing

Create a virtual environment with the venv module:

python3 -m venv venv

Then activate the environment and install the requirements from the requirements.txt file:

. venv/bin/activate
pip install -r requirements.txt

Be sure to run the tests with nosetests before issuing a PR:

nosetests

You can also run mypy, the Python static analyzer, to check for typing errors:

mypy --ignore-missing-imports -p editorconfig_git_preserve_history

About

Rewrite files in git to follow editorconfig, while preserving history and without force commits.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •