MCPcopy
hub / github.com/GitGuardian/ggshield / parse_patch

Function parse_patch

ggshield/core/scan/commit_utils.py:204–313  ·  view source on GitHub ↗

Parse a patch generated with `git show` or `git diff` using PATCH_COMMON_ARGS. Returns a list of Scannable. A patch looks like this: ``` commit $SHA Author: $NAME <$EMAIL> Date: $DATE $SUBJECT $BODY1 $BODY2 ... $AFFECTED_FILE_LI

(
    sha: Optional[str], patch: str, exclusion_regexes: Optional[Set[Pattern[str]]]
)

Source from the content-addressed store, hash-verified

202
203
204def parse_patch(
205 sha: Optional[str], patch: str, exclusion_regexes: Optional[Set[Pattern[str]]]
206) -> Iterable[Scannable]:
207 """
208 Parse a patch generated with `git show` or `git diff` using PATCH_COMMON_ARGS.
209 Returns a list of Scannable.
210
211 A patch looks like this:
212
213 ```
214 commit $SHA
215 Author: $NAME <$EMAIL>
216 Date: $DATE
217
218 $SUBJECT
219
220 $BODY1
221 $BODY2
222 ...
223
224 $AFFECTED_FILE_LINE\0$DIFF1
225 $DIFF2
226 ...
227 ```
228
229 For a non-merge commit, $DIFFn looks like this:
230
231 ```
232 diff --git $A_NAME $B_NAME
233 $META_INFO
234 $META_INFO
235 ...
236 --- $A_NAME
237 +++ $A_NAME
238 @@ $FROM $TO @@
239 $ACTUAL_CHANGES
240 @@ $FROM $TO @@
241 $MORE_CHANGES
242 ```
243
244 $A_NAME and $B_NAME may be /dev/null in case of creation or removal. When they are
245 not, they start with "a/" and "b/" respectively.
246
247 For a 2-parent merge commit with resolved conflicts, $DIFFn looks like this:
248
249 ```
250 diff --cc $NAME
251 $META_INFO
252 $META_INFO
253 ...
254 --- $A_NAME
255 +++ $B_NAME
256 @@@ $FROM1 $FROM2 $TO @@@
257 $ACTUAL_CHANGES
258 ```
259
260 Note that:
261 - The diff line only contains one name, without any "a/" or "b/" prefixes.

Callers 2

parserMethod · 0.85
parser_mergeMethod · 0.85

Calls 5

is_path_excludedFunction · 0.90
PatchParseErrorClass · 0.85
CommitScannableClass · 0.85
from_stringMethod · 0.45

Tested by

no test coverage detected