diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9286a60 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bur-manager +repos/* +!repos/ diff --git a/git2/git2.c.v b/git2/git2.c.v index 82cae92..7d20fd5 100644 --- a/git2/git2.c.v +++ b/git2/git2.c.v @@ -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 diff --git a/git2/git2.v b/git2/git2.v index f6918dc..e9ad47f 100644 --- a/git2/git2.v +++ b/git2/git2.v @@ -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') + } } diff --git a/git2/v.mod b/git2/v.mod new file mode 100644 index 0000000..3112ed3 --- /dev/null +++ b/git2/v.mod @@ -0,0 +1,3 @@ +Module{ + name: 'git2' +} diff --git a/gitea/gitea.v b/gitea/gitea.v new file mode 100644 index 0000000..dc19024 --- /dev/null +++ b/gitea/gitea.v @@ -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(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 +} diff --git a/gitea/orgs.v b/gitea/orgs.v new file mode 100644 index 0000000..7dd42a3 --- /dev/null +++ b/gitea/orgs.v @@ -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) +} diff --git a/gitea/v.mod b/gitea/v.mod new file mode 100644 index 0000000..03ded3c --- /dev/null +++ b/gitea/v.mod @@ -0,0 +1,4 @@ +Module{ + name: 'gitea' + description: 'Wrapper around the Gitea API' +} diff --git a/main.v b/main.v index 88bee84..01eb741 100644 --- a/main.v +++ b/main.v @@ -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')! } diff --git a/repos/.keep b/repos/.keep new file mode 100644 index 0000000..e69de29 diff --git a/sync.v b/sync.v new file mode 100644 index 0000000..a3cec90 --- /dev/null +++ b/sync.v @@ -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()! +} diff --git a/v.mod b/v.mod new file mode 100644 index 0000000..1656e45 --- /dev/null +++ b/v.mod @@ -0,0 +1,3 @@ +Module{ + name: 'bur-manager' +}