-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
105 lines (79 loc) · 3.14 KB
/
Copy pathcommon.py
File metadata and controls
105 lines (79 loc) · 3.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Common utilities for gitsl.
This module contains shared data structures and functions used across
the gitsl codebase, including argument parsing, debug utilities,
and subprocess execution.
"""
import os
import shlex
import subprocess
import sys
from dataclasses import dataclass
from importlib.metadata import version, PackageNotFoundError
from typing import List, Optional
# ============================================================
# CONSTANTS
# ============================================================
try:
VERSION = version("gitsl")
except PackageNotFoundError:
VERSION = "0.0.0" # Fallback for uninstalled development
# ============================================================
# DATA STRUCTURES
# ============================================================
@dataclass
class ParsedCommand:
"""Parsed representation of a git command."""
command: Optional[str] # e.g., "commit", "status", None if empty
args: List[str] # remaining arguments after command
raw_argv: List[str] # original argv for debugging
# ============================================================
# PARSING
# ============================================================
def parse_argv(argv: List[str]) -> ParsedCommand:
"""
Parse git-style arguments.
Args:
argv: Command line arguments (without script name, i.e., sys.argv[1:])
Returns:
ParsedCommand with extracted command and remaining args
"""
if not argv:
return ParsedCommand(command=None, args=[], raw_argv=[])
command = argv[0]
args = argv[1:]
return ParsedCommand(command=command, args=args, raw_argv=argv)
# ============================================================
# DEBUG MODE
# ============================================================
def is_debug_mode() -> bool:
"""Check if debug mode is enabled via GITSL_DEBUG environment variable."""
debug_val = os.environ.get("GITSL_DEBUG", "").lower()
return debug_val in ("1", "true", "yes", "on")
def print_debug_info(parsed: ParsedCommand) -> None:
"""Print debug information about the parsed command."""
print(f"[DEBUG] Command: {parsed.command}", file=sys.stderr)
print(f"[DEBUG] Args: {parsed.args}", file=sys.stderr)
# Show what would be executed
if parsed.command:
would_execute = ["sl", parsed.command] + parsed.args
print(f"[DEBUG] Would execute: {shlex.join(would_execute)}", file=sys.stderr)
# ============================================================
# SUBPROCESS EXECUTION
# ============================================================
def run_sl(args: List[str]) -> int:
"""
Execute sl command with I/O passthrough.
Args:
args: Arguments to pass to sl (command and flags)
Returns:
Exit code from sl process
Notes:
- stdin=None, stdout=None, stderr=None (defaults) mean child
inherits parent's file descriptors
- stdout appears on caller's stdout in real-time
- stderr appears on caller's stderr in real-time
- Child receives SIGINT directly (same process group)
"""
result = subprocess.run(["sl"] + args)
return result.returncode