使用 git 识别修订版中所有修改的函数

发布于 2024-11-02 11:37:50 字数 197 浏览 1 评论 0原文

有没有一个好的方法可以使用git来识别历史记录中每个修订版中所有修改过的功能?我尝试过使用 -p 开关,但它的工作方式似乎与 svn 的 show-c-function 参数的工作方式不同。

我的假设是我想要使用“git diff HEAD~i HEAD~i-1 -p”来增加 i 的值。我是否缺少一些参数,这些参数将有助于识别 diff 对已修改函数的最佳猜测?

Is there a good way to use git to identify all the modified functions in each revision in the history? I've tried using the -p switch, but it doesn't seem to work in the same way that svn's show-c-function parameter works.

My assumption is that I'll want to use "git diff HEAD~i HEAD~i-1 -p" for increasing values of i. Am I missing some parameters that will help identify diff's best guess on the functions that were modified?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

殤城〤 2024-11-09 11:37:50

这是一个神奇的圣歌,用于列出 git diff * 中的函数

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

......以及它的作用。 ..

  1. 选择当前差异,
  2. next 仅选择带有“hunk-headers”的行,
  3. next 仅选取带有左括号的行(可能包含函数名称),
  4. next 忽略大块标头,
  5. next 忽略左括号后面的文本,
  6. next 仅选取左括号之前的单词,
  7. 最后忽略列表中多次出现的同一单词。

瞧!您有一个正在被当前 git diff 修改的函数列表。


* 在运行 bash 的 Ubuntu 16.04 上使用 git version 2.7.4 进行验证。

Here is a magic chant to list functions within a git diff *

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

...and what it does is...

  1. Picks the current diff,
  2. next picks only lines with "hunk-headers" ,
  3. next picks only lines with opening parenthesis (as potentially containing function names),
  4. next ignores the hunk headers,
  5. next ignores the text after the opening parenthesis,
  6. next picks only the word immediately before the opening parenthesis,
  7. and lastly ignores multiple occurrences of the same word in the list.

Voila! you have a list of functions being modified by the current git diff.


* Verified using git version 2.7.4 on Ubuntu 16.04 running bash.

枫以 2024-11-09 11:37:50

这是我认为你想要的一个快速而肮脏的尝试。它使用 git log 来显示所有修订,使用 -p 将差异包含在日志中,使用 grep 来仅包含包含提交 ID 和 hunk 标头,并使用 sed 过滤掉行号,只留下对 Git 在 hunk 标头后面写入的函数名称的猜测。

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

Here's a quick and dirty attempt at what I think you're going for. It does a git log to show all revisions, -p to include the diff in the log, a grep to only include the lines containing the commit ID and the hunk headers, and uses sed to filter out the line numbers, leaving only the guess at the function name that Git writes after the hunk header.

git log -p | grep -E '^(commit|@@)' | sed 's/@@.*@@//'
记忆消瘦 2024-11-09 11:37:50

您可以使用 git textconv 过滤器列出修订版中所有修改的函数。这个想法是创建一个特定的过滤器,列出所有函数/方法,以及所有函数的主体校验和。这给出了这种过滤后的textconv视图:(

m() 12
bar() 42

这里m()是一个函数签名,12是其主体的校验和))

git diff 在修订前后的两个版本上使用此过滤器:

  • 如果添加了函数,则在 diff 中添加一行

示例:如果修改了函数,则添加 foo

m() 12
+ foo() 24 
bar() 42
  • ,校验和更改,并且在 diff 中更新了一行diff

示例:修改 foo 的主体

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

如何做到这一点?

  1. 创建过滤器: java-ls-methods.groovy
  2. > 是使用 Groovy 和 Spoon在 git 中注册此过滤器 : git config diff.java-ls-methods.textconv /home/path/to/java-ls-methods.groovy
  3. 将此过滤器与 Java 文件关联: echo "*.java diff=java-ls-methods ”>> .gitattributes
  4. 进行差异:git diff(与上次提交进行差异)或 git diff master(与另一个分支进行差异)
  5. 差异完成后,注释.gitattributes 中的行返回到正常的 diff

学分:解决方案灵感来自 https://stackoverflow.com/a /16929266

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

m() 12
bar() 42

(here m() is a function signature, 12 is a checksum of its body))

When git diff uses this filter on the two versions before and after the revision:

  • if a function is added, a line is added in the diff

Example: foo is added

m() 12
+ foo() 24 
bar() 42
  • if a function is modified, the checksum changes, and a line is updated in the diff

Example: the body of foo is modified

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

How to do that?

  1. Create the filter: java-ls-methods.groovy is an implementation of such a filter using Groovy and Spoon
  2. Register this filter in git: git config diff.java-ls-methods.textconv /home/path/to/java-ls-methods.groovy
  3. Associate this filter with Java files: echo "*.java diff=java-ls-methods" >> .gitattributes
  4. Make the diff: git diff (diff against last commit) or git diff master (diff against another branch)
  5. Once your diff is done, comment the line in .gitattributes to go back to a normal diff

Credits: solution inspired from https://stackoverflow.com/a/16929266

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文