Actually making some progress

main
Jef Roosens 2022-11-26 15:28:45 +01:00
parent 1ac069bf28
commit a4c2d0abd5
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
11 changed files with 155 additions and 7 deletions

3
.gitignore vendored 100644
View File

@ -0,0 +1,3 @@
bur-manager
repos/*
!repos/

View File

@ -1,15 +1,29 @@
module git2
#flag -lgit2
//#flag -I @VMODROOT/c
//#flag @VMODROOT/c/wrappers.o
#flag -lgit2
#include "git2.h"
pub struct C.git_repository {}
pub struct C.git_remote {}
pub struct C.git_clone_options {}
pub struct C.git_strarray {}
pub struct C.git_fetch_options {}
fn C.git_libgit2_init()
fn C.git_libgit2_shutdown()
fn C.git_clone(&&C.git_repository, &char, &char, &C.git_clone_options) int
fn C.git_remote_create(&&C.git_remote, &C.git_repository, &char, &char) int
fn C.git_remote_fetch(&C.git_remote, &C.git_strarray, &C.git_fetch_options, &char) int
// fn C.vgit_clone(&char, &char) &C.git_repository

View File

@ -1,16 +1,38 @@
module git2
type GitRepository = C.git_repository
type GitRemote = C.git_remote
fn init() {
C.git_libgit2_init()
}
pub fn clone(url string, path string) !&C.git_repository {
out := &C.git_repository{}
res := C.git_clone(&out, url.str, path.str, 0)
pub fn clone(url string, path string) !&GitRepository {
repo := &C.git_repository(0)
res := C.git_clone(&repo, url.str, path.str, 0)
if res != 0 {
return error('Exit code $res')
return error('An error occured')
}
return out
return repo
}
pub fn (r &GitRepository) create_remote(name string, url string) !&GitRemote {
remote := &C.git_remote(0)
res := C.git_remote_create(&remote, r, name.str, url.str)
if res != 0 {
return error('An error occured')
}
return remote
}
pub fn (r &GitRemote) fetch() ! {
res := C.git_remote_fetch(r, 0, 0, 0)
if res != 0 {
return error('An error occured')
}
}

3
git2/v.mod 100644
View File

@ -0,0 +1,3 @@
Module{
name: 'git2'
}

55
gitea/gitea.v 100644
View File

@ -0,0 +1,55 @@
module gitea
import net.http
import json
const api_prefix = '/api/v1'
pub struct Client {
base_url string
api_key string
}
[params]
pub struct PagingConfig {
page int
limit int
}
fn (p PagingConfig) str() string {
if p.page == 0 && p.limit == 0 {
return ''
}
mut parts := []string{}
if p.page != 0 {
parts << 'page=$p.page'
}
if p.limit != 0 {
parts << 'limit=$p.limit'
}
return parts.join('&')
}
pub fn new(base_url string, api_key string) Client {
return Client{
base_url: base_url.trim_right('/') + gitea.api_prefix
api_key: api_key
}
}
fn (c &Client) get<T>(path string, conf PagingConfig) !T {
mut url := '$c.base_url$path'
if conf.str() != '' {
url += '&$conf.str()'
}
res := http.get(url)!
data := json.decode(T, res.body)!
return data
}

13
gitea/orgs.v 100644
View File

@ -0,0 +1,13 @@
module gitea
pub struct Repository {
pub:
id i64
name string
description string
clone_url string
}
pub fn (c &Client) org_repos(name string, conf PagingConfig) ![]Repository {
return c.get<[]Repository>('/orgs/$name/repos', conf)
}

4
gitea/v.mod 100644
View File

@ -0,0 +1,4 @@
Module{
name: 'gitea'
description: 'Wrapper around the Gitea API'
}

12
main.v
View File

@ -1,5 +1,15 @@
import git2
import gitea
fn main() {
git2.clone("https://aur.archlinux.org/vlang.git", "test")!
bur_url := 'https://git.rustybever.be/bur/nheko-git.git'
mirror_url := 'https://aur.archlinux.org/nheko-git.git'
c := gitea.new('https://git.rustybever.be', '')
repo := c.org_repos('bur')!.filter(it.description == 'https://aur.archlinux.org/nheko-git.git')[0]
println(repo)
sync_repo(c, repo)!
// println(repos)
// repo := git2.clone('https://aur.archlinux.org/thisdefinitelydoesntexist.git', 'test')!
}

0
repos/.keep 100644
View File

21
sync.v 100644
View File

@ -0,0 +1,21 @@
import gitea
import git2
import os
fn sync_repo(c &gitea.Client, repo gitea.Repository) ! {
git_repo_path := os.join_path_single('repos', repo.name)
// Remove old clone if present
if os.exists(git_repo_path) {
os.rmdir_all(git_repo_path)!
}
// Clone the remote repository
git_repo := git2.clone(repo.description, os.join_path_single('repos', '$repo.name-remote'))!
// Add the Gitea repository as a remote
remote := git_repo.create_remote('gitea', repo.clone_url)!
// Fetch the Gitea repository
remote.fetch()!
}

3
v.mod 100644
View File

@ -0,0 +1,3 @@
Module{
name: 'bur-manager'
}