Split combined unified diff text into per-file FileDiff sections.
(diff_text)
| 122 | |
| 123 | |
| 124 | def parse_diff(diff_text): |
| 125 | """Split combined unified diff text into per-file FileDiff sections.""" |
| 126 | files = [] |
| 127 | current = None |
| 128 | buf = [] |
| 129 | |
| 130 | def flush(): |
| 131 | if current is not None: |
| 132 | current.text = "\n".join(buf) |
| 133 | files.append(current) |
| 134 | |
| 135 | for line in diff_text.split("\n"): |
| 136 | m = _DIFF_HEADER_RE.match(line) |
| 137 | if m: |
| 138 | flush() |
| 139 | buf = [] |
| 140 | current = FileDiff(old_path=m.group(1), new_path=m.group(2)) |
| 141 | if current is None: |
| 142 | continue |
| 143 | if line.startswith("Binary files ") or line.startswith("GIT binary patch"): |
| 144 | current.is_binary = True |
| 145 | elif line.startswith("new file mode"): |
| 146 | current.is_new = True |
| 147 | elif line.startswith("deleted file mode"): |
| 148 | current.is_deleted = True |
| 149 | elif line.startswith("--- "): |
| 150 | path = line[4:] |
| 151 | if path == "/dev/null": |
| 152 | current.is_new = True |
| 153 | current.old_path = "/dev/null" |
| 154 | elif path.startswith("a/"): |
| 155 | current.old_path = path[2:] |
| 156 | elif line.startswith("+++ "): |
| 157 | path = line[4:] |
| 158 | if path == "/dev/null": |
| 159 | current.is_deleted = True |
| 160 | current.new_path = "/dev/null" |
| 161 | elif path.startswith("b/"): |
| 162 | current.new_path = path[2:] |
| 163 | buf.append(line) |
| 164 | |
| 165 | flush() |
| 166 | return files |
| 167 | |
| 168 | |
| 169 | # --------------------------------------------------------------------------- # |
no test coverage detected