In a series of posts for a future, forgetful me; today's topic is a way to search for changes in a file content over past git commits. Turns out it's not that easy, it's either a two step process; or, one gets laundry list of every occurrence of whatever is searched for that ever existed.
I opted for two step process; first search for commit IDs of interest, then get files in that commit:
- git log --grep=<regexp> lists all commits with a message pattern,
- git log -G <regexp> lists all commits with a content pattern,
- git log -S <regexp> lists all commits with different count of content pattern (additions vs. deletions),
- every search can be further restricted to a file (or folder), e.g. git log ... -- path/to/file/.
Now that commit IDs are filtered-out, one can get file by using:
git cat-file --textconv <commit-ID>:path/to/file; --filters can be used instead of --textconv. I find it more convenient to just dump committed file into a temporary file, like so: git cat-file ... > temp.c, then open said file in a text editor. Every text editor nowadays is aware of external content change in opened files; so, to repeat search with another commit-ID one has to do as little as just dump into the same file. It's also convenient if temporary file has the same extension as committed file, that way text editor can do syntax highlighting, without having to be set manually.
For a laundry list of everything everywhere, one can use git grep <regexp> $(git rev-list --all); the problem is that it's very long, even if restricted to only one file both in grep and in rev-list, like so: git grep <regexp> $(git rev-list --all -- path/to/file) -- path/to/file.
No comments:
Post a Comment