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

Function parse_stdin

ggshield/core/git_hooks/prereceive.py:61–96  ·  view source on GitHub ↗

Parse stdin and return the first and last commit to scan, or None if there is nothing to do, allowing for early stopping.

()

Source from the content-addressed store, hash-verified

59
60
61def parse_stdin() -> Optional[Tuple[str, str]]:
62 """
63 Parse stdin and return the first and last commit to scan,
64 or None if there is nothing to do, allowing for early stopping.
65 """
66 prereceive_input = sys.stdin.read().strip()
67 if not prereceive_input:
68 raise UnexpectedError(f"Invalid input arguments: '{prereceive_input}'")
69
70 # TODO There can be more than one line here, for example when pushing multiple
71 # branches. We should support this.
72 line = prereceive_input.splitlines()[0]
73 logger.debug("stdin: %s", line)
74 _old_commit, new_commit, _ = line.split(maxsplit=2)
75
76 if new_commit == EMPTY_SHA:
77 # Deletion event, nothing to do
78 ui.display_info("Deletion event or nothing to scan.")
79 return None
80
81 # ignore _old_commit because in case of a force-push, it is going to be overwritten
82 # and should not be scanned (see #437)
83 start_commit = find_branch_start(new_commit)
84 if start_commit is None:
85 # branch does not contain any new commit
86 old_commit = new_commit
87 else:
88 old_commit = f"{start_commit}~1"
89
90 assert old_commit != EMPTY_SHA
91 assert new_commit != EMPTY_SHA
92 if old_commit == new_commit:
93 ui.display_info("Pushed branch does not contain any new commit.")
94 return None
95
96 return (old_commit, new_commit)

Callers 1

prereceive_cmdFunction · 0.90

Calls 4

UnexpectedErrorClass · 0.90
readMethod · 0.80
display_infoMethod · 0.80
find_branch_startFunction · 0.70

Tested by

no test coverage detected