【问题标题】:How can I use Git to identify function changes across different revisions of a repository?如何使用 Git 识别存储库的不同修订版之间的功能更改?
【发布时间】:2017-08-06 18:52:38
【问题描述】:

我有一个包含一堆 C 文件的存储库。给定两次提交的 SHA 哈希,

<commit-sha-1><commit-sha-2>

我想编写一个脚本(可能是 bash/ruby/python)来检测存储库中 C 文件中的哪些函数在这两个提交中发生了更改。

我目前正在查看git loggit commitgit diff 的文档。如果有人以前做过类似的事情,你能给我一些关于从哪里开始或如何继续的指示吗?

【问题讨论】:

  • 可能值得检查一下它为您的更改提供了哪些输出,但 gif diff-W/--function-context 标志可能是一个很好的起点。

标签: ruby git bash github version-control


【解决方案1】:

this question

Bash 脚本:

#!/usr/bin/env bash

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

解释:

1:获取差异

2:只获取带有大块标题的行;如果大块头的“可选节标题”存在,它将是修改函数的函数定义

3:只选择包含左括号的大块标题,因为它们将包含函数定义

4:去掉行中的 '@@ [old-file-range] [new-file-range] @@' 部分

5:去掉括号后的一切

6:从指针中去掉'*'

7:[参见 'awk']:打印记录(即:行)的最后一个字段(即:列)。

8:去掉重复的名字。

【讨论】:

  • 您可以标记自己的答案,以便其他有类似问题的人受益。
【解决方案2】:

这看起来不太好,但您可以将 git 与您的 最喜欢的标记系统,例如GNU global 来实现这一点。为了 示例:

#!/usr/bin/env sh

global -f main.c | awk '{print $NF}'  | cut -d '(' -f1 | while read i
do
    if [ $(git log -L:"$i":main.c HEAD^..HEAD | wc -l) -gt 0 ]
    then
        printf "%s() changed\n" "$i"
    else
        printf "%s() did not change\n" "$i"
    fi
done

首先,您需要在项目中创建一个函数数据库:

$ gtags .

然后运行上面的脚本来查找main.c中的函数 自上次提交后修改。剧本当然可以更多 灵活,例如,它可以处理git diff --stats 报告的两次提交之间更改的所有*.c 文件。

在脚本中我们使用git log-L 选项:

  -L <start>,<end>:<file>, -L :<funcname>:<file>

       Trace the evolution of the line range given by
       "<start>,<end>" (or the function name regex <funcname>)
       within the <file>. You may not give any pathspec
       limiters. This is currently limited to a walk starting from
       a single revision, i.e., you may only give zero or one
       positive revision arguments. You can specify this option
       more than once.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-24
    • 2018-06-15
    • 2017-09-07
    • 1970-01-01
    相关资源
    最近更新 更多