Live data from Hacker News

See the History of a Method with Git log -L

calebhearth.com

21–26 of 26 posts

Re: See the History of a Method with Git log -L

#21
post #9

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]?" "|[-+*/ %&^|=!]=|//=?| >=?|\\*\\*=?"), /* -- */ PAT…

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

#22
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

#23
post #10

Earlier quoted context omitted.

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.

Wow, that file must be paradise for regex nerds, assuming there are any such...

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

#24

Earlier quoted context omitted.

there are, I am, and it's not (sorry). some languages have the ability to comment regexes, and that would be very useful here.

It has lots of comments inside the regexes. How would this better comment support look like?

These are not technically "comments inside the regexes", that would be something like the "Delete (most) C comments." regex here: https://perldoc.perl.org/perlre#/x-and-/xx

Here, instead, they've used string juxtaposition cleverly to write comments between parts of the regex/string. It effectively serves the same purpose though.

Re: See the History of a Method with Git log -L

#25

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…

> 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

#26
post #22

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"

I often have to remind myself that the pathspec is defined here in the glossary: https://git-scm.com/docs/gitglossary#Documentation/gitglossa....
Post reply on HN