forked from tcharding/kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-am-dir.sh
executable file
·188 lines (151 loc) · 3.95 KB
/
git-am-dir.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
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
#!/bin/bash
#
# git-am-dir.sh - apply patches
# Author: Tobin Harding <[email protected]>
# Copyright (c) 2017 Tobin Harding
# The skeleton for this script and all the standard
# utilities are taken from The Rust Project
# Copyright (c) 2015 The Rust Project Developers
# Permission is hereby granted, free of charge, to any
# person obtaining a copy of this software and associated
# documentation files (the "Software"), to deal in the
# Software without restriction, including without
# limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of
# the Software, and to permit persons to whom the Software
# is furnished to do so, subject to the following
# conditions:
# The above copyright notice and this permission notice
# shall be included in all copies or substantial portions
# of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
# ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
# TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
# SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
# IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
set -u # Undefined variables are errors
main() {
assert_cmds
set_globals
handle_command_line_args "$@"
apply
}
set_globals() {
# Environment sanity checks
assert_nz "$HOME" "\$HOME is undefined"
assert_nz "$0" "\$0 is undefined"
script=${0##*/} # script name
nthreads=9
patch_dir=""
}
handle_command_line_args () {
if (( $# < 1 ))
then
echo "Usage: $0 <patch-dir>"
exit 1
fi
patch_dir=$1
}
apply () {
# check we are in a git repo
ensure ls '.git' > /dev/null
# apply each patches
for file in $(ls $patch_dir)
do
# Skip archives, apply, and old versions.
if [ -d "$patch_dir/$file" ]; then
continue
fi
sub=${file:0:4} # 0000-foo-bar.patch
subv=${file:3:4} # v4-0000-foo-bar.patch
if [ $sub == "0000" ] || [ $subv == "0000" ]; then
# echo "found cover-letter: $patch"
continue
fi
_patch="$patch_dir/$file"
ensure git apply --check $_patch
ensure git am -3 < $_patch
done
echo ""
echo "All patches applied and built successfully!"
}
assert_success() {
err_msg="$1"
if (( $? != 0 ))
then
echo $err_msg
exit 1
fi
}
# Standard utilities
#
# Taken from: https://github.com/rust-lang/rustup.sh
say() {
echo "$script: $1"
}
say_err() {
say "$1" >&2
}
verbose_say() {
if [ "$flag_verbose" = true ]; then
say "$1"
fi
}
err() {
say "$1" >&2
exit 1
}
need_cmd() {
if ! command -v "$1" > /dev/null 2>&1
then err "need '$1' (command not found)"
fi
}
need_ok() {
if [ $? != 0 ]; then err "$1"; fi
}
assert_nz() {
if [ -z "$1" ]; then err "assert_nz $2"; fi
}
# Run a command that should never fail. If the command fails execution
# will immediately terminate with an error showing the failing
# command.
ensure() {
"$@"
need_ok "command failed: $*"
}
# This is just for indicating that commands' results are being
# intentionally ignored. Usually, because it's being executed
# as part of error handling.
ignore() {
run "$@"
}
# Runs a command and prints it to stderr if it fails.
run() {
"$@"
local _retval=$?
if [ $_retval != 0 ]; then
say_err "command failed: $*"
fi
return $_retval
}
# Prints the absolute path of a directory to stdout
abs_path() {
local _path="$1"
# Unset CDPATH because it causes havok: it makes the destination unpredictable
# and triggers 'cd' to print the path to stdout. Route `cd`'s output to /dev/null
# for good measure.
(unset CDPATH && cd "$_path" > /dev/null && pwd)
}
assert_cmds() {
need_cmd ls
need_cmd git
need_cmd make
}
#
# Run main
#
main "$@"