askgit
is a command-line tool for running SQL queries on git repositories.
It's meant for ad-hoc querying of git repositories on disk through a common interface (SQL), as an alternative to patching together various shell commands.
It can execute queries that look like:
-- how many commits have been authored by user@email.com?
SELECT count(*) FROM commits WHERE author_email = 'user@email.com'
You can try queries on public git repositories without installing anything at https://try.askgit.com/
More in-depth examples and documentation can be found below. Also checkout our newsletter to stay up to date with feature releases and interesting queries and use cases.
brew tap askgitdev/askgit
brew install askgit
The latest releases should have pre-built binaries for Mac and Linux.
You can download and add the askgit
binary somewhere on your $PATH
to use.
libaskgit.so
is also available to be loaded as a SQLite run-time extension.
libgit2
is a build dependency (used via git2go
) and must be available on your system for linking.
The following (long π¬) go install
commands can be used to install a binary via the go toolchain.
On Mac:
CGO_CFLAGS=-DUSE_LIBSQLITE3 CGO_LDFLAGS=-Wl,-undefined,dynamic_lookup go install -tags="sqlite_vtable,vtable,sqlite_json1,static,system_libgit2" github.com/askgitdev/askgit@latest
On Linux:
CGO_CFLAGS=-DUSE_LIBSQLITE3 CGO_LDFLAGS=-Wl,--unresolved-symbols=ignore-in-object-files go install -tags="sqlite_vtable,vtable,sqlite_json1,static,system_libgit2" github.com/askgitdev/askgit@latest
See the Makefile
for more context.
Checking out this repository and running make
in the root will produce two files in the .build
directory:
askgit
- the CLI binary (which can then be moved into your$PATH
for use)libaskgit.so
- a shared object file SQLite extension that can be used by SQLite directly
Build an image locally using docker
docker build -t askgit:latest .
Or use an official image from docker hub
docker pull augmentable/askgit:latest
askgit
operates on a git repository. This repository needs to be attached as a volume. This example uses the (bash) built-in command pwd
for the current working directory
[pwd] Print the absolute pathname of the current working directory.
docker run --rm -v `pwd`:/repo:ro augmentable/askgit "SELECT * FROM commits"
For piping commands via STDIN, the docker command needs to be told to run non-interactively, as well as attaching the repository at /repo
.
cat query.sql | docker run --rm -i -v `pwd`:/repo:ro augmentable/askgit
askgit -h
Will output the most up to date usage instructions for your version of the CLI. Typically the first argument is a SQL query string:
askgit "SELECT * FROM commits"
Your current working directory will be used as the path to the git repository to query by default.
Use the --repo
flag to specify an alternate path, or even a remote repository reference (http(s) or ssh).
askgit
will clone the remote repository to a temporary directory before executing a query.
You can also pass a query in via stdin
:
cat query.sql | askgit
By default, output will be an ASCII table.
Use --format json
or --format csv
for alternatives.
See -h
for all the options.
The following tables access a git repository in the current directory by default.
If the --repo
flag is specified, they will use the path provided there instead.
A parameter (usually the first) can also be provided to any of the tables below to override the default repo path.
For instance, SELECT * FROM commits('https://github.com/askgitdev/askgit')
will clone this repo to a temporary directory on disk and return its commits.
Similar to git log
, the commits
table includes all commits in the history of the currently checked out commit.
Column | Type |
---|---|
hash | TEXT |
message | TEXT |
author_name | TEXT |
author_email | TEXT |
author_when | DATETIME |
committer_name | TEXT |
committer_email | TEXT |
committer_when | DATETIME |
parents | INT |
Params:
repository
- path to a local (on disk) or remote (http(s)) repositoryrev
- return commits starting at this revision (i.e. branch name or SHA), defaults toHEAD
-- return all commits starting at HEAD
SELECT * FROM commits
-- specify an alternative repo on disk
SELECT * FROM commits('/some/path/to/repo')
-- clone a remote repo and use it
SELECT * FROM commits('https://github.com/askgitdev/askgit')
-- use the default repo, but provide an alternate branch
SELECT * FROM commits('', 'some-ref')
Column | Type |
---|---|
name | TEXT |
type | TEXT |
remote | TEXT |
full_name | TEXT |
hash | TEXT |
target | TEXT |
Params:
repository
- path to a local (on disk) or remote (http(s)) repository
Column | Type |
---|---|
file_path | TEXT |
additions | INT |
deletions | INT |
Params:
repository
- path to a local (on disk) or remote (http(s)) repositoryrev
- commit hash (or branch/tag name) to use for retrieving stats, defaults toHEAD
to_rev
- commit hash to calculate stats relative to
-- return stats of HEAD
SELECT * FROM stats
-- return stats of a specific commit
SELECT * FROM stats('', 'COMMIT_HASH')
-- return stats for every commit in the current history
SELECT commits.hash, stats.* FROM commits, stats('', commits.hash)
Column | Type |
---|---|
path | TEXT |
executable | BOOL |
contents | TEXT |
Params:
repository
- path to a local (on disk) or remote (http(s)) repositoryrev
- commit hash (or branch/tag name) to use for retrieving files in, defaults toHEAD
Similar to git blame
, the blame
table includes blame information for all files in the current HEAD.
Column | Type |
---|---|
line_no | INT |
commit_hash | TEXT |
Params:
repository
- path to a local (on disk) or remote (http(s)) repositoryrev
- commit hash (or branch/tag name) to use for retrieving blame information from, defaults toHEAD
file_path
- path of file to blame
The SQLite JSON1 extension is included for working with JSON data.
Scalar function that converts toml
to json
.
SELECT toml_to_json('[some-toml]')
-- +-----------------------------+
-- | TOML_TO_JSON('[SOME-TOML]') |
-- +-----------------------------+
-- | {"some-toml":{}} |
-- +-----------------------------+
Scalar function that converts xml
to json
.
SELECT xml_to_json('<some-xml>hello</some-xml>')
-- +-------------------------------------------+
-- | XML_TO_JSON('<SOME-XML>HELLO</SOME-XML>') |
-- +-------------------------------------------+
-- | {"some-xml":"hello"} |
-- +-------------------------------------------+
Scalar function that converts yaml
to json
.
SELECT yaml_to_json('hello: world')
-- +------------------------------+
-- | YAML_TO_JSON('HELLO: WORLD') |
-- +------------------------------+
-- | {"hello":"world"} |
-- +------------------------------+
Helper for splitting strings on some separator.
SELECT str_split('hello,world', ',', 0)
-- +----------------------------------+
-- | STR_SPLIT('HELLO,WORLD', ',', 0) |
-- +----------------------------------+
-- | hello |
-- +----------------------------------+
SELECT str_split('hello,world', ',', 1)
-- +----------------------------------+
-- | STR_SPLIT('HELLO,WORLD', ',', 1) |
-- +----------------------------------+
-- | world |
-- +----------------------------------+
Functions from the enry
project are also available as SQL scalar functions
Supply a file path and some source code to detect the language.
SELECT enry_detect_language('some/path/to/file.go', '<contents of file>')
Given a blob, determine if it's a binary file or not (returns 1 or 0).
SELECT enry_is_binary('<contents of file>')
Detect whether a file path is to a configuration file (returns 1 or 0).
SELECT enry_is_configuration('some/path/to/file/config.json')
Detect whether a file path is to a documentation file (returns 1 or 0).
SELECT enry_is_documentation('some/path/to/file/README.md')
Detect whether a file path is to a dot file (returns 1 or 0).
SELECT enry_is_dot_file('some/path/to/file/.gitignore')
Detect whether a file path is generated (returns 1 or 0).
SELECT enry_is_generated('some/path/to/file/generated.go', '<contents of file>')
Detect whether a file path is to an image (returns 1 or 0).
SELECT enry_is_image('some/path/to/file/image.png')
Detect whether a file path is to a test file (returns 1 or 0).
SELECT enry_is_test('some/path/to/file/image.png')
Detect whether a file path is to a vendored file (returns 1 or 0).
SELECT enry_is_vendor('vendor/file.go')
This will return all commits in the history of the currently checked out branch/commit of the repo.
SELECT * FROM commits
Return the (de-duplicated) email addresses of commit authors:
SELECT DISTINCT author_email FROM commits
Return the commit counts of every author (by email):
SELECT author_email, count(*) FROM commits GROUP BY author_email ORDER BY count(*) DESC
Same as above, but excluding merge commits:
SELECT author_email, count(*) FROM commits WHERE parents < 2 GROUP BY author_email ORDER BY count(*) DESC
Outputs the set of files in the current tree:
SELECT * FROM files
Returns author emails with lines added/removed, ordered by total number of commits in the history (excluding merges):
SELECT count(DISTINCT commits.hash) AS commits, SUM(additions) AS additions, SUM(deletions) AS deletions, author_email
FROM commits LEFT JOIN stats('', commits.hash)
WHERE commits.parents < 2
GROUP BY author_email ORDER BY commits
Returns commit counts by author, broken out by day of the week:
SELECT
count(*) AS commits,
count(CASE WHEN strftime('%w',author_when)='0' THEN 1 END) AS sunday,
count(CASE WHEN strftime('%w',author_when)='1' THEN 1 END) AS monday,
count(CASE WHEN strftime('%w',author_when)='2' THEN 1 END) AS tuesday,
count(CASE WHEN strftime('%w',author_when)='3' THEN 1 END) AS wednesday,
count(CASE WHEN strftime('%w',author_when)='4' THEN 1 END) AS thursday,
count(CASE WHEN strftime('%w',author_when)='5' THEN 1 END) AS friday,
count(CASE WHEN strftime('%w',author_when)='6' THEN 1 END) AS saturday,
author_email
FROM commits GROUP BY author_email ORDER BY commits
You can use the askgit export
sub command to save the output of queries into a sqlite database file.
The command expects a path to a db file (which will be created if it doesn't already exist) and a variable number of "export pairs," specified by the -e
flag.
Each pair represents the name of a table to create and a query to generate its contents.
askgit export my-export-file -e commits -e "SELECT * FROM commits" -e files -e "SELECT * FROM files"
This can be useful if you're looking to use another tool to examine the data emitted by askgit
.
Since the exported file is a plain SQLite database, queries should be much faster (as the original git repository is no longer traversed) and you should be able to use any tool that supports querying SQLite database files.