Initialize repo and update tracked refs

This commit is contained in:
Joscha 2023-08-05 02:47:43 +02:00
parent 5871b19f8e
commit 5dbd8e886b
7 changed files with 141 additions and 4 deletions

View file

@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "UPDATE commits SET new = 0",
"describe": {
"columns": [],
"parameters": {
"Right": 0
},
"nullable": []
},
"hash": "0d36a4c7d21f4688db1cf309fd04c1f56ff147b7f80d8844bb2b5bad63dab372"
}

View file

@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "INSERT OR IGNORE INTO tracked_refs (name, hash) VALUES (?, ?)",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "4a8f4b5856c5c4d117f80983e83d700592728bc0d8301b9054082958332d2ebd"
}

View file

@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "UPDATE tracked_refs SET hash = ? WHERE name = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "6e255c9db5fab15a188c0a1ecd333cc918937598a0c88851ac6f94de41796a33"
}

View file

@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM tracked_refs WHERE name = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 1
},
"nullable": []
},
"hash": "d6249ada8a6f58fabb3877446e851cfc88c6188163e5726d83941cdfb6e41c9e"
}

View file

@ -0,0 +1,26 @@
{
"db_name": "SQLite",
"query": "SELECT name, hash FROM tracked_refs",
"describe": {
"columns": [
{
"name": "name",
"ordinal": 0,
"type_info": "Text"
},
{
"name": "hash",
"ordinal": 1,
"type_info": "Text"
}
],
"parameters": {
"Right": 0
},
"nullable": [
false,
false
]
},
"hash": "f39201c5c3a530a7f659a923a230e3e42dbe19c1ea5d71e6dd5910905e4402d4"
}

View file

@ -11,7 +11,7 @@ CREATE TABLE commit_links (
FOREIGN KEY (child) REFERENCES commits (hash) ON DELETE CASCADE
) STRICT;
CREATE TABLE branches (
CREATE TABLE tracked_refs (
name TEXT NOT NULL PRIMARY KEY,
hash TEXT NOT NULL,
FOREIGN KEY (hash) REFERENCES commits (hash) ON DELETE CASCADE

View file

@ -11,8 +11,11 @@ use tracing::{debug, debug_span, error, info, Instrument};
use crate::state::AppState;
/// Add new commits from the repo to the database, marked as new.
// TODO Initialize tracked refs?
// TODO Update tracked refs?
// TODO Rename so the name fits better (maybe rename update module to recurring as well?)
// upadte module -> recurring
// Fetching new commits -> fetch submodule
// Updating repo (current file) -> repo submodule
// Updating queue -> queue submodule
async fn add_new_commits_to_db(db: &SqlitePool, repo: &Repository) -> anyhow::Result<()> {
debug!("Adding new commits to db");
let mut tx = db.begin().await?;
@ -37,6 +40,17 @@ async fn add_new_commits_to_db(db: &SqlitePool, repo: &Repository) -> anyhow::Re
insert_new_commit_links(conn, &new).await?;
debug!("Inserted {} new commits into db", new.len());
if old.is_empty() {
// We've never seen any repo before, so we need to do additional
// initialization.
info!("Detected new repo, initializing");
mark_all_commits_as_old(conn).await?;
track_main_branch(conn, repo).await?;
info!("Initialized new repo");
}
update_tracked_refs(conn, repo).await?;
tx.commit().await?;
debug!("Finished adding new commits to db");
Ok(())
@ -102,7 +116,7 @@ async fn insert_new_commit_links(conn: &mut SqliteConnection, new: &[Info]) -> a
sqlx::query!(
"INSERT OR IGNORE INTO commit_links (parent, child) VALUES (?, ?)",
parent,
child
child,
)
.execute(&mut *conn)
.await?;
@ -111,6 +125,55 @@ async fn insert_new_commit_links(conn: &mut SqliteConnection, new: &[Info]) -> a
Ok(())
}
async fn mark_all_commits_as_old(conn: &mut SqliteConnection) -> anyhow::Result<()> {
sqlx::query!("UPDATE commits SET new = 0")
.execute(conn)
.await?;
Ok(())
}
async fn track_main_branch(conn: &mut SqliteConnection, repo: &Repository) -> anyhow::Result<()> {
let Some(head) = repo.head_ref()? else { return Ok(()); };
let name = head.inner.name.to_string();
let hash = head.into_fully_peeled_id()?.to_string();
sqlx::query!(
"INSERT OR IGNORE INTO tracked_refs (name, hash) VALUES (?, ?)",
name,
hash,
)
.execute(conn)
.await?;
Ok(())
}
async fn update_tracked_refs(conn: &mut SqliteConnection, repo: &Repository) -> anyhow::Result<()> {
let tracked_refs = sqlx::query!("SELECT name, hash FROM tracked_refs")
.fetch_all(&mut *conn)
.await?;
for tracked_ref in tracked_refs {
if let Some(reference) = repo.try_find_reference(&tracked_ref.name)? {
let hash = reference.id().to_string();
if hash != tracked_ref.hash {
debug!("Updated tracked ref {}", tracked_ref.name);
sqlx::query!(
"UPDATE tracked_refs SET hash = ? WHERE name = ?",
hash,
tracked_ref.name
)
.execute(&mut *conn)
.await?;
}
} else {
debug!("Deleted tracked ref {}", tracked_ref.name);
sqlx::query!("DELETE FROM tracked_refs WHERE name = ?", tracked_ref.name)
.execute(&mut *conn)
.await?;
}
}
Ok(())
}
async fn update_repo(state: &AppState) -> anyhow::Result<()> {
info!("Updating repo");
let repo = state.repo.to_thread_local();