|
21 | 21 | - [CPU](#cpu) |
22 | 22 | - [Prerequisites](#prerequisites) |
23 | 23 | - [Installation](#installation) |
| 24 | +- [Documentation](#documentation) |
24 | 25 | - [Usage](#usage) |
25 | 26 | - [License](#license) |
26 | 27 | - [Contact](#contact) |
@@ -66,6 +67,93 @@ pip3 install PyRepScan |
66 | 67 | ``` |
67 | 68 |
|
68 | 69 |
|
| 70 | +## Documentation |
| 71 | + |
| 72 | +```python |
| 73 | +class GitRepositoryScanner: |
| 74 | + def __init__( |
| 75 | + self, |
| 76 | + ) -> None |
| 77 | +``` |
| 78 | +This class holds all the added rules for fast reuse. |
| 79 | + |
| 80 | + |
| 81 | +```python |
| 82 | +def add_rule( |
| 83 | + self, |
| 84 | + name: str, |
| 85 | + match_pattern: str, |
| 86 | + match_whitelist_patterns: typing.List[str], |
| 87 | + match_blacklist_patterns: typing.List[str], |
| 88 | +) -> None |
| 89 | +``` |
| 90 | +The `add_rule` function adds a new rule to an internal list of rules that could be reused multiple times against different repositories. The same name can be used multiple times and would lead to results which can hold the same name. |
| 91 | +- `name` - The name of the rule so it can be identified. |
| 92 | +- `match_pattern` - The regex pattern (RE2 syntax) to match against the content of the commited files. |
| 93 | +- `match_whitelist_patterns` - A list of regex patterns (RE2 syntax) to match against the content of the committed file to filter in results. Only one of the patterns should be matched to pass through the result. There is an OR relation between the patterns. |
| 94 | +- `match_blacklist_patterns` - A list of regex patterns (RE2 syntax) to match against the content of the committed file to filter out results. Only one of the patterns should be matched to omit the result. There is an OR relation between the patterns. |
| 95 | + |
| 96 | + |
| 97 | +```python |
| 98 | +def add_ignored_file_extension( |
| 99 | + self, |
| 100 | + file_extension: str, |
| 101 | +) -> None |
| 102 | +``` |
| 103 | +The `add_ignored_file_extension` function adds a new file extension to the filtering phase to reduce the amount of inspected files and to increase the performance of the scan. |
| 104 | +- `file_extension` - A file extension, without a leading dot, to filter out from the scan. |
| 105 | + |
| 106 | + |
| 107 | +```python |
| 108 | +def add_ignored_file_path( |
| 109 | + self, |
| 110 | + file_path: str, |
| 111 | +) -> None |
| 112 | +``` |
| 113 | +The `add_ignored_file_path` function adds a new file pattern to the filtering phase to reduce the amount of inspected files and to increase the performance of the scan. Every file path that would include the `file_path` substring would be left out of the scanned files. |
| 114 | +- `file_path` - If the inspected file path would include this substring, it won't be scanned. This parameter is a free text. |
| 115 | + |
| 116 | + |
| 117 | +```python |
| 118 | +def scan( |
| 119 | + self, |
| 120 | + repository_path: str, |
| 121 | + branch_glob_pattern: '*', |
| 122 | + from_timestamp: int, |
| 123 | +) -> typing.List[typing.Dict[str, str]] |
| 124 | +``` |
| 125 | +The `scan` function is the main function in the library. Calling this function would trigger a new scan that would return a list of matches. The scan function is a multithreaded operation, that would utilize all the available core in the system. The results would not include the file content but only the regex matching group. To retrieve the full file content one should take the `results['oid']` and to call `get_file_content` function. |
| 126 | +- `repository_path` - The git repository folder path. |
| 127 | +- `branch_glob_pattern` - A glob pattern to filter branches for the scan. |
| 128 | +- `from_timestamp` - A UTC timestamp (Int) that only commits that were created after this timestamp would be included in the scan. |
| 129 | + |
| 130 | +A sample result would look like this: |
| 131 | +```python |
| 132 | +{ |
| 133 | + 'rule_name': 'First Rule', |
| 134 | + 'author_email': '[email protected]', |
| 135 | + 'author_name': 'Author Name', |
| 136 | + 'commit_id': '1111111111111111111111111111111111111111', |
| 137 | + 'commit_message': 'The commit message', |
| 138 | + 'commit_time': '2020-01-01T00:00:00e', |
| 139 | + 'file_path': 'full/file/path', |
| 140 | + 'file_oid': '47d2739ba2c34690248c8f91b84bb54e8936899a', |
| 141 | + 'match': 'The matched group', |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | + |
| 146 | +```python |
| 147 | +def get_file_content( |
| 148 | + repository_path: str, |
| 149 | + file_oid: str, |
| 150 | +) -> bytes |
| 151 | +``` |
| 152 | +The `get_file_content` function exists to retrieve the content of a file that was previously matched. The full file content is omitted from the results to reduce the results list size and to deliver better performance. |
| 153 | +- `repository_path` - The git repository folder path. |
| 154 | +- `file_oid` - A string representing the file oid. This parameter exists in the results dictionary returned by the `scan` function. |
| 155 | + |
| 156 | + |
69 | 157 | ## Usage |
70 | 158 |
|
71 | 159 | ```python |
@@ -101,6 +189,7 @@ grs.add_ignored_file_path( |
101 | 189 | results = grs.scan( |
102 | 190 | repository_path='/repository/path', |
103 | 191 | branch_glob_pattern='*', |
| 192 | + from_timestamp=0, |
104 | 193 | ) |
105 | 194 |
|
106 | 195 | # Results is a list of dicts. Each dict is in the following format: |
|
0 commit comments