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
93 changes: 61 additions & 32 deletions git_context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Binary file added git_context/__pycache__/__init__.cpython-314.pyc
Binary file not shown.
Binary file added git_context/__pycache__/__main__.cpython-314.pyc
Binary file not shown.