-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_sub_tree.sh
executable file
·65 lines (55 loc) · 2.35 KB
/
update_sub_tree.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
#!/bin/bash
#list_subtrees() {
## git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2- | sort | uniq
#git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2- | sort | uniq | while read -r prefix; do
# repo_url=$(git config -f .gitmodules --get submodule.$prefix.url)
# branch=$(git config -f .gitmodules --get submodule.$prefix.branch)
# echo "$prefix $repo_url $branch"
#done
#}
list_subtrees() {
# List subtrees by looking for git-subtree-dir in the log and extracting prefixes
git log | grep git-subtree-dir | tr -d ' ' | cut -d ":" -f2- | sort | uniq | while read -r prefix; do
# Check if a submodule exists in .gitmodules for the current prefix
repo_url=$(git config -f .gitmodules --get submodule.$prefix.url)
branch=$(git config -f .gitmodules --get submodule.$prefix.branch)
# If repo_url or branch are not set, try to extract them from the git-subtree information
if [ -z "$repo_url" ] || [ -z "$branch" ]; then
echo "Warning: No submodule information found for prefix: $prefix"
echo "Skipping $prefix due to missing repo_url or branch"
continue
fi
# Display the details for each subtree
echo "$prefix $repo_url $branch"
done
}
list_subtrees
## List all subtrees and update each one
#list_subtrees | while read -r line; do
# # Extract the prefix and repository details
# prefix=$(echo "$line" | awk '{print $1}')
# repo_url=$(echo "$line" | awk '{print $2}')
# branch=$(echo "$line" | awk '{print $3}')
#
# # Pull updates for the current subtree
# echo "Pulling updates for subtree at $prefix from $repo_url (branch: $branch)"
# git subtree pull --prefix="$prefix" "$repo_url" "$branch"
#done
list_subtrees | while read -r line; do
# Extract the prefix and repository details
prefix=$(echo "$line" | cut -d ' ' -f1)
repo_url=$(echo "$line" | cut -d ' ' -f2)
branch=$(echo "$line" | cut -d ' ' -f3)
# Check if repo_url and branch are not empty
if [ -n "$repo_url" ] && [ -n "$branch" ]; then
# Stash any modifications in the working tree
git stash --include-untracked
# Pull updates for the current subtree
echo "Pulling updates for subtree at $prefix from $repo_url (branch: $branch)"
git subtree pull --prefix="$prefix" "$repo_url" "$branch"
# Apply stashed changes
git stash pop
else
echo "Skipping subtree at $prefix due to missing repo_url or branch"
fi
done