Add more info to chunk lines
parent: tbd commit: 7ae7f1f
Showing 2 changed files with 43 insertions and 6 deletions
giterated-daemon/src/backend/git.rs
@@ -11,7 +11,7 @@ use giterated_models::repository::{ | ||
11 | 11 | RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, |
12 | 12 | RepositoryFileInspectRequest, RepositoryIssue, RepositoryIssueLabelsRequest, |
13 | 13 | RepositoryIssuesCountRequest, RepositoryIssuesRequest, RepositoryObjectType, |
14 | RepositoryTreeEntry, RepositoryVisibility, Visibility, | |
14 | RepositoryTreeEntry, RepositoryVisibility, Visibility, RepositoryDiffFileChunkLine, | |
15 | 15 | }; |
16 | 16 | use giterated_models::settings::{AnySetting, Setting}; |
17 | 17 | use giterated_models::user::{User, UserParseError}; |
@@ -821,12 +821,17 @@ impl RepositoryBackend for GitBackend { | ||
821 | 821 | if let Some(patch) = git2::Patch::from_diff(&diff, i).ok().flatten() { |
822 | 822 | for chunk_num in 0..patch.num_hunks() { |
823 | 823 | if let Ok((chunk, chunk_num_lines)) = patch.hunk(chunk_num) { |
824 | let mut lines: Vec<String> = vec![]; | |
824 | let mut lines: Vec<RepositoryDiffFileChunkLine> = vec![]; | |
825 | 825 | |
826 | 826 | for line_num in 0..chunk_num_lines { |
827 | 827 | if let Ok(line) = patch.line_in_hunk(chunk_num, line_num) { |
828 | 828 | if let Ok(line_utf8) = String::from_utf8(line.content().to_vec()) { |
829 | lines.push(line_utf8); | |
829 | lines.push(RepositoryDiffFileChunkLine { | |
830 | line_type: line.origin_value(), | |
831 | content: line_utf8, | |
832 | old_line_num: line.old_lineno(), | |
833 | new_line_num: line.new_lineno() | |
834 | }); | |
830 | 835 | } |
831 | 836 | |
832 | 837 | continue; |
giterated-models/src/repository/mod.rs
@@ -217,7 +217,7 @@ pub struct RepositoryDiffFile { | ||
217 | 217 | pub chunks: Vec<RepositoryDiffFileChunk>, |
218 | 218 | } |
219 | 219 | |
220 | /// Represents one side of a file diff | |
220 | /// Represents one side of a file diff [`RepositoryDiffFile`] | |
221 | 221 | #[derive(Clone, Debug, Serialize, Deserialize)] |
222 | 222 | pub struct RepositoryDiffFileInfo { |
223 | 223 | /// ID of the file |
@@ -230,7 +230,7 @@ pub struct RepositoryDiffFileInfo { | ||
230 | 230 | pub binary: bool, |
231 | 231 | } |
232 | 232 | |
233 | /// Represents a single chunk of a file diff | |
233 | /// Represents a single chunk of a file diff [`RepositoryDiffFile`] | |
234 | 234 | #[derive(Clone, Debug, Serialize, Deserialize)] |
235 | 235 | pub struct RepositoryDiffFileChunk { |
236 | 236 | /// Header of the chunk |
@@ -244,7 +244,39 @@ pub struct RepositoryDiffFileChunk { | ||
244 | 244 | /// Number of lines in "to" side of this chunk |
245 | 245 | pub new_lines: u32, |
246 | 246 | /// Lines of the chunk |
247 | pub lines: Vec<String>, | |
247 | pub lines: Vec<RepositoryChunkLine>, | |
248 | } | |
249 | ||
250 | /// Represents the change type of the [`RepositoryChunkLine`], incomplete of what git actually provides. | |
251 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
252 | pub enum RepositoryChunkLineType { | |
253 | Context, | |
254 | Addition, | |
255 | Deletion, | |
256 | } | |
257 | ||
258 | impl From<git2::DiffLineType> for RepositoryChunkLineType { | |
259 | fn from(line_type: git2::DiffLineType) -> Self { | |
260 | match line_type { | |
261 | git2::DiffLineType::Context => Self::Context, | |
262 | git2::DiffLineType::Addition => Self::Addition, | |
263 | git2::DiffLineType::Deletion => Self::Deletion, | |
264 | _ => Self::Context, | |
265 | } | |
266 | } | |
267 | } | |
268 | ||
269 | /// Represents a single line of a [`RepositoryDiffFileChunk`] | |
270 | #[derive(Clone, Debug, Serialize, Deserialize)] | |
271 | pub struct RepositoryChunkLine { | |
272 | /// Type of change the line is | |
273 | pub change_type: RepositoryChunkLineType, | |
274 | /// Content of the line | |
275 | pub content: String, | |
276 | /// Line number in old file | |
277 | pub old_line_num: Option<u32>, | |
278 | /// Line number in new file | |
279 | pub new_line_num: Option<u32>, | |
248 | 280 | } |
249 | 281 | |
250 | 282 | #[derive(Debug, Clone, Serialize, Deserialize)] |