https://calebhearth.com/git-method-history [ ] Menu Caleb Hearth * Engineering Blog * Conference Talks * TTRPG * Bookshelf * Linktree See the History of a Method with git log -L November 03, 2023 Git can "trace the evolution" of a specific method when passed -L using the : variant. Let's take a look at the history of this blog's posts' published scope, which is used in most places that articles are listed out. You can see that git is able to identify the boundaries of the method even as the length changes and it prints out the full method with diff from each commit in the log, which is filtered to changes to that method. $ git log -L :self.published:app/models/post.rb -n3 commit d1fbdf3b9a5bd37dd49d61c61ee0f51c75fdb806 Author: Caleb Hearth Date: Thu Feb 2 16:36:34 2023 -0700 Decree, LFW, new homepage, RSS-only posts diff --git a/app/models/post.rb b/app/models/post.rb --- a/app/models/post.rb +++ b/app/models/post.rb @@ -15,6 +15,6 @@ def self.published all - .reject { _1.frontmatter[:draft] } + .reject { _1.frontmatter[:draft] || _1.tags.include?("rss-only") } .select { _1.date <= Date.today } end commit 37a830b625db4e60e0ffbfec881d693369706169 Author: Caleb Hearth Date: Fri Nov 12 11:39:48 2021 -0600 Publish posts published today diff --git a/app/models/post.rb b/app/models/post.rb --- a/app/models/post.rb +++ b/app/models/post.rb @@ -15,6 +15,6 @@ def self.published all .reject { _1.frontmatter[:draft] } - .select { _1.date < Date.today } + .select { _1.date <= Date.today } end commit b1ed34a897af9ff2f882e140132a6e6045257b10 Author: Caleb Hearth Date: Mon Nov 1 10:33:13 2021 -0500 Don't filter out all posts diff --git a/app/models/post.rb b/app/models/post.rb --- a/app/models/post.rb +++ b/app/models/post.rb @@ -12,6 +15,6 @@ def self.published all - .reject { |post| post.frontmatter[:draft] } - .reject { |post| post.frontmatter[:date] < Date.today } + .reject { _1.frontmatter[:draft] } + .select { _1.date < Date.today } end If you got excited and tried this on your own, you may have been disappointed to see a message like fatal: -L parameter 'self.published' starting at line 1: no match While Git knows how to do this for some languages (mostly the C family), it needs a bit of help for a lot of others including Ruby and Swift. Fortunately it's as simple as adding one these lines (you only need the one relevant to your project's language) to your project's .gitattributes *.rb diff=ruby *.swift diff=swift If you'd like this to be global so you don't get that error in each new project, you can set up a global .gitattributes file for yourself like this: $ git config --global core.attributesfile ~/.gitattributes Then add the above lines to ~/.gitattributes. * Programming * Git * Ruby See the History of a Method with git log -L is part of the NaBloPoMo series. [caleb-hex-] Join me at the Hearthside I'll occasionally send out a newsletter to this mailing list including new articles on this site, links to articles I've enjoyed elsewhere, and any updates on open source stuff I've worked on. [ ] [Subscribe] (c) 2011-2023 Caleb Hearth. Made with in Denver, CO. Happy Sunday!