Executor-Tyrant-Framework commited on
Commit
acf2df3
·
unverified ·
1 Parent(s): f6e1cf1

Update recursive_context.py

Browse files
Files changed (1) hide show
  1. recursive_context.py +76 -0
recursive_context.py CHANGED
@@ -398,7 +398,83 @@ class RecursiveContextManager:
398
  return f"STDOUT: {result.stdout}\nSTDERR: {result.stderr}"
399
  except Exception as e:
400
  return f"Execution Error: {e}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
  # =====================================================================
403
  # RECURSIVE SEARCH TOOLS
404
  # =====================================================================
 
398
  return f"STDOUT: {result.stdout}\nSTDERR: {result.stderr}"
399
  except Exception as e:
400
  return f"Execution Error: {e}"
401
+ def push_to_github(self, commit_message="Auto-backup from Clawdbot"):
402
+ """Pushes the current workspace to the configured GitHub repository."""
403
+ token = os.getenv("GITHUB_TOKEN")
404
+ repo = os.getenv("GITHUB_REPO")
405
+
406
+ if not token or not repo:
407
+ return "❌ Error: GITHUB_TOKEN or GITHUB_REPO secret is missing."
408
+
409
+ # authenticated URL
410
+ remote_url = f"https://{token}@github.com/{repo}.git"
411
+
412
+ try:
413
+ # 1. Initialize if needed (Docker containers often lack .git)
414
+ if not (self.repo_path / ".git").exists():
415
+ subprocess.run(["git", "init"], cwd=self.repo_path, check=True)
416
+ subprocess.run(["git", "config", "user.email", "clawdbot@e-t-systems.ai"], cwd=self.repo_path)
417
+ subprocess.run(["git", "config", "user.name", "Clawdbot"], cwd=self.repo_path)
418
+ subprocess.run(["git", "branch", "-M", "main"], cwd=self.repo_path)
419
+
420
+ # 2. Configure Remote (Idempotent)
421
+ # Remove existing remote to ensure token is fresh/correct
422
+ subprocess.run(["git", "remote", "remove", "origin"], cwd=self.repo_path, stderr=subprocess.DEVNULL)
423
+ subprocess.run(["git", "remote", "add", "origin", remote_url], cwd=self.repo_path, check=True)
424
+
425
+ # 3. Add, Commit, Push
426
+ subprocess.run(["git", "add", "."], cwd=self.repo_path, check=True)
427
+
428
+ # Commit (allow empty if nothing changed)
429
+ commit_res = subprocess.run(
430
+ ["git", "commit", "-m", commit_message],
431
+ cwd=self.repo_path, capture_output=True, text=True
432
+ )
433
+
434
+ # Push (force is safer for a backup mirror to overwrite conflicts)
435
+ push_res = subprocess.run(
436
+ ["git", "push", "-u", "origin", "main", "--force"],
437
+ cwd=self.repo_path, capture_output=True, text=True
438
+ )
439
+
440
+ if push_res.returncode == 0:
441
+ return f"✅ Successfully pushed to GitHub: https://github.com/{repo}"
442
+ else:
443
+ return f"⚠️ Git Push Failed: {push_res.stderr}"
444
 
445
+ except Exception as e:
446
+ return f"❌ Critical Git Error: {e}"
447
+
448
+ def pull_from_github(self, branch="main"):
449
+ """Hard reset: Destroys local changes and pulls clean code from GitHub."""
450
+ token = os.getenv("GITHUB_TOKEN")
451
+ repo = os.getenv("GITHUB_REPO")
452
+
453
+ if not token or not repo:
454
+ return "❌ Error: GITHUB_TOKEN or GITHUB_REPO secret is missing."
455
+
456
+ remote_url = f"https://{token}@github.com/{repo}.git"
457
+
458
+ try:
459
+ # 1. Init if missing
460
+ if not (self.repo_path / ".git").exists():
461
+ subprocess.run(["git", "init"], cwd=self.repo_path, check=True)
462
+ subprocess.run(["git", "remote", "add", "origin", remote_url], cwd=self.repo_path)
463
+
464
+ # 2. Fetch and Reset (Destructive but safe for recovery)
465
+ subprocess.run(["git", "fetch", "origin"], cwd=self.repo_path, check=True)
466
+ res = subprocess.run(
467
+ ["git", "reset", "--hard", f"origin/{branch}"],
468
+ cwd=self.repo_path, capture_output=True, text=True
469
+ )
470
+
471
+ if res.returncode == 0:
472
+ return f"✅ RESTORE COMPLETED. Local files replaced with GitHub/{branch}."
473
+ else:
474
+ return f"⚠️ Pull Failed: {res.stderr}"
475
+
476
+ except Exception as e:
477
+ return f"❌ Critical Git Error: {e}"
478
  # =====================================================================
479
  # RECURSIVE SEARCH TOOLS
480
  # =====================================================================