MCPcopy Create free account
hub / github.com/Graphify-Labs/graphify / _bash_read_target

Function _bash_read_target

graphify/cli.py:1479–1556  ·  view source on GitHub ↗

The in-project source file a Bash command prints, as an absolute path. The Read tool gets the read nudge; `cat src/app.py`, `sed -n 1,80p src/app.py`, `head -50 src/app.py` and the like read the same code through Bash and got nothing (#3121's parser only looked for searches). Uses _

(cmd_str: str, cwd: str = "")

Source from the content-addressed store, hash-verified

1477
1478
1479def _bash_read_target(cmd_str: str, cwd: str = "") -> "str | None":
1480 """The in-project source file a Bash command prints, as an absolute path.
1481
1482 The Read tool gets the read nudge; `cat src/app.py`, `sed -n 1,80p
1483 src/app.py`, `head -50 src/app.py` and the like read the same code through
1484 Bash and got nothing (#3121's parser only looked for searches). Uses
1485 _bash_commands, so only executed commands count, never a reader named in
1486 prose, quotes or a heredoc body.
1487
1488 A file counts when a reader names it as an operand (or `< file`), it exists,
1489 its extension is a graphed code extension, it resolves inside the project
1490 (CLAUDE_PROJECT_DIR or cwd) and outside the graph output (#1840, #4961).
1491 Relative paths resolve against the Bash tool&#x27;s ``cwd`` from the hook payload
1492 when it is inside the project. Writes are not reads: a stdout redirect
1493 (`cat > x.py`, `sed -n 1p a.py > b.py`) or `sed -i` disqualifies the
1494 command, and a reader with no file operand (`echo x | cat`) reads stdin.
1495 Paths with shell expansion are skipped. None when nothing qualifies.
1496 """
1497 from graphify.paths import out_path
1498 try:
1499 root = Path(os.environ.get("CLAUDE_PROJECT_DIR") or os.getcwd()).resolve()
1500 base = root
1501 if cwd and not _is_cwd_relative(cwd):
1502 c = Path(cwd).resolve()
1503 if c.is_relative_to(root):
1504 base = c
1505 outs = []
1506 for o in (out_path(), Path("graphify-out")):
1507 outs.append((root / o if _is_cwd_relative(str(o)) else o).resolve())
1508 except (OSError, RuntimeError, ValueError):
1509 return None
1510 for name, args, _piped in _bash_commands(cmd_str):
1511 if name not in _BASH_READERS:
1512 continue
1513 if name == "sed" and (_sed_awk_in_place(name, args) or not any(
1514 a in ("--quiet", "--silent") or (a.startswith("-") and not a.startswith("--") and "n" in a)
1515 for a in args)):
1516 continue
1517 files: "list[str]" = []
1518 it = iter(args)
1519 for tok in it:
1520 if ">" in tok:
1521 if tok.split(">", 1)[0] in ("", "1", "&"):
1522 files = []
1523 break # stdout goes to a file: a write, not a read
1524 if tok.endswith(">"):
1525 next(it, None) # `2> err.log`
1526 continue
1527 if tok.startswith("<<"):
1528 if tok in ("<<", "<<-", "<<<"):
1529 next(it, None)
1530 continue
1531 if tok == "<":
1532 tok = next(it, "")
1533 elif tok.startswith("<"):
1534 tok = tok[1:]
1535 elif name == "sed" and tok in ("-e", "-f", "--expression", "--file"):
1536 next(it, None) # the program, not a file

Callers 1

_run_hook_guardFunction · 0.85

Calls 6

out_pathFunction · 0.90
_is_cwd_relativeFunction · 0.85
_bash_commandsFunction · 0.85
_sed_awk_in_placeFunction · 0.85
resolveMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected