feat: add functionality to check and update dotfiles Git repository
Some checks failed
Nix Format Check / check-format (push) Failing after 38s

This commit is contained in:
Menno van Leeuwen 2025-03-11 11:47:29 +01:00
parent 4ac3a57411
commit b3a4c4bd78
Signed by: vleeuwenmenno
SSH Key Fingerprint: SHA256:OJFmjANpakwD3F2Rsws4GLtbdz1TJ5tkQF0RZmF0TRE

View File

@ -17,9 +17,88 @@ def help_message():
printfe("green", " --ansible, -A Upgrade Ansible packages.")
printfe("green", " --ansible-verbose Upgrade Ansible packages with verbose output. (-vvv)")
printfe("green", " --full-speed, -F Upgrade packages and use all available cores for compilation. (Default: 8 cores)")
printfe("green", " --git, -G Check and update dotfiles Git repository.")
printfe("green", " --help, -h Display this help message.")
return 0
def check_git_repository():
"""Check for changes in the dotfiles git repository and prompt user to pull if needed"""
dotfiles_path = os.environ.get("DOTFILES_PATH", os.path.expanduser("~/.dotfiles"))
printfe("cyan", "Checking for updates in dotfiles repository...")
# Change to dotfiles directory
current_dir = os.getcwd()
os.chdir(dotfiles_path)
# Check if this is a git repository
status, _ = run_command(["git", "rev-parse", "--is-inside-work-tree"], shell=False)
if not status:
printfe("red", "The dotfiles directory is not a git repository.")
os.chdir(current_dir)
return False
# Get the current branch name
status, current_branch = run_command(["git", "rev-parse", "--abbrev-ref", "HEAD"], shell=False)
if not status:
printfe("red", "Failed to determine current branch.")
os.chdir(current_dir)
return False
current_branch = current_branch.strip()
# Fetch the latest changes
status, output = run_command(["git", "fetch"], shell=False)
if not status:
printfe("red", f"Failed to fetch changes from git repository: {output}")
os.chdir(current_dir)
return False
# Check if remote branch exists
status, output = run_command(["git", "ls-remote", "--heads", "origin", current_branch], shell=False)
if not status or not output.strip():
printfe("yellow", f"Remote branch 'origin/{current_branch}' not found. Using local branch only.")
os.chdir(current_dir)
return True
# Check if we're behind the remote
status, output = run_command(["git", "rev-list", f"HEAD..origin/{current_branch}", "--count"], shell=False)
if not status:
printfe("red", f"Failed to check for repository updates: {output}")
os.chdir(current_dir)
return False
behind_count = output.strip()
if behind_count == "0":
printfe("green", f"Dotfiles repository is up to date on branch '{current_branch}'.")
os.chdir(current_dir)
return True
# Show what changes are available
status, output = run_command(["git", "log", f"HEAD..origin/{current_branch}", "--oneline"], shell=False)
if status:
printfe("yellow", f"Your dotfiles repository is {behind_count} commit(s) behind on branch '{current_branch}'. Changes:")
for line in output.strip().splitlines():
printfe("yellow", f"{line}")
else:
printfe("yellow", f"Your dotfiles repository is {behind_count} commit(s) behind on branch '{current_branch}'.")
# Ask user if they want to pull changes
response = input("Do you want to pull these changes? (yes/no): ").strip().lower()
if response in ["yes", "y"]:
status, output = run_command(["git", "pull", "origin", current_branch], shell=False)
if not status:
printfe("red", f"Failed to pull changes: {output}")
os.chdir(current_dir)
return False
printfe("green", "Successfully updated dotfiles repository.")
else:
printfe("yellow", "Skipping repository update.")
os.chdir(current_dir)
return True
def ensure_ansible_collections():
"""Ensure required Ansible collections are installed"""
# List of required collections that can be expanded in the future
@ -95,6 +174,7 @@ def main():
parser.add_argument("--ansible", "-A", action="store_true", help="Upgrade Ansible packages")
parser.add_argument("--ansible-verbose", action="store_true", help="Upgrade Ansible packages with verbose output")
parser.add_argument("--full-speed", "-F", action="store_true", help="Use all available cores")
parser.add_argument("--git", "-G", action="store_true", help="Check and update dotfiles Git repository")
parser.add_argument("--help", "-h", action="store_true", help="Display help message")
args = parser.parse_args()
@ -102,10 +182,17 @@ def main():
if args.help:
return help_message()
# If no specific option provided, run both
if not args.ha and not args.ansible and not args.ansible_verbose:
# If no specific option provided, run all
if not args.ha and not args.ansible and not args.ansible_verbose and not args.git:
args.ha = True
args.ansible = True
args.git = True
# Git repository update
if args.git:
if not check_git_repository():
printfe("red", "Failed to check or update dotfiles repository.")
return 1
# If ansible_verbose is set, also set ansible
if args.ansible_verbose: