Git how to view the change history/deletion of a function?

Viewed 1267

I am trying to find a function that was deleted at some point in time. I tried to use git log -L :<funcname>:<file> to locate the function that was deleted. The result ends up being fatal: -L parameter '<funcname>' starting at line 1: no match.

For example if I am searching for function findMe.

I enter git log -L :findMe:index.php

index.php

 <?php
 class stuff {.....}
 class otherStuff{....}
 class moreStuff{
   public function test(){...}
   public function findMe() {....}
   public function test1() {....}
 }
?>

Result fatal: -L parameter '<funcname>' starting at line 1: no match.

Reference git log:

-L :<funcname>:<file>

If “:<funcname>” is given in place of <start> and <end>, it is a regular expression that denotes the range from the first funcname line that matches <funcname>, up to the next funcname line. “:<funcname>” searches from the end of the previous -L range, if any, otherwise from the start of file. “^:<funcname>” searches from the start of file.

3 Answers

That should be easier with Git 2.30 (Q1 2021).
Before, "git blame -L :funcname -- path"(man) did not work well for a path for which a userdiff driver is defined.

See commit 3af31e8, commit 88894aa, commit 9466e38, commit 180d641, commit 0cce88f, commit a4514a4, commit fd5c74e, commit f9c8d8c (01 Nov 2020) by Philippe Blain (phil-blain).
(Merged by Junio C Hamano -- gitster -- in commit 3f6dc9c, 18 Nov 2020)

blame: enable funcname blaming with userdiff driver

Signed-off-by: Philippe Blain

In blame.c::cmd_blame, we send the 'path' field of the 'sb' 'struct blame_scoreboard' as the 'path' argument to 'line-range.c::parse_range_arg', but 'sb.path' is not set yet; it's set to the local variable 'path' a few lines later at line 1137.

This 'path' argument is only used in 'parse_range_arg' if we are blaming a funcname, i.e. git blame -L :<funcname> <path>(man), and in that case it is sent to 'parse_range_funcname', where it is used to determine if a userdiff driver should be used for said <path> to match the given funcname.

Since 'path' is yet unset, the userdiff driver is never used, so we fall back to the default funcname regex, which is usually not appropriate for paths that are set to use a specific userdiff driver, and thus either we match some unrelated lines, or we die with

fatal: -L parameter '<funcname>' starting at line 1: no match  

This has been the case ever since git blame(man) learned to blame a funcname in 13b8f68c1f ("log -L: :pattern:file syntax to find by funcname", 2013-03-28, Git v1.8.4-rc0 -- merge).

Enable funcname blaming for paths using specific userdiff drivers by initializing 'sb.path' earlier in 'cmd_blame', when some of its other fields are initialized, so that it is set when passed to 'parse_range_arg'.

git log -L only searches for a funcname on the highest level of the file. You can use the command to see the history of any of your classes or any function outside of a class, but unfortunately it can't find functions inside of a class.

The best you can do is to git log -L :moreStuff:index.php, and search the output for the function logs.

Related