TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

See the History of a Method with Git log -L

116 pointsby caleb_thompsonover 1 year ago

9 comments

simonwover 1 year ago
If you&#x27;re curious how Git knows the syntax of different languages in order to support this kind of feature, take a look in <a href="https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;blob&#x2F;master&#x2F;userdiff.c">https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;blob&#x2F;master&#x2F;userdiff.c</a><p>Here&#x27;s how support for Python and Ruby are defined:<p><pre><code> PATTERNS(&quot;python&quot;, &quot;^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$&quot;, &#x2F;* -- *&#x2F; &quot;[a-zA-Z_][a-zA-Z0-9_]*&quot; &quot;|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?&quot; &quot;|[-+*&#x2F;&lt;&gt;%&amp;^|=!]=|&#x2F;&#x2F;=?|&lt;&lt;=?|&gt;&gt;=?|\\*\\*=?&quot;), &#x2F;* -- *&#x2F; PATTERNS(&quot;ruby&quot;, &quot;^[ \t]*((class|module|def)[ \t].*)$&quot;, &#x2F;* -- *&#x2F; &quot;(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*&quot; &quot;|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?.&quot; &quot;|&#x2F;&#x2F;=?|[-+*&#x2F;&lt;&gt;%&amp;^|=!]=|&lt;&lt;=?|&gt;&gt;=?|===|\\.{1,3}|::|[!=]~&quot;),</code></pre>
评论 #38155545 未加载
评论 #38157480 未加载
评论 #38155426 未加载
nneonneoover 1 year ago
Oh wow, this is very cool!<p>The way this works boils down to the following: by default, Git has a heuristic for determining the &quot;context&quot; of a diff hunk by looking for lines that start with certain non-whitespace characters. This context is printed out after the &quot;@@&quot; marker in the hunk header. Within git, this context is referred to as the &quot;function name&quot;, but that&#x27;s a bit inaccurate as the patterns will usually match other scopes like namespaces and classes.<p>Setting &quot;diff=LANG&quot; activates a different (regular expression) pattern which is used to identify context; for example, in Python, this will look for &quot;class&quot; and &quot;def&quot; keywords. Git ships with a bunch of built-in patterns (defined in <a href="https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;blob&#x2F;master&#x2F;userdiff.c">https:&#x2F;&#x2F;github.com&#x2F;git&#x2F;git&#x2F;blob&#x2F;master&#x2F;userdiff.c</a>), and the &quot;diff.LANG.xfuncname&quot; config option can be used to specify a custom pattern.<p>-L can then be used to look for hunks which have context matching a certain pattern. For example, if you want to look for function &quot;foo&quot;, you could use -L &#x27;:\bfoo\b:file.py&#x27; (note that if you don&#x27;t use \b you&#x27;ll get every function that <i>contains</i> the word foo). Also related is the -W flag, which will show the entire function&#x2F;class&#x2F;scope in the diff, again based on context.<p>Note some limitations: the matching is line-by-line, so it will pick up &quot;context&quot; from things like string literals and comments, and you will only get the first line of the context (so multi-line signatures will be truncated). Also, since -L takes a regular expression to match against the context line, you&#x27;ll want to take care to use an appropriate pattern to avoid matching unwanted functions (e.g. use \b to avoid substring matches, or even &quot;def foo(&quot; to ensure you only match to methods and not to classes or parameter names).<p>See also <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;28111035&#x2F;where-does-the-excerpt-in-the-git-diff-hunk-header-come-from" rel="nofollow noreferrer">https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;28111035&#x2F;where-does-the-...</a> for a very comprehensive overview of this feature.
评论 #38160014 未加载
Neztebover 1 year ago
I love using the `-G` flag for tracking the history of any occurrence of a given regex across all directories&#x2F;files. It feels more flexible than `-L`. As an example:<p><pre><code> git log \ -G &quot;$some_regex&quot; \ --patch \ --stat \ --source \ --all \ --decorate=full \ --pretty=fuller \ -- . &quot;:(exclude)\*.lock&quot;</code></pre>
评论 #38160537 未加载
g0xA52A2Aover 1 year ago
Huh TIL Git can do this for non C-like languages out of the box. Is there a reason these custom hunk handlers are not defined by default for the languages Git ships support with?
baggy_troughover 1 year ago
This is a great capability. Wonder how many more are lurking within tools I use every day but never read the man pages.
forrestthewoodsover 1 year ago
Git really needs an equivalent to Perforce&#x27;s Timelapse View.
n_sdover 1 year ago
How can we handle function overloading case with this?
gigatexalover 1 year ago
I had no idea about this. This is amazing
chiefalchemistover 1 year ago
How can this be uses with PHP?
评论 #38154187 未加载
评论 #38154704 未加载