From 9a697c19a3df5c8380c5eecbc82147aaaa890e36 Mon Sep 17 00:00:00 2001 From: y-sudharshan Date: Sun, 27 Apr 2025 16:25:05 +0530 Subject: [PATCH 1/3] made changes to fix the issue --- script.py | 56 ++++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/script.py b/script.py index 66a0630..b137e19 100644 --- a/script.py +++ b/script.py @@ -7,27 +7,23 @@ REPO_OWNER = "NammaLakes" REPO_NAME = "node" BRANCH = "main" -LOCAL_REPO_PATH = "Add Local Path" -BACKUP_PATH = "Add a path for backup" +LOCAL_REPO_PATH = r"local_repo_path" +BACKUP_PATH = "" #add if this is a private repo # GitHub API URL for commits -GITHUB_API_URL = ( - f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/commits?sha={BRANCH}" -) +GITHUB_API_URL = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/commits?sha={BRANCH}" - -# Optional: GitHub Personal Access Token (if using a private repo) -GITHUB_TOKEN = "your-github-token" +# Optional: GitHub Personal Access Token +GITHUB_TOKEN = "" # keep empty if public repo def get_latest_commit(): - """Fetch the latest commit SHA from GitHub API.""" headers = {} if GITHUB_TOKEN: headers["Authorization"] = f"token {GITHUB_TOKEN}" try: - response = requests.get(GITHUB_API_URL, headers=headers) + response = requests.get(GITHUB_API_URL, headers=headers, timeout=10) response.raise_for_status() commits = response.json() return commits[0]["sha"] if commits else None @@ -36,23 +32,28 @@ def get_latest_commit(): return None -def get_latest_commit(): - """Fetch the latest commit SHA from GitHub API for a public repo.""" +def get_local_commit(): try: - response = requests.get(GITHUB_API_URL, timeout=10) # No auth needed - response.raise_for_status() - commits = response.json() - return commits[0]["sha"] if commits else None - except requests.exceptions.RequestException as e: - print(f"Error checking for updates: {e}") + result = subprocess.run( + ["git", "rev-parse", "HEAD"], + cwd=LOCAL_REPO_PATH, + capture_output=True, + text=True, + ) + if result.returncode == 0: + return result.stdout.strip() + else: + print(f"Error getting local commit: {result.stderr}") + return None + except Exception as e: + print(f"Exception getting local commit: {e}") return None def create_backup(): - """Create a backup of the current repository.""" try: if os.path.exists(BACKUP_PATH): - shutil.rmtree(BACKUP_PATH) # Remove old backup + shutil.rmtree(BACKUP_PATH) shutil.copytree(LOCAL_REPO_PATH, BACKUP_PATH) print("Backup created successfully.") return True @@ -62,13 +63,12 @@ def create_backup(): def restore_backup(): - """Restore the backup if update fails.""" try: if os.path.exists(BACKUP_PATH): if os.path.exists(LOCAL_REPO_PATH): - shutil.rmtree(LOCAL_REPO_PATH) # Delete failed update + shutil.rmtree(LOCAL_REPO_PATH) shutil.copytree(BACKUP_PATH, LOCAL_REPO_PATH) - print("Rollback successful: Restored backup.") + print("Rollback successful.") else: print("No backup found to restore.") except Exception as e: @@ -76,8 +76,7 @@ def restore_backup(): def update_repository(): - """Pull the latest changes from GitHub after backing up.""" - if create_backup(): # Only proceed if backup is successful + if create_backup(): try: print("Pulling latest changes from GitHub...") result = subprocess.run( @@ -86,14 +85,12 @@ def update_repository(): capture_output=True, text=True, ) - if result.returncode == 0: print("Repository updated successfully.") - shutil.rmtree(BACKUP_PATH) # Delete backup after a successful update + shutil.rmtree(BACKUP_PATH) else: print(f"Update failed: {result.stderr}. Restoring backup...") restore_backup() - except Exception as e: print(f"Update process failed: {e}. Rolling back...") restore_backup() @@ -102,10 +99,9 @@ def update_repository(): def main(): - """Check for updates and apply them safely.""" print("Checking for updates...") latest_commit = get_latest_commit() - local_commit = get_latest_commit() + local_commit = get_local_commit() if latest_commit and local_commit: if latest_commit != local_commit: From ad4ab1eb93c98abd46aea07cbe33e4b3fec879dc Mon Sep 17 00:00:00 2001 From: Suvan Banerjee Date: Mon, 28 Apr 2025 10:23:50 +0530 Subject: [PATCH 2/3] thanks copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- script.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/script.py b/script.py index b137e19..7b9cf12 100644 --- a/script.py +++ b/script.py @@ -45,8 +45,14 @@ def get_local_commit(): else: print(f"Error getting local commit: {result.stderr}") return None - except Exception as e: - print(f"Exception getting local commit: {e}") + except FileNotFoundError as e: + print(f"File not found error: {e}. Ensure the 'git' executable and the repository path exist.") + return None + except PermissionError as e: + print(f"Permission error: {e}. Check your access rights to the repository path.") + return None + except subprocess.SubprocessError as e: + print(f"Subprocess error: {e}. An issue occurred while running the 'git' command.") return None From d597d0581b2ae21c316a9b0f878a65db4aaf420e Mon Sep 17 00:00:00 2001 From: suvanbanerjee Date: Mon, 28 Apr 2025 10:24:46 +0530 Subject: [PATCH 3/3] fix build --- script.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/script.py b/script.py index 7b9cf12..7d0354e 100644 --- a/script.py +++ b/script.py @@ -7,11 +7,13 @@ REPO_OWNER = "NammaLakes" REPO_NAME = "node" BRANCH = "main" -LOCAL_REPO_PATH = r"local_repo_path" -BACKUP_PATH = "" #add if this is a private repo +LOCAL_REPO_PATH = r"local_repo_path" +BACKUP_PATH = "" # add if this is a private repo # GitHub API URL for commits -GITHUB_API_URL = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/commits?sha={BRANCH}" +GITHUB_API_URL = ( + f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/commits?sha={BRANCH}" +) # Optional: GitHub Personal Access Token GITHUB_TOKEN = "" # keep empty if public repo @@ -46,13 +48,19 @@ def get_local_commit(): print(f"Error getting local commit: {result.stderr}") return None except FileNotFoundError as e: - print(f"File not found error: {e}. Ensure the 'git' executable and the repository path exist.") + print( + f"File not found error: {e}. Ensure the 'git' executable and the repository path exist." + ) return None except PermissionError as e: - print(f"Permission error: {e}. Check your access rights to the repository path.") + print( + f"Permission error: {e}. Check your access rights to the repository path." + ) return None except subprocess.SubprocessError as e: - print(f"Subprocess error: {e}. An issue occurred while running the 'git' command.") + print( + f"Subprocess error: {e}. An issue occurred while running the 'git' command." + ) return None