From a068b4c81846fbbef2a8d5831ec471e384849508 Mon Sep 17 00:00:00 2001 From: hanshenrik Date: Sat, 17 Feb 2024 02:20:34 +0100 Subject: [PATCH 1/3] script to format pull requests usage: make sure your git working dir is clean (like "git reset --hard" clean) and run php scripts/dev/format_pull_request.php 'https://github.com/php/php-src/pull/13401.diff' > formatted.diff --- scripts/dev/format_pull_request.php | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 scripts/dev/format_pull_request.php diff --git a/scripts/dev/format_pull_request.php b/scripts/dev/format_pull_request.php new file mode 100644 index 0000000000000..54d7c14af888c --- /dev/null +++ b/scripts/dev/format_pull_request.php @@ -0,0 +1,68 @@ + 'WebKit', + 'UseTab' => 'ForIndentation', + // clang-format's support for tabs isn't great, and we have to say "4 spaces" to get 1 tab + 'IndentWidth' => 4, + 'TabWidth' => 4, +); + +$cmd = array( + 'git diff -U0 --no-color --relative |', + 'clang-format-diff -p1 -style=' . escapeshellarg(json_encode($clangFormatOptions)), + '-iregex=' . escapeshellarg('.*\.(c|cc|cpp|c\+\+|cxx|h|hh|hpp|h\+\+|h)$'), +); +$cmd = implode(' ', $cmd); +passthru($cmd, $ret); +if($ret !== 0){ + fwrite(STDERR, "Error: clang-format-diff failed with exit code {$ret}\n"); + fwrite(STDERR, "cmd:\n{$cmd}\n"); + //passthru("git reset --hard"); + exit(1); +} +shell_exec("git reset --hard"); From 649e2617505ee9cb866aa603e419b7115be6486c Mon Sep 17 00:00:00 2001 From: hanshenrik Date: Sat, 17 Feb 2024 02:37:07 +0100 Subject: [PATCH 2/3] forgot to remove debugging --- scripts/dev/format_pull_request.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/dev/format_pull_request.php b/scripts/dev/format_pull_request.php index 54d7c14af888c..682fc9f6376ec 100644 --- a/scripts/dev/format_pull_request.php +++ b/scripts/dev/format_pull_request.php @@ -62,7 +62,7 @@ if($ret !== 0){ fwrite(STDERR, "Error: clang-format-diff failed with exit code {$ret}\n"); fwrite(STDERR, "cmd:\n{$cmd}\n"); - //passthru("git reset --hard"); + passthru("git reset --hard"); exit(1); } shell_exec("git reset --hard"); From 406669a9fa819c7d64e6ee673b0d42512952aed2 Mon Sep 17 00:00:00 2001 From: hanshenrik Date: Sat, 17 Feb 2024 02:55:29 +0100 Subject: [PATCH 3/3] make sure git-apply-compatible prefix is present ... that was a lot harder than it should have been x.x --- scripts/dev/format_pull_request.php | 44 ++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/scripts/dev/format_pull_request.php b/scripts/dev/format_pull_request.php index 682fc9f6376ec..7b2e23e5ceb79 100644 --- a/scripts/dev/format_pull_request.php +++ b/scripts/dev/format_pull_request.php @@ -1,8 +1,9 @@ 'WebKit', 'UseTab' => 'ForIndentation', // clang-format's support for tabs isn't great, and we have to say "4 spaces" to get 1 tab - 'IndentWidth' => 4, + 'IndentWidth' => 4, 'TabWidth' => 4, ); - $cmd = array( - 'git diff -U0 --no-color --relative |', - 'clang-format-diff -p1 -style=' . escapeshellarg(json_encode($clangFormatOptions)), + 'git diff -U0 --no-color --relative HEAD^ |', + 'clang-format-diff -i -p1 -style=' . escapeshellarg(json_encode($clangFormatOptions)), '-iregex=' . escapeshellarg('.*\.(c|cc|cpp|c\+\+|cxx|h|hh|hpp|h\+\+|h)$'), ); $cmd = implode(' ', $cmd); passthru($cmd, $ret); -if($ret !== 0){ +if ($ret !== 0) { fwrite(STDERR, "Error: clang-format-diff failed with exit code {$ret}\n"); fwrite(STDERR, "cmd:\n{$cmd}\n"); - passthru("git reset --hard"); + if (!$force) { + // remove last commit and reset + passthru('git reset --hard HEAD^ --quiet'); + } exit(1); } -shell_exec("git reset --hard"); +passthru('git diff'); +if (!$force) { + // remove last commit and reset + passthru('git reset --hard HEAD^ --quiet'); +}