mauvehed's dotfiles for personal and work environments
at main 110 lines 3.1 kB view raw
1#!/bin/bash 2 3# Define colors 4GREEN='\033[0;32m' 5RED='\033[0;31m' 6YELLOW='\033[0;33m' 7NC='\033[0m' # No Color 8 9# Define GitHub username 10github_username="mauvehed" 11 12# Define the directory path 13directory_path=~/gitwork/github/*/* 14 15# Check if the directory contains subdirectories 16check_directory_exists() { 17 shopt -s nullglob 18 local files=($directory_path) 19 shopt -u nullglob 20 if [[ ${#files[@]} -eq 0 ]]; then 21 echo -e "${RED}No directories found in $directory_path.${NC}" 22 exit 1 23 fi 24} 25 26# Verify if the repository is a fork and has upstream defined 27validate_repository() { 28 if ! git remote -v | grep -q "origin.*fetch"; then 29 echo -e "${YELLOW}This repository is not a fork. Skipping.${NC}" 30 return 1 31 fi 32 33 if ! git remote -v | grep -q "upstream"; then 34 echo -e "${YELLOW}No upstream remote defined. Skipping.${NC}" 35 return 1 36 fi 37 38 # Check if origin repository belongs to the user 39 local owner 40 owner=$(git remote get-url origin | sed -E 's/.*[:/]([^/]+)\/[^/]+\.git/\1/') 41 if [[ "$owner" != "$github_username" ]]; then 42 echo -e "${YELLOW}Repository does not belong to $github_username. Skipping.${NC}" 43 return 1 44 fi 45 46 return 0 47} 48 49# Switch to main or master branch 50switch_to_main_or_master() { 51 local current_branch 52 current_branch=$(git rev-parse --abbrev-ref HEAD) 53 if [[ "$current_branch" != "main" && "$current_branch" != "master" ]]; then 54 echo -e "${YELLOW}Switching to main branch...${NC}" 55 git checkout main 2>/dev/null || { 56 echo -e "${YELLOW}Main branch not found. Trying master...${NC}" 57 git checkout master || { 58 echo -e "${RED}Failed to switch to main or master branch.${NC}" 59 return 1 60 } 61 } 62 fi 63 return 0 64} 65 66# Pull and reset branch with upstream 67sync_with_upstream() { 68 local branch 69 branch=$(git rev-parse --abbrev-ref HEAD) 70 71 echo -e "${YELLOW}Pulling new commits from upstream/$branch...${NC}" 72 git pull upstream "$branch" || { 73 echo -e "${RED}Failed to pull upstream/$branch.${NC}" 74 return 1 75 } 76 77 echo -e "${YELLOW}Resetting local $branch branch to match upstream/$branch...${NC}" 78 git reset --hard upstream/"$branch" || { 79 echo -e "${RED}Failed to reset local $branch.${NC}" 80 return 1 81 } 82 83 echo -e "${YELLOW}Force pushing changes to origin $branch...${NC}" 84 git push origin "$branch" --force || { 85 echo -e "${RED}Failed to force push to origin $branch.${NC}" 86 return 1 87 } 88 89 return 0 90} 91 92# Main script execution 93check_directory_exists 94for directory in $directory_path; do 95 cd "$directory" || { 96 echo -e "${RED}Failed to access directory: $directory.${NC}" 97 continue 98 } 99 100 echo -e "${GREEN}Processing repository: $(pwd)${NC}" 101 if validate_repository; then 102 if switch_to_main_or_master; then 103 sync_with_upstream 104 fi 105 fi 106 107 echo -e "\n${YELLOW}--------------------------------------------------${NC}\n" 108done 109 110echo -e "${GREEN}Script executed successfully.${NC}"