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