-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommon.sh
More file actions
188 lines (141 loc) · 3.03 KB
/
common.sh
File metadata and controls
188 lines (141 loc) · 3.03 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
ANALOG_NVIDIA_REMOTE_URL="https://github.com/analogdevicesinc/nvidia.git"
push_dir() {
pushd "$@" > /dev/null
}
pop_dir() {
popd "$@" > /dev/null
}
starts_with() { case $2 in "$1"*) true;; *) false;; esac; }
rel_path() {
TO_PATH="$1"
FROM_PATH="$2"
realpath --relative-to="$TO_PATH" "$FROM_PATH"
}
find_repos() {
find "$1" -type d -name ".git" | xargs dirname | sort
}
find_patches() {
find "$1" -type f -name "*.patch" | sort
}
find_patch_dirs() {
find_patches "$1" | xargs dirname | sort | uniq
}
count_lines() {
echo "$1" | wc -l
}
encode_path() {
STR="$1"
if echo "$STR" | grep -q "__"; then
echo "FIXME: Handle __ in path name"
exit 1
fi
if echo "$STR" | grep -q "#"; then
echo "FIXME: Handle # in path name"
exit 1
fi
STR=$(echo "$STR" | sed 's/_/__/g')
STR=$(echo "$STR" | tr '/' '_')
echo "$STR"
}
decode_path() {
STR="$1"
STR=$(echo "$STR" | sed 's/__/#/g')
STR=$(echo "$STR" | tr '_' '/')
STR=$(echo "$STR" | tr '#' '_')
echo "$STR"
}
extract_refs() {
URL="$1"
git ls-remote --refs "$URL" | cut -f2 | sed -e 's#^refs/heads/##'
}
match_refs() {
REFS="$1"
PROJECT="$2"
BASE_REF="$3"
echo "$REFS" | grep -E "^$PROJECT/$BASE_REF"
}
extract_path() {
REF="$1"
PROJECT="$2"
BASE_REF="$3"
echo "$REF" | sed -e "s#^$PROJECT/$BASE_REF/##"
}
add_remote() {
REMOTE_NAME="$1"
REMOTE_URL="$2"
EXISTING_REMOTE_URL=$(git config "remote.$REMOTE_NAME.url")
if [[ -n "$EXISTING_REMOTE_URL" ]]; then
if [[ "$EXISTING_REMOTE_URL" != "$REMOTE_URL" ]]; then
echo "Remote $REMOTE_NAME exists but URL is different"
exit 1
fi
else
git remote add "$REMOTE_NAME" "$REMOTE_URL"
fi
}
fetch_remote_ref() {
REMOTE_NAME="$1"
BRANCH="$2"
git fetch -q "$REMOTE_NAME" "$BRANCH" > /dev/null 2>&1
return $?
}
push_head_to_branch() {
REMOTE_NAME="$1"
BRANCH_NAME="$2"
FORCE="$3"
if [[ -n "$FORCE" ]]; then
FORCE_ARG="--force"
fi
git push $FORCE_ARG "$REMOTE_NAME" HEAD:"$BRANCH_NAME"
}
reset_to_fetched_ref() {
git reset -q --hard FETCH_HEAD
}
init_repo() {
REPO_PATH="$1"
if [[ ! -d "$REPO_PATH" ]]; then
mkdir -p "$REPO_PATH"
git init -q -b master "$REPO_PATH"
fi
}
count_patches() {
RANGE="$1"
git rev-list --count "$RANGE"
}
format_patches_to_output() {
RANGE="$1"
ROOT="$2"
OUTPUT_PATH="$3"
if [[ -n "$ROOT" ]]; then
ROOT_ARG="--root"
fi
git format-patch --output-directory "$OUTPUT_PATH" $ROOT_ARG "$RANGE"
}
get_root_commit() {
git rev-list --max-parents=0 HEAD
}
apply_patch() {
PATCH="$1"
git am --empty=keep "$PATCH"
}
compose_branch_name() {
PROJECT="$1"
TAG_NAME="$2"
REPO_PATH_REL="$3"
ENCODED_PATH=$(encode_path "$REPO_PATH_REL")
echo "$PROJECT/$TAG_NAME/$ENCODED_PATH"
}
find_monorepo_paths() {
BASE_PATH="$1"
REMOTE_NAME="$2"
PROJECT="$3"
TAG_NAME="$4"
REPO_REFS=$(extract_refs "$REMOTE_NAME")
MATCHING_BRANCHES=$(match_refs "$REPO_REFS" "$PROJECT" "$TAG_NAME")
while read -r BRANCH
do
ENCODED_PATH=$(extract_path "$BRANCH" "$PROJECT" "$TAG_NAME")
REPO_PATH_REL=$(decode_path "$ENCODED_PATH")
echo "$BASE_PATH/$REPO_PATH_REL"
done <<< "$MATCHING_BRANCHES"
}