|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Colors for output |
| 4 | +RED='\033[0;31m' |
| 5 | +GREEN='\033[0;32m' |
| 6 | +YELLOW='\033[1;33m' |
| 7 | +NC='\033[0m' # No Color |
| 8 | + |
| 9 | +# Parse command line arguments |
| 10 | +FETCH_ALL=0 |
| 11 | +while [[ "$#" -gt 0 ]]; do |
| 12 | + case $1 in |
| 13 | + -a|--all) FETCH_ALL=1 ;; |
| 14 | + *) echo "Unknown parameter: $1"; exit 1 ;; |
| 15 | + esac |
| 16 | + shift |
| 17 | +done |
| 18 | + |
| 19 | +# Function to check if a command exists |
| 20 | +command_exists() { |
| 21 | + command -v "$1" >/dev/null 2>&1 |
| 22 | +} |
| 23 | + |
| 24 | +# Check if git-lfs is installed and initialized |
| 25 | +if ! command -v git-lfs >/dev/null 2>&1; then |
| 26 | + echo -e "${RED}Error: git-lfs is not installed${NC}" |
| 27 | + echo "Please install git-lfs first:" |
| 28 | + echo " brew install git-lfs # On macOS" |
| 29 | + echo " apt-get install git-lfs # On Ubuntu/Debian" |
| 30 | + exit 1 |
| 31 | +fi |
| 32 | + |
| 33 | +# Function to get LFS objects that need syncing |
| 34 | +get_missing_lfs_objects() { |
| 35 | + git lfs ls-files | awk '$2 == "-" {$1=""; $2=""; sub(/^[ \t]+/, ""); print}' |
| 36 | +} |
| 37 | + |
| 38 | +# Function to get all LFS objects |
| 39 | +get_all_lfs_objects() { |
| 40 | + git lfs ls-files | awk '{$1=""; $2=""; sub(/^[ \t]+/, ""); print}' |
| 41 | +} |
| 42 | + |
| 43 | +# Clean up any existing LFS config |
| 44 | +echo -e "${YELLOW}Cleaning up existing LFS configurations...${NC}" |
| 45 | +git config --unset-all lfs.url |
| 46 | +git config --unset-all lfs.pushurl |
| 47 | +git config --local --unset-all lfs.url |
| 48 | +git config --local --unset-all lfs.pushurl |
| 49 | + |
| 50 | +# Backup current LFS config |
| 51 | +if [ -f .lfsconfig ]; then |
| 52 | + cp .lfsconfig .lfsconfig.backup |
| 53 | +fi |
| 54 | + |
| 55 | +# Switch to upstream config and pull missing objects |
| 56 | +echo -e "${YELLOW}Using upstream LFS configuration...${NC}" |
| 57 | +if [ -f .lfsconfig-comma ]; then |
| 58 | + cp .lfsconfig-comma .lfsconfig |
| 59 | + echo -e "${GREEN}Switched to upstream LFS configuration${NC}" |
| 60 | + |
| 61 | + if [ $FETCH_ALL -eq 1 ]; then |
| 62 | + echo -e "${YELLOW}Fetching all LFS objects (including already present)...${NC}" |
| 63 | + |
| 64 | + git lfs fetch --all || { |
| 65 | + echo -e "${RED}Failed to fetch LFS objects${NC}" |
| 66 | + if [ -f .lfsconfig.backup ]; then |
| 67 | + mv .lfsconfig.backup .lfsconfig |
| 68 | + fi |
| 69 | + exit 1 |
| 70 | + } |
| 71 | + |
| 72 | + git lfs checkout || { |
| 73 | + echo -e "${RED}Failed to checkout LFS objects${NC}" |
| 74 | + if [ -f .lfsconfig.backup ]; then |
| 75 | + mv .lfsconfig.backup .lfsconfig |
| 76 | + fi |
| 77 | + exit 1 |
| 78 | + } |
| 79 | + |
| 80 | + echo -e "${GREEN}Successfully fetched all LFS objects${NC}" |
| 81 | + else |
| 82 | + # Get list of missing LFS objects |
| 83 | + MISSING_FILES=$(get_missing_lfs_objects) |
| 84 | + |
| 85 | + if [ ! -z "$MISSING_FILES" ]; then |
| 86 | + echo -e "${YELLOW}Missing files to fetch:${NC}" |
| 87 | + echo "$MISSING_FILES" |
| 88 | + |
| 89 | + git lfs fetch || { |
| 90 | + echo -e "${RED}Failed to fetch LFS objects${NC}" |
| 91 | + if [ -f .lfsconfig.backup ]; then |
| 92 | + mv .lfsconfig.backup .lfsconfig |
| 93 | + fi |
| 94 | + exit 1 |
| 95 | + } |
| 96 | + |
| 97 | + git lfs checkout || { |
| 98 | + echo -e "${RED}Failed to checkout LFS objects${NC}" |
| 99 | + if [ -f .lfsconfig.backup ]; then |
| 100 | + mv .lfsconfig.backup .lfsconfig |
| 101 | + fi |
| 102 | + exit 1 |
| 103 | + } |
| 104 | + |
| 105 | + echo -e "${GREEN}Successfully fetched missing LFS objects${NC}" |
| 106 | + else |
| 107 | + echo -e "${GREEN}No missing LFS objects to fetch${NC}" |
| 108 | + } |
| 109 | + fi |
| 110 | +else |
| 111 | + echo -e "${RED}Warning: .lfsconfig-comma not found${NC}" |
| 112 | +fi |
| 113 | + |
| 114 | +# Restore original config |
| 115 | +if [ -f .lfsconfig.backup ]; then |
| 116 | + mv .lfsconfig.backup .lfsconfig |
| 117 | + echo -e "${GREEN}Restored original LFS configuration${NC}" |
| 118 | +else |
| 119 | + rm -f .lfsconfig |
| 120 | +fi |
| 121 | + |
| 122 | +echo -e "${GREEN}LFS sync completed successfully${NC}" |
| 123 | + |
| 124 | +# Continue with the commit |
| 125 | +exit 0 |
0 commit comments