-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload-remove-git-submodules.sh
executable file
·71 lines (59 loc) · 2.12 KB
/
load-remove-git-submodules.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
#!/bin/bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 [c|r]"
exit 1
fi
# Assign the arguments to variables
ACTION=$1
# Define the submodule URL
SUBMODULE_URL="https://github.com/jabrena/java-cursor-rules"
SUBMODULE_PATH="java-cursor-rules"
# Perform the action based on the first argument
case $ACTION in
c)
# Clean up any leftover .git files from previous attempts
if [ -d "$SUBMODULE_PATH/.git" ] || [ -f "$SUBMODULE_PATH/.git" ]; then
rm -rf "$SUBMODULE_PATH"
fi
# Clean up any previous entries in .git/modules
if [ -d ".git/modules/$SUBMODULE_PATH" ]; then
rm -rf ".git/modules/$SUBMODULE_PATH"
fi
# Add the submodule with force option
git submodule add --force $SUBMODULE_URL $SUBMODULE_PATH
;;
r)
# Deinitialize the submodule
git submodule deinit -f $SUBMODULE_PATH
# Remove the submodule directory
rm -rf $SUBMODULE_PATH
# Remove from git
git rm -f $SUBMODULE_PATH
# Clean up .git/modules
if [ -d ".git/modules/$SUBMODULE_PATH" ]; then
rm -rf ".git/modules/$SUBMODULE_PATH"
fi
# Remove the corresponding section from .gitmodules
if [ -f .gitmodules ]; then
# Use sed in a way compatible with both GNU and BSD (macOS) sed
if [[ "$OSTYPE" == "darwin"* ]]; then
# macOS requires an extension argument with -i
sed -i '' "/\\[submodule \"$SUBMODULE_PATH\"\\]/,/^$/d" .gitmodules
else
# Linux version
sed -i "/\\[submodule \"$SUBMODULE_PATH\"\\]/,/^$/d" .gitmodules
fi
# If .gitmodules is empty after the removal, delete it
if [ ! -s .gitmodules ]; then
rm .gitmodules
git add .gitmodules
fi
fi
;;
*)
echo "Invalid action: Use 'c' to create or 'r' to remove."
exit 1
;;
esac
echo "Action completed successfully."