Skip to content

Git Integration

You can overload the AST node with extra methods to get information from Git.

Let's start with some basic setup to reuse in the next examples:

Git require

By default, this extension is not loaded in the fast environment, so you should require it.

require 'fast/git'

Then it will work with any AST node.

ast = Fast.ast_from_file('lib/fast.rb')

Log

First commit from git:

ast.git_log.first.author.name # => "Jonatas Davi Paganini"

It uses ruby-git gem, so all methods are available:

ast.git_log.since(Time.mktime(2019)).entries.map(&:message)

Counting commits per year:

ast.git_log.entries.group_by{|t|t.date.year}.transform_values(&:size)
# => {2020=>4, 2019=>22, 2018=>4}

Counting commits per contributor:

ast.git_log.entries.group_by{|t|t.author.name}.transform_values(&:size)
# => {"Jônatas Davi Paganini"=>29, ...}

Selecting last commit message:

ast.last_commit.message # => "Add node extensions for extracting info from git (#21)"

Remote git URL:

ast.remote_url  # => "git@github.com:jonatas/fast.git"
ast.project_url # => "https://github.com/jonatas/fast"

The sha from last commit:

ast.sha # => "cd1c036b55ec1d41e5769ad73b282dd6429a90a6"

Pick a link from the files to master version:

ast.link # => "https://github.com/jonatas/fast/blob/master/lib/fast.rb#L3-L776"

Getting permalink from current commit:

ast.permalink # => "https://github.com/jonatas/fast/blob/cd1c036b55ec1d41e5769ad73b282dd6429a90a6/lib/fast.rb#L3-L776"

Let's say you'd like to capture a list of class names that inherits the Find class:

puts ast.capture("(class $(const nil _) (const nil Find)").map(&:md_link).join("\n* ")

It will output the following links:

If you need to get a permanent link to the code, use the permalink method:

ast.search("(class (const nil _) (const nil Find)").map(&:permalink)
# => ["https://github.com/jonatas/fast/blob/cd1c036b55ec1d41e5769ad73b282dd6429a90a6/lib/fast.rb#L524-L541",
#     "https://github.com/jonatas/fast/blob/cd1c036b55ec1d41e5769ad73b282dd6429a90a6/lib/fast.rb#L551-L571", ...]