git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 3/3] completion: offer ctags symbol names for 'git log -S', '-G' and '-L:'

From
Jeff King <peff@peff.net>
Date
Mar 24, 2017, 00:52 UTC
Message-ID
<20170324005256.ji2wijhyqnwbpp5t@sigill.intra.peff.net>
In-Reply-To
<20170323153839.24283-4-szeder.dev@gmail.com>
On Thu, Mar 23, 2017 at 04:38:39PM +0100, SZEDER Gábor wrote:
Show 7 quoted lines
> Just like in the case of search patterns for 'git grep', see 29eec71f2
> (completion: match ctags symbol names in grep patterns, 2011-10-21)),
> a common thing to look for using 'git log -S', '-G' and '-L:' is the
> name of a symbol.
> 
> Teach the completion for 'git log' to offer ctags symbol names after
> these options, both in stuck and in unstuck forms.
I think this makes sense and is an improvement over the status quo.
There are two gotchas with completing "-L" like this:
  1. You still have to come up with the filename yourself for "-L".
  2. The function name is actually a regex, so you can get bit when your
     function name is a subset of another.

I have a script (below) which makes this easier (and I complete its argument using the tags file). It's probably too gross to even go into contrib, but I thought I'd share.

"log -S" sometimes benefits from limiting by filename, too, but it depends what you're doing. I don't have a gross script for that. :)

-- >8 --
#!/usr/bin/env perl
if (!@ARGV) {
  print STDERR "usage: git flog [options] <function>\n";
  exit 1;
}

my $func = pop @ARGV; my $file = get_file_from_tags($func); my $regex = '[^A-Za-z_]' . $func . '[^A-Za-z0-9_]'; exec qw(git log), "-L:$regex:$file", @ARGV; exit 1;

sub get_file_from_tags {
  my $token = shift;
  open(my $fh, '<', 'tags')
    or die "unable to open tags: $!\n";
  while (<$fh>) {
    chomp;
    # this isn't exactly right, as the Ex command may contain
    # embedded tabs, but it's accurate for the token and filename,
    # which come before, and probably good enough to match extension fields
    # which come after
    my @fields = split /\t/;
    next unless $fields[0] eq $token;
    # only look for functions; assumes your ctags uses the "kind"
    # extension field. Note also that some implementations write the "kind:"
    # header and some do not. This handles both.
    next unless grep { /^(kind:\s*)?f$/ } @fields;
    # there may be more, but we don't have any way of disambiguating,
    # so just return the first match
    return $fields[1];
  }
  die "unknown token: $token\n";
}
__END__
Previous: SZEDER GáborNext: SZEDER Gábor
Message 7 of 9 in “completion: updates to ctags symbol names”
  1. 0/3 completion: updates to ctags symbol namesSZEDER Gábor, Mar 23, 2017
  2. 1/3 completion: put matching ctags symbol names directly into COMPREPLYSZEDER Gábor, Mar 23, 2017
  3. Jeff KingMar 24, 2017
  4. 2/3 completion: extract completing ctags symbol names into helper functionSZEDER Gábor, Mar 23, 2017
  5. Jeff KingMar 24, 2017
  6. 3/3 completion: offer ctags symbol names for 'git log -S', '-G' and '-L:'SZEDER Gábor, Mar 23, 2017
  7. Jeff KingMar 24, 2017
  8. SZEDER GáborMar 30, 2017
  9. Jeff KingApr 1, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.