From dafa337d8c9d2d114f942c0fcdafc63c0f00b648 Mon Sep 17 00:00:00 2001 From: AI Worker Date: Sat, 13 Jun 2026 06:29:20 +0200 Subject: [PATCH] Add JSON output mode --- README.md | 4 +++- git-context | 35 +++++++++++++++++++++++++++++++---- git_context/__init__.py | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index bfa51e7..f79e01f 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,9 @@ python3 git-context --depth 2 # Write to file instead of stdout python3 git-context --files -o context.txt +# Emit machine-readable JSON +python3 git-context --json + # Any git repo, anywhere python3 git-context --dir /path/to/repo ``` @@ -54,4 +57,3 @@ If git-context saves you time, consider buying me a coffee: **Ko-fi:** [https://ko-fi.com/promptpolish](https://ko-fi.com/promptpolish) *(coming soon)* **BTC:** `bc1qxlj7xlhp7e6v2qw2k6uy7n3z3q3p3k3z3q3p3` - diff --git a/git-context b/git-context index 92e2b81..106127a 100755 --- a/git-context +++ b/git-context @@ -8,6 +8,7 @@ Usage: git context [--depth N] [--files] [--log N] [--output file] [--dir 0 else "", + "branches": branches, + "project_structure": tree_out, + "file_contents": contents if args.files else "", + }, indent=2) + else: + output = "\n".join(sections) if args.output: Path(args.output).write_text(output) diff --git a/git_context/__init__.py b/git_context/__init__.py index 92e2b81..106127a 100755 --- a/git_context/__init__.py +++ b/git_context/__init__.py @@ -8,6 +8,7 @@ """ import argparse +import json import os import subprocess import sys @@ -128,6 +129,7 @@ def main(): p.add_argument('--log', type=int, default=20, help='Number of recent commits (default: 20, 0=skip)') p.add_argument('--output', '-o', help='Write to file instead of stdout') p.add_argument('--dir', default=os.getcwd(), help='Target directory (default: cwd)') + p.add_argument('--json', action='store_true', help='Output machine-readable JSON') args = p.parse_args() target = os.path.abspath(args.dir) @@ -136,9 +138,10 @@ def main(): sys.exit(1) repo_name = os.path.basename(target) + generated = datetime.now().strftime('%Y-%m-%d %H:%M:%S') sections = [] sections.append(f"# git-context: {repo_name}") - sections.append(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") + sections.append(f"Generated: {generated}") sections.append(f"Path: {target}") sections.append("") @@ -150,9 +153,16 @@ def main(): has_unstaged = run(["git", "diff", "--stat"], target) has_staged = run(["git", "diff", "--cached", "--stat"], target) + json_status = {"working_tree": "dirty" if has_unstaged or has_staged else "clean"} status = "" - if has_unstaged: status += f"\n- Unstaged changes: {has_unstaged.split(chr(10))[-1]}" - if has_staged: status += f"\n- Staged changes: {has_staged.split(chr(10))[-1]}" + if has_unstaged: + unstaged = has_unstaged.split(chr(10))[-1] + json_status["unstaged_changes"] = unstaged + status += f"\n- Unstaged changes: {unstaged}" + if has_staged: + staged = has_staged.split(chr(10))[-1] + json_status["staged_changes"] = staged + status += f"\n- Staged changes: {staged}" if not has_unstaged and not has_staged: status += "\n- Working tree: clean" sections.append(status) @@ -177,13 +187,30 @@ def main(): sections.append(f"```\n{tree_out}\n```") # File contents + contents = "" if args.files: contents = file_contents(target) if contents: sections.append("\n## File Contents") sections.append(contents) - output = "\n".join(sections) + if args.json: + output = json.dumps({ + "repo": repo_name, + "generated": generated, + "path": target, + "git": { + "branch": branch, + "remote": remote, + "status": json_status, + }, + "recent_commits": log if args.log > 0 else "", + "branches": branches, + "project_structure": tree_out, + "file_contents": contents if args.files else "", + }, indent=2) + else: + output = "\n".join(sections) if args.output: Path(args.output).write_text(output)