|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -# Enable error handling |
4 | | -set -e |
| 3 | +# ========== SCRIPT CONFIGURATION ========== |
| 4 | +SCRIPT_VERSION="1.3" |
| 5 | +SCRIPT_TITLE="Git Repository Synchronization Check Utility v$SCRIPT_VERSION" |
| 6 | + |
| 7 | +# Color configuration |
| 8 | +COLOR_NORMAL="\033[0m" |
| 9 | +COLOR_RED="\033[91m" |
| 10 | +COLOR_GREEN="\033[92m" |
| 11 | +COLOR_YELLOW="\033[93m" |
| 12 | +COLOR_BLUE="\033[94m" |
| 13 | +COLOR_MAGENTA="\033[95m" |
| 14 | +COLOR_CYAN="\033[96m" |
| 15 | +COLOR_WHITE="\033[97m" |
| 16 | +COLOR_ORANGE="\033[38;5;208m" |
| 17 | +COLOR_LIGHT_BLUE="\033[94m" |
| 18 | +COLOR_LIGHT_GREEN="\033[92m" |
| 19 | +COLOR_GRAY="\033[90m" |
| 20 | + |
| 21 | +# Enable proper error handling |
| 22 | +set -o pipefail |
| 23 | + |
| 24 | +# ========== MAIN PROCESS ========== |
| 25 | +echo -e "${COLOR_BLUE}=======================================================${COLOR_NORMAL}" |
| 26 | +echo -e "${COLOR_MAGENTA}${SCRIPT_TITLE}${COLOR_NORMAL}" |
| 27 | +echo -e "${COLOR_BLUE}=======================================================${COLOR_NORMAL}" |
| 28 | +echo |
5 | 29 |
|
6 | 30 | # Save the current directory |
7 | 31 | currentDir=$(pwd) |
| 32 | +echo -e "${COLOR_CYAN}Current directory saved: ${currentDir}${COLOR_NORMAL}" |
8 | 33 |
|
9 | | -# Change to the script directory |
10 | | -cd "$(dirname "$0")" |
11 | | - |
12 | | -# Change to the parent folder |
| 34 | +# Change to script directory and then parent folder |
| 35 | +echo -e "${COLOR_CYAN}Changing to repository root directory...${COLOR_NORMAL}" |
| 36 | +cd "$(dirname "$(readlink -f "$0")")" |
13 | 37 | cd .. |
14 | 38 |
|
15 | | -# Remove all desktop.ini files |
16 | | -find . -type f -name "desktop.ini" -exec rm -f {} \; |
| 39 | +# Check if .git directory exists |
| 40 | +if [ ! -d ".git" ]; then |
| 41 | + echo -e "${COLOR_RED}ERROR: This is not a Git repository. Please run this script from a Git repository.${COLOR_NORMAL}" |
| 42 | + # Return to original directory |
| 43 | + cd "$currentDir" |
| 44 | + exit 1 |
| 45 | +fi |
| 46 | + |
| 47 | +echo |
| 48 | +echo -e "${COLOR_BLUE}==== Cleaning desktop.ini Files ====${COLOR_NORMAL}" |
| 49 | +found_files=0 |
| 50 | + |
| 51 | +# Delete all desktop.ini files |
| 52 | +echo -e "${COLOR_CYAN}Searching for desktop.ini files...${COLOR_NORMAL}" |
| 53 | +find . -type f -name "desktop.ini" -print0 2>/dev/null | while IFS= read -r -d '' file; do |
| 54 | + echo -e " ${COLOR_WHITE}Found: ${file}${COLOR_NORMAL}" |
| 55 | + |
| 56 | + # Try to remove from Git tracking |
| 57 | + git rm --cached --force "$file" >/dev/null 2>&1 |
| 58 | + if [ $? -eq 0 ]; then |
| 59 | + echo -e " ${COLOR_GREEN}REMOVED from Git tracking: ${file}${COLOR_NORMAL}" |
| 60 | + fi |
| 61 | + |
| 62 | + # Delete the file |
| 63 | + rm -f "$file" >/dev/null 2>&1 |
| 64 | + if [ $? -eq 0 ]; then |
| 65 | + echo -e " ${COLOR_GREEN}DELETED: ${file}${COLOR_NORMAL}" |
| 66 | + ((found_files++)) |
| 67 | + else |
| 68 | + echo -e " ${COLOR_RED}ERROR: Failed to delete: ${file}${COLOR_NORMAL}" |
| 69 | + fi |
| 70 | +done |
| 71 | + |
| 72 | +echo -e "${COLOR_CYAN}Processed approximately ${found_files} desktop.ini files${COLOR_NORMAL}" |
| 73 | + |
| 74 | +echo |
| 75 | +echo -e "${COLOR_BLUE}==== Checking Git Repository Status ====${COLOR_NORMAL}" |
17 | 76 |
|
18 | 77 | # Check for uncommitted changes and list them if found |
19 | | -if git status --porcelain > temp_status.txt; then |
20 | | - echo "Warning: You have uncommitted changes." |
21 | | - echo "Uncommitted changes:" |
22 | | - cat temp_status.txt |
23 | | - rm -f temp_status.txt |
| 78 | +echo -e "${COLOR_CYAN}Checking for uncommitted changes...${COLOR_NORMAL}" |
| 79 | + |
| 80 | +# Count different types of changes without using temporary files |
| 81 | +has_changes=0 |
| 82 | +modified_count=0 |
| 83 | +added_count=0 |
| 84 | +deleted_count=0 |
| 85 | +untracked_count=0 |
| 86 | +renamed_count=0 |
| 87 | + |
| 88 | +# Create arrays to store filenames by category |
| 89 | +declare -a modified_files=() |
| 90 | +declare -a added_files=() |
| 91 | +declare -a deleted_files=() |
| 92 | +declare -a untracked_files=() |
| 93 | +declare -a renamed_files=() |
| 94 | + |
| 95 | +# Process git status output directly |
| 96 | +while IFS= read -r line; do |
| 97 | + if [[ -n "$line" ]]; then |
| 98 | + has_changes=1 |
| 99 | + status="${line:0:2}" |
| 100 | + filename="${line:3}" |
| 101 | + |
| 102 | + # Skip temp_status.txt from counting if it exists |
| 103 | + if [[ "$filename" != "temp_status.txt" ]]; then |
| 104 | + if [[ "${status:0:1}" == "M" || "${status:1:1}" == "M" ]]; then |
| 105 | + ((modified_count++)) |
| 106 | + modified_files+=("$filename") |
| 107 | + elif [[ "${status:0:1}" == "A" || "${status:1:1}" == "A" ]]; then |
| 108 | + ((added_count++)) |
| 109 | + added_files+=("$filename") |
| 110 | + elif [[ "${status:0:1}" == "D" || "${status:1:1}" == "D" ]]; then |
| 111 | + ((deleted_count++)) |
| 112 | + deleted_files+=("$filename") |
| 113 | + elif [[ "$status" == "??" ]]; then |
| 114 | + ((untracked_count++)) |
| 115 | + untracked_files+=("$filename") |
| 116 | + elif [[ "${status:0:1}" == "R" ]]; then |
| 117 | + ((renamed_count++)) |
| 118 | + renamed_files+=("$filename") |
| 119 | + fi |
| 120 | + fi |
| 121 | + fi |
| 122 | +done < <(git status --porcelain 2>/dev/null) |
| 123 | + |
| 124 | +if [ "$has_changes" -eq 1 ]; then |
| 125 | + echo -e "${COLOR_YELLOW}WARNING: You have uncommitted changes${COLOR_NORMAL}" |
| 126 | + |
| 127 | + # Display summary of changes |
| 128 | + echo -e " ${COLOR_WHITE}Summary of Changes:${COLOR_NORMAL}" |
| 129 | + [ $modified_count -gt 0 ] && echo -e " ${COLOR_ORANGE}Modified files: $modified_count${COLOR_NORMAL}" |
| 130 | + [ $added_count -gt 0 ] && echo -e " ${COLOR_GREEN}Added files: $added_count${COLOR_NORMAL}" |
| 131 | + [ $deleted_count -gt 0 ] && echo -e " ${COLOR_RED}Deleted files: $deleted_count${COLOR_NORMAL}" |
| 132 | + [ $untracked_count -gt 0 ] && echo -e " ${COLOR_CYAN}Untracked files: $untracked_count${COLOR_NORMAL}" |
| 133 | + [ $renamed_count -gt 0 ] && echo -e " ${COLOR_BLUE}Renamed files: $renamed_count${COLOR_NORMAL}" |
| 134 | + |
| 135 | + echo |
| 136 | + echo -e " ${COLOR_WHITE}Changes by Category:${COLOR_NORMAL}" |
| 137 | + |
| 138 | + # Display modified files - IMPROVED METHOD |
| 139 | + if [ $modified_count -gt 0 ]; then |
| 140 | + echo -e " ${COLOR_ORANGE}Modified Files:${COLOR_NORMAL}" |
| 141 | + |
| 142 | + # Use a fixed limit for how many files to display |
| 143 | + max_display=15 |
| 144 | + display_count=0 |
| 145 | + |
| 146 | + for file in "${modified_files[@]}"; do |
| 147 | + ((display_count++)) |
| 148 | + if [ $display_count -le $max_display ]; then |
| 149 | + echo -e " ${COLOR_ORANGE}- $file${COLOR_NORMAL}" |
| 150 | + else |
| 151 | + break |
| 152 | + fi |
| 153 | + done |
| 154 | + |
| 155 | + # Show message if we have more files than the display limit |
| 156 | + if [ $modified_count -gt $max_display ]; then |
| 157 | + remaining=$((modified_count - max_display)) |
| 158 | + echo -e " ${COLOR_ORANGE}... and $remaining more files${COLOR_NORMAL}" |
| 159 | + fi |
| 160 | + fi |
| 161 | + |
| 162 | + # Display added files - IMPROVED METHOD |
| 163 | + if [ $added_count -gt 0 ]; then |
| 164 | + echo -e " ${COLOR_GREEN}Added Files:${COLOR_NORMAL}" |
| 165 | + |
| 166 | + # Use a fixed limit for how many files to display |
| 167 | + max_display=15 |
| 168 | + display_count=0 |
| 169 | + |
| 170 | + for file in "${added_files[@]}"; do |
| 171 | + ((display_count++)) |
| 172 | + if [ $display_count -le $max_display ]; then |
| 173 | + echo -e " ${COLOR_GREEN}+ $file${COLOR_NORMAL}" |
| 174 | + else |
| 175 | + break |
| 176 | + fi |
| 177 | + done |
| 178 | + |
| 179 | + # Show message if we have more files than the display limit |
| 180 | + if [ $added_count -gt $max_display ]; then |
| 181 | + remaining=$((added_count - max_display)) |
| 182 | + echo -e " ${COLOR_GREEN}... and $remaining more files${COLOR_NORMAL}" |
| 183 | + fi |
| 184 | + fi |
| 185 | + |
| 186 | + # Display deleted files - IMPROVED METHOD |
| 187 | + if [ $deleted_count -gt 0 ]; then |
| 188 | + echo -e " ${COLOR_RED}Deleted Files:${COLOR_NORMAL}" |
| 189 | + |
| 190 | + # Use a fixed limit for how many files to display |
| 191 | + max_display=15 |
| 192 | + display_count=0 |
| 193 | + |
| 194 | + for file in "${deleted_files[@]}"; do |
| 195 | + ((display_count++)) |
| 196 | + if [ $display_count -le $max_display ]; then |
| 197 | + echo -e " ${COLOR_RED}- $file${COLOR_NORMAL}" |
| 198 | + else |
| 199 | + break |
| 200 | + fi |
| 201 | + done |
| 202 | + |
| 203 | + # Show message if we have more files than the display limit |
| 204 | + if [ $deleted_count -gt $max_display ]; then |
| 205 | + remaining=$((deleted_count - max_display)) |
| 206 | + echo -e " ${COLOR_RED}... and $remaining more files${COLOR_NORMAL}" |
| 207 | + fi |
| 208 | + fi |
| 209 | + |
| 210 | + # Display untracked files - IMPROVED METHOD |
| 211 | + if [ $untracked_count -gt 0 ]; then |
| 212 | + echo -e " ${COLOR_CYAN}Untracked Files:${COLOR_NORMAL}" |
| 213 | + |
| 214 | + # Use a fixed limit for how many files to display |
| 215 | + max_display=15 |
| 216 | + display_count=0 |
| 217 | + |
| 218 | + for file in "${untracked_files[@]}"; do |
| 219 | + ((display_count++)) |
| 220 | + if [ $display_count -le $max_display ]; then |
| 221 | + echo -e " ${COLOR_CYAN}? $file${COLOR_NORMAL}" |
| 222 | + else |
| 223 | + break |
| 224 | + fi |
| 225 | + done |
| 226 | + |
| 227 | + # Show message if we have more files than the display limit |
| 228 | + if [ $untracked_count -gt $max_display ]; then |
| 229 | + remaining=$((untracked_count - max_display)) |
| 230 | + echo -e " ${COLOR_CYAN}... and $remaining more files${COLOR_NORMAL}" |
| 231 | + fi |
| 232 | + fi |
| 233 | + |
| 234 | + # Display renamed files - IMPROVED METHOD |
| 235 | + if [ $renamed_count -gt 0 ]; then |
| 236 | + echo -e " ${COLOR_BLUE}Renamed Files:${COLOR_NORMAL}" |
| 237 | + |
| 238 | + # Use a fixed limit for how many files to display |
| 239 | + max_display=15 |
| 240 | + display_count=0 |
| 241 | + |
| 242 | + for file in "${renamed_files[@]}"; do |
| 243 | + ((display_count++)) |
| 244 | + if [ $display_count -le $max_display ]; then |
| 245 | + echo -e " ${COLOR_BLUE}* $file${COLOR_NORMAL}" |
| 246 | + else |
| 247 | + break |
| 248 | + fi |
| 249 | + done |
| 250 | + |
| 251 | + # Show message if we have more files than the display limit |
| 252 | + if [ $renamed_count -gt $max_display ]; then |
| 253 | + remaining=$((renamed_count - max_display)) |
| 254 | + echo -e " ${COLOR_BLUE}... and $remaining more files${COLOR_NORMAL}" |
| 255 | + fi |
| 256 | + fi |
| 257 | + |
| 258 | + echo |
24 | 259 | else |
25 | | - rm -f temp_status.txt |
| 260 | + echo -e "${COLOR_GREEN}Working directory is clean - no uncommitted changes.${COLOR_NORMAL}" |
26 | 261 | fi |
27 | 262 |
|
28 | 263 | # Fetch the latest changes from the remote repository |
29 | | -git fetch |
| 264 | +echo |
| 265 | +echo -e "${COLOR_CYAN}Fetching updates from remote repository...${COLOR_NORMAL}" |
| 266 | +if ! git fetch 2>/dev/null; then |
| 267 | + echo -e "${COLOR_RED}ERROR: Failed to fetch from remote. Remote repository may not be configured or accessible.${COLOR_NORMAL}" |
| 268 | + cd "$currentDir" |
| 269 | + exit 1 |
| 270 | +fi |
| 271 | + |
| 272 | +# Get the current branch name |
| 273 | +current_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) |
| 274 | +if [ -z "$current_branch" ]; then |
| 275 | + echo -e "${COLOR_RED}ERROR: Failed to determine current branch.${COLOR_NORMAL}" |
| 276 | + cd "$currentDir" |
| 277 | + exit 1 |
| 278 | +fi |
| 279 | + |
| 280 | +# Try to get the remote branch information |
| 281 | +if ! git rev-parse --verify @{u} >/dev/null 2>&1; then |
| 282 | + echo -e "${COLOR_YELLOW}WARNING: No upstream branch set for '${current_branch}'.${COLOR_NORMAL}" |
| 283 | + cd "$currentDir" |
| 284 | + echo |
| 285 | + echo -e "${COLOR_BLUE}=======================================================${COLOR_NORMAL}" |
| 286 | + echo -e "${COLOR_GREEN}GIT SYNC CHECK COMPLETED${COLOR_NORMAL}" |
| 287 | + echo -e "${COLOR_BLUE}=======================================================${COLOR_NORMAL}" |
| 288 | + exit 0 |
| 289 | +fi |
30 | 290 |
|
31 | | -# Get the local branch, remote branch, and their base |
32 | | -local=$(git rev-parse @) |
33 | | -remote=$(git rev-parse @{u}) |
| 291 | +# Get full commit hashes |
| 292 | +full_local_hash=$(git rev-parse HEAD) |
| 293 | +full_remote_hash=$(git rev-parse @{u}) |
| 294 | + |
| 295 | +# Get short commit hashes (7 characters) |
| 296 | +local=$(git rev-parse --short HEAD) |
| 297 | +remote=$(git rev-parse --short @{u}) |
34 | 298 | base=$(git merge-base @ @{u}) |
35 | 299 |
|
36 | | -if [ "$local" == "$remote" ]; then |
37 | | - echo "Local branch is up-to-date with the remote branch." |
38 | | -elif [ "$local" == "$base" ]; then |
39 | | - echo "Local branch is behind the remote branch." |
40 | | -elif [ "$remote" == "$base" ]; then |
41 | | - echo "Local branch is ahead of the remote branch." |
| 300 | +# Get timestamps and authors for the commits |
| 301 | +local_date=$(git log -1 --format=%cd --date=short HEAD) |
| 302 | +local_author=$(git log -1 --format=%an HEAD) |
| 303 | + |
| 304 | +remote_date=$(git log -1 --format=%cd --date=short @{u}) |
| 305 | +remote_author=$(git log -1 --format=%an @{u}) |
| 306 | + |
| 307 | +echo -e "${COLOR_CYAN}Current branch: ${COLOR_WHITE}${current_branch}${COLOR_NORMAL}" |
| 308 | +echo -e "${COLOR_GRAY}Local commit: ${local} (${local_date} by ${local_author})${COLOR_NORMAL}" |
| 309 | +echo -e "${COLOR_GRAY}Remote commit: ${remote} (${remote_date} by ${remote_author})${COLOR_NORMAL}" |
| 310 | + |
| 311 | +echo |
| 312 | +echo -e "${COLOR_BLUE}==== Synchronization Status ====${COLOR_NORMAL}" |
| 313 | + |
| 314 | +if [ "$full_local_hash" = "$full_remote_hash" ]; then |
| 315 | + echo -e "${COLOR_GREEN}SUCCESS: Local branch '${current_branch}' is up-to-date with the remote branch.${COLOR_NORMAL}" |
| 316 | + echo -e "${COLOR_GREEN}Both local and remote are at commit ${local}${COLOR_NORMAL}" |
| 317 | + echo -e "${COLOR_GREEN}Commit date: ${local_date} by ${local_author}${COLOR_NORMAL}" |
| 318 | +elif [ "$local" = "$base" ]; then |
| 319 | + behind_count=$(git rev-list --count HEAD..@{u}) |
| 320 | + echo -e "${COLOR_YELLOW}WARNING: Local branch '${current_branch}' is ${behind_count} commits behind the remote branch.${COLOR_NORMAL}" |
| 321 | + echo -e "${COLOR_YELLOW}Local is at: ${local}${COLOR_NORMAL}" |
| 322 | + echo -e "${COLOR_YELLOW}Remote is at: ${remote}${COLOR_NORMAL}" |
| 323 | + echo -e "${COLOR_YELLOW}Local commit date: ${local_date} by ${local_author}${COLOR_NORMAL}" |
| 324 | + echo -e "${COLOR_YELLOW}Remote commit date: ${remote_date} by ${remote_author}${COLOR_NORMAL}" |
| 325 | + echo -e "${COLOR_CYAN}Recommendation: Run 'git pull' to update your local branch.${COLOR_NORMAL}" |
| 326 | +elif [ "$remote" = "$base" ]; then |
| 327 | + ahead_count=$(git rev-list --count @{u}..HEAD) |
| 328 | + echo -e "${COLOR_YELLOW}WARNING: Local branch '${current_branch}' is ${ahead_count} commits ahead of the remote branch.${COLOR_NORMAL}" |
| 329 | + echo -e "${COLOR_YELLOW}Local is at: ${local}${COLOR_NORMAL}" |
| 330 | + echo -e "${COLOR_YELLOW}Remote is at: ${remote}${COLOR_NORMAL}" |
| 331 | + echo -e "${COLOR_YELLOW}Local commit date: ${local_date} by ${local_author}${COLOR_NORMAL}" |
| 332 | + echo -e "${COLOR_YELLOW}Remote commit date: ${remote_date} by ${remote_author}${COLOR_NORMAL}" |
| 333 | + echo -e "${COLOR_CYAN}Recommendation: Run 'git push' to update the remote branch.${COLOR_NORMAL}" |
42 | 334 | else |
43 | | - echo "Local and remote branches have diverged." |
| 335 | + ahead_count=$(git rev-list --count @{u}..HEAD) |
| 336 | + behind_count=$(git rev-list --count HEAD..@{u}) |
| 337 | + echo -e "${COLOR_RED}ALERT: Local and remote branches have diverged.${COLOR_NORMAL}" |
| 338 | + echo -e "${COLOR_RED}Local is ${ahead_count} commits ahead and ${behind_count} commits behind the remote.${COLOR_NORMAL}" |
| 339 | + echo -e "${COLOR_YELLOW}Local is at: ${local}${COLOR_NORMAL}" |
| 340 | + echo -e "${COLOR_YELLOW}Remote is at: ${remote}${COLOR_NORMAL}" |
| 341 | + echo -e "${COLOR_YELLOW}Local commit date: ${local_date} by ${local_author}${COLOR_NORMAL}" |
| 342 | + echo -e "${COLOR_YELLOW}Remote commit date: ${remote_date} by ${remote_author}${COLOR_NORMAL}" |
| 343 | + echo -e "${COLOR_CYAN}Recommendation: You may need to merge or rebase to reconcile the differences.${COLOR_NORMAL}" |
| 344 | + echo -e "${COLOR_CYAN} - To merge remote changes: git pull${COLOR_NORMAL}" |
| 345 | + echo -e "${COLOR_CYAN} - To rebase onto remote: git pull --rebase${COLOR_NORMAL}" |
44 | 346 | fi |
45 | 347 |
|
46 | | -# Revert to the original directory |
| 348 | +# Return to original directory |
| 349 | +echo |
| 350 | +echo -e "${COLOR_CYAN}Returning to original directory: ${currentDir}${COLOR_NORMAL}" |
47 | 351 | cd "$currentDir" |
| 352 | + |
| 353 | +echo |
| 354 | +echo -e "${COLOR_BLUE}=======================================================${COLOR_NORMAL}" |
| 355 | +echo -e "${COLOR_GREEN}GIT SYNC CHECK COMPLETED${COLOR_NORMAL}" |
| 356 | +echo -e "${COLOR_BLUE}=======================================================${COLOR_NORMAL}" |
0 commit comments