From 64a93e7c72e1cacdcaabd0716d7b0372dfd8381d Mon Sep 17 00:00:00 2001 From: SanJiu Date: Mon, 15 Jun 2026 19:32:16 +0800 Subject: [PATCH] feat: add --json output mode Add --json flag to output repository context as structured JSON instead of markdown. Useful for programmatic consumption and AI pipeline integration. Includes: repo name, path, branch, remote, recent commits, branches, project structure tree, and optional file contents. Closes #2 --- git_context/.gitignore | 1 + git_context/__init__.py | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 git_context/.gitignore diff --git a/git_context/.gitignore b/git_context/.gitignore new file mode 100644 index 0000000..c18dd8d --- /dev/null +++ b/git_context/.gitignore @@ -0,0 +1 @@ +__pycache__/ diff --git a/git_context/__init__.py b/git_context/__init__.py index 92e2b81..52a6991 100755 --- a/git_context/__init__.py +++ b/git_context/__init__.py @@ -128,6 +128,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 as JSON instead of markdown') args = p.parse_args() target = os.path.abspath(args.dir) @@ -184,7 +185,27 @@ def main(): sections.append(contents) output = "\n".join(sections) - + + if args.json: + import json + json_data = { + "repo": repo_name, + "path": target, + "branch": branch, + "remote": remote, + "generated": datetime.now().isoformat(), + "has_changes": bool(has_unstaged or has_staged), + "recent_commits": [] if args.log <= 0 else [l.strip() for l in (log.split('\n') if log else [])], + "branches": [b.strip() for b in (branches.split('\n') if branches else [])], + "structure": tree_out.strip(), + "files": {} if not args.files else {}, + } + output = json.dumps(json_data, indent=2, ensure_ascii=False) + if args.files: + output_lines = output.split('\n') + output_lines.insert(-1, ' "files_included": true') + output = '\n'.join(output_lines) + if args.output: Path(args.output).write_text(output) print(f"✅ Written to {args.output}")