fix: Clone and analyze the repository correctly #15

Merged
Hare merged 2 commits from develop into master 2024-09-27 00:49:42 +09:00
2 changed files with 22 additions and 17 deletions
Showing only changes of commit 5b441ff0cf - Show all commits

View File

@ -1,5 +1,5 @@
import type { Postgres } from '$lib/server/database/postgres';
import type { SimpleGit } from 'simple-git';
import { type SimpleGit, CheckRepoActions } from 'simple-git';
import fs from 'fs';
import compile from '$lib/server/article_compiler';
import type { TableSchema } from '$lib/server/database/table';
@ -11,8 +11,9 @@ const load = async (db: Postgres, git: SimpleGit) => {
await db.query('update tag set ref_count = 0');
await db.begin();
let i = 0;
for (const { path, id } of articleFiles) {
await db.savepoint(id);
await db.savepoint(`load${i}`);
console.log(`Processing ${id}...`);
try {
const gitlog = await git.log({
@ -66,38 +67,29 @@ const load = async (db: Postgres, git: SimpleGit) => {
}
} catch (err) {
console.log(err);
await db.rollbackTo(id);
await db.rollbackTo(`load${i}`);
} finally {
console.log('');
await db.releaseSavepoint(id);
await db.releaseSavepoint(`load${i}`);
i++;
}
}
await db.commit();
}
export async function init(db: Postgres, git: SimpleGit) {
await createTable(db, server_table);
await cloneRepo(git);
await load(db, git);
}
export async function reload(db: Postgres, git: SimpleGit) {
await cloneRepo(git);
await load(db, git);
}
type ArticleFileItem = {
path: string,
id: string,
}
const cloneRepo = async (git: SimpleGit) => {
if (fs.existsSync('./articles/')) {
const isRepoRoot = await git.checkIsRepo(CheckRepoActions.IS_REPO_ROOT);
if (isRepoRoot) {
console.log('Pulling articles from git..');
await git.pull();
} else {
console.log('Cloning articles from git..');
await git.clone('git@gitea.hareworks.net:Hare/blog-articles.git', 'articles');
await git.clone('git@gitea.hareworks.net:Hare/blog-articles.git', './');
}
}
@ -139,3 +131,14 @@ const crawlArticles = async (db: Postgres): Promise<ArticleFileItem[]> => {
scanDir('./articles/article');
return articleFiles;
}
export async function init(db: Postgres, git: SimpleGit) {
await createTable(db, server_table);
await cloneRepo(git);
await load(db, git);
}
export async function reload(db: Postgres, git: SimpleGit) {
await cloneRepo(git);
await load(db, git);
}

View File

@ -1,7 +1,9 @@
import fs from 'fs';
import { simpleGit, } from 'simple-git';
import type { SimpleGit, SimpleGitOptions } from 'simple-git';
console.log(process.cwd()+'/articles');
fs.mkdirSync(process.cwd()+'/articles', { recursive: true });
export const gitOptions: Partial<SimpleGitOptions> = {
baseDir: process.cwd()+'/articles',
binary: 'git',