News for hackers. Stuff that matters.
Re:See the History of a Method with Git log -L by chiefalchemist on Sunday November 05, 2023 @12:50PM (#38153532)
How can this be uses with PHP?
Re:See the History of a Method with Git log -L by mschuster91 on Sunday November 05, 2023 @01:44PM (#38154187)
According to `man gitattributes` [1] a `*.php diff=php` should be enough.

[1] https://git-scm.com/docs/gitattributes

Re:See the History of a Method with Git log -L by OJFord on Sunday November 05, 2023 @02:30PM (#38154704)
It's just a regex to match the line you want to see the history of. Function would be a common case, but it's completely arbitrary.

For example, `git log -L:members:Cargo.toml` will show the history of constituent rust projects in a Cargo workspace.

Re:See the History of a Method with Git log -L by staunton on Sunday November 05, 2023 @02:38PM (#38154794)
It's a regex? Is it even guaranteed to work then? I would think you have to parse non-regular languages to always find the end of a function in a some languages?
Re:See the History of a Method with Git log -L by simonw on Sunday November 05, 2023 @03:26PM (#38155293)
There's a comment about that here: https://github.com/git/git/blob/bc5204569f7db44d22477485afd5...

    When writing or updating patterns, assume that the contents these
    patterns are applied to are syntactically correct.  The patterns
    can be simple without implementing all syntactical corner cases, as
    long as they are sufficiently permissive.
Re:See the History of a Method with Git log -L by staunton on Sunday November 05, 2023 @03:35PM (#38155392)
Wow, that file must be paradise for regex nerds, assuming there are any such...
Re:See the History of a Method with Git log -L by tessierashpool on Sunday November 05, 2023 @03:58PM (#38155576)
there are, I am, and it's not (sorry). some languages have the ability to comment regexes, and that would be very useful here.
Re:See the History of a Method with Git log -L by staunton on Sunday November 05, 2023 @06:21PM (#38156802)
It has lots of comments inside the regexes. How would this better comment support look like?
Re:See the History of a Method with Git log -L by sundarurfriend on Monday November 06, 2023 @12:40AM (#38159269)
[delayed]
Re:See the History of a Method with Git log -L by sundarurfriend on Monday November 06, 2023 @12:27AM (#38159192)
there are, I am, and it is. (Except for the proliferation of backslashes due to C not having "raw" strings.)
Re:See the History of a Method with Git log -L by OJFord on Sunday November 05, 2023 @06:31PM (#38156885)
It's just to match the (starting) line. I assume the +n context uses the same method diffs do anywhere else, however that works (and sometimes doesn't).
Re:See the History of a Method with Git log -L by baggy_trough on Sunday November 05, 2023 @02:06PM (#38154460)
This is a great capability. Wonder how many more are lurking within tools I use every day but never read the man pages.
Re:See the History of a Method with Git log -L by n_sd on Sunday November 05, 2023 @02:17PM (#38154571)
How can we handle function overloading case with this?
Re:See the History of a Method with Git log -L by g0xA52A2A on Sunday November 05, 2023 @02:38PM (#38154797)
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?
Re:See the History of a Method with Git log -L by simonw on Sunday November 05, 2023 @03:21PM (#38155249)
If you're curious how Git knows the syntax of different languages in order to support this kind of feature, take a look in https://github.com/git/git/blob/master/userdiff.c

Here's how support for Python and Ruby are defined:

    PATTERNS("python",
        "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$",
        /* -- */
        "[a-zA-Z_][a-zA-Z0-9_]*"
        "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
        "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
        /* -- */
    PATTERNS("ruby",
        "^[ \t]*((class|module|def)[ \t].*)$",
        /* -- */
        "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
        "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
        "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
Re:See the History of a Method with Git log -L by lloydatkinson on Sunday November 05, 2023 @03:39PM (#38155426)
Interesting. Also I am surprised at how short the list is!
Re:See the History of a Method with Git log -L by tessierashpool on Sunday November 05, 2023 @03:54PM (#38155545)
it's a fantastic feature in theory. in practice, it's imprecise and error-prone, and I believe these regular expressions are probably why. I hadn't looked at the implementation before, but I approached it from the other end: I set up a bunch of test cases, and I was pretty disappointed.

there were two disappointments. first, `git log -L` seems to prioritize tracking blocks of code over lines of code. that's just a design choice I disagree with, so it wasn't a big deal. but it also lost track of lines of code for me quite often, and produced a number of false positives to boot.

to be fair, I haven't tried using `diff=LANG` (per a comment below), and that might get more reliable results.

Re:See the History of a Method with Git log -L by Groxx on Sunday November 05, 2023 @04:52PM (#38156028)
Yeah, this has been my experience too. Easily confused by common constructions in some codebases, and that can make it almost completely useless. I would happily sacrifice a lot of speed to get a difftastic level of precision.
Re:See the History of a Method with Git log -L by anitil on Sunday November 05, 2023 @07:49PM (#38157480)
I've attempted something similar to your ast-search tool, but it instead iterates through git history, pulls out the relevant text and then provides the diff to the user.

It's a tricky problem because it sits somewhere between text, where a function name could get renamed and it's obvious because it is textually similar, and an AST where 'similarity' is a difficult concept.

I struggled to make it usable, but of course there's a module to do half of it that I didn't find initially - https://pypi.org/project/pyastsim/

Re:See the History of a Method with Git log -L by forrestthewoods on Sunday November 05, 2023 @03:38PM (#38155420)
Git really needs an equivalent to Perforce's Timelapse View.
Re:See the History of a Method with Git log -L by nneonneo on Sunday November 05, 2023 @03:46PM (#38155485)
Oh wow, this is very cool!

The way this works boils down to the following: by default, Git has a heuristic for determining the "context" of a diff hunk by looking for lines that start with certain non-whitespace characters. This context is printed out after the "@@" marker in the hunk header. Within git, this context is referred to as the "function name", but that's a bit inaccurate as the patterns will usually match other scopes like namespaces and classes.

Setting "diff=LANG" activates a different (regular expression) pattern which is used to identify context; for example, in Python, this will look for "class" and "def" keywords. Git ships with a bunch of built-in patterns (defined in https://github.com/git/git/blob/master/userdiff.c), and the "diff.LANG.xfuncname" config option can be used to specify a custom pattern.

-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 "foo", you could use -L ':\bfoo\b:file.py' (note that if you don't use \b you'll get every function that contains the word foo). Also related is the -W flag, which will show the entire function/class/scope in the diff, again based on context.

Note some limitations: the matching is line-by-line, so it will pick up "context" 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'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 "def foo(" to ensure you only match to methods and not to classes or parameter names).

See also https://stackoverflow.com/questions/28111035/where-does-the-... for a very comprehensive overview of this feature.

Re:See the History of a Method with Git log -L by sundarurfriend on Monday November 06, 2023 @03:23AM (#38160014)
> Within git, this context is referred to as the "function name", but that's a bit inaccurate as the patterns will usually match other scopes like namespaces and classes.

Thank you for mentioning this (and the other details). The userdiff.c file was mentioned elsewhere in the thread, but I was doubting it since its regexes also matched classes, Perl POD blocks, etc. Good to have it clarified that it's the Git man pages that are inaccurate, helps understand this file (userdiff.c) and this feature better.

Re:See the History of a Method with Git log -L by gigatexal on Sunday November 05, 2023 @04:02PM (#38155612)
I had no idea about this. This is amazing
Re:See the History of a Method with Git log -L by Nezteb on Sunday November 05, 2023 @08:15PM (#38157660)
I love using the `-G` flag for tracking the history of any occurrence of a given regex across all directories/files. It feels more flexible than `-L`. As an example:

  git log \
    -G "$some_regex" \
    --patch \
    --stat \
    --source \
    --all \
    --decorate=full \
    --pretty=fuller \
    -- . ":(exclude)\*.lock"
Re:See the History of a Method with Git log -L by penguin_booze on Monday November 06, 2023 @04:49AM (#38160537)
I often have to remind myself that the pathspec is defined here in the glossary: https://git-scm.com/docs/gitglossary#Documentation/gitglossa....