A better way of using git blame to give you a more accurate result.

Today in Who really wrote that code? we introduce:

git blame -w -C -C -C 

If you had a question about any of the code in this Operation and wanted to ask the author, you’d git blame and see that, yours truly adamdawkins wrote every line of this file. In reality, I’ve extracted this code from somewhere else.

› git blame --date=short app/services/calculate_something.rb

298ae6c24e (adamdawkins 2024-05-07  1) class CalculateSomething
298ae6c24e (adamdawkins 2024-05-07  2)   def initialize(value)
298ae6c24e (adamdawkins 2024-05-07  3)     @value = value
298ae6c24e (adamdawkins 2024-05-07  4)   end
298ae6c24e (adamdawkins 2024-05-07  5)
298ae6c24e (adamdawkins 2024-05-07  6)   def call
298ae6c24e (adamdawkins 2024-05-07  7)     # ...
298ae6c24e (adamdawkins 2024-05-07  8)     # ...
298ae6c24e (adamdawkins 2024-05-07  9)     # some calculating code
298ae6c24e (adamdawkins 2024-05-07 10)     # ...
298ae6c24e (adamdawkins 2024-05-07 11)     # ...
298ae6c24e (adamdawkins 2024-05-07 12)   end
298ae6c24e (adamdawkins 2024-05-07 13) end

Well, this is very new code, you cry.

Hold your horses.

Add a -C

Let’s ask git to detect lines moved or copied in the same commit:

› git blame --date=short -C app/services/calculate_something.rb

298ae6c24e (adamdawkins 2024-05-07  1) class CalculateSomething
298ae6c24e (adamdawkins 2024-05-07  2)   def initialize(value)
298ae6c24e (adamdawkins 2024-05-07  3)     @value = value
298ae6c24e (adamdawkins 2024-05-07  4)   end
298ae6c24e (adamdawkins 2024-05-07  5)
298ae6c24e (adamdawkins 2024-05-07  6)   def call
298ae6c24e (adamdawkins 2024-05-07  7)     # ...
298ae6c24e (adamdawkins 2024-05-07  8)     # ...
298ae6c24e (adamdawkins 2024-05-07  9)     # some calculating code
298ae6c24e (adamdawkins 2024-05-07 10)     # ...
298ae6c24e (adamdawkins 2024-05-07 11)     # ...
298ae6c24e (adamdawkins 2024-05-07 12)   end
298ae6c24e (adamdawkins 2024-05-07 13) end

Well, that was never going to work, I write really small commits.

Add another -C

Ok, let’s ask git to also check for copies from other files in the commit that creates the file:

298ae6c24e  app/services/calculate_something.rb (adamdawkins 2024-05-07  1) class CalculateSomething
298ae6c24e  app/services/calculate_something.rb (adamdawkins 2024-05-07  2)   def initialize(value)
298ae6c24e  app/services/calculate_something.rb (adamdawkins 2024-05-07  3)     @value = value
298ae6c24e  app/services/calculate_something.rb (adamdawkins 2024-05-07  4)   end
298ae6c24e  app/services/calculate_something.rb (adamdawkins 2024-05-07  5)
298ae6c24e  app/services/calculate_something.rb (adamdawkins 2024-05-07  6)   def call
298ae6c24e  app/services/original_file.rb       (johnnorris  2023-09-15  7)     # ...
298ae6c24e  app/services/original_file.rb       (johnnorris  2023-09-15  8)     # ...
298ae6c24e  app/services/original_file.rb       (johnnorris  2023-09-15  9)     # some calculating code
298ae6c24e  app/services/original_file.rb       (johnnorris  2023-09-15 10)     # ...
298ae6c24e  app/services/original_file.rb       (johnnorris  2023-09-15 11)     # ...
298ae6c24e  app/services/calculate_something.rb (adamdawkins 2024-05-07 12)   end
298ae6c24e  app/services/calculate_something.rb (adamdawkins 2024-05-07 13) end

The output, is of course horrible, but that’s bcause it’s showing us that the lines I copied here in the meat of the call were written by John originally. It’s much more useful than looking through all of the code, and, if your code editor can make it look less ugly than this - it’s worth it as a default I think.

NB: None of this is about blaming anyone for anything, it’s about getting to the write information. [John: thanks Adam]

Add another -C

A third -C would check for code copied from any commit whatsoever, but we didn’t need that here.

VS Code

For those of you who use VS Code you should be using GitLens already for this sort of thing, and you can configure what flags are used for the blame with the setting "gitlens.advanced.blame.customArguments": ["-C", "-C", "-C"]

Vim

I couldn’t find config for fugitive with this, so I added a command to use instead of :G blame:

command Gblame execute ":Git blame -w -C -C -C"
:Gblame

Adam Dawkins


Who are Dragon Drop?

Dragon Drop is a specialist web and app development agency. The team has extensive full-stack technical credentials and a strong focus on user experience.

The Dragon Drop founding team have over 40 years of web development and project experience. They have managed or developed over 100 significant ecommerce and web projects during their careers, including implementations for major UK high street retailers, financial services companies and government agencies.

Their approach to innovative solutions stems from perspectives gained as retailer, software supplier and web agency.