-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit-binaries.sh
More file actions
executable file
·217 lines (177 loc) · 6.02 KB
/
Copy pathcommit-binaries.sh
File metadata and controls
executable file
·217 lines (177 loc) · 6.02 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/bin/bash
set -e
set -u
set -o pipefail
SRC=$(dirname "$(readlink -e "$0")")
function usage {
cat <<DELIM__
usage: $(basename "$0") [options]
$ $(basename "$0") --from-repo=<repo root directory> --to-repo=<repo root directory> --to-project=<project sub-path>
Options:
--from-repo Absolute path to the source repo
--from-projects (OPTIONAL) space-separated list of relative source projects. Defaults to all
--from-remotes (OPTIONAL) space-separated list of remotes to fetch the version information from. Defaults to "baylibre"
--to-repo Absolute path to the destination repo
--to-project Relative path in the destination repo where git commit is ran
--title-prefix (OPTIONAL) commit message title prefix. Defaults to "generic"
--dry-run (OPTIONAL) don't commit, pass --dry-run to git instead
--help (OPTIONAL) display usage
Examples:
$ $(basename "$0") --from-repo=/home/user/src/android-common-kernel --from-projects='common hikey-modules' \\
--to-repo=/home/user/src/aosp --to-project=device/amlogic/yukawa-kernel
DELIM__
}
function error {
local error="$1"
printf "\033[0;31mERROR:\033[0m ${error}\n\n"
}
function error_usage_exit {
error "$1"
usage
exit 1
}
function find_path {
local path="$1"
local real_path=""
if [ -e "${path}" ]; then
real_path=$(readlink -e "${path}")
fi
echo "${real_path}"
}
function check_local_changes {
local repo_path="$1" && shift
local projects=("$@")
for project in "${projects[@]}"; do
pushd "${repo_path}/${project}"
# always run status before to trigger an index rebuild
# This is important when many files have a different mtime
# see: https://github.com/MestreLion/git-tools/issues/38#issuecomment-894182421
git status
if ! git diff --quiet HEAD; then
error_exit "Local changes detected in: ${project}"
fi
popd
done
}
function all_projects_for_repo {
local repo_path=$1
local projects=$(repo --color=never list --path-only)
echo $projects
}
function display_center_msg {
local line_length="$1"
local msg="$2"
local length=${#msg}
local pad=$(((line_length - length) / 2))
printf "%0.s " $(seq 1 ${pad})
printf "${msg}"
# if line_length is an odd number, add 1 to pad
if [ $(((pad * 2) + length)) != "${line_length}" ]; then
pad=$((pad + 1))
fi
printf "%0.s " $(seq 1 ${pad})
}
function display_commit_msg_header {
local path="$1"
printf "%0.s#" {1..90}
printf "\n##"
display_center_msg 86 "COMMIT MESSAGE IN:"
printf "##\n##"
display_center_msg 86 "${path}"
printf "##\n"
printf "%0.s#" {1..90}
printf "\n\n"
}
function commit_msg_body {
local remote_name_list="$1" && shift
local from_repo=$1 && shift
local projects=("$@")
local remote_url=""
local head=""
local branch=""
# Temporarily disable strict script mode
# Because some functions might fail.
# When fails happens, we don't want the whole function to abort
# It's better to have a partial commit message than an empty one
set +e
set +u
set +o pipefail
for project in "${projects[@]}"; do
pushd "${from_repo}/${project}"
body+="- Project: ${project}:\n"
for remote_name in $remote_name_list; do
remote_url=$(git remote get-url $remote_name)
if [[ "$remote_url" != "" ]]; then
# We found the url matching the remote
# early exit the loop
break
fi
done
body+="URL: ${remote_url}\n"
branch=$(repo --color=never info . 2>&1 | perl -ne 'print "$1" if /^Manifest revision: (.*)/')
body+="Branch: ${branch}\n"
head=$(git log --oneline --no-decorate -1)
body+="HEAD: ${head}\n\n"
popd
done
set -e
set -u
set -o pipefail
echo "${body}"
}
function commit_binaries {
local from_repo=""
local from_projects=""
local from_remotes=""
local to_repo=""
local to_project=""
local title_prefix="generic"
local dry_run=false
local opts_args="from-repo:,from-projects:,from-remotes:,to-repo:,to-project:,title-prefix:,dry-run,help"
local opts=$(getopt -o '' -l "${opts_args}" -- "$@")
eval set -- "${opts}"
while true; do
case "$1" in
--from-repo) from_repo=$(find_path "$2"); shift 2 ;;
--from-projects) from_projects="$2"; shift 2;;
--from-remotes) from_remotes="$2"; shift 2;;
--to-repo) to_repo=$(find_path "$2"); shift 2 ;;
--to-project) to_project="$2"; shift 2;;
--title-prefix) title_prefix="$2"; shift 2;;
--dry-run) dry_run=true; shift ;;
--help) usage; exit 0 ;;
--) shift; break ;;
esac
done
# check arguments
! [ -d "${from_repo}" ] && error_usage_exit "invalid --from-repo: $from_repo"
! [ -d "${to_repo}" ] && error_usage_exit "invalid --to-repo: $to_repo"
! [ -d "${to_repo}/${to_project}" ] && error_usage_exit "invalid --to-project: ${to_repo}/${to_project}"
# if no projects are specified, use all of them.
if [ -z "$from_projects" ]; then
from_projects="$(all_projects_for_repo ${from_repo})"
fi
#check_local_changes "${from_repo}" $from_projects
# commits message
local commit_body=$(commit_msg_body "$from_remotes" $from_repo $from_projects)
local commit_title=""
local commit_msg=""
pushd "${to_repo}/${to_project}"
# display commit
display_commit_msg_header "${to_repo}/${to_project}"
commit_title="${title_prefix}: update binaries\n\n"
commit_msg=$(echo -e "${commit_title}${commit_body}")
echo "${commit_msg}"
if [[ "${dry_run}" == true ]]; then
git add --all
git commit --dry-run -s -m "${commit_msg}"
git restore --staged .
else
git add --all
git commit --quiet -s -m "${commit_msg}"
fi
popd
}
if [ "$0" = "$BASH_SOURCE" ]; then
commit_binaries "$@"
fi