Spaces:
Sleeping
Sleeping
fix: immediate per-command git sync after container writes
Browse files
app.py
CHANGED
|
@@ -74,6 +74,22 @@ def is_allowed(args):
|
|
| 74 |
return base in SAFE_COMMANDS
|
| 75 |
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
def spawn(command: str, owner: str = "", repo: str = ""):
|
| 78 |
if not command:
|
| 79 |
return ""
|
|
@@ -86,11 +102,13 @@ def spawn(command: str, owner: str = "", repo: str = ""):
|
|
| 86 |
return f"Error: command '{args[0]}' not allowed in demo container\n"
|
| 87 |
|
| 88 |
cwd = os.environ.get("SPACE_ROOT", "/home/user")
|
|
|
|
| 89 |
if owner and repo:
|
| 90 |
rpath = repo_path(owner, repo)
|
| 91 |
if not os.path.exists(rpath):
|
| 92 |
init_repo(owner, repo)
|
| 93 |
-
|
|
|
|
| 94 |
try:
|
| 95 |
result = subprocess.run(
|
| 96 |
['sh', '-lc', command],
|
|
@@ -99,7 +117,10 @@ def spawn(command: str, owner: str = "", repo: str = ""):
|
|
| 99 |
timeout=10,
|
| 100 |
cwd=cwd,
|
| 101 |
)
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
except subprocess.TimeoutExpired:
|
| 104 |
return "Error: command timed out\n"
|
| 105 |
except Exception as e:
|
|
|
|
| 74 |
return base in SAFE_COMMANDS
|
| 75 |
|
| 76 |
|
| 77 |
+
def _try_sync_repo(owner: str, repo: str):
|
| 78 |
+
path = repo_path(owner, repo)
|
| 79 |
+
if not os.path.isdir(path) or not os.path.exists(os.path.join(path, ".git")):
|
| 80 |
+
return
|
| 81 |
+
try:
|
| 82 |
+
branch = subprocess.run(["git", "branch", "--show-current"], cwd=path, capture_output=True, text=True).stdout.strip() or "main"
|
| 83 |
+
subprocess.run(["git", "pull", "--rebase", "origin", branch], cwd=path, capture_output=True, text=True, timeout=30)
|
| 84 |
+
status = subprocess.run(["git", "status", "--short"], cwd=path, capture_output=True, text=True).stdout.strip()
|
| 85 |
+
if status:
|
| 86 |
+
subprocess.run(["git", "add", "-A"], cwd=path, capture_output=True)
|
| 87 |
+
subprocess.run(["git", "commit", "-m", "codexa: auto-sync"], cwd=path, capture_output=True)
|
| 88 |
+
subprocess.run(["git", "push", "origin", f"HEAD:{branch}"], cwd=path, capture_output=True, text=True, timeout=30)
|
| 89 |
+
except Exception:
|
| 90 |
+
pass
|
| 91 |
+
|
| 92 |
+
|
| 93 |
def spawn(command: str, owner: str = "", repo: str = ""):
|
| 94 |
if not command:
|
| 95 |
return ""
|
|
|
|
| 102 |
return f"Error: command '{args[0]}' not allowed in demo container\n"
|
| 103 |
|
| 104 |
cwd = os.environ.get("SPACE_ROOT", "/home/user")
|
| 105 |
+
repo_dir = ""
|
| 106 |
if owner and repo:
|
| 107 |
rpath = repo_path(owner, repo)
|
| 108 |
if not os.path.exists(rpath):
|
| 109 |
init_repo(owner, repo)
|
| 110 |
+
repo_dir = rpath
|
| 111 |
+
cwd = repo_dir
|
| 112 |
try:
|
| 113 |
result = subprocess.run(
|
| 114 |
['sh', '-lc', command],
|
|
|
|
| 117 |
timeout=10,
|
| 118 |
cwd=cwd,
|
| 119 |
)
|
| 120 |
+
output = (result.stdout + result.stderr).strip() or "(no output)"
|
| 121 |
+
if owner and repo:
|
| 122 |
+
threading.Thread(target=_try_sync_repo, args=(owner, repo), daemon=True).start()
|
| 123 |
+
return output
|
| 124 |
except subprocess.TimeoutExpired:
|
| 125 |
return "Error: command timed out\n"
|
| 126 |
except Exception as e:
|