Using git to identify all changed functions in a revision

Is there a good way to use git to identify all changed features at every revision in history? I tried using the -p switch, but it doesn't work the way the svn show-c-function parameter does.

My guess is that I want to use "git diff HEAD ~ i HEAD ~ i-1 -p" to increment the i values. Are there some options missing to help determine diff's best guess about the features that have changed?

+2


source to share


3 answers


Here's a quick and dirty attempt at what I think you are going for. It shows git log

to show all changes, -p

to include a diff in the log, grep

to include only the lines containing the commit id and hunk headers, and uses sed

line numbers to filter out the line, leaving only guesswork about the function name that Git writes after the hunk header.



git log -p | grep -E '^(commit|@@)' | sed 's/@@.*@@//'

      

+2


source


Here is a magic chant to list the functions in * git diff

git diff |                  \
grep -E '^(@@)' |           \
grep "(" |                  \
sed 's/@@.*@@//' |          \
sed 's/(.*//' |             \
awk -F " " '{print $NF}' |  \
uniq

      

... and what does he do ...

  • Selects the current diff,
  • next selects only lines with "hunk-headers",
  • next only selects lines with an opening parenthesis (as potentially containing function names),
  • next ignores hunk headers,
  • next ignores text after opening parenthesis,
  • next only selects the word immediately before the opening parenthesis,
  • and finally, it ignores multiple occurrences of the same word in the list.


Voila! you have a list of functions that are being changed by the current one git diff

.


* Tested with git version 2.7.4

on Ubuntu 16.04 bash.

+2


source


You can list all changed functions in revision using git textconv filters. The idea is to create a specific filter that lists all functions / methods and for all functions - the checksum of the body. This gives a filtered view of textconv like this:

m() 12
bar() 42

      

(here m()

is the signature of the function, 12

is the checksum of its body))

When git diff

using this filter for two versions before and after revision:

  • If the function is added, the line is added to diff

Example: added foo

m() 12
+ foo() 24 
bar() 42

      

  • if the function is changed the checksum is changed and the line is updated in diff

Example: foo body modified

m() 12
- foo() 23 
+ foo() 24 
bar() 42

      

How to do it?

  • Create filter: java-ls-methods.groovy is an implementation of such a filter using Groovy and Spoon
  • Register this filter with git: git config diff.java-ls-methods.textconv /home/path/to/java-ls-methods.groovy

  • Link this filter with Java files: echo "*.java diff=java-ls-methods" >> .gitattributes

  • Do a diff: git diff

    (diff against the last commit) or git diff master

    (diff against another branch)
  • Once your diff is done, comment out line in .gitattributes

    to revert to normal diff

Credits: Solution based on fooobar.com/questions/91151 / ...

0


source







All Articles