plz / repo / branch.v
137 lines · 106 sloc · 3.22 KB · bed64285555e6c9bf5ebf34b7538e1da67e926b2
Raw
1module main
2
3import time
4import git
5
6struct Branch {
7mut:
8 id int @[primary; sql: serial]
9 repo_id int @[unique: 'branch']
10 name string @[unique: 'branch']
11 author string // author of latest commit on branch
12 hash string // hash of latest commit on branch
13 date int // time of latest commit on branch
14}
15
16fn (mut app App) fetch_branches(repo Repo) ! {
17 branches_output := repo.git('branch -a')
18
19 for branch_output in branches_output.split_into_lines() {
20 branch_name := git.parse_git_branch_output(branch_output)
21
22 app.fetch_branch(repo, branch_name)!
23 }
24}
25
26fn (mut app App) fetch_branch(repo Repo, branch_name string) ! {
27 last_commit_hash := repo.get_last_branch_commit_hash(branch_name)
28
29 branch_data :=
30 repo.git('log ${branch_name} -1 --pretty="%aE${log_field_separator}%cD" ${last_commit_hash}')
31 log_parts := branch_data.split(log_field_separator)
32
33 author_email := log_parts[0]
34 committed_at := time.parse_rfc2822(log_parts[1]) or {
35 app.info('Error: ${err}')
36
37 return
38 }
39
40 user := app.get_user_by_email(author_email) or {
41 User{
42 username: author_email
43 }
44 }
45
46 app.create_branch_or_update(repo.id, branch_name, user.username, last_commit_hash,
47 int(committed_at.unix()))!
48}
49
50fn (mut app App) create_branch_or_update(repository_id int, branch_name string, author string, hash string, date int) ! {
51 branches := sql app.db {
52 select from Branch where repo_id == repository_id && name == branch_name limit 1
53 } or { []Branch{} }
54
55 // app.debug("branches: ${branches}")
56
57 if branches.len != 0 {
58 branch := branches.first()
59 app.update_branch(branch.id, author, hash, date)!
60
61 return
62 }
63
64 new_branch := Branch{
65 repo_id: repository_id
66 name: branch_name
67 author: author
68 hash: hash
69 date: date
70 }
71
72 app.debug('inserting branch: ${new_branch}')
73
74 sql app.db {
75 insert new_branch into Branch
76 }!
77}
78
79fn (mut app App) update_branch(branch_id int, author string, hash string, date int) ! {
80 sql app.db {
81 update Branch set author = author, hash = hash, date = date where id == branch_id
82 }!
83}
84
85fn (mut app App) find_repo_branch_by_name(repo_id int, name string) Branch {
86 branches := sql app.db {
87 select from Branch where name == name && repo_id == repo_id limit 1
88 } or { []Branch{} }
89
90 if branches.len == 0 {
91 return Branch{}
92 }
93
94 return branches.first()
95}
96
97fn (mut app App) find_repo_branch_by_id(repo_id int, id int) Branch {
98 branches := sql app.db {
99 select from Branch where id == id && repo_id == repo_id limit 1
100 } or { []Branch{} }
101
102 if branches.len == 0 {
103 return Branch{}
104 }
105
106 return branches.first()
107}
108
109fn (app App) get_all_repo_branches(repo_id int) []Branch {
110 return sql app.db {
111 select from Branch where repo_id == repo_id order by date desc
112 } or { []Branch{} }
113}
114
115fn (mut app App) get_count_repo_branches(repo_id int) int {
116 return sql app.db {
117 select count from Branch where repo_id == repo_id
118 } or { 0 }
119}
120
121fn (mut app App) contains_repo_branch(repo_id int, name string) bool {
122 count := sql app.db {
123 select count from Branch where repo_id == repo_id && name == name
124 } or { 0 }
125
126 return count == 1
127}
128
129fn (mut app App) delete_repo_branches(repo_id int) ! {
130 sql app.db {
131 delete from Branch where repo_id == repo_id
132 }!
133}
134
135fn (branch Branch) relative() string {
136 return time.unix(branch.date).relative()
137}
138