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