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.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..d13a20fb 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) @@ -18,9 +18,12 @@ fn (mut app App) handle_commits_count(username string, repo_name string, branch_ return ctx.json_error('Not found') } + branch := app.find_repo_branch_by_name(repo.id, branch_name) count := app.get_repo_commit_count(repo.id, branch.id) + // app.debug("${branch} ${count}" ) + return ctx.json(api.ApiCommitCount{ success: true result: count @@ -28,7 +31,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 +75,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..9f862cc7 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 @@ -240,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) @@ -256,8 +258,11 @@ 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.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) @@ -284,7 +289,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) { 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 1f33d328..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()! } @@ -230,6 +234,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 }