From c516ce206080960cf8d621eeff774dc38ec043e8 Mon Sep 17 00:00:00 2001
From: Paul Stern <paulstern45@gmail.com>
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 <paulstern45@gmail.com>
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 <paulstern45@gmail.com>
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 <paulstern45@gmail.com>
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 <paulstern45@gmail.com>
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 940c71e5a399033d71a6b173f3065a2751553229 Mon Sep 17 00:00:00 2001
From: Paul Stern <paulstern45@gmail.com>
Date: Wed, 2 Apr 2025 13:57:54 +0300
Subject: [PATCH 06/10] add dumps to debug

---
 git/libgit.v      | 7 +++++++
 src/repo.v        | 1 +
 src/repo_routes.v | 3 +++
 3 files changed, 11 insertions(+)

diff --git a/git/libgit.v b/git/libgit.v
index 69549eb1..37cbd70a 100644
--- a/git/libgit.v
+++ b/git/libgit.v
@@ -223,6 +223,8 @@ fn get_branch_name_from_reference(ref string) string {
 }
 
 pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
+	dump(branch)
+	dump(file_path)
 	mut blob := &C.git_blob(unsafe { nil })
 	mut branch_ref := &C.git_reference(unsafe { nil })
 	if C.git_branch_lookup(&branch_ref, r.obj, branch.str, C.GIT_BRANCH_LOCAL) != 0 {
@@ -230,11 +232,14 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 		return error('sdf')
 	}
 
+	dump(branch_ref)
+
 	mut treeish := &C.git_object(unsafe { nil })
 	if C.git_reference_peel(&treeish, branch_ref, C.GIT_OBJECT_COMMIT) != 0 {
 		C.printf(c'Failed to peel reference to commit: %s\n', C.git_error_last().message)
 		return error('sdf')
 	}
+	dump(treeish)
 
 	commit := &C.git_commit(treeish)
 	C.git_commit_tree(&treeish, commit)
@@ -244,6 +249,7 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 	}
 
 	tree := unsafe { &C.git_tree(treeish) }
+	// dump(tree)
 
 	// Iterate through the tree entries to find the file
 	entry_count := C.git_tree_entrycount(tree)
@@ -267,6 +273,7 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 			// C.fwrite(content, 1, size, C.stdout)
 
 			text := unsafe { cstring_to_vstring(content) }
+			// dump(text)
 			C.git_blob_free(blob)
 			return text
 		}
diff --git a/src/repo.v b/src/repo.v
index a74bd32a..42f5fc95 100644
--- a/src/repo.v
+++ b/src/repo.v
@@ -812,6 +812,7 @@ fn (r &Repo) read_file(branch string, path string) string {
 	// s := r.git('--no-pager show ${branch}:${valid_path}')
 
 	s := r.git_repo.show_file_blob(branch, valid_path) or { '' }
+	dump(s)
 	println(time.since(t))
 	println(':)')
 	return s
diff --git a/src/repo_routes.v b/src/repo_routes.v
index 3961f214..c5f0fa8c 100644
--- a/src/repo_routes.v
+++ b/src/repo_routes.v
@@ -501,8 +501,11 @@ pub fn (mut app App) blob(mut ctx Context, username string, repo_name string, br
 
 	raw_url := '/${username}/${repo_name}/raw/${branch_name}/${path}'
 	file := app.find_repo_file_by_path(repo.id, branch_name, path) or { return ctx.not_found() }
+	// dump(file)
 	is_markdown := file.name.to_lower().ends_with('.md')
 	plain_text := repo.read_file(branch_name, path)
+	// dump(repo)
+	// dump(plain_text)
 	highlighted_source, _, _ := highlight.highlight_text(plain_text, file.name, false)
 	source := veb.RawHtml(highlighted_source)
 	loc, sloc := calculate_lines_of_code(plain_text)

From c2f7b11ce0c9242bf74361106ce8bbf652b190a3 Mon Sep 17 00:00:00 2001
From: Paul Stern <paulstern45@gmail.com>
Date: Wed, 2 Apr 2025 14:42:03 +0300
Subject: [PATCH 07/10] add dumps to debug

---
 git/libgit.v | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/git/libgit.v b/git/libgit.v
index 37cbd70a..6ea962a0 100644
--- a/git/libgit.v
+++ b/git/libgit.v
@@ -254,12 +254,16 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 	// Iterate through the tree entries to find the file
 	entry_count := C.git_tree_entrycount(tree)
 	// println('number of entires ${entry_count}')
+	dump(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)
+		dump(entry_name)
+		dump(file_path)
 
 		if unsafe { C.strcmp(entry_name, file_path.str) } == 0 {
+			// dump(file_path)
 			// 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)
@@ -273,7 +277,7 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 			// C.fwrite(content, 1, size, C.stdout)
 
 			text := unsafe { cstring_to_vstring(content) }
-			// dump(text)
+			dump(text)
 			C.git_blob_free(blob)
 			return text
 		}

From 951270f5fae4182dfb4167afa3265f2df1cf5b68 Mon Sep 17 00:00:00 2001
From: Paul Stern <paulstern45@gmail.com>
Date: Fri, 4 Apr 2025 10:44:34 +0300
Subject: [PATCH 08/10] hotfix blob view of files in subdirs

---
 git/libgit.v      |  9 ---------
 src/file.v        |  1 +
 src/repo.v        |  7 ++++---
 src/repo_routes.v | 12 ++++++------
 4 files changed, 11 insertions(+), 18 deletions(-)

diff --git a/git/libgit.v b/git/libgit.v
index 6ea962a0..1caf6426 100644
--- a/git/libgit.v
+++ b/git/libgit.v
@@ -223,8 +223,6 @@ fn get_branch_name_from_reference(ref string) string {
 }
 
 pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
-	dump(branch)
-	dump(file_path)
 	mut blob := &C.git_blob(unsafe { nil })
 	mut branch_ref := &C.git_reference(unsafe { nil })
 	if C.git_branch_lookup(&branch_ref, r.obj, branch.str, C.GIT_BRANCH_LOCAL) != 0 {
@@ -232,14 +230,12 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 		return error('sdf')
 	}
 
-	dump(branch_ref)
 
 	mut treeish := &C.git_object(unsafe { nil })
 	if C.git_reference_peel(&treeish, branch_ref, C.GIT_OBJECT_COMMIT) != 0 {
 		C.printf(c'Failed to peel reference to commit: %s\n', C.git_error_last().message)
 		return error('sdf')
 	}
-	dump(treeish)
 
 	commit := &C.git_commit(treeish)
 	C.git_commit_tree(&treeish, commit)
@@ -249,7 +245,6 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 	}
 
 	tree := unsafe { &C.git_tree(treeish) }
-	// dump(tree)
 
 	// Iterate through the tree entries to find the file
 	entry_count := C.git_tree_entrycount(tree)
@@ -259,11 +254,8 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 		entry := C.git_tree_entry_byindex(tree, i)
 		entry_name := C.git_tree_entry_name(entry)
 		C.printf(c'%s\n', entry_name)
-		dump(entry_name)
-		dump(file_path)
 
 		if unsafe { C.strcmp(entry_name, file_path.str) } == 0 {
-			// dump(file_path)
 			// 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)
@@ -277,7 +269,6 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 			// C.fwrite(content, 1, size, C.stdout)
 
 			text := unsafe { cstring_to_vstring(content) }
-			dump(text)
 			C.git_blob_free(blob)
 			return text
 		}
diff --git a/src/file.v b/src/file.v
index 2209e894..3723b339 100644
--- a/src/file.v
+++ b/src/file.v
@@ -78,6 +78,7 @@ fn (mut app App) add_file(file File) ! {
 
 fn (mut app App) find_repository_items(repo_id int, branch string, parent_path string) []File {
 	valid_parent_path := if parent_path == '' { '.' } else { parent_path }
+	// app.debug("parent_path: ${parent_path} valid_parent_path: ${valid_parent_path}")
 
 	items := sql app.db {
 		select from File where repo_id == repo_id && parent_path == valid_parent_path
diff --git a/src/repo.v b/src/repo.v
index 42f5fc95..46caaabb 100644
--- a/src/repo.v
+++ b/src/repo.v
@@ -809,10 +809,11 @@ fn (r &Repo) read_file(branch string, path string) string {
 		return 'nil'
 	}
 	t := time.now()
-	// s := r.git('--no-pager show ${branch}:${valid_path}')
+	// works. Uncommented:
+	s := r.git('--no-pager show ${branch}:${valid_path}')
 
-	s := r.git_repo.show_file_blob(branch, valid_path) or { '' }
-	dump(s)
+	// doesn't work, commented out:
+	// s := r.git_repo.show_file_blob(branch, valid_path) or { '' }
 	println(time.since(t))
 	println(':)')
 	return s
diff --git a/src/repo_routes.v b/src/repo_routes.v
index c5f0fa8c..29778ab9 100644
--- a/src/repo_routes.v
+++ b/src/repo_routes.v
@@ -345,7 +345,7 @@ pub fn (mut app App) tree(mut ctx Context, username string, repo_name string, br
 	branch := app.find_repo_branch_by_name(repo.id, branch_name)
 
 	app.info('${log_prefix}: ${items.len} items found in branch ${branch_name}')
-	println(items)
+	// println(items)
 
 	if items.len == 0 {
 		// No files in the db, fetch them from git and cache in db
@@ -404,6 +404,7 @@ pub fn (mut app App) tree(mut ctx Context, username string, repo_name string, br
 	items = []
 	items << dirs
 	items << files
+	// app.debug('${items.len} items')
 
 	commits_count := app.get_repo_commit_count(repo.id, branch.id)
 	has_commits := commits_count > 0
@@ -485,10 +486,12 @@ pub fn (mut app App) contributors(mut ctx Context, username string, repo_name st
 
 @['/:username/:repo_name/blob/:branch_name/:path...']
 pub fn (mut app App) blob(mut ctx Context, username string, repo_name string, branch_name string, path string) veb.Result {
+	// app.debug("app.blob(): path: ${path}")
 	repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
 
 	mut path_parts := path.split('/')
 	path_parts.pop()
+	// app.debug("app.blob(): path_parts: ${path_parts}")
 
 	ctx.current_path = path
 	ctx.path_split = [repo_name]
@@ -500,12 +503,9 @@ pub fn (mut app App) blob(mut ctx Context, username string, repo_name string, br
 	}
 
 	raw_url := '/${username}/${repo_name}/raw/${branch_name}/${path}'
-	file := app.find_repo_file_by_path(repo.id, branch_name, path) or { return ctx.not_found() }
-	// dump(file)
+	file := app.find_repo_file_by_path(repo.id, branch_name, ctx.current_path) or { return ctx.not_found() }
 	is_markdown := file.name.to_lower().ends_with('.md')
-	plain_text := repo.read_file(branch_name, path)
-	// dump(repo)
-	// dump(plain_text)
+	plain_text := repo.read_file(branch_name, ctx.current_path)
 	highlighted_source, _, _ := highlight.highlight_text(plain_text, file.name, false)
 	source := veb.RawHtml(highlighted_source)
 	loc, sloc := calculate_lines_of_code(plain_text)

From 1369964c6e35839a52a909272fcaaa44ce5a9cdb Mon Sep 17 00:00:00 2001
From: Paul Stern <paulstern45@gmail.com>
Date: Fri, 4 Apr 2025 10:53:30 +0300
Subject: [PATCH 09/10] clean up

---
 git/libgit.v      | 1 -
 src/repo_routes.v | 4 ++--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/git/libgit.v b/git/libgit.v
index 1caf6426..1bb0e24e 100644
--- a/git/libgit.v
+++ b/git/libgit.v
@@ -249,7 +249,6 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 	// Iterate through the tree entries to find the file
 	entry_count := C.git_tree_entrycount(tree)
 	// println('number of entires ${entry_count}')
-	dump(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)
diff --git a/src/repo_routes.v b/src/repo_routes.v
index 29778ab9..2393aade 100644
--- a/src/repo_routes.v
+++ b/src/repo_routes.v
@@ -503,9 +503,9 @@ pub fn (mut app App) blob(mut ctx Context, username string, repo_name string, br
 	}
 
 	raw_url := '/${username}/${repo_name}/raw/${branch_name}/${path}'
-	file := app.find_repo_file_by_path(repo.id, branch_name, ctx.current_path) or { return ctx.not_found() }
+	file := app.find_repo_file_by_path(repo.id, branch_name, path) or { return ctx.not_found() }
 	is_markdown := file.name.to_lower().ends_with('.md')
-	plain_text := repo.read_file(branch_name, ctx.current_path)
+	plain_text := repo.read_file(branch_name, path)
 	highlighted_source, _, _ := highlight.highlight_text(plain_text, file.name, false)
 	source := veb.RawHtml(highlighted_source)
 	loc, sloc := calculate_lines_of_code(plain_text)

From 78dd4ab01a9f11434a93ffff175c78f1fadb7b62 Mon Sep 17 00:00:00 2001
From: Paul Stern <paulstern45@gmail.com>
Date: Fri, 4 Apr 2025 10:54:15 +0300
Subject: [PATCH 10/10] clean up

---
 git/libgit.v | 1 -
 1 file changed, 1 deletion(-)

diff --git a/git/libgit.v b/git/libgit.v
index 1bb0e24e..69549eb1 100644
--- a/git/libgit.v
+++ b/git/libgit.v
@@ -230,7 +230,6 @@ pub fn (r &Repo) show_file_blob(branch string, file_path string) !string {
 		return error('sdf')
 	}
 
-
 	mut treeish := &C.git_object(unsafe { nil })
 	if C.git_reference_peel(&treeish, branch_ref, C.GIT_OBJECT_COMMIT) != 0 {
 		C.printf(c'Failed to peel reference to commit: %s\n', C.git_error_last().message)