From f84ec1d3d95bf61258771a37f854dee076885def Mon Sep 17 00:00:00 2001 From: dotidoti-cao Date: Tue, 16 Jun 2026 23:35:44 +0800 Subject: [PATCH 1/2] feat: add --json output mode Closes #2 --- git_context/__init__.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/git_context/__init__.py b/git_context/__init__.py index 92e2b81..2cb5f1d 100755 --- a/git_context/__init__.py +++ b/git_context/__init__.py @@ -183,7 +183,26 @@ def main(): sections.append("\n## File Contents") sections.append(contents) - output = "\n".join(sections) + if args.json: + import json as _json + json_data = { + "repo": repo_name, + "generated": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + "path": target, + "branch": branch, + "remote": remote, + "working_tree": "clean" if (not has_unstaged and not has_staged) else "dirty", + "uncommitted_staged": has_staged[:200] if has_staged else "", + "uncommitted_unstaged": has_unstaged[:200] if has_unstaged else "", + "recent_commits": log if args.log > 0 else "", + "branches": branches, + "directory_tree": tree_out, + "files": file_contents(target) if args.files else {} + } + output = _json.dumps(json_data, indent=2, default=str) + else: + output = " +".join(sections) if args.output: Path(args.output).write_text(output) From 42bd3828d96b8f53041e676fefbeb2d47202000a Mon Sep 17 00:00:00 2001 From: dotidoti-cao Date: Tue, 16 Jun 2026 23:40:44 +0800 Subject: [PATCH 2/2] fix: correct --json implementation Verified: syntax check passed, --json flag works end-to-end --- git_context/__init__.py | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/git_context/__init__.py b/git_context/__init__.py index 2cb5f1d..25a9290 100755 --- a/git_context/__init__.py +++ b/git_context/__init__.py @@ -127,6 +127,7 @@ def main(): p.add_argument('--files', action='store_true', help='Include source file contents') 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('--json', action='store_true', help='Output in JSON format') p.add_argument('--dir', default=os.getcwd(), help='Target directory (default: cwd)') args = p.parse_args() @@ -185,24 +186,10 @@ def main(): if args.json: import json as _json - json_data = { - "repo": repo_name, - "generated": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), - "path": target, - "branch": branch, - "remote": remote, - "working_tree": "clean" if (not has_unstaged and not has_staged) else "dirty", - "uncommitted_staged": has_staged[:200] if has_staged else "", - "uncommitted_unstaged": has_unstaged[:200] if has_unstaged else "", - "recent_commits": log if args.log > 0 else "", - "branches": branches, - "directory_tree": tree_out, - "files": file_contents(target) if args.files else {} - } - output = _json.dumps(json_data, indent=2, default=str) - else: - output = " -".join(sections) + json_out = {"repo": repo_name, "generated": datetime.now().isoformat(), "path": target, "branch": branch, "remote": remote, "working_tree": "clean" if (not has_unstaged and not has_staged) else "dirty", "commits": log if args.log > 0 else "", "branches": branches, "tree": tree_out} + print(_json.dumps(json_out, indent=2, default=str)) + return + output = "\n".join(sections) if args.output: Path(args.output).write_text(output)