-
-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
ein tool query
- a git analytics engine.
A tool to build and efficiently maintain a database of information contained in a git repository, preferably the kind of information that is expensive to obtain, in order to facilitate queries that would be prohibitive without an accelerating data structure.
- Loading branch information
Showing
19 changed files
with
1,062 additions
and
130 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use anyhow::Context; | ||
use rusqlite::{params, OptionalExtension}; | ||
|
||
/// A version to be incremented whenever the database layout is changed, to refresh it automatically. | ||
const VERSION: usize = 1; | ||
|
||
pub fn create(path: impl AsRef<std::path::Path>) -> anyhow::Result<rusqlite::Connection> { | ||
let path = path.as_ref(); | ||
let mut con = rusqlite::Connection::open(path)?; | ||
let meta_table = r#" | ||
CREATE TABLE if not exists meta( | ||
version int | ||
)"#; | ||
con.execute_batch(meta_table)?; | ||
let version: Option<usize> = con.query_row("SELECT version FROM meta", [], |r| r.get(0)).optional()?; | ||
match version { | ||
None => { | ||
con.execute("INSERT into meta(version) values(?)", params![VERSION])?; | ||
} | ||
Some(version) if version != VERSION => match con.close() { | ||
Ok(()) => { | ||
std::fs::remove_file(path) | ||
.with_context(|| format!("Failed to remove incompatible database file at {path:?}"))?; | ||
con = rusqlite::Connection::open(path)?; | ||
con.execute_batch(meta_table)?; | ||
con.execute("INSERT into meta(version) values(?)", params![VERSION])?; | ||
} | ||
Err((_, err)) => return Err(err.into()), | ||
}, | ||
_ => {} | ||
} | ||
con.execute_batch( | ||
r#" | ||
CREATE TABLE if not exists commits( | ||
hash blob(20) NOT NULL PRIMARY KEY | ||
) | ||
"#, | ||
)?; | ||
// Files are stored as paths which also have an id for referencing purposes | ||
con.execute_batch( | ||
r#" | ||
CREATE TABLE if not exists files( | ||
file_id integer NOT NULL PRIMARY KEY, | ||
file_path text UNIQUE | ||
) | ||
"#, | ||
)?; | ||
con.execute_batch( | ||
r#" | ||
CREATE TABLE if not exists commit_file( | ||
hash blob(20), | ||
file_id text, | ||
has_diff boolean NOT NULL, | ||
lines_added integer NOT NULL, | ||
lines_removed integer NOT NULL, | ||
lines_before integer NOT NULL, | ||
lines_after integer NOT NULL, | ||
mode integer, | ||
source_file_id integer, | ||
FOREIGN KEY (hash) REFERENCES commits (hash), | ||
FOREIGN KEY (file_id) REFERENCES files (file_id), | ||
PRIMARY KEY (hash, file_id) | ||
) | ||
"#, | ||
)?; | ||
|
||
Ok(con) | ||
} |
Oops, something went wrong.