Read more

Git: Show commits that have touched specific text in a file

Emanuel
October 14, 2016Software engineer at makandra GmbH

If you want to find the commits that touched a specific text in a file, use

git log -S 'text in the code' -- path/to/file
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

If you use tig you may run a similar command to get a navigatable list of affected files:

tig -S'text in the code'

Example

Here is an example, where the move of the convert_number_column_value(value) method in active record is traced (simplified output):

git log -n 1 --pretty=oneline -S 'convert_number_column_value(value)' -- activerecord/lib/active_record/base.rb
ceb33f84933639d3b61aac62e5e71fd087ab65ed Split out most of the AR::Base code into separate modules :cake:
git show ceb33f84933639d3b61aac62e5e71fd087ab65ed

 activerecord/lib/active_record/base.rb
 -      def convert_number_column_value(value)		
 -        if value == false		
 -          0		
 -        elsif value == true		
 -          1		
 -        elsif value.is_a?(String) && value.blank?		
 -          nil		
 -        else		
 -          value		
 -        end		
 -      end

 activerecord/lib/active_record/attribute_methods/write.rb
 +        def convert_number_column_value(value)
 +          if value == false
 +            0
 +          elsif value == true
 +            1
 +          elsif value.is_a?(String) && value.blank?
 +            nil
 +          else
 +            value
 +          end
 +        end
git log -n 1 --pretty=oneline -S 'convert_number_column_value(value)' -- activerecord/lib/active_record/attribute_methods/write.rb
5dec3dd59c9f4b5c881860b0f629c8d6b3706f90 delegate attribute typecasting to the column
git show 5dec3dd59c9f4b5c881860b0f629c8d6b3706f90

 activerecord/lib/active_record/attribute_methods/write.rb
 -        def convert_number_column_value(value)		 
 -          if value == false		
 -            0		
 -          elsif value == true		
 -            1		
 -          elsif value.is_a?(String) && value.blank?		
 -            nil		
 -          else		
 -            value		
 -          end

 activerecord/lib/active_record/connection_adapters/column.rb
 +      def type_cast_for_write(value)
 +        return value unless number?
 +
 +        if value == false
 +          0
 +        elsif value == true
 +          1
 +        elsif value.is_a?(String) && value.blank?
 +          nil
 +        else
 +          value
 +        end
 +      end
Emanuel
October 14, 2016Software engineer at makandra GmbH
Posted by Emanuel to makandra dev (2016-10-14 14:21)