gitlyx / commit / commit_routes.v
113 lines · 89 sloc · 3.25 KB · 13ca859655a005b2a87a4935764694302b4f2fda
Raw
1module main
2
3import veb
4import time
5import api
6
7@['/api/v1/:user/:repo_name/:branch_name/commits/count']
8fn (mut app App) handle_commits_count(mut ctx Context, username string, repo_name string, branch_name string) veb.Result {
9 has_access := app.has_user_repo_read_access_by_repo_name(ctx, ctx.user.id, username, repo_name)
10
11 if !has_access {
12 return ctx.json_error('Not found')
13 }
14
15 repo := app.find_repo_by_name_and_username(repo_name, username) or {
16 return ctx.json_error('Not found')
17 }
18
19 branch := app.find_repo_branch_by_name(repo.id, branch_name)
20 count := app.get_repo_commit_count(repo.id, branch.id)
21
22 // app.debug("${branch} ${count}" )
23
24 return ctx.json(api.ApiCommitCount{
25 success: true
26 result: count
27 })
28}
29
30@['/:username/:repo_name/:branch_name/commits/:page']
31pub fn (mut app App) commits(mut ctx Context, username string, repo_name string, branch_name string, page string) veb.Result {
32 repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
33
34 if !app.can_read_repo(ctx, repo) {
35 return ctx.not_found()
36 }
37
38 page_i := page.int()
39 branch := app.find_repo_branch_by_name(repo.id, branch_name)
40 commits_count := app.get_repo_commit_count(repo.id, branch.id)
41
42 offset := commits_per_page * page_i
43 // FIXME: b_author always false
44 b_author := false
45 page_count := calculate_pages(commits_count, commits_per_page)
46 is_first_page := check_first_page(page_i)
47 is_last_page := check_last_page(commits_count, offset, commits_per_page)
48 prev_page, next_page := generate_prev_next_pages(page_i)
49
50 mut commits := app.find_repo_commits_as_page(repo.id, branch.id, offset)
51
52 mut d_commits := map[string][]Commit{}
53 mut author_avatars := map[int]string{}
54 mut author_usernames := map[int]string{}
55 for commit in commits {
56 date := time.unix(commit.created_at)
57 author := commit.author_id.str()
58 date_s := date.custom_format('MMMM D, YYYY')
59
60 if b_author {
61 if author !in d_commits {
62 d_commits[author] = []Commit{}
63 }
64 d_commits[author] << commit
65 } else {
66 if date_s !in d_commits {
67 d_commits[date_s] = []Commit{}
68 }
69 d_commits[date_s] << commit
70 }
71
72 if commit.author_id != 0 && commit.author_id !in author_avatars {
73 if user := app.get_user_by_id(commit.author_id) {
74 author_avatars[commit.author_id] = app.prepare_user_avatar_url(user.avatar)
75 author_usernames[commit.author_id] = user.username
76 }
77 }
78 }
79
80 return $veb.html()
81}
82
83@['/:username/:repo_name/commit/:hash']
84pub fn (mut app App) commit(mut ctx Context, username string, repo_name string, hash string) veb.Result {
85 repo := app.find_repo_by_name_and_username(repo_name, username) or { return ctx.not_found() }
86
87 if !app.can_read_repo(ctx, repo) {
88 return ctx.not_found()
89 }
90
91 is_patch_request := hash.ends_with('.patch')
92
93 if is_patch_request {
94 commit_hash := hash.trim_string_right('.patch')
95 patch := repo.get_commit_patch(commit_hash) or { return ctx.not_found() }
96
97 return ctx.ok(patch)
98 }
99
100 patch_url := '/${username}/${repo_name}/commit/${hash}.patch'
101 commit := app.find_repo_commit_by_hash(repo.id, hash)
102 raw_diff := repo.git('show --no-color --pretty=format: ${commit.hash}')
103 file_diffs := parse_unified_diff(raw_diff)
104
105 mut all_adds := 0
106 mut all_dels := 0
107 for fd in file_diffs {
108 all_adds += fd.additions
109 all_dels += fd.deletions
110 }
111
112 return $veb.html()
113}
114