Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Bug 1384791 - Fix clang-format script, r=franziskus
--HG--
extra : rebase_source : 23415a599f9ca10b599ab141986df15ac70efe9d
extra : amend_source : bc54132f9cdf01288338c9c9315799ad7f7174b9
  • Loading branch information
martinthomson committed Jul 27, 2017
1 parent bb3f9ca commit dceafaa
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
21 changes: 11 additions & 10 deletions automation/clang-format/run_clang_format.sh
Expand Up @@ -6,6 +6,8 @@ if [[ $(id -u) -eq 0 ]]; then
exec su worker -c "$0 $*"
fi

set -e

# Apply clang-format on the provided folder and verify that this doesn't change any file.
# If any file differs after formatting, the script eventually exits with 1.
# Any differences between formatted and unformatted files is printed to stdout to give a hint what's wrong.
Expand All @@ -21,17 +23,16 @@ blacklist=(
"./lib/zlib" \
"./lib/sqlite" \
"./gtests/google_test" \
"./.hg" \
"./out" \
)

top="$(dirname $0)/../.."
cd "$top"
top=$(cd "$(dirname $0)/../.."; pwd -P)

if [ $# -gt 0 ]; then
dirs=("$@")
else
dirs=($(find . -maxdepth 2 -mindepth 1 -type d ! -path . \( ! -regex '.*/' \)))
cd "$top"
dirs=($(find . -maxdepth 2 -mindepth 1 -type d ! -path '*/.*' -print))
fi

format_folder()
Expand All @@ -46,20 +47,20 @@ format_folder()
}

for dir in "${dirs[@]}"; do
if format_folder "$dir" ; then
if format_folder "$dir"; then
c="${dir//[^\/]}"
echo "formatting $dir ..."
depth=""
depth=()
if [ "${#c}" == "1" ]; then
depth="-maxdepth 1"
depth+=(-maxdepth 1)
fi
find "$dir" $depth -type f \( -name '*.[ch]' -o -name '*.cc' \) -exec clang-format -i {} \+
find "$dir" "${depth[@]}" -type f \( -name '*.[ch]' -o -name '*.cc' \) -exec clang-format -i {} \+
fi
done

TMPFILE=$(mktemp /tmp/$(basename $0).XXXXXX)
trap 'rm $TMPFILE' exit
if (cd $(dirname $0); hg root >/dev/null 2>&1); then
trap 'rm -f $TMPFILE' exit
if [[ -d "$top/.hg" ]]; then
hg diff --git "$top" | tee $TMPFILE
else
git -C "$top" diff | tee $TMPFILE
Expand Down
12 changes: 10 additions & 2 deletions mach
Expand Up @@ -20,12 +20,16 @@ cwd = os.path.dirname(os.path.abspath(__file__))

class cfAction(argparse.Action):
docker_command = ["docker"]
restorecon = None

def __call__(self, parser, args, values, option_string=None):
if "noroot" not in values:
self.setDockerCommand()
else:
values.remove("noroot")
files = [os.path.join('/home/worker/nss',
os.path.relpath(os.path.abspath(x), start=cwd))
for x in values]

# First check if we can run docker.
try:
Expand Down Expand Up @@ -55,10 +59,12 @@ class cfAction(argparse.Action):
self.buildImage(docker_image, cf_docker_folder)

command = self.docker_command + [
'run', '-v', cwd + ':/home/worker/nss', '--rm', '-ti', docker_image
'run', '-v', cwd + ':/home/worker/nss:Z', '--rm', '-ti', docker_image
]
# The clang format script returns 1 if something's to do. We don't care.
subprocess.call(command + values)
subprocess.call(command + files)
if self.restorecon is not None:
subprocess.call([self.restorecon, '-R', cwd])

def filesChanged(self, path):
hash = sha256()
Expand Down Expand Up @@ -87,6 +93,8 @@ class cfAction(argparse.Action):

def setDockerCommand(self):
if platform.system() == "Linux":
from distutils.spawn import find_executable
self.restorecon = find_executable('restorecon')
self.docker_command = ["sudo"] + self.docker_command


Expand Down

0 comments on commit dceafaa

Please sign in to comment.