MCPcopy Create free account
hub / github.com/alibaba/open-code-review / parse_diff

Function parse_diff

examples/gitflic_ci/post_review.py:124–166  ·  view source on GitHub ↗

Split combined unified diff text into per-file FileDiff sections.

(diff_text)

Source from the content-addressed store, hash-verified

122
123
124def 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# --------------------------------------------------------------------------- #

Callers 1

load_diffs_by_pathFunction · 0.85

Calls 3

FileDiffClass · 0.85
flushFunction · 0.70
appendMethod · 0.45

Tested by

no test coverage detected