forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_tool.sh
More file actions
executable file
·147 lines (132 loc) · 4.37 KB
/
Copy pathgit_tool.sh
File metadata and controls
executable file
·147 lines (132 loc) · 4.37 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/bin/bash
echo "========================================"
echo "[Menu] Git 操作選擇"
echo "========================================"
echo "[0] Initialize remote & submodules (already cloned)"
echo "[1] Initialize submodules (with check)"
echo "[2] Pull updates from upstream (main + submodules)"
echo "[3] Push local changes to GitHub (main + submodules)"
echo "[X] Exit"
echo "========================================"
read -p "Enter your choice: " choice
function submodule_init_line() {
subPath="$1"
originURL="$2"
upstreamURL="$3"
existing_url=$(git config -f .gitmodules --get submodule."$subPath".url)
if [ -z "$existing_url" ]; then
echo "[INFO] Adding submodule: $subPath"
git submodule add "$originURL" "$subPath"
else
echo "[INFO] Submodule $subPath already exists. Skipping."
fi
}
case "$choice" in
0)
echo "[INFO] Checking upstream remote..."
git remote get-url upstream >/dev/null 2>&1
if [ $? -ne 0 ]; then
read -p "Enter upstream URL for main repo: " upstreamURL
git remote add upstream "$upstreamURL"
else
echo "[INFO] Upstream already exists. Skipping."
fi
if [ -f "submodules_config.txt" ]; then
echo "[INFO] Initializing submodules..."
while read -r line; do
set -- $line
submodule_init_line "$1" "$2" "$3"
done < submodules_config.txt
git submodule init
git submodule update --remote --merge
else
echo "[INFO] No submodules_config.txt found. Skipping."
fi
;;
1)
if [ -f ".gitmodules" ]; then
read -p "[INFO] Submodules already exist. Re-initialize? (Y/N): " redoInit
if [[ ! "$redoInit" =~ ^[Yy]$ ]]; then
echo "[INFO] Submodule init canceled."
exit 0
fi
fi
if [ -f "submodules_config.txt" ]; then
echo "[INFO] Initializing submodules from submodules_config.txt..."
while read -r line; do
set -- $line
submodule_init_line "$1" "$2" "$3"
done < submodules_config.txt
git submodule init
git submodule update --remote --merge
else
echo "[INFO] No submodules_config.txt found. Skipping."
fi
;;
2)
echo "[INFO] Pulling from upstream..."
git remote get-url upstream >/dev/null 2>&1
if [ $? -ne 0 ]; then
read -p "Enter upstream URL for main repo: " upstreamURL
git remote add upstream "$upstreamURL"
fi
git fetch upstream
git pull upstream main --allow-unrelated-histories
if [ -f "submodules_config.txt" ]; then
while read -r line; do
set -- $line
subPath="$1"
originURL="$2"
upstreamURL="$3"
if [ -d "$subPath" ]; then
cd "$subPath"
git remote set-url origin "$originURL"
git remote get-url upstream >/dev/null 2>&1
if [ $? -ne 0 ]; then
git remote add upstream "$upstreamURL"
else
git remote set-url upstream "$upstreamURL"
fi
git checkout main
git fetch upstream
git pull upstream main
cd ..
fi
done < submodules_config.txt
fi
;;
3)
read -p "Enter commit message (default: 更新): " commitMsg
[ -z "$commitMsg" ] && commitMsg="更新"
timestamp=$(date "+%Y-%m-%d_%H-%M")
echo "Commit Log - $timestamp" > commit_log.txt
echo "--------------------------" >> commit_log.txt
if [ -f "submodules_config.txt" ]; then
echo "[INFO] Pushing submodules..."
while read -r line; do
set -- $line
subPath="$1"
if [ -d "$subPath" ]; then
cd "$subPath"
git add .
git commit -m "$commitMsg - $timestamp" 2>/dev/null
git push origin main
echo "[submodule] $subPath committed: $commitMsg - $timestamp" >> ../commit_log.txt
cd ..
fi
done < submodules_config.txt
fi
echo "[INFO] Committing main repo"
git add .
git commit -m "$commitMsg - $timestamp" 2>/dev/null
git push origin main
echo "[main] Main repo committed: $commitMsg - $timestamp" >> commit_log.txt
;;
X|x)
echo "👋 Exit"
exit 0
;;
*)
echo "❌ Invalid choice"
;;
esac