Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122421 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 02A8C1ADA75 for ; Sun, 18 Feb 2024 00:41:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1708216908; bh=EGEmLYag5uAaOWjKmnTZvGbdyHKHDnLI4vErwjD0lGU=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=AQf0aL54Wwo9fj0rrAyk98TSxwcV3E80tMx468AhxOD9D30AG+q7Boc670Py6Jd5G W3pZF0hVPq6xAeoIs/GRdfIOAE+Ohd4d5VVMb9QG5LNzbuzy1Kdj727sNBN3mcK6uo ZKjemFPXimLks4HluzcrKeVCw3jmg9TxEOEUHaGw89lZnKnwtpB1ZDkDbNWn7ct8li BCA4FZebRybvfNmoamUe7C/gRNoVpcZMcjuhY6oe9lk0ZVrJ1VlFFyEPelcdDri9zf hYiGk9r/4kzFzz8TiOB/bWEFxU4S0Ni++YXYkplIZQIsjVspOCJ9OKSXKYvRg3yuaM oFlw1t9sNM8wA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id EDFDF182BFD for ; Sun, 18 Feb 2024 00:41:44 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS, FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-oa1-f46.google.com (mail-oa1-f46.google.com [209.85.160.46]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sat, 17 Feb 2024 16:41:42 -0800 (PST) Received: by mail-oa1-f46.google.com with SMTP id 586e51a60fabf-21e95f4ed73so255368fac.0 for ; Sat, 17 Feb 2024 16:41:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1708216899; x=1708821699; darn=lists.php.net; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=nPxVPnC1SYNNZmiWxtpR8bFXTdhSMZhx4Cb907X95XU=; b=lWOI7P/wcanTFa8+OQeZ5lHZ2pK02nTMIgpkOAt6SZ67qSUyizVSJfZXroY/Gkqr1o ja//EsIu/rjATJRQmqHPfteoDqUXR/iNmLxF9kV3REWM3MsvmKLpXoWy8FpSjuoWU4vW FSf4dflP9YiJz0gsQoiYDIi9T7lv0lSDVBVZslkSkcw0jw71MPaabcmitp3lIb3Zl58o BJafZqLtJKmMyU5VZnaHjwu5zGX5CJWJ0LGyKsbXgUALZQ5YC8jLt68Sxwu8eku6krI7 8fYW42aeNXSxhe8b5H+nUxuTLW3Fq4N/zuACRSq+f2jY5fye8/g3i2KEeZPKOf/87/5f 5aug== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708216899; x=1708821699; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nPxVPnC1SYNNZmiWxtpR8bFXTdhSMZhx4Cb907X95XU=; b=f822WUGqCqGRIjRIn/EKGL76PkSvqkkdBpizS9/uqqJtxxEgV6Bmsaodv9V3bwuIVQ kukgac/Q/4EaIbImwXY825P2fEFTw0HSw+wYEwZDbSbLqTk2EFaFOEU28MqaLYa5GiQT BNutGu6noqbdzhUHhU52AYj5QhhJgFMQ7fUCtedHC1tf0kcKofMoOpZGSk/hSfwV5Sj7 vvvVlRZk3YQIAAnMebngKKNpFzM3ok7iiFEzcNTeotc2DrpgpQgAYgWu11qPSFKsrOIA wODrnmEMdqjttGKKeyJ4DolVXRw8M/uz2WL05mjhv+emLSoLyKLZ4uqChPhqDo8Evg+t uKHw== X-Gm-Message-State: AOJu0YzHxkcoDy+N3Svjt01rbFDESyUZ3e53x7tXRLU5SV3Rwj3F4R30 6ghi9YJtWyv+XRiSfte6Yh3ik5kUO0OieQZRVsXryOhioix9TdlY3hciXczw61urMZGxYRk9f7B jHD0CMhM7sbb1Wr8l7Aj3S6rePcLPBj9qpmDO9Q== X-Google-Smtp-Source: AGHT+IGz7HHZweV4NYdsbcCd+KCOukdnDmE7iT44IyEJT3PcwgQmVLS1sHPT/qAJSyXOVY0fkrNOiumyFUEZdVmRMQ8= X-Received: by 2002:a05:6870:b6a4:b0:21e:4a2a:f05e with SMTP id cy36-20020a056870b6a400b0021e4a2af05emr8594070oab.8.1708216898734; Sat, 17 Feb 2024 16:41:38 -0800 (PST) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: In-Reply-To: Date: Sun, 18 Feb 2024 01:41:02 +0100 Message-ID: Subject: Re: [PHP-DEV] automatic formatting checks for pull requests? To: Ilija Tovilo Cc: PHP Internals List Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: divinity76@gmail.com (Hans Henrik Bergan) On Sun, 18 Feb 2024 at 00:51, Ilija Tovilo wrote: > > Hi Hans > > On Sat, Feb 17, 2024 at 3:31=E2=80=AFPM Gina P. Banyard wrote: > > > > On Saturday, 17 February 2024 at 11:24, Hans Henrik Bergan wrote: > > > > > Can we add automatic formatting checks for pull requests? > > > Made a PR: https://github.com/php/php-src/pull/13417 > > > > It would be nice to have some formatting rules to harmonize the codebas= e as it is somewhat the wild west, > > but as far as my understanding goes is that Clang format struggles to u= nderstand our codebase (namely macros) and is difficult to set-up for php-s= rc. > > Right. Consistent code style is nice, but what we have now is really > not that bad. There are a couple things I'd want if we enforce code > style: > > * Fixing the style should be easy, running a single command without > first pushing to CI. > * It should be fast too, so that I can easily run it for every commit, Yeah.. Basically clang-format-diff (not clang-format, but clang-format-dif= f) is difficult to work with, the easiest approach would be to make a full copy of the current working dir, and in that copy instruct git to reset to master and apply the diff between master and the parent working dir, and then have clang-format-diff analyze it and make its formatting suggesti= ons, -but- that will not be fast, that will be slow. Haven't found a fast way to get clang-format-diff to work with a *dirty* workingdir against master, -yet- at least. Feel free to investigate, I'm pretty sure it can be done, but don't know how to do it. > preferably even on-save in my editor. If we can make it fast, we could make some opt-in git-commit-hook to format= it (but we need to figure out the dirty-working-dir-speed thing first) > * The new code style should be applied only to newly added sections or > changed code, not entire files. Otherwise, we'll have many changes in > large files, with endless merge conflicts when merging up from lower > branches. Yeah, that's already in place :) > * The formatting tool should work for all php-src code, not just plain > C code. We don't want to be forced to refactor old macros just because > we need to add a single line to some long-standing code. Last time I > tried clang-format, it utterly failed with our macros. Seems to work with macros when I tested it :) Tested it with the following macros as of writing: ZEND_PARSE_PARAMETERS_START Z_PARAM_NUMBER ZEND_PARSE_PARAMETERS_END Z_TYPE_P Z_LVAL_P UNEXPECTED ZEND_ASSERT RETURN_DOUBLE RETURN_LONG RETURN_THROWS While .php and .phpt and whatever formatting can be implemented in the futu= re, I would prefer baby steps, once we have *.[ch] accepted, improving it with .php/whatever should be easier in the future. > > I haven't looked at your PR in detail, so I'm not sure which of these > points it satisfies. It would be great if you could quickly describe > how it works, and what the goals are. > > Essentially, I'm just sceptical that this isn't more trouble than it's wo= rth. > > Ilija Here's how it works, roughly: for pull requests: - creates a new dedicated CI job specifically to check for formatting error= s - If formatting errors are detected, the CI fails, with the last message being a command to fix the styling issue, it looks like, and I quote: ``` please run curl https://termbin.com/zdzn | git apply -v ``` running that command, followed by git commit+git push should make the CI ha= ppy, git apply will not stage/commit/push automatically however, so in practice people will have to manually do: curl https://termbin.com/zdzn | git apply -v; (optionally run a `git diff` to just check that it looks right) git commit -a --message 'formatting'; git push; - we could add the last 2 steps to the actual error message, to make all 3 steps a copypasta.. would not be opposed. More technically, it - creates a new dedicated CI job specifically to check for formatting error= s - Ubuntu 22.04, clang-format-diff, php-cli, git, netcat - creates a diff between current PR code and master branch - does *a bunch of tricks/hacks to get the environment suitable for clang-format-diff* including switching to master, applying the diff to master, commiting the diff under a fake username/email (because CI git doesn't have a username/email by default and git refuse to commit without them) - actually runs clang-format-diff against the last commit to format all the code in the last commit - generate a diff against the changes clang-format-diff just made - revert the last "fake username/email commit" - if that clang-format-diff is empty, it just prints "code formatting check succeeded :)" and the CI finishes successfully - if it's not empty, it prints, and I quote: ``` + cat ../formatted.diff diff --git a/test.c b/test.c index 6df39be994..64cfab717d 100644 --- a/test.c +++ b/test.c @@ -1,5 +1,7 @@ #include -int main(){ - printf("Hello, World!\n");return 0; +int main() +{ + printf("Hello, World!\n"); + return 0; } \ No newline at end of file + echo 'code formatting check failed :(' code formatting check failed :( ++ cat ../formatted.diff ++ nc termbin.com 9999 /home/runner/work/_temp/3131a261-eaca-4987-9f8c-1b12e4645d76.sh: line 20: warning: command substitution: ignored null byte in input + patch_url=3Dhttps://termbin.com/ul0f Please run + echo 'Please run' + echo 'curl https://termbin.com/ul0f | git apply -v' + exit 1 curl https://termbin.com/ul0f | git apply -v Error: Process completed with exit code 1. ``` and the CI run fails. I think there is room for improvement with regards to all the hacks/tricks currently done to get the environment suitable for clang-format-diff, but I also think it's already good enough for a CI (but not good enough to comfortably run locally) FWIW many real PHP projects already have something very similar called StyleCI today, where StyleCI will give a link to a patch to fix styling issues after pushing to a pull request, for example chrome-php/chrome ( https://github.com/chrome-php/chrome/ ) use it: https://styleci.io