diff --git a/git_context/__init__.py b/git_context/__init__.py index 92e2b81..53a2512 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 in JSON format') args = p.parse_args() target = os.path.abspath(args.dir) @@ -136,54 +137,82 @@ def main(): sys.exit(1) repo_name = os.path.basename(target) - sections = [] - sections.append(f"# git-context: {repo_name}") - sections.append(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") - sections.append(f"Path: {target}") - sections.append("") - + # Git info branch = run(["git", "rev-parse", "--abbrev-ref", "HEAD"], target) remote = run(["git", "remote", "get-url", "origin"], target) - sections.append(f"## Git Info\n- Branch: `{branch}`") - sections.append(f"- Remote: {remote}") has_unstaged = run(["git", "diff", "--stat"], target) has_staged = run(["git", "diff", "--cached", "--stat"], target) - 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]}" + + status_str = "" + if has_unstaged: status_str += f"Unstaged changes: {has_unstaged.split(chr(10))[-1]}\n" + if has_staged: status_str += f"Staged changes: {has_staged.split(chr(10))[-1]}\n" if not has_unstaged and not has_staged: - status += "\n- Working tree: clean" - sections.append(status) - - # Recent commits + status_str += "Working tree: clean" + + log = "" if args.log > 0: log = run(["git", "log", f"--max-count={args.log}", "--oneline", "--graph", - "--pretty=format:%h %d %s (%an, %ar)"], target) - if log: - sections.append(f"\n## Recent Commits (last {args.log})") - sections.append(f"```\n{log}\n```") - - # Branch topology + "--pretty=format:%h %d %s (%an, %ar)"], target) + branches = run(["git", "branch", "-a"], target) - if branches: - sections.append("\n## Branches") - sections.append(f"```\n{branches}\n```") - - # Directory tree tree_out = tree(target, ignored=DEFAULT_IGNORE, depth=args.depth) - sections.append(f"\n## Project Structure (depth={args.depth})") - sections.append(f"```\n{tree_out}\n```") - - # File contents + + contents = "" if args.files: contents = file_contents(target) + + if args.json: + import json + context_data = { + "repo": repo_name, + "generated": datetime.now().strftime('%Y-%m-%d %H:%M:%S'), + "path": target, + "git": { + "branch": branch, + "remote": remote, + "status": status_str.strip(), + "recent_commits": log if log else None, + "branches": branches if branches else None + }, + "structure": tree_out if tree_out else None, + "file_contents": contents if contents else None + } + output = json.dumps(context_data, indent=2) + else: + sections = [] + sections.append(f"# git-context: {repo_name}") + sections.append(f"Generated: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}") + sections.append(f"Path: {target}") + sections.append("") + + sections.append(f"## Git Info\n- Branch: `{branch}`") + sections.append(f"- Remote: {remote}") + + 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 not has_unstaged and not has_staged: + status += "\n- Working tree: clean" + sections.append(status) + + if log: + sections.append(f"\n## Recent Commits (last {args.log})") + sections.append(f"```\n{log}\n```") + + if branches: + sections.append("\n## Branches") + sections.append(f"```\n{branches}\n```") + + sections.append(f"\n## Project Structure (depth={args.depth})") + sections.append(f"```\n{tree_out}\n```") + if contents: sections.append("\n## File Contents") sections.append(contents) - - output = "\n".join(sections) + + output = "\n".join(sections) if args.output: Path(args.output).write_text(output) diff --git a/git_context/__pycache__/__init__.cpython-314.pyc b/git_context/__pycache__/__init__.cpython-314.pyc new file mode 100644 index 0000000..20151de Binary files /dev/null and b/git_context/__pycache__/__init__.cpython-314.pyc differ diff --git a/git_context/__pycache__/__main__.cpython-314.pyc b/git_context/__pycache__/__main__.cpython-314.pyc new file mode 100644 index 0000000..53f2dc6 Binary files /dev/null and b/git_context/__pycache__/__main__.cpython-314.pyc differ