From c516ce206080960cf8d621eeff774dc38ec043e8 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Fri, 21 Mar 2025 09:29:33 +0300 Subject: [PATCH 01/10] hotfix login --- src/user_routes.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user_routes.v b/src/user_routes.v index 3bffdd6b..4c5e825d 100644 --- a/src/user_routes.v +++ b/src/user_routes.v @@ -11,7 +11,7 @@ pub fn (mut app App) login(mut ctx Context) veb.Result { csrf := rand.string(30) ctx.set_cookie(name: 'csrf', value: csrf) - if app.is_logged_in(mut ctx) { + if !app.is_logged_in(mut ctx) { return ctx.not_found() } From 6818b7fb25d6ad333e8ce95241503975c658a043 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Fri, 21 Mar 2025 11:51:07 +0300 Subject: [PATCH 02/10] main: Add veb.Middleware to App;make use of app.before_request --- src/gitly.v | 1 + src/main.v | 2 ++ 2 files changed, 3 insertions(+) diff --git a/src/gitly.v b/src/gitly.v index ece88086..214d97c3 100644 --- a/src/gitly.v +++ b/src/gitly.v @@ -23,6 +23,7 @@ const namechange_period = time.hour * 24 @[heap] pub struct App { veb.StaticHandler + veb.Middleware[Context] started_at i64 pub mut: db sqlite.DB diff --git a/src/main.v b/src/main.v index f38b405c..5af48d6e 100644 --- a/src/main.v +++ b/src/main.v @@ -12,6 +12,8 @@ fn main() { return } mut app := new_app()! + + app.use(handler: app.before_request) // vweb.run_at(new_app()!, http_port) veb.run_at[App, Context](mut app, port: http_port, family: .ip, timeout_in_seconds: 2) or { From bd260777c59e01a7c9495d4e20ef73000c3d9220 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Fri, 21 Mar 2025 14:11:39 +0300 Subject: [PATCH 03/10] login: get logic back --- src/user_routes.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user_routes.v b/src/user_routes.v index 4c5e825d..3bffdd6b 100644 --- a/src/user_routes.v +++ b/src/user_routes.v @@ -11,7 +11,7 @@ pub fn (mut app App) login(mut ctx Context) veb.Result { csrf := rand.string(30) ctx.set_cookie(name: 'csrf', value: csrf) - if !app.is_logged_in(mut ctx) { + if app.is_logged_in(mut ctx) { return ctx.not_found() } From 8a0bbf4b941114a7f9fde0bca9ac614f417407bc Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Fri, 21 Mar 2025 16:46:59 +0300 Subject: [PATCH 04/10] app: hotfix request timeout, add url var and dump it (don't ask me) --- src/gitly.v | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/gitly.v b/src/gitly.v index 214d97c3..02391037 100644 --- a/src/gitly.v +++ b/src/gitly.v @@ -132,6 +132,7 @@ pub fn (mut app App) init_server() { } pub fn (mut app App) before_request(mut ctx Context) { + url := ctx.req.url ctx.logged_in = app.is_logged_in(mut ctx) app.load_settings() @@ -142,6 +143,7 @@ pub fn (mut app App) before_request(mut ctx Context) { User{} } } + dump(url) } @['/'] From 1d4f83197b0a3d3372f2ba594461159660e701e5 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Fri, 21 Mar 2025 17:25:41 +0300 Subject: [PATCH 05/10] fix logic if logged in --- src/user_routes.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user_routes.v b/src/user_routes.v index 3bffdd6b..61179a50 100644 --- a/src/user_routes.v +++ b/src/user_routes.v @@ -12,7 +12,7 @@ pub fn (mut app App) login(mut ctx Context) veb.Result { ctx.set_cookie(name: 'csrf', value: csrf) if app.is_logged_in(mut ctx) { - return ctx.not_found() + return ctx.redirect("/" + ctx.user.username) } return $veb.html() From f4845aeda12ab2fe1e33fa508f25475098f6f666 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Mon, 7 Apr 2025 09:01:21 +0300 Subject: [PATCH 06/10] introduce alternative show_file_blob algo working with subdirs --- git/libgit.v | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/git/libgit.v b/git/libgit.v index 69549eb1..1b8b6f82 100644 --- a/git/libgit.v +++ b/git/libgit.v @@ -89,6 +89,10 @@ fn C.git_blob_free(&C.git_blob) fn C.git_clone(&&C.git_repository, &char, &char, &C.git_clone_options) int fn C.git_clone_options_init(&C.git_clone_options, int) +fn C.git_object_lookup_bypath(&&C.git_object, &C.git_object, &char, int) int +fn C.git_object_peel(&&C.git_object, &C.git_object, int) int +fn C.git_object_id(&C.git_object) &C.git_oid + fn init() { C.git_libgit2_init() } @@ -242,7 +246,35 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string { C.printf(c'Failed to get commit tree\n') return error('sdf') } + // create file object + mut file_obj := &C.git_object(unsafe { nil }) + + // get file object + if C.git_object_lookup_bypath(&file_obj, treeish, file_path.str, 3) != 0 { + C.printf(c'Failed to lookup file: %s\n', C.git_error_last().message) + return error('sdf') + } + // get file oid + mut oid := C.git_object_id(file_obj) + + // get blob object + if C.git_blob_lookup(&blob, r.obj, oid) != 0 { + C.printf(c'Failed to lookup blob: %s\n', C.git_error_last().message) + return error('sdf') + } + + // get blob content + content := C.git_blob_rawcontent(blob) + // size := C.git_blob_rawsize(blob) + + C.printf(c'Content of %s (from branch %s):\n', file_path.str, branch.str) + + text := unsafe { cstring_to_vstring(content) } + C.git_blob_free(blob) + return text + + /* tree := unsafe { &C.git_tree(treeish) } // Iterate through the tree entries to find the file @@ -272,6 +304,7 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string { } } return '' + */ } pub fn clone(url string, path string) { From 5b79a4a4fac15161aed2a6a5abd301398fd6bf2a Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Mon, 7 Apr 2025 09:16:24 +0300 Subject: [PATCH 07/10] make use of the name of the object type --- git/libgit.v | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/git/libgit.v b/git/libgit.v index 1b8b6f82..3a921c70 100644 --- a/git/libgit.v +++ b/git/libgit.v @@ -250,7 +250,7 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string { mut file_obj := &C.git_object(unsafe { nil }) // get file object - if C.git_object_lookup_bypath(&file_obj, treeish, file_path.str, 3) != 0 { + if C.git_object_lookup_bypath(&file_obj, treeish, file_path.str, C.GIT_OBJECT_BLOB) != 0 { C.printf(c'Failed to lookup file: %s\n', C.git_error_last().message) return error('sdf') } From d60dd8952732afebff7d698d7eb353141dc9a0b7 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Mon, 7 Apr 2025 16:56:18 +0300 Subject: [PATCH 08/10] test blob view. draft. WIP --- tests/first_run.v | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/first_run.v b/tests/first_run.v index 833fada5..e17f80f3 100644 --- a/tests/first_run.v +++ b/tests/first_run.v @@ -61,6 +61,7 @@ fn main() { test_commits_page(test_username, repo_name, test_github_repo_primary_branch) test_branches_page(test_username, repo_name) test_repo_tree(test_username, repo_name, test_github_repo_primary_branch, 'c') + test_blob_page(test_username, repo_name, test_github_repo_primary_branch, 'examples/hello.v') // test_refs_page(test_username, repo_name) // test_api_branches_count(test_username, repo_name) ilog("all tests passed!") @@ -300,6 +301,18 @@ fn test_settings_page(username string) { assert settings_page_result.status_code == 200 } +fn test_blob_page(username string, repo_name string, branch_name string, path string) { + ilog('Testing the new blob /${username}/${repo_name}/blob/${branch_name}/${path} page is up') + // blob_page_result := http.get(prepare_url("${username}/${repo_name}/blob/${branch_name}/${path}")) or { exit_with_message(err.str()) } + blob_page_result := http.fetch( + method: .get + url: prepare_url("${username}/${repo_name}/blob/${branch_name}/${path}") + ) or { exit_with_message(err.str()) } + + assert blob_page_result.status_code == 200 + assert blob_page_result.body.str().contains('m := r.match_str') +} + fn test_repo_settings_page(username string, repo_name string) { test_endpoint_page("${username}/${repo_name}/settings", 'settings') } From 5f07b62d71d76018992a6d0e97cd4ae785394d7d Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Mon, 7 Apr 2025 19:35:47 +0300 Subject: [PATCH 09/10] test if subdir blob works correctly --- tests/first_run.v | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tests/first_run.v b/tests/first_run.v index e17f80f3..2b5df3e4 100644 --- a/tests/first_run.v +++ b/tests/first_run.v @@ -61,6 +61,8 @@ fn main() { test_commits_page(test_username, repo_name, test_github_repo_primary_branch) test_branches_page(test_username, repo_name) test_repo_tree(test_username, repo_name, test_github_repo_primary_branch, 'c') + // this makes sure that the blob (and the tree?) is ready + test_repo_tree(test_username, repo_name, test_github_repo_primary_branch, 'examples') test_blob_page(test_username, repo_name, test_github_repo_primary_branch, 'examples/hello.v') // test_refs_page(test_username, repo_name) // test_api_branches_count(test_username, repo_name) @@ -302,11 +304,11 @@ fn test_settings_page(username string) { } fn test_blob_page(username string, repo_name string, branch_name string, path string) { - ilog('Testing the new blob /${username}/${repo_name}/blob/${branch_name}/${path} page is up') - // blob_page_result := http.get(prepare_url("${username}/${repo_name}/blob/${branch_name}/${path}")) or { exit_with_message(err.str()) } + url := "${username}/${repo_name}/blob/${branch_name}/${path}" + ilog('Testing the new blob /${url} page is up') blob_page_result := http.fetch( method: .get - url: prepare_url("${username}/${repo_name}/blob/${branch_name}/${path}") + url: prepare_url(url) ) or { exit_with_message(err.str()) } assert blob_page_result.status_code == 200 From cda2c51e61f7f9e14e00ed2620d44f3d88a8dd8e Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Mon, 7 Apr 2025 19:38:13 +0300 Subject: [PATCH 10/10] clean up --- git/libgit.v | 31 ------------------------------- 1 file changed, 31 deletions(-) diff --git a/git/libgit.v b/git/libgit.v index 3a921c70..1ef18037 100644 --- a/git/libgit.v +++ b/git/libgit.v @@ -274,37 +274,6 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string { C.git_blob_free(blob) return text - /* - tree := unsafe { &C.git_tree(treeish) } - - // Iterate through the tree entries to find the file - entry_count := C.git_tree_entrycount(tree) - // println('number of entires ${entry_count}') - for i := 0; i < entry_count; i++ { - entry := C.git_tree_entry_byindex(tree, i) - entry_name := C.git_tree_entry_name(entry) - C.printf(c'%s\n', entry_name) - - if unsafe { C.strcmp(entry_name, file_path.str) } == 0 { - // Found the file - if C.git_blob_lookup(&blob, r.obj, C.git_tree_entry_id(entry)) != 0 { - C.printf(c'Failed to lookup blob: %s\n', C.git_error_last().message) - return error('sdf') - } - - content := C.git_blob_rawcontent(blob) - // size := C.git_blob_rawsize(blob) - - C.printf(c'Content of %s (from branch %s):\n', file_path.str, branch.str) - // C.fwrite(content, 1, size, C.stdout) - - text := unsafe { cstring_to_vstring(content) } - C.git_blob_free(blob) - return text - } - } - return '' - */ } pub fn clone(url string, path string) {