(lines: &[GitDiffLine])
| 1916 | } |
| 1917 | |
| 1918 | fn parse_git_diff_replacements(lines: &[GitDiffLine]) -> Option<Vec<GitReplacementBlock>> { |
| 1919 | let mut blocks = Vec::new(); |
| 1920 | let mut old_start: Option<usize> = None; |
| 1921 | let mut new_start: Option<usize> = None; |
| 1922 | let mut old_len = 0usize; |
| 1923 | let mut new_lines = Vec::new(); |
| 1924 | let mut old_cursor = 1usize; |
| 1925 | let mut new_cursor = 1usize; |
| 1926 | |
| 1927 | let flush = |blocks: &mut Vec<GitReplacementBlock>, |
| 1928 | old_start: &mut Option<usize>, |
| 1929 | new_start: &mut Option<usize>, |
| 1930 | old_len: &mut usize, |
| 1931 | new_lines: &mut Vec<Vec<u8>>| |
| 1932 | -> Option<()> { |
| 1933 | if *old_len > 0 || !new_lines.is_empty() { |
| 1934 | let old = old_start.take()?; |
| 1935 | let new = new_start.take().unwrap_or(old); |
| 1936 | blocks.push(GitReplacementBlock { |
| 1937 | old_start: old, |
| 1938 | old_len: *old_len, |
| 1939 | new_start: new, |
| 1940 | new_lines: std::mem::take(new_lines), |
| 1941 | }); |
| 1942 | *old_len = 0; |
| 1943 | } |
| 1944 | Some(()) |
| 1945 | }; |
| 1946 | |
| 1947 | for line in lines { |
| 1948 | match line.origin { |
| 1949 | ' ' => { |
| 1950 | flush( |
| 1951 | &mut blocks, |
| 1952 | &mut old_start, |
| 1953 | &mut new_start, |
| 1954 | &mut old_len, |
| 1955 | &mut new_lines, |
| 1956 | )?; |
| 1957 | old_cursor = line |
| 1958 | .old_lineno |
| 1959 | .map(|n| n as usize + 1) |
| 1960 | .unwrap_or(old_cursor + 1); |
| 1961 | new_cursor = line |
| 1962 | .new_lineno |
| 1963 | .map(|n| n as usize + 1) |
| 1964 | .unwrap_or(new_cursor + 1); |
| 1965 | } |
| 1966 | '-' => { |
| 1967 | if old_start.is_none() { |
| 1968 | old_start = Some(line.old_lineno.map(|n| n as usize).unwrap_or(old_cursor)); |
| 1969 | } |
| 1970 | old_cursor = line |
| 1971 | .old_lineno |
| 1972 | .map(|n| n as usize + 1) |
| 1973 | .unwrap_or(old_cursor + 1); |
| 1974 | old_len += 1; |
| 1975 | } |
no test coverage detected