Change to new UTC chrono types
parent: tbd commit: 779637c
1 | use Error; |
2 | use BranchType; |
3 | use |
4 | , | Object
5 | |
6 | BranchStaleAfter, DefaultBranch, Repository, RepositoryBranch, RepositoryBranchFilter, |
7 | RepositoryBranchRequest, RepositoryBranchesRequest, |
8 | , |
9 | ; |
10 | use ; |
11 | |
12 | use ; |
13 | |
14 | |
15 | /// .0: List of branches filtering by passed requirements. |
16 | /// .1: Total amount of branches after being filtered |
17 | pub async |
18 | &mut self, |
19 | requester: & , |
20 | repository_object: &mut , |
21 | OperationState | : ,
22 | request: &RepositoryBranchesRequest, |
23 | |
24 | let repository = repository_object.object; |
25 | let git = self |
26 | .open_repository_and_check_permissions |
27 | .await?; |
28 | |
29 | let default_branch_name = repository_object |
30 | . |
31 | .await?; |
32 | let default_branch = git |
33 | .find_branch |
34 | .map_err?; |
35 | |
36 | // Get the stale(after X seconds) setting |
37 | let stale_after = repository_object |
38 | . |
39 | .await |
40 | .unwrap_or_default |
41 | .0; |
42 | |
43 | // Could be done better with the RepositoryBranchFilter::None check done beforehand. |
44 | let mut filtered_branches = git |
45 | .branches? |
46 | .filter_map |
47 | let branch = branch.ok?.0; |
48 | |
49 | let Some = branch.name .ok .flatten else |
50 | return None; |
51 | ; |
52 | |
53 | // TODO: Non UTF-8? |
54 | let Some = get_last_commit_in_rev |
55 | &git, |
56 | branch.get .name .unwrap, |
57 | &default_branch_name, |
58 | |
59 | .ok else |
60 | return None; |
61 | ; |
62 | |
63 | let stale = now |
64 | .signed_duration_since |
65 | .num_seconds |
66 | > stale_after.into; |
67 | |
68 | // Filter based on if the branch is stale or not |
69 | if request.filter != None |
70 | |
71 | if stale && request.filter == Active |
72 | return None; |
73 | else if !stale && request.filter == Stale |
74 | return None; |
75 | |
76 | |
77 | |
78 | Some |
79 | |
80 | .; |
81 | |
82 | // Get the total amount of filtered branches |
83 | let branch_count = filtered_branches.len; |
84 | |
85 | if let Some = &request.search |
86 | // TODO: Caching |
87 | // Search by sorting using a simple fuzzy search algorithm |
88 | filtered_branches.sort_by |
89 | damerau_levenshtein |
90 | .cmp |
91 | ; |
92 | else |
93 | // Sort the branches by commit date |
94 | filtered_branches.sort_by; |
95 | |
96 | |
97 | // Go to the requested position |
98 | let mut filtered_branches = filtered_branches.iter .skip; |
99 | |
100 | let mut branches = vec!; |
101 | |
102 | // Iterate through the filtered branches using the passed range |
103 | for _ in request.range.0..request.range.1 |
104 | let Some = filtered_branches.next else |
105 | break; |
106 | ; |
107 | |
108 | // Get how many commits are ahead of and behind of the head |
109 | let ahead_behind_default = |
110 | if default_branch.get .target .is_some && branch.get .target .is_some |
111 | git.graph_ahead_behind |
112 | branch.get .target .unwrap, |
113 | default_branch.get .target .unwrap, |
114 | |
115 | .ok |
116 | else |
117 | None |
118 | ; |
119 | |
120 | branches.push |
121 | name: name.to_string, |
122 | stale: *stale, |
123 | last_commit: Some, |
124 | ahead_behind_default, |
125 | |
126 | |
127 | |
128 | Ok |
129 | |
130 | |
131 | pub async |
132 | &mut self, |
133 | requester: & , |
134 | repository_object: &mut , |
135 | OperationState | : ,
136 | request: &RepositoryBranchRequest, |
137 | |
138 | let repository = repository_object.object; |
139 | let git = self |
140 | .open_repository_and_check_permissions |
141 | .await?; |
142 | |
143 | // TODO: Don't duplicate search when the default branch and the requested one are the same |
144 | // Get the default branch to compare against |
145 | let default_branch_name = repository_object |
146 | . |
147 | .await?; |
148 | let default_branch = git |
149 | .find_branch |
150 | .map_err?; |
151 | |
152 | // Find the requested branch |
153 | let branch = git |
154 | .find_branch |
155 | .map_err?; |
156 | |
157 | // Get the stale(after X seconds) setting |
158 | let stale_after = repository_object |
159 | . |
160 | .await |
161 | .unwrap_or_default |
162 | .0; |
163 | |
164 | // TODO: Non UTF-8? |
165 | let last_commit = get_last_commit_in_rev |
166 | &git, |
167 | branch.get .name .unwrap, |
168 | &default_branch_name, |
169 | |
170 | .ok; |
171 | |
172 | let stale = if let Some = last_commit |
173 | now |
174 | .signed_duration_since |
175 | .num_seconds |
176 | > stale_after.into |
177 | else |
178 | // TODO: Make sure it's acceptable to return false here |
179 | false |
180 | ; |
181 | |
182 | // Get how many commits are ahead of and behind of the head |
183 | let ahead_behind_default = |
184 | if default_branch.get .target .is_some && branch.get .target .is_some |
185 | git.graph_ahead_behind |
186 | branch.get .target .unwrap, |
187 | default_branch.get .target .unwrap, |
188 | |
189 | .ok |
190 | else |
191 | None |
192 | ; |
193 | |
194 | Ok |
195 | name: request.name.clone, |
196 | stale, |
197 | last_commit, |
198 | ahead_behind_default, |
199 | |
200 | |
201 | |
202 |