-
Notifications
You must be signed in to change notification settings - Fork 9
/
moby-components
executable file
·196 lines (169 loc) · 4.41 KB
/
moby-components
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/bash
set -e
cmd_usage() {
cat <<EOF
Usage: $(basename $0) <command>
Tool to manage multiple upstream git repositories in one big repository.
Commands:
list
fetch <component> | --all
update <component> | --all
EOF
}
cmd_list() {
for component in $(list_components); do
echo -n "$component "
dir="$REPOROOT/components/$component"
[ ! -d "$dir" ] && echo "not-merged" && continue
latestCommit=$(get_latest_commit "$dir")
mergeCommit=$(get_merge_commit "$component" master)
upstreamCommit=$(get_upstream_commit "$mergeCommit")
echo "latest:$latestCommit merge:$mergeCommit upstream:$upstreamCommit"
done
}
get_components() {
[ -z "$1" ] && cmd_usage >&2 && exit 1
case "$1" in
--help)
cmd_usage
exit 0
;;
--all)
list_components
;;
*)
echo "$1"
;;
esac
}
cmd_fetch() {
components=$(get_components $*)
fetch_all_source $components
}
newer_git_version() {
needed_major_version="2"
needed_minor_version="9"
actual_version="$(git --version | awk '{print $3}')"
actual_version=${actual_version//./ }
actual_major_version="$(echo "$actual_version" | awk '{print $1}')"
actual_minor_version="$(echo "$actual_version" | awk '{print $2}')"
if [ "$actual_major_version" -lt "$needed_major_version" ]; then
return 1
fi
if [ "$actual_minor_version" -lt "$needed_minor_version" ]; then
return 1
fi
}
cmd_update() {
components=$(get_components $*)
fetch_all_source $components
echo "Updating: $components"
for component in $components; do
update_cache "$component"
url=$(git config --get component.$component.url)
branch=$(git config --get component.$component.branch)
# if you change the wording make sure you update the grep in get_merge_commits
allow_unrelated_histories=""
if newer_git_version; then
allow_unrelated_histories="--allow-unrelated-histories"
fi
git merge -m "Merge component '$component' from $url $branch" $allow_unrelated_histories "refs/components/$component/HEAD"
done
}
get_latest_commit() {
dir="$1"
git log --format=%H -1 "$dir"
}
get_merge_commit() {
component="$1"
branch="$2"
git log --merges -1 --format=%H --grep "Merge component '$component' from " "$branch"
}
get_upstream_commit() {
mergeCommit="$1"
# get the second parent of mergecommit
upstreamCommits=$(git rev-list --merges --parents "$mergeCommit" -1 --min-parents=2)
upstreamCommit=$(echo "$upstreamCommits" | cut -d' ' -f3-)
[[ "$upstreamCommit" != "${upstreamCommit/ /}" ]] && echo "Merge commit with more than 2 parents detected: $upstreamCommits" && return 1
echo "$upstreamCommit"
}
list_components() {
git config --name-only --get-regexp 'component\.[^.]*\.url' |
sed -e 's/^component\.//' -e 's/\.url$//'
}
fetch_source() {
component="$1"
url=$(git config --get component.$component.url)
branch=$(git config --get component.$component.branch)
echo "[$component] fetching $url $branch"
git fetch $url $branch:refs/components/$component/FETCH_HEAD
}
fetch_all_source() {
components="$*"
echo "Fetching: $components"
for component in $components; do
fetch_source "$component"
done
}
get_mountpoint() {
echo "components/$1"
}
update_cache() {
component="$1"
src=refs/components/$component/FETCH_HEAD
tmp=refs/components/$component/tmp/$RANDOM
cache=refs/components/$component/HEAD
# create $tmp ref starting at same commit as $src ref
git update-ref $tmp $(git show-ref --verify -s $src)
git show-ref --verify $tmp
mountpoint=$(get_mountpoint "$component")
transform_branch $tmp $mountpoint $component
echo "[$component] moving $tmp to $cache"
git update-ref $cache $(git show-ref --verify -s $tmp)
git update-ref -d $tmp
git update-ref -d refs/original/$tmp
git show-ref --verify $cache
}
transform_branch() {
branch=$1
mountpoint=$2
component=$3
if [ -z "$mountpoint" ]; then
return
fi
git filter-branch -f --index-filter "
git ls-files -s | sed 's, , $mountpoint/,' |
GIT_INDEX_FILE=\$GIT_INDEX_FILE.new git update-index --index-info &&
if test -f \"\$GIT_INDEX_FILE.new\"; then
mv \$GIT_INDEX_FILE.new \$GIT_INDEX_FILE;
fi
" --msg-filter "
sed '\${/^\$/d;p;}' | sed '\$d'
echo Upstream-commit: \$GIT_COMMIT
echo Component: $component
" $branch
}
export GIT_CONFIG=`pwd`/components.conf
# Change the working directory to the root of the git repo
REPOROOT=$(git rev-parse --show-toplevel)
cd $REPOROOT
case "$1" in
list)
cmd_list
;;
fetch)
shift
cmd_fetch $*
;;
update)
shift
cmd_update $*
;;
help)
cmd_usage
;;
*)
cmd_usage >&2
exit 1
;;
esac