From c516ce206080960cf8d621eeff774dc38ec043e8 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Fri, 21 Mar 2025 09:29:33 +0300 Subject: [PATCH 01/13] 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/13] 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/13] 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/13] 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/13] 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 40651ab8715146407bc499f6f80005ab3c51b834 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Sat, 22 Mar 2025 08:56:59 +0300 Subject: [PATCH 06/13] (draft) debug with dump info --- src/repo_routes.v | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/repo_routes.v b/src/repo_routes.v index 77b55ca9..bb8e2bb2 100644 --- a/src/repo_routes.v +++ b/src/repo_routes.v @@ -151,8 +151,9 @@ pub fn (mut app App) handle_tree(mut ctx Context, username string, repo_name str } repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } - + dump(repo) return app.tree(mut ctx, username, repo_name, repo.primary_branch, '') + // return app.tree(mut ctx, username, repo_name, repo.primary_branch, repo.git_dir) } @['/:username/:repo_name/tree/:branch_name'] @@ -258,6 +259,8 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str repo_id := new_repo2.id // primary_branch := git.get_repository_primary_branch(repo_path) primary_branch := new_repo2.git_repo.primary_branch() + dump(primary_branch) + dump(repo_id) app.update_repo_primary_branch(repo_id, primary_branch) or { ctx.error('There was an error while adding the repo') return app.new(mut ctx) @@ -279,6 +282,8 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str if !has_first_repo_activity { app.add_activity(ctx.user.id, 'first_repo') or { app.info(err.str()) } } + // dump(new_repo) + // dump(new_repo2) return ctx.redirect('/${ctx.user.username}/repos') } @@ -309,6 +314,9 @@ pub fn (mut app App) tree(mut ctx Context, username string, repo_name string, br // app.fetch_tags(repo) or { app.info(err.str()) } ctx.current_path = '/${path}' + // if ctx.current_path.starts_with('.') { + // ctx.current_path = ctx.current_path[1..] + // } if ctx.current_path.contains('/favicon.svg') { return ctx.not_found() } @@ -331,10 +339,12 @@ pub fn (mut app App) tree(mut ctx Context, username string, repo_name string, br up = ctx.req.url.all_before_last('/') } } + dump(up) if ctx.current_path.starts_with('/') { ctx.current_path = ctx.current_path[1..] } + // dump(ctx) mut items := app.find_repository_items(repo_id, branch_name, ctx.current_path) branch := app.find_repo_branch_by_name(repo.id, branch_name) From ab7e1a918659866d6fe655776099fa6c0159dacd Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Sat, 22 Mar 2025 12:04:18 +0300 Subject: [PATCH 07/13] fix git primary_branch. draft. TODO: clear debug stmts --- src/repo_routes.v | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/repo_routes.v b/src/repo_routes.v index bb8e2bb2..8439138f 100644 --- a/src/repo_routes.v +++ b/src/repo_routes.v @@ -237,6 +237,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str clone_url: valid_clone_url is_public: is_public } + dump(new_repo) if is_clone_url_empty { os.mkdir(new_repo.git_dir) or { panic(err) } new_repo.git('init --bare') @@ -258,9 +259,10 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str } repo_id := new_repo2.id // primary_branch := git.get_repository_primary_branch(repo_path) - primary_branch := new_repo2.git_repo.primary_branch() + // dump(new_repo2) + primary_branch := new_repo2.primary_branch dump(primary_branch) - dump(repo_id) + // dump(repo_id) app.update_repo_primary_branch(repo_id, primary_branch) or { ctx.error('There was an error while adding the repo') return app.new(mut ctx) From 7267f2cea3741ca5edb68155e81c2d88cf0d643f Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Sat, 22 Mar 2025 13:39:19 +0300 Subject: [PATCH 08/13] clean up: remove dumps --- src/repo_routes.v | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/repo_routes.v b/src/repo_routes.v index 8439138f..087766cd 100644 --- a/src/repo_routes.v +++ b/src/repo_routes.v @@ -151,7 +151,6 @@ pub fn (mut app App) handle_tree(mut ctx Context, username string, repo_name str } repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } - dump(repo) return app.tree(mut ctx, username, repo_name, repo.primary_branch, '') // return app.tree(mut ctx, username, repo_name, repo.primary_branch, repo.git_dir) } @@ -237,7 +236,6 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str clone_url: valid_clone_url is_public: is_public } - dump(new_repo) if is_clone_url_empty { os.mkdir(new_repo.git_dir) or { panic(err) } new_repo.git('init --bare') @@ -259,10 +257,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str } repo_id := new_repo2.id // primary_branch := git.get_repository_primary_branch(repo_path) - // dump(new_repo2) primary_branch := new_repo2.primary_branch - dump(primary_branch) - // dump(repo_id) app.update_repo_primary_branch(repo_id, primary_branch) or { ctx.error('There was an error while adding the repo') return app.new(mut ctx) @@ -284,8 +279,6 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str if !has_first_repo_activity { app.add_activity(ctx.user.id, 'first_repo') or { app.info(err.str()) } } - // dump(new_repo) - // dump(new_repo2) return ctx.redirect('/${ctx.user.username}/repos') } @@ -341,12 +334,10 @@ pub fn (mut app App) tree(mut ctx Context, username string, repo_name string, br up = ctx.req.url.all_before_last('/') } } - dump(up) if ctx.current_path.starts_with('/') { ctx.current_path = ctx.current_path[1..] } - // dump(ctx) mut items := app.find_repository_items(repo_id, branch_name, ctx.current_path) branch := app.find_repo_branch_by_name(repo.id, branch_name) From f81e29772f7cbb7d726fb7daaa5ad9c3c609e572 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Sat, 22 Mar 2025 13:55:56 +0300 Subject: [PATCH 09/13] repo_routes: clean up --- src/repo_routes.v | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/repo_routes.v b/src/repo_routes.v index 087766cd..dca67006 100644 --- a/src/repo_routes.v +++ b/src/repo_routes.v @@ -151,8 +151,8 @@ pub fn (mut app App) handle_tree(mut ctx Context, username string, repo_name str } repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } + return app.tree(mut ctx, username, repo_name, repo.primary_branch, '') - // return app.tree(mut ctx, username, repo_name, repo.primary_branch, repo.git_dir) } @['/:username/:repo_name/tree/:branch_name'] @@ -309,9 +309,6 @@ pub fn (mut app App) tree(mut ctx Context, username string, repo_name string, br // app.fetch_tags(repo) or { app.info(err.str()) } ctx.current_path = '/${path}' - // if ctx.current_path.starts_with('.') { - // ctx.current_path = ctx.current_path[1..] - // } if ctx.current_path.contains('/favicon.svg') { return ctx.not_found() } From 30dae6e0f1e6dd56f278b1d79f3f847ca5f61549 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Sat, 22 Mar 2025 14:58:03 +0300 Subject: [PATCH 10/13] dump critical info --- tests/first_run.v | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/first_run.v b/tests/first_run.v index 1f33d328..2d17702f 100644 --- a/tests/first_run.v +++ b/tests/first_run.v @@ -230,6 +230,7 @@ fn get_repo_commit_count(token string, username string, repo_name string, branch response_json := json.decode(api.ApiCommitCount, response.body) or { exit_with_message(err.str()) } + dump(response_json.result) return response_json.result } From 8b6ad182288a6274392a3f75532dabfc423d4968 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Sun, 23 Mar 2025 11:02:14 +0300 Subject: [PATCH 11/13] make repo show commit quantity --- src/commit.v | 3 +++ src/commit_routes.v | 6 +++--- src/repo.v | 15 +++++++++++++-- src/repo_routes.v | 4 ++++ 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/commit.v b/src/commit.v index 624eed5a..e40e36c1 100644 --- a/src/commit.v +++ b/src/commit.v @@ -86,6 +86,8 @@ fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash s select from Commit where repo_id == repo_id && branch_id == branch_id && hash == last_hash limit 1 } or { []Commit{} } + // $dbg; + if commits.len > 0 { return } @@ -100,6 +102,7 @@ fn (mut app App) add_commit_if_not_exist(repo_id int, branch_id int, last_hash s message: message } + // $dbg; sql app.db { insert new_commit into Commit }! diff --git a/src/commit_routes.v b/src/commit_routes.v index 098b1c9f..703f3872 100644 --- a/src/commit_routes.v +++ b/src/commit_routes.v @@ -6,7 +6,7 @@ import time import api @['/api/v1/:user/:repo_name/:branch_name/commits/count'] -fn (mut app App) handle_commits_count(username string, repo_name string, branch_name string) veb.Result { +fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_name string, branch_name string) veb.Result { has_access := app.has_user_repo_read_access_by_repo_name(ctx, ctx.user.id, username, repo_name) @@ -28,7 +28,7 @@ fn (mut app App) handle_commits_count(username string, repo_name string, branch_ } @['/:username/:repo_name/:branch_name/commits/:page'] -pub fn (mut app App) commits(username string, repo_name string, branch_name string, page int) veb.Result { +pub fn (mut app App) commits(mut ctx Context, username string, repo_name string, branch_name string, page int) veb.Result { repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } branch := app.find_repo_branch_by_name(repo.id, branch_name) @@ -72,7 +72,7 @@ pub fn (mut app App) commits(username string, repo_name string, branch_name stri } @['/:username/:repo_name/commit/:hash'] -pub fn (mut app App) commit(username string, repo_name string, hash string) veb.Result { +pub fn (mut app App) commit(mut ctx Context, username string, repo_name string, hash string) veb.Result { repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() } is_patch_request := hash.ends_with('.patch') diff --git a/src/repo.v b/src/repo.v index e249ea3c..08bb6da0 100644 --- a/src/repo.v +++ b/src/repo.v @@ -207,6 +207,12 @@ fn (mut app App) increment_repo_issues(repo_id int) ! { }! } +fn (mut app App) get_count_repo() int { + return sql app.db { + select count from Repo + } or {0} +} + fn (mut app App) add_repo(repo Repo) ! { sql app.db { insert repo into Repo @@ -288,6 +294,7 @@ fn (mut app App) update_repo_from_fs(mut repo Repo) ! { app.info('Repo updated') } +// fn (mut app App) update_repo_branch_from_fs(mut ctx Context, mut repo Repo, branch_name string) ! { fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) ! { repo_id := repo.id branch := app.find_repo_branch_by_name(repo.id, branch_name) @@ -295,10 +302,11 @@ fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) ! if branch.id == 0 { return } + // $dbg; data := repo.git('--no-pager log ${branch_name} --abbrev-commit --abbrev=7 --pretty="%h${log_field_separator}%aE${log_field_separator}%cD${log_field_separator}%s${log_field_separator}%aN"') - println('DATA=') - println(data) + // println('DATA=') + // println(data) for line in data.split_into_lines() { args := line.split(log_field_separator) @@ -323,6 +331,8 @@ fn (mut app App) update_repo_branch_from_fs(mut repo Repo, branch_name string) ! commit_author_id = user.id } + // $dbg; + app.add_commit_if_not_exist(repo_id, branch.id, commit_hash, commit_author, commit_author_id, commit_message, int(commit_date.unix()))! } @@ -397,6 +407,7 @@ fn (mut app App) update_repo_branch_data(mut repo Repo, branch_name string) ! { commit_author_id = user.id } + // $dbg; app.add_commit_if_not_exist(repo_id, branch.id, commit_hash, commit_author, commit_author_id, commit_message, int(commit_date.unix()))! } diff --git a/src/repo_routes.v b/src/repo_routes.v index dca67006..5a4bb04d 100644 --- a/src/repo_routes.v +++ b/src/repo_routes.v @@ -225,9 +225,11 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str } } repo_path := os.join_path(app.config.repo_storage_path, ctx.user.username, name) + id := app.get_count_repo() + 1 mut new_repo := &Repo{ git_repo: git.new_repo(repo_path) name: name + id: id description: description git_dir: repo_path user_id: ctx.user.id @@ -256,6 +258,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str return ctx.redirect('/new') } repo_id := new_repo2.id + // $dbg; // primary_branch := git.get_repository_primary_branch(repo_path) primary_branch := new_repo2.primary_branch app.update_repo_primary_branch(repo_id, primary_branch) or { @@ -283,6 +286,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str } pub fn (mut app App) foo(mut new_repo Repo) { + // $dbg; new_repo.clone() println('CLONING DONE') app.update_repo_from_fs(mut new_repo) or {} From 197084c0978b24383f9596363514f8a858ee9211 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Sun, 23 Mar 2025 13:11:55 +0300 Subject: [PATCH 12/13] add debug logging --- src/commit_routes.v | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/commit_routes.v b/src/commit_routes.v index 703f3872..b6fe380a 100644 --- a/src/commit_routes.v +++ b/src/commit_routes.v @@ -9,6 +9,7 @@ import api fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_name string, branch_name string) veb.Result { has_access := app.has_user_repo_read_access_by_repo_name(ctx, ctx.user.id, username, repo_name) + app.info("${username} ${repo_name} ${branch_name}") if !has_access { return ctx.json_error('Not found') @@ -18,9 +19,13 @@ fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_nam return ctx.json_error('Not found') } + app.info("${repo}") + branch := app.find_repo_branch_by_name(repo.id, branch_name) count := app.get_repo_commit_count(repo.id, branch.id) + app.info("${branch} ${count}" ) + return ctx.json(api.ApiCommitCount{ success: true result: count From e1129f85db121aca4eeab3d3f40e23f13226c677 Mon Sep 17 00:00:00 2001 From: Paul Stern Date: Sun, 23 Mar 2025 13:54:16 +0300 Subject: [PATCH 13/13] fix tests --- src/branch.v | 4 ++++ src/commit_routes.v | 4 +--- src/repo_routes.v | 6 +++--- tests/first_run.v | 4 ++++ 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/branch.v b/src/branch.v index 560131a6..38ec4cc6 100644 --- a/src/branch.v +++ b/src/branch.v @@ -51,6 +51,8 @@ fn (mut app App) create_branch_or_update(repository_id int, branch_name string, select from Branch where repo_id == repository_id && name == branch_name limit 1 } or { []Branch{} } + // app.debug("branches: ${branches}") + if branches.len != 0 { branch := branches.first() app.update_branch(branch.id, author, hash, date)! @@ -66,6 +68,8 @@ fn (mut app App) create_branch_or_update(repository_id int, branch_name string, date: date } + app.debug("inserting branch: ${new_branch}") + sql app.db { insert new_branch into Branch }! diff --git a/src/commit_routes.v b/src/commit_routes.v index b6fe380a..d13a20fb 100644 --- a/src/commit_routes.v +++ b/src/commit_routes.v @@ -9,7 +9,6 @@ import api fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_name string, branch_name string) veb.Result { has_access := app.has_user_repo_read_access_by_repo_name(ctx, ctx.user.id, username, repo_name) - app.info("${username} ${repo_name} ${branch_name}") if !has_access { return ctx.json_error('Not found') @@ -19,12 +18,11 @@ fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_nam return ctx.json_error('Not found') } - app.info("${repo}") branch := app.find_repo_branch_by_name(repo.id, branch_name) count := app.get_repo_commit_count(repo.id, branch.id) - app.info("${branch} ${count}" ) + // app.debug("${branch} ${count}" ) return ctx.json(api.ApiCommitCount{ success: true diff --git a/src/repo_routes.v b/src/repo_routes.v index 5a4bb04d..5086c851 100644 --- a/src/repo_routes.v +++ b/src/repo_routes.v @@ -242,7 +242,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str os.mkdir(new_repo.git_dir) or { panic(err) } new_repo.git('init --bare') } else { - println('GO CLONING:') + app.debug("cloning") // t := time.now() spawn app.foo(mut new_repo) @@ -261,6 +261,7 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str // $dbg; // primary_branch := git.get_repository_primary_branch(repo_path) primary_branch := new_repo2.primary_branch + // app.debug("new_repo2: ${new_repo2}") app.update_repo_primary_branch(repo_id, primary_branch) or { ctx.error('There was an error while adding the repo') return app.new(mut ctx) @@ -286,9 +287,8 @@ pub fn (mut app App) handle_new_repo(mut ctx Context, name string, clone_url str } pub fn (mut app App) foo(mut new_repo Repo) { - // $dbg; new_repo.clone() - println('CLONING DONE') + app.debug("cloning done") app.update_repo_from_fs(mut new_repo) or {} // git.clone(valid_clone_url, repo_path) } diff --git a/tests/first_run.v b/tests/first_run.v index 2d17702f..2169d04d 100644 --- a/tests/first_run.v +++ b/tests/first_run.v @@ -42,9 +42,13 @@ fn main() { assert get_repo_branch_count(token, test_username, 'test1') == 0 test_create_repo(token, 'test2', test_github_repo_url) + // wait while repo is cloning + time.sleep(3 * time.second) + // get repo assert get_repo_commit_count(token, test_username, 'test2', test_github_repo_primary_branch) > 0 assert get_repo_issue_count(token, test_username, 'test2') == 0 assert get_repo_branch_count(token, test_username, 'test2') > 0 + ilog("all tests passed!") after()! }