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