mauvehed's dotfiles for personal and work environments
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# Base directory
10BASE_DIR=~/gitwork
11
12# Check if the base directory exists
13if [ ! -d "$BASE_DIR" ]; then
14 echo -e "${RED}Directory $BASE_DIR does not exist.${NC}"
15 exit 1
16fi
17
18# Function to check if the directory exists
19check_directory_exists() {
20 if [ ! -d "$1" ]; then
21 echo -e "${RED}Directory $1 does not exist.${NC}"
22 return 1
23 fi
24}
25
26# Function to pull changes from git
27pull_changes() {
28 echo -e "${YELLOW}Pulling changes from git...${NC}"
29 git pull || { echo -e "${RED}Failed to pull changes from git.${NC}"; return 1; }
30}
31
32# Function to check and switch to main or master branch
33switch_to_main_or_master() {
34 local branch
35 branch=$(git rev-parse --abbrev-ref HEAD)
36
37 if [[ "$branch" == "main" || "$branch" == "master" ]]; then
38 return 0
39 fi
40
41 echo -e "${YELLOW}Attempting to switch to main or master branch...${NC}"
42 git checkout main 2>/dev/null || git checkout master 2>/dev/null || {
43 echo -e "${RED}Failed to switch to main or master branch.${NC}"
44 return 1
45 }
46}
47
48# Main script
49for directory in "$BASE_DIR"/*/*/*; do
50 if [ -d "$directory" ]; then
51 # Change to the repository directory
52 cd "$directory" || { echo -e "${RED}Failed to change directory to $directory.${NC}"; continue; }
53 echo -e "${GREEN}Processing repository: $(pwd)${NC}"
54
55 # Check if upstream branch is set
56 if ! git rev-parse --abbrev-ref --symbolic-full-name @{u} >/dev/null 2>&1; then
57 echo -e "${YELLOW}No upstream branch set. Skipping pull.${NC}"
58 echo
59 continue
60 fi
61
62 # Attempt to switch to main or master branch
63 if ! switch_to_main_or_master; then
64 echo -e "${YELLOW}Skipping pull for repository: $(pwd)${NC}"
65 echo
66 continue
67 fi
68
69 # Pull changes from git
70 if ! pull_changes; then
71 echo -e "${YELLOW}Skipping further actions for repository: $(pwd)${NC}"
72 echo
73 continue
74 fi
75
76 echo -e "${GREEN}Successfully updated repository: $(pwd)${NC}"
77 echo
78 fi
79done
80
81echo -e "${GREEN}Script execution completed.${NC}"