diff --git a/giterated-daemon/src/backend/git.rs b/giterated-daemon/src/backend/git.rs index cbdc72d..fb46630 100644 --- a/giterated-daemon/src/backend/git.rs +++ b/giterated-daemon/src/backend/git.rs @@ -11,7 +11,7 @@ use giterated_models::repository::{ RepositoryDiffRequest, RepositoryFile, RepositoryFileFromIdRequest, RepositoryFileInspectRequest, RepositoryIssue, RepositoryIssueLabelsRequest, RepositoryIssuesCountRequest, RepositoryIssuesRequest, RepositoryObjectType, - RepositoryTreeEntry, RepositoryVisibility, Visibility, + RepositoryTreeEntry, RepositoryVisibility, Visibility, RepositoryDiffFileChunkLine, }; use giterated_models::settings::{AnySetting, Setting}; use giterated_models::user::{User, UserParseError}; @@ -821,12 +821,17 @@ impl RepositoryBackend for GitBackend { if let Some(patch) = git2::Patch::from_diff(&diff, i).ok().flatten() { for chunk_num in 0..patch.num_hunks() { if let Ok((chunk, chunk_num_lines)) = patch.hunk(chunk_num) { - let mut lines: Vec = vec![]; + let mut lines: Vec = vec![]; for line_num in 0..chunk_num_lines { if let Ok(line) = patch.line_in_hunk(chunk_num, line_num) { if let Ok(line_utf8) = String::from_utf8(line.content().to_vec()) { - lines.push(line_utf8); + lines.push(RepositoryDiffFileChunkLine { + line_type: line.origin_value(), + content: line_utf8, + old_line_num: line.old_lineno(), + new_line_num: line.new_lineno() + }); } continue; diff --git a/giterated-models/src/repository/mod.rs b/giterated-models/src/repository/mod.rs index 71c889e..303ad80 100644 --- a/giterated-models/src/repository/mod.rs +++ b/giterated-models/src/repository/mod.rs @@ -217,7 +217,7 @@ pub struct RepositoryDiffFile { pub chunks: Vec, } -/// Represents one side of a file diff +/// Represents one side of a file diff [`RepositoryDiffFile`] #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryDiffFileInfo { /// ID of the file @@ -230,7 +230,7 @@ pub struct RepositoryDiffFileInfo { pub binary: bool, } -/// Represents a single chunk of a file diff +/// Represents a single chunk of a file diff [`RepositoryDiffFile`] #[derive(Clone, Debug, Serialize, Deserialize)] pub struct RepositoryDiffFileChunk { /// Header of the chunk @@ -244,7 +244,39 @@ pub struct RepositoryDiffFileChunk { /// Number of lines in "to" side of this chunk pub new_lines: u32, /// Lines of the chunk - pub lines: Vec, + pub lines: Vec, +} + +/// Represents the change type of the [`RepositoryChunkLine`], incomplete of what git actually provides. +#[derive(Clone, Debug, Serialize, Deserialize)] +pub enum RepositoryChunkLineType { + Context, + Addition, + Deletion, +} + +impl From for RepositoryChunkLineType { + fn from(line_type: git2::DiffLineType) -> Self { + match line_type { + git2::DiffLineType::Context => Self::Context, + git2::DiffLineType::Addition => Self::Addition, + git2::DiffLineType::Deletion => Self::Deletion, + _ => Self::Context, + } + } +} + +/// Represents a single line of a [`RepositoryDiffFileChunk`] +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct RepositoryChunkLine { + /// Type of change the line is + pub change_type: RepositoryChunkLineType, + /// Content of the line + pub content: String, + /// Line number in old file + pub old_line_num: Option, + /// Line number in new file + pub new_line_num: Option, } #[derive(Debug, Clone, Serialize, Deserialize)]