Move towards having GitBackend split into files
parent: tbd commit: e55da0e
1 | use Error; |
2 | use |
3 | , | Object
4 | |
5 | Commit, Repository, RepositoryChunkLine, RepositoryDiff, RepositoryDiffFile, |
6 | RepositoryDiffFileChunk, RepositoryDiffFileInfo, RepositoryDiffFileStatus, |
7 | RepositoryDiffPatchRequest, RepositoryDiffRequest, |
8 | , |
9 | ; |
10 | use ; |
11 | |
12 | use ; |
13 | |
14 | |
15 | pub async |
16 | &mut self, |
17 | requester: & , |
18 | repository_object: &mut , |
19 | OperationState | : ,
20 | request: &RepositoryDiffRequest, |
21 | |
22 | let repository = repository_object.object; |
23 | let git = self |
24 | .open_repository_and_check_permissions |
25 | .await?; |
26 | |
27 | // Parse the passed object ids |
28 | let oid_old = from_str |
29 | .map_err?; |
30 | let oid_new = from_str |
31 | .map_err?; |
32 | |
33 | // Get the ids associates commits |
34 | let commit_old = git |
35 | .find_commit |
36 | .map_err?; |
37 | let commit_new = git |
38 | .find_commit |
39 | .map_err?; |
40 | |
41 | // Get the commit trees |
42 | let tree_old = commit_old |
43 | .tree |
44 | .map_err?; |
45 | let tree_new = commit_new |
46 | .tree |
47 | .map_err?; |
48 | |
49 | // Diff the two trees against each other |
50 | let diff = git |
51 | .diff_tree_to_tree |
52 | .map_err |
53 | FailedDiffing |
54 | ?; |
55 | |
56 | // Should be safe to unwrap? |
57 | let stats = diff.stats .unwrap; |
58 | let mut files: = vec!; |
59 | |
60 | diff.deltas .enumerate .for_each |
61 | // Parse the old file info from the delta |
62 | let old_file_info = match delta.old_file .exists |
63 | true => Some |
64 | id: delta.old_file .id .to_string, |
65 | path: delta |
66 | .old_file |
67 | .path |
68 | .unwrap |
69 | .to_str |
70 | .unwrap |
71 | .to_string, |
72 | size: delta.old_file .size, |
73 | binary: delta.old_file .is_binary, |
74 | , |
75 | false => None, |
76 | ; |
77 | // Parse the new file info from the delta |
78 | let new_file_info = match delta.new_file .exists |
79 | true => Some |
80 | id: delta.new_file .id .to_string, |
81 | path: delta |
82 | .new_file |
83 | .path |
84 | .unwrap |
85 | .to_str |
86 | .unwrap |
87 | .to_string, |
88 | size: delta.new_file .size, |
89 | binary: delta.new_file .is_binary, |
90 | , |
91 | false => None, |
92 | ; |
93 | |
94 | let mut chunks: = vec!; |
95 | if let Some = from_diff .ok .flatten |
96 | for chunk_num in 0..patch.num_hunks |
97 | if let Ok = patch.hunk |
98 | let mut lines: = vec!; |
99 | |
100 | for line_num in 0..chunk_num_lines |
101 | if let Ok = patch.line_in_hunk |
102 | if let Ok = String from_utf8 |
103 | lines.push |
104 | change_type: line.origin_value .into, |
105 | content: line_utf8, |
106 | old_line_num: line.old_lineno, |
107 | new_line_num: line.new_lineno, |
108 | ; |
109 | |
110 | |
111 | continue; |
112 | |
113 | |
114 | |
115 | chunks.push |
116 | header: String from_utf8 .ok, |
117 | old_start: chunk.old_start, |
118 | old_lines: chunk.old_lines, |
119 | new_start: chunk.new_start, |
120 | new_lines: chunk.new_lines, |
121 | lines, |
122 | ; |
123 | |
124 | |
125 | ; |
126 | |
127 | let file = RepositoryDiffFile |
128 | status: from, |
129 | old_file_info, |
130 | new_file_info, |
131 | chunks, |
132 | ; |
133 | |
134 | files.push; |
135 | ; |
136 | |
137 | Ok |
138 | new_commit: from, |
139 | files_changed: stats.files_changed, |
140 | insertions: stats.insertions, |
141 | deletions: stats.deletions, |
142 | files, |
143 | |
144 | |
145 | |
146 | pub async |
147 | &mut self, |
148 | requester: & , |
149 | repository_object: &mut , |
150 | OperationState | : ,
151 | request: &RepositoryDiffPatchRequest, |
152 | |
153 | let repository = repository_object.object; |
154 | let git = self |
155 | .open_repository_and_check_permissions |
156 | .await?; |
157 | |
158 | // Parse the passed object ids |
159 | let oid_old = from_str |
160 | .map_err?; |
161 | let oid_new = from_str |
162 | .map_err?; |
163 | |
164 | // Get the ids associates commits |
165 | let commit_old = git |
166 | .find_commit |
167 | .map_err?; |
168 | let commit_new = git |
169 | .find_commit |
170 | .map_err?; |
171 | |
172 | // Get the commit trees |
173 | let tree_old = commit_old |
174 | .tree |
175 | .map_err?; |
176 | let tree_new = commit_new |
177 | .tree |
178 | .map_err?; |
179 | |
180 | // Diff the two trees against each other |
181 | let diff = git |
182 | .diff_tree_to_tree |
183 | .map_err |
184 | FailedDiffing |
185 | ?; |
186 | |
187 | // Print the entire patch |
188 | let mut patch = String new; |
189 | |
190 | diff.print |
191 | match line.origin |
192 | '+' | '-' | ' ' => patch.push, |
193 | _ => |
194 | |
195 | patch.push_str; |
196 | true |
197 | |
198 | .unwrap; |
199 | |
200 | Ok |
201 | |
202 | |
203 |