古川制御日記

佐賀県武雄市の組み込み開発屋ブログ

gitで特定の関数(サブルーチン)の履歴を追う

> git log -L :main:main.c

みたいな感じでOK

$ git log -L :main:main.c --pretty=

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -3,4 +5,5 @@
 int main(void) {
-    printf("Hello World\n");
+    sub();
     return 0;
 }
+

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -3,4 +3,4 @@
 int main(void) {
-    printf("hello world\n");
+    printf("Hello World\n");
     return 0;
 }

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -3,4 +3,4 @@
 int main(void) {
-    printf("hello world");
+    printf("hello world\n");
     return 0;
 }

でも以下のような行頭に宣言部を含むstatic関数の場合は。

#include <stdio.h>

static void sub(void);

int main(void) {
    sub();
    return 0;
}

static void sub(void) {
    printf("Hello World\n");
}
$ git log -L :sub:main.c  --pretty=

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -3,0 +3,2 @@
+static void sub(void);
+

行頭の宣言部を追跡してしまう。。
こういう場合は正規表現で追跡部の開始位置と終了位置を指定してあげればうまく追跡してくれる。

$ git log -L '/static void sub(void) {/,/^}/':main.c  --pretty=

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -10,3 +10,3 @@
 static void sub(void) {
-    printf("hello world\n");
+    printf("Hello World\n");
 }

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -10,3 +10,3 @@
 static void sub(void) {
-    printf("hello world");
+    printf("hello world\n");
 }

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -7,0 +10,3 @@
+static void sub(void) {
+    printf("hello world");
+}

けど面倒くさい。。
git-log(1)読むと前の-Lレンジの最後から追跡を開始する、みたいなこと書いてあるので-Lを2つ並べてみると

$ git log -L 1,4:main.c -L :sub:main.c --pretty=

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -10,3 +10,3 @@
 static void sub(void) {
-    printf("hello world\n");
+    printf("Hello World\n");
 }

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -10,3 +10,3 @@
 static void sub(void) {
-    printf("hello world");
+    printf("hello world\n");
 }

diff --git a/main.c b/main.c
--- a/main.c
+++ b/main.c
@@ -1,2 +1,4 @@
 #include <stdio.h>

+static void sub(void);
+
@@ -7,0 +10,3 @@
+static void sub(void) {
+    printf("hello world");
+}

diff --git a/main.c b/main.c
--- /dev/null
+++ b/main.c
@@ -0,0 +1,2 @@
+#include <stdio.h>

イマイチ。。

単純に行末から探してくれるようなオプションはないぽい
git-log(1)には

“^:<funcname>” searches from the start of file.

from the end of fileな検索があればいいですね。