Extract bash edges/nodes from in-memory *source* bytes anchored at *source_path*.
(
source: bytes,
*,
source_path: Path,
base_dir: Path | None = None,
file_nid: str | None = None,
entry_nid: str | None = None,
scan_root: Path | None = None,
line_offset: int = 0,
include_declarations: bool = True,
initial_var_bases: dict[str, Path] | None = None,
gha_mode: bool = False,
)
| 273 | |
| 274 | |
| 275 | def extract_bash_source( |
| 276 | source: bytes, |
| 277 | *, |
| 278 | source_path: Path, |
| 279 | base_dir: Path | None = None, |
| 280 | file_nid: str | None = None, |
| 281 | entry_nid: str | None = None, |
| 282 | scan_root: Path | None = None, |
| 283 | line_offset: int = 0, |
| 284 | include_declarations: bool = True, |
| 285 | initial_var_bases: dict[str, Path] | None = None, |
| 286 | gha_mode: bool = False, |
| 287 | ) -> dict: |
| 288 | """Extract bash edges/nodes from in-memory *source* bytes anchored at *source_path*.""" |
| 289 | try: |
| 290 | import tree_sitter_bash as tsbash |
| 291 | from tree_sitter import Language, Parser |
| 292 | except ImportError: |
| 293 | return {"nodes": [], "edges": [], "error": "tree-sitter-bash not installed"} |
| 294 | |
| 295 | try: |
| 296 | if gha_mode: |
| 297 | source = _neutralize_gha_expressions(source.decode("utf-8", errors="replace")).encode("utf-8") |
| 298 | language = Language(tsbash.language()) |
| 299 | parser = Parser(language) |
| 300 | tree = parser.parse(source) |
| 301 | root = tree.root_node |
| 302 | except Exception as e: |
| 303 | return {"nodes": [], "edges": [], "error": str(e)} |
| 304 | |
| 305 | path = source_path |
| 306 | base_path = base_dir if base_dir is not None else path.parent |
| 307 | stem = _file_stem(path) |
| 308 | str_path = str(path) |
| 309 | # Scan root of the extraction in flight (set by extract()); None on a direct |
| 310 | # call. Used to keep corpus-chosen source/invocation paths from being stat-ed |
| 311 | # outside the indexed tree, which on Windows coerces SMB auth over a UNC path |
| 312 | # (GHSA-g6c9-j4vm-mwq4). Gated on via _safe_to_probe before every probe. |
| 313 | if scan_root is None and include_declarations: |
| 314 | scan_root = active_scan_root() |
| 315 | nodes: list[dict] = [] |
| 316 | edges: list[dict] = [] |
| 317 | # Cross-file resolution scaffolding consumed by resolve_bash_source_edges in |
| 318 | # the extract pipeline: `bash_sources` records which files this one `source`s, |
| 319 | # `raw_calls` records calls whose callee isn't defined in this file (candidate |
| 320 | # calls into a sourced library). The extractor sees one file at a time and so |
| 321 | # can't resolve these itself (#2141). |
| 322 | raw_calls: list[dict] = [] |
| 323 | bash_sources: list[dict] = [] |
| 324 | bash_var_sources: list[dict] = [] |
| 325 | raw_seen: set[tuple[str, str]] = set() |
| 326 | seen_ids: set[str] = set() |
| 327 | function_bodies: list[tuple[str, Any]] = [] |
| 328 | defined_functions: set[str] = set() |
| 329 | scan_roots: tuple[Path, ...] = () |
| 330 | resolved_scan_root: Path | None = None |
| 331 | if scan_root is not None: |
| 332 | resolved_scan_root = scan_root.resolve() |
no test coverage detected