| 1002 | } |
| 1003 | |
| 1004 | pub fn build_git_import_file_diffs(change: &Change) -> Option<(Vec<FileDiff>, DiffStats)> { |
| 1005 | let diff_files_value = change |
| 1006 | .unhashed |
| 1007 | .as_ref()? |
| 1008 | .get("git")? |
| 1009 | .get("diff_lines")? |
| 1010 | .clone(); |
| 1011 | |
| 1012 | let diff_files: Vec<GitMetadataDiffFile> = serde_json::from_value(diff_files_value).ok()?; |
| 1013 | let mut file_diffs = Vec::new(); |
| 1014 | let mut stats = DiffStats::new(); |
| 1015 | |
| 1016 | for entry in diff_files { |
| 1017 | let mut insertions = 0usize; |
| 1018 | let mut deletions = 0usize; |
| 1019 | let mut hunk_lines = Vec::new(); |
| 1020 | let mut old_start = None; |
| 1021 | let mut new_start = None; |
| 1022 | |
| 1023 | for line in entry.lines { |
| 1024 | let content = String::from_utf8_lossy(&line.content) |
| 1025 | .trim_end_matches('\n') |
| 1026 | .to_string(); |
| 1027 | match line.origin { |
| 1028 | '+' => { |
| 1029 | let new_num = line.new_lineno.unwrap_or((insertions + 1) as u32) as usize; |
| 1030 | if new_start.is_none() { |
| 1031 | new_start = Some(new_num); |
| 1032 | } |
| 1033 | hunk_lines.push(HunkLine::added(content, new_num)); |
| 1034 | insertions += 1; |
| 1035 | } |
| 1036 | '-' => { |
| 1037 | let old_num = line.old_lineno.unwrap_or((deletions + 1) as u32) as usize; |
| 1038 | if old_start.is_none() { |
| 1039 | old_start = Some(old_num); |
| 1040 | } |
| 1041 | hunk_lines.push(HunkLine::removed(content, old_num)); |
| 1042 | deletions += 1; |
| 1043 | } |
| 1044 | _ => {} |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | if hunk_lines.is_empty() { |
| 1049 | continue; |
| 1050 | } |
| 1051 | |
| 1052 | let status = match (insertions > 0, deletions > 0) { |
| 1053 | (true, false) => FileChangeStatus::Added, |
| 1054 | (false, true) => FileChangeStatus::Deleted, |
| 1055 | _ => FileChangeStatus::Modified, |
| 1056 | }; |
| 1057 | |
| 1058 | let mut file_diff = match status { |
| 1059 | FileChangeStatus::Added => FileDiff::added(&entry.path), |
| 1060 | FileChangeStatus::Deleted => FileDiff::deleted(&entry.path), |
| 1061 | _ => FileDiff::modified(&entry.path), |