| 1 | |
| 2 | |
| 3 | use crate::{ |
| 4 | application::port::{GitQueryError, MergePreview}, |
| 5 | domain::ObjectId, |
| 6 | }; |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | pub(super) fn parse_merge_tree( |
| 23 | stdout: &[u8], |
| 24 | conflicted: bool, |
| 25 | ) -> Result<MergePreview, GitQueryError> { |
| 26 | let text = String::from_utf8_lossy(stdout); |
| 27 | let mut lines = text.lines(); |
| 28 | |
| 29 | let tree = lines |
| 30 | .next() |
| 31 | .map(str::trim) |
| 32 | .filter(|line| !line.is_empty()) |
| 33 | .ok_or_else(|| GitQueryError::new("git merge-tree named no tree"))?; |
| 34 | |
| 35 | if !conflicted { |
| 36 | return Ok(MergePreview::Clean { |
| 37 | tree: ObjectId::new(tree).map_err(|error| { |
| 38 | GitQueryError::new(format!("git merge-tree named a bad tree: {error}")) |
| 39 | })?, |
| 40 | }); |
| 41 | } |
| 42 | |
| 43 | let mut paths: Vec<String> = Vec::new(); |
| 44 | |
| 45 | for line in lines { |
| 46 | if line.is_empty() { |
| 47 | break; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | let Some((_, path)) = line.split_once('\t') else { |
| 55 | continue; |
| 56 | }; |
| 57 | |
| 58 | let path = path.to_owned(); |
| 59 | |
| 60 | if !paths.contains(&path) { |
| 61 | paths.push(path); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | Ok(MergePreview::Conflicted { paths }) |
| 66 | } |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | pub(super) fn split_numstat(stdout: &[u8]) -> (&[u8], &[u8]) { |
| 79 | const MARKER: &[u8] = b"diff --git "; |
| 80 | |
| 81 | if stdout.starts_with(MARKER) { |
| 82 | return (&[], stdout); |
| 83 | } |
| 84 | |
| 85 | for (index, byte) in stdout.iter().enumerate() { |
| 86 | if *byte == b'\n' && stdout[index + 1..].starts_with(MARKER) { |
| 87 | return stdout.split_at(index + 1); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | (stdout, &[]) |
| 92 | } |
| 93 | |
| 94 | #[cfg(test)] |
| 95 | mod tests { |
| 96 | use super::*; |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | #[test] |
| 104 | fn the_counts_and_the_patch_are_split_at_the_first_file_header() { |
| 105 | let (numstat, patch) = split_numstat(b"1\t1\ta.txt\n\ndiff --git a/a.txt b/a.txt\n@@\n"); |
| 106 | |
| 107 | assert_eq!(numstat, b"1\t1\ta.txt\n\n"); |
| 108 | assert!(patch.starts_with(b"diff --git ")); |
| 109 | } |
| 110 | |
| 111 | #[test] |
| 112 | fn a_diff_with_nothing_in_it_splits_into_two_empties() { |
| 113 | let (numstat, patch) = split_numstat(b""); |
| 114 | |
| 115 | assert!(numstat.is_empty()); |
| 116 | assert!(patch.is_empty()); |
| 117 | } |
| 118 | |
| 119 | #[test] |
| 120 | fn a_conflict_block_stops_at_the_blank_line() { |
| 121 | |
| 122 | |
| 123 | |
| 124 | let stdout = concat!( |
| 125 | "0123456789012345678901234567890123456789\n", |
| 126 | "100644 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 1\tshared.txt\n", |
| 127 | "100644 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb 2\tshared.txt\n", |
| 128 | "100644 cccccccccccccccccccccccccccccccccccccccc 3\tshared.txt\n", |
| 129 | "100644 dddddddddddddddddddddddddddddddddddddddd 1\tsrc/with space.rs\n", |
| 130 | "\n", |
| 131 | "Auto-merging shared.txt\n", |
| 132 | "CONFLICT (content): Merge conflict in\tshared.txt\n", |
| 133 | ); |
| 134 | |
| 135 | assert_eq!( |
| 136 | parse_merge_tree(stdout.as_bytes(), true).expect("should parse"), |
| 137 | MergePreview::Conflicted { |
| 138 | paths: vec!["shared.txt".to_owned(), "src/with space.rs".to_owned()], |
| 139 | } |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | #[test] |
| 144 | fn a_clean_merge_reads_only_its_tree() { |
| 145 | |
| 146 | |
| 147 | let stdout = "0123456789012345678901234567890123456789\n\nAuto-merging other.txt\n"; |
| 148 | |
| 149 | assert_eq!( |
| 150 | parse_merge_tree(stdout.as_bytes(), false).expect("should parse"), |
| 151 | MergePreview::Clean { |
| 152 | tree: ObjectId::from_trusted("0123456789012345678901234567890123456789"), |
| 153 | } |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | #[test] |
| 158 | fn output_with_no_tree_is_a_fault_not_an_empty_merge() { |
| 159 | assert!(parse_merge_tree(b"", false).is_err()); |
| 160 | assert!(parse_merge_tree(b"\n", true).is_err()); |
| 161 | } |
| 162 | } |