Skip to content

Commit 767fc86

Browse files
committed
use libgit for cloning; minor fixes
1 parent a7eecd7 commit 767fc86

File tree

10 files changed

+171
-86
lines changed

10 files changed

+171
-86
lines changed

git/libgit.v

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ struct C.git_error {
4747

4848
[typedef]
4949
struct C.git_tree_entry {}
50+
struct C.git_clone_options {
51+
mut:
52+
bare int
53+
}
5054

5155
fn C.git_commit_message(voidptr) &char
5256
fn C.git_reference_lookup(&&C.git_reference, &C.git_repository, &char)
@@ -71,6 +75,8 @@ fn C.git_tree_entry_id(&C.git_tree_entry) &C.git_oid
7175
fn C.git_blob_rawcontent(&C.git_blob) voidptr
7276
fn C.git_blob_rawsize(&C.git_blob) int
7377
fn C.git_blob_free(&C.git_blob)
78+
fn C.git_clone(&&C.git_repository, &char, &char, &C.git_clone_options) int
79+
fn C.git_clone_options_init(&C.git_clone_options, int)
7480

7581
fn init() {
7682
C.git_libgit2_init()
@@ -151,19 +157,19 @@ fn (r Repo) log(p LogParams) []Commit {
151157
return commits
152158
}
153159

154-
fn new_repo(path string) Repo {
160+
pub fn new_repo(path string) &Repo {
155161
repo_obj := &C.git_repository(unsafe { nil })
156162
// ret := C.git_repository_init(&repo_obj, path.str, false)
157163
ret := C.git_repository_open(&repo_obj, path.str)
158164
println('ff ${ret}')
159165
// git_reference_free(head_ref);
160-
return Repo{
166+
return &Repo{
161167
obj: repo_obj
162168
path: path
163169
}
164170
}
165171

166-
fn (r &Repo) current_branch() string {
172+
pub fn (r &Repo) current_branch() string {
167173
head_ref := &C.git_reference(unsafe { nil })
168174
C.git_reference_lookup(&head_ref, r.obj, c'HEAD')
169175
symbolic_ref := C.git_reference_symbolic_target(head_ref)
@@ -223,6 +229,30 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
223229
return ''
224230
}
225231

232+
pub fn clone(url string, path string) {
233+
mut r := new_repo(path)
234+
r.clone(url, path)
235+
}
236+
237+
pub fn (mut r Repo) clone(url string, path string) {
238+
println('new clone url="${url}" path="${path}"')
239+
// Clone options
240+
mut clone_opts := C.git_clone_options{} //( unsafe{nil})
241+
C.git_clone_options_init(&clone_opts, C.GIT_CLONE_OPTIONS_VERSION)
242+
243+
// Set the bare flag to create a bare repository
244+
clone_opts.bare = 1
245+
246+
// Clone the repository
247+
// r.git_repo = &C.git_repository(unsafe { nil })
248+
ret := C.git_clone(&r.obj, url.str, path.str, &clone_opts)
249+
if ret != 0 {
250+
C.printf(c'Failed to clone: %s\n', C.git_error_last().message)
251+
} else {
252+
println('Bare repository cloned successfully!\n')
253+
}
254+
}
255+
226256
/*
227257
fn main() {
228258
t0 := time.now()

src/issue_routes.v

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ pub fn (mut app App) issues(username string, repo_name string, page int) vweb.Re
8484
}
8585
mut first := false
8686
mut last := false
87-
if repo.open_issues_count > commits_per_page {
87+
if repo.nr_open_issues > commits_per_page {
8888
offset := page * commits_per_page
89-
delta := repo.open_issues_count - offset
89+
delta := repo.nr_open_issues - offset
9090
if delta > 0 {
91-
if delta == repo.open_issues_count && page == 0 {
91+
if delta == repo.nr_open_issues && page == 0 {
9292
first = true
9393
} else {
9494
last = true
@@ -98,7 +98,7 @@ pub fn (mut app App) issues(username string, repo_name string, page int) vweb.Re
9898
last = true
9999
first = true
100100
}
101-
page_count := calculate_pages(repo.open_issues_count, commits_per_page)
101+
page_count := calculate_pages(repo.nr_open_issues, commits_per_page)
102102
prev_page, next_page := generate_prev_next_pages(page)
103103
return $vweb.html()
104104
}

src/repo.v

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import validation
1010

1111
struct Repo {
1212
id int [primary; sql: serial]
13-
git_repo &git.Repo [skip] // libgit wrapper repo
1413
git_dir string
1514
name string
1615
user_id int
@@ -21,24 +20,26 @@ struct Repo {
2120
is_public bool
2221
users_contributed []string [skip]
2322
users_authorized []string [skip]
24-
topics_count int [skip]
23+
nr_topics int [skip]
2524
views_count int
2625
latest_update_hash string [skip]
2726
latest_activity time.Time [skip]
2827
mut:
29-
webhook_secret string
30-
tags_count int
31-
open_issues_count int
32-
open_prs_count int
33-
releases_count int
34-
branches_count int
35-
stars_count int
36-
lang_stats []LangStat [skip]
37-
created_at int
38-
contributors_count int
39-
labels []Label [skip]
40-
status RepoStatus [skip]
41-
msg_cache map[string]string [skip]
28+
git_repo &git.Repo [skip] // libgit wrapper repo
29+
webhook_secret string
30+
tags_count int
31+
nr_open_issues int [orm: 'open_issues_count']
32+
nr_open_prs int [orm: 'open_prs_count']
33+
nr_releases int [orm: 'releases_count']
34+
nr_branches int [orm: 'branches_count']
35+
nr_tags int
36+
nr_stars int [orm: 'stars_count']
37+
lang_stats []LangStat [skip]
38+
created_at int
39+
nr_contributors int
40+
labels []Label [skip]
41+
status RepoStatus [skip]
42+
msg_cache map[string]string [skip]
4243
}
4344

4445
// log_field_separator is declared as constant in case we need to change it later
@@ -74,32 +75,37 @@ fn (mut app App) save_repo(repo Repo) ! {
7475
webhook_secret := repo.webhook_secret
7576
tags_count := repo.tags_count
7677
is_public := if repo.is_public { 1 } else { 0 }
77-
open_issues_count := repo.open_issues_count
78-
open_prs_count := repo.open_prs_count
79-
branches_count := repo.branches_count
80-
releases_count := repo.releases_count
81-
stars_count := repo.stars_count
82-
contributors_count := repo.contributors_count
78+
open_issues_count := repo.nr_open_issues
79+
open_prs_count := repo.nr_open_prs
80+
branches_count := repo.nr_branches
81+
releases_count := repo.nr_releases
82+
stars_count := repo.nr_stars
83+
contributors_count := repo.nr_contributors
84+
85+
// XTODO sql update all fields automatically
86+
// repo.update()
8387

8488
sql app.db {
8589
update Repo set description = desc, views_count = views_count, is_public = is_public,
86-
webhook_secret = webhook_secret, tags_count = tags_count, open_issues_count = open_issues_count,
87-
open_prs_count = open_prs_count, releases_count = releases_count, contributors_count = contributors_count,
88-
stars_count = stars_count, branches_count = branches_count where id == id
90+
webhook_secret = webhook_secret, tags_count = tags_count, nr_open_issues = open_issues_count,
91+
nr_open_prs = open_prs_count, nr_releases = releases_count, nr_contributors = contributors_count,
92+
nr_stars = stars_count, nr_branches = branches_count where id == id
8993
}!
9094
}
9195

9296
fn (app App) find_repo_by_name_and_user_id(repo_name string, user_id int) ?Repo {
9397
repos := sql app.db {
9498
select from Repo where name == repo_name && user_id == user_id limit 1
95-
} or { []Repo{} }
99+
} or { return none }
96100

97101
if repos.len == 0 {
98102
return none
99103
}
100104

101-
mut repo := repos.first()
105+
mut repo := repos[0]
102106
repo.lang_stats = app.find_repo_lang_stats(repo.id)
107+
println('GIT DIR = ${repo.git_dir}')
108+
repo.git_repo = git.new_repo(repo.git_dir)
103109

104110
return repo
105111
}
@@ -145,7 +151,7 @@ fn (app App) search_public_repos(query string) []Repo {
145151
name: row.vals[1]
146152
user_name: user.username
147153
description: row.vals[3]
148-
stars_count: row.vals[4].int()
154+
nr_stars: row.vals[4].int()
149155
}
150156
}
151157

@@ -175,13 +181,13 @@ fn (mut app App) increment_repo_views(repo_id int) ! {
175181

176182
fn (mut app App) increment_repo_stars(repo_id int) ! {
177183
sql app.db {
178-
update Repo set stars_count = stars_count + 1 where id == repo_id
184+
update Repo set nr_stars = nr_stars + 1 where id == repo_id
179185
}!
180186
}
181187

182188
fn (mut app App) decrement_repo_stars(repo_id int) ! {
183189
sql app.db {
184-
update Repo set stars_count = stars_count - 1 where id == repo_id
190+
update Repo set nr_stars = nr_stars - 1 where id == repo_id
185191
}!
186192
}
187193

@@ -199,7 +205,7 @@ fn (mut app App) set_repo_webhook_secret(repo_id int, secret string) ! {
199205

200206
fn (mut app App) increment_repo_issues(repo_id int) ! {
201207
sql app.db {
202-
update Repo set open_issues_count = open_issues_count + 1 where id == repo_id
208+
update Repo set nr_open_issues = nr_open_issues + 1 where id == repo_id
203209
}!
204210
}
205211

@@ -254,9 +260,9 @@ fn (mut app App) update_repo_from_fs(mut repo Repo) ! {
254260

255261
app.db.exec('BEGIN TRANSACTION')!
256262

257-
repo.analyse_lang(app)!
263+
repo.analyze_lang(app)!
258264

259-
app.info(repo.contributors_count.str())
265+
app.info(repo.nr_contributors.str())
260266
app.fetch_branches(repo)!
261267

262268
branches_output := repo.git('branch -a')
@@ -267,14 +273,14 @@ fn (mut app App) update_repo_from_fs(mut repo Repo) ! {
267273
app.update_repo_branch_from_fs(mut repo, branch_name)!
268274
}
269275

270-
repo.contributors_count = app.get_count_repo_contributors(repo_id)!
271-
repo.branches_count = app.get_count_repo_branches(repo_id)
276+
repo.nr_contributors = app.get_count_repo_contributors(repo_id)!
277+
repo.nr_branches = app.get_count_repo_branches(repo_id)
272278

273279
// TODO: TEMPORARY - UNTIL WE GET PERSISTENT RELEASE INFO
274280
for tag in app.get_all_repo_tags(repo_id) {
275281
app.add_release(tag.id, repo_id, time.unix(tag.created_at), tag.message)!
276282

277-
repo.releases_count++
283+
repo.nr_releases++
278284
}
279285

280286
app.save_repo(repo)!
@@ -329,9 +335,9 @@ fn (mut app App) update_repo_from_remote(mut repo Repo) ! {
329335

330336
app.db.exec('BEGIN TRANSACTION')!
331337

332-
repo.analyse_lang(app)!
338+
repo.analyze_lang(app)!
333339

334-
app.info(repo.contributors_count.str())
340+
app.info(repo.nr_contributors.str())
335341
app.fetch_branches(repo)!
336342
app.fetch_tags(repo)!
337343

@@ -345,12 +351,11 @@ fn (mut app App) update_repo_from_remote(mut repo Repo) ! {
345351

346352
for tag in app.get_all_repo_tags(repo_id) {
347353
app.add_release(tag.id, repo_id, time.unix(tag.created_at), tag.message)!
348-
349-
repo.releases_count++
354+
repo.nr_releases++
350355
}
351356

352-
repo.contributors_count = app.get_count_repo_contributors(repo_id)!
353-
repo.branches_count = app.get_count_repo_branches(repo_id)
357+
repo.nr_contributors = app.get_count_repo_contributors(repo_id)!
358+
repo.nr_branches = app.get_count_repo_branches(repo_id)
354359

355360
app.save_repo(repo)!
356361
app.db.exec('END TRANSACTION')!
@@ -404,7 +409,7 @@ fn (mut app App) update_repo_after_push(repo_id int, branch_name string) ! {
404409
app.delete_repository_files_in_branch(repo_id, branch_name)!
405410
}
406411

407-
fn (r &Repo) analyse_lang(app &App) ! {
412+
fn (r &Repo) analyze_lang(app &App) ! {
408413
file_paths := r.get_all_file_paths()
409414

410415
mut all_size := 0
@@ -760,6 +765,15 @@ fn (mut app App) update_repo_primary_branch(repo_id int, branch string) ! {
760765
}
761766

762767
fn (mut r Repo) clone() {
768+
if r.git_repo != unsafe { nil } {
769+
r.git_repo.clone(r.clone_url, r.git_dir)
770+
} else {
771+
println('nil')
772+
}
773+
774+
/*
775+
cmd := 'git clone --bare "${r.clone_url}" ${r.git_dir}'
776+
println('CLONE() ${cmd}')
763777
clone_result := os.execute('git clone --bare "${r.clone_url}" ${r.git_dir}')
764778
close_exit_code := clone_result.exit_code
765779
@@ -768,17 +782,22 @@ fn (mut r Repo) clone() {
768782
println('git clone failed with exit code ${close_exit_code}')
769783
return
770784
}
785+
*/
771786

772787
r.status = .clone_done
773788
}
774789

775790
fn (r &Repo) read_file(branch string, path string) string {
776791
// valid_path := path.trim_string_left('/')
777792

778-
println('yEPP')
793+
println('yEPP path=${valid_path}')
794+
if r.git_repo == unsafe { nil } {
795+
return 'nil'
796+
}
779797
t := time.now()
780798
// s := r.git('--no-pager show ${branch}:${valid_path}')
781-
s := r.git_repo.show_file_blob(branch, path) or { '' }
799+
800+
s := r.git_repo.show_file_blob(branch, valid_path) or { '' }
782801
println(time.since(t))
783802
println(':)')
784803
return s

0 commit comments

Comments
 (0)