Fix update-from_repo scripts to pull if already cloned

Fixes Copilot review comments:
- Added error checking on package-manager calls
- Verified git availability post-install
- Scripts now fail fast on installer errors
This commit is contained in:
WickedYoda authored and GitHub committed 2026-09-14 13:33:30 -05:00
1 parent bcbe8b1d40
commit cddd0be078
3 files changed
+130 -46

No files matched your search

View File
File renamed without changes.
Regular → Executable
+91 -31
View File
@@ -1,37 +1,97 @@
#!/bin/bash
#!/usr/bin/env python3
"""
Update or clone the public-setupfiles repository.
If already inside the repo, pulls latest changes.
Otherwise clones from GitHub to ./public-setupfiles.
"""
import subprocess
subprocess.run(["apt", "install", "python", "python3", "-y"])
# run apt update to make sure the system has the latest repo files
subprocess.run(["apt", "update", "-y"])
# Check if git is installed
try:
subprocess.run(["git", "--version"], check=True)
except subprocess.CalledProcessError:
# Install git
subprocess.run(["apt-get", "install", "git", "-y"])
# Specify the remote repository URL
remote_repo = "https://github.com/wickedyoda/public-setupfiles.git"
# Specify the local file path
local_file = "./pubic-setupfiles"
# Check if the local file path exists and is not empty
import os
import shutil
if os.path.isdir(local_file) and os.listdir(local_file):
# Delete the existing directory
subprocess.run(["sudo", "rm", "-rf", local_file])
# Clone the remote repository to the local file pathh
subprocess.run(["git", "clone", remote_repo, local_file])
remote_repo = "https://github.com/wickedyoda/public-setupfiles.git"
local_dir = "public-setupfiles"
# Make all files executable and public ownership
subprocess.run(["sudo", "chmod", "777", "-R", local_file])
def run(cmd, **kwargs):
"""Run a command and return the result."""
print(f" Running: {' '.join(cmd)}")
return subprocess.run(cmd, **kwargs)
def install_git():
"""Install git if not present."""
# Already installed?
if shutil.which("git"):
return True
if shutil.which("apt-get"):
subprocess.run(["apt-get", "update", "-y"], check=True)
subprocess.run(["apt-get", "install", "-y", "git"], check=True)
elif shutil.which("opkg"):
subprocess.run(["opkg", "update"], check=True)
subprocess.run(["opkg", "install", "git", "git-http"], check=True)
else:
print("Error: Neither apt-get nor opkg found to install git.")
return False
# Verify git is actually available after install
return shutil.which("git") is not None
def is_in_repo():
"""Check if we're inside the public-setupfiles git repo."""
if not os.path.isdir(".git"):
return False
try:
result = subprocess.run(
["git", "remote", "get-url", "origin"],
capture_output=True, text=True
)
return "wickedyoda/public-setupfiles" in result.stdout
except Exception:
return False
def main():
if os.geteuid() != 0:
print("Warning: Not running as root. Some operations may fail.")
if not install_git():
print("Failed to install or find git. Exiting.")
return 1
# If we're already inside the cloned repo, pull latest
if is_in_repo():
print("Already in public-setupfiles repo. Pulling latest changes...")
result = run(["git", "pull", "origin", "main"])
if result.returncode == 0:
print("Update complete!")
return 0
else:
print(f"Update failed with exit code {result.returncode}")
return result.returncode
# Check if directory already exists
if os.path.isdir(local_dir) and os.listdir(local_dir):
print(f"Directory '{local_dir}' exists. Pulling latest changes...")
os.chdir(local_dir)
result = run(["git", "pull", "origin", "main"])
if result.returncode != 0:
print("Git pull failed, re-cloning...")
os.chdir("..")
shutil.rmtree(local_dir)
run(["git", "clone", remote_repo, local_dir])
else:
os.chdir(local_dir)
else:
print("Cloning repository...")
run(["git", "clone", remote_repo, local_dir])
os.chdir(local_dir)
subprocess.run(["chmod", "-R", "755", "."], check=True)
print("Done!")
return 0
if __name__ == "__main__":
exit(main())
Regular → Executable
+39 -15
View File
@@ -1,4 +1,5 @@
#!/bin/sh
set -euo pipefail
# Ensure script is run as root
if [ "$(id -u)" -ne 0 ]; then
@@ -6,8 +7,8 @@ if [ "$(id -u)" -ne 0 ]; then
exit 1
fi
# Function to auto-install git dynamically based on platform
install_git() {
# Auto-install Git if missing instead of failing silently
if ! command -v git >/dev/null 2>&1; then
if command -v apt-get >/dev/null 2>&1; then
echo "Debian detected. Installing git..."
apt-get update && apt-get install -y git
@@ -15,25 +16,48 @@ install_git() {
echo "OpenWrt detected. Installing git..."
opkg update && opkg install git git-http
else
echo "Error: Neither apt nor opkg found."
echo "Error: Neither apt-get nor opkg found to install git."
exit 1
fi
# Verify git is actually available after install
if ! command -v git >/dev/null 2>&1; then
echo "Error: Git installation failed. git is not available."
exit 1
fi
}
# Auto-install Git if missing instead of failing silently
if ! command -v git >/dev/null 2>&1; then
install_git
fi
REMOTE_REPO="https://github.com/wickedyoda/public-setupfiles.git"
LOCAL_DIR="./public-setupfiles"
LOCAL_DIR="public-setupfiles"
if [ -d "$LOCAL_DIR" ] && [ "$(ls -A "$LOCAL_DIR" 2>/dev/null)" ]; then
echo "Cleaning old directory..."
rm -rf "$LOCAL_DIR"
# If we're already inside the cloned repo, pull latest
if [ -d ".git" ] && git remote get-url origin 2>/dev/null | grep -q "wickedyoda/public-setupfiles"; then
echo "Already in public-setupfiles repo. Pulling latest changes..."
git pull origin main
exit_code=$?
if [ $exit_code -eq 0 ]; then
echo "Update complete!"
else
echo "Update failed with exit code $exit_code"
exit $exit_code
fi
exit 0
fi
echo "Cloning repository..."
git clone "$REMOTE_REPO" "$LOCAL_DIR"
chmod -R 755 "$LOCAL_DIR"
# Check if directory already exists
if [ -d "$LOCAL_DIR" ] && [ "$(ls -A "$LOCAL_DIR" 2>/dev/null)" ]; then
echo "Existing directory found. Pulling latest changes..."
cd "$LOCAL_DIR" || exit 1
if ! git pull origin main; then
echo "Git pull failed, re-cloning..."
cd ..
rm -rf "$LOCAL_DIR"
git clone "$REMOTE_REPO" "$LOCAL_DIR"
fi
else
echo "Cloning repository..."
git clone "$REMOTE_REPO" "$LOCAL_DIR"
fi
cd "$LOCAL_DIR" || exit 1
chmod -R 755 .
echo "Done!"