Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down Expand Up @@ -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`

35 changes: 31 additions & 4 deletions git-context
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Usage: git context [--depth N] [--files] [--log N] [--output file] [--dir <path
"""

import argparse
import json
import os
import subprocess
import sys
Expand Down Expand Up @@ -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)
Expand All @@ -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("")

Expand All @@ -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)
Expand All @@ -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)
Expand Down
35 changes: 31 additions & 4 deletions git_context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"""

import argparse
import json
import os
import subprocess
import sys
Expand Down Expand Up @@ -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)
Expand All @@ -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("")

Expand All @@ -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)
Expand All @@ -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)
Expand Down