[php-src] PR #13414: script to format pull requests

From: Date: Sat, 17 Feb 2024 01:44:14 +0000
Subject: [php-src] PR #13414: script to format pull requests
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-25121@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/13414 Author: divinity76 a script to format pull requests. related discussion: https://github.com/php/php-src/pull/13401/files#r1492699756 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' ``` and if the pull request is not properly formatted, you should get a diff like ```diff hans@DESKTOP-EE15SLU:~/projects/php-src$ php scripts/dev/format_pull_request.php 'https://github.com/php/php-src/pull/13401.diff' --- ext/standard/basic_functions.c (before formatting) +++ ext/standard/basic_functions.c (after formatting) @@ -1128,58 +1128,58 @@ /* }}} */ /* {{{ Delay for a given number of seconds */ -PHP_FUNCTION(sleep) { - zval *num; - - ZEND_PARSE_PARAMETERS_START(1, 1) - Z_PARAM_NUMBER(num) - ZEND_PARSE_PARAMETERS_END(); - if (Z_TYPE_P(num) == IS_DOUBLE) { - const double seconds = Z_DVAL_P(num); - if (UNEXPECTED(seconds < 0)) { - zend_argument_value_error(1, "must be greater than or equal to 0"); - RETURN_THROWS(); - } +PHP_FUNCTION(sleep) +{ + zval* num; + + ZEND_PARSE_PARAMETERS_START(1, 1) + Z_PARAM_NUMBER(num) + ZEND_PARSE_PARAMETERS_END(); + if (Z_TYPE_P(num) == IS_DOUBLE) { + const double seconds = Z_DVAL_P(num); + if (UNEXPECTED(seconds < 0)) { + zend_argument_value_error(1, "must be greater than or equal to 0"); + RETURN_THROWS(); + } #ifdef HAVE_NANOSLEEP - time_t seconds_long = (time_t)seconds; - zend_long fraction_nanoseconds = (zend_long)((seconds - seconds_long) * 1000000000); - if(fraction_nanoseconds > 999999999) { - // for this to happen, you have to request to sleep for longer than - // 0.999999999 seconds and yet less than 1 second.. - // nanosleep() has a documented limit of 999999999 nanoseconds, so let's just round it up to 1 second. - // that means we'll be off by <=0.9 nanoseconds in this edge-case, probably close enough. - fraction_nanoseconds = 0; - seconds_long += 1; - } - struct timespec php_req, php_rem; - php_req.tv_sec = (time_t)seconds_long; - php_req.tv_nsec = fraction_nanoseconds; - const int result = nanosleep(&php_req, &php_rem); - if(UNEXPECTED(result == -1)) { - ZEND_ASSERT(errno != EINVAL); // this should be impossible, we carefully checked the input above - // it's probably EINTR - RETURN_DOUBLE(php_rem.tv_sec + (((double)php_rem.tv_nsec) / 1000000000.0)); - } - RETURN_LONG(0); + time_t seconds_long = (time_t)seconds; + zend_long fraction_nanoseconds = (zend_long)((seconds - seconds_long) * 1000000000); + if (fraction_nanoseconds > 999999999) { + // for this to happen, you have to request to sleep for longer than + // 0.999999999 seconds and yet less than 1 second.. + // nanosleep() has a documented limit of 999999999 nanoseconds, so let's just round it up to 1 second. + // that means we'll be off by <=0.9 nanoseconds in this edge-case, probably close enough. + fraction_nanoseconds = 0; + seconds_long += 1; + } + struct timespec php_req, php_rem; + php_req.tv_sec = (time_t)seconds_long; + php_req.tv_nsec = fraction_nanoseconds; + const int result = nanosleep(&php_req, &php_rem); + if (UNEXPECTED(result == -1)) { + ZEND_ASSERT(errno != EINVAL); // this should be impossible, we carefully checked the input above + // it's probably EINTR + RETURN_DOUBLE(php_rem.tv_sec + (((double)php_rem.tv_nsec) / 1000000000.0)); + } + RETURN_LONG(0); #elif defined(HAVE_USLEEP) - const unsigned int fraction_microseconds = - (unsigned int)((seconds - (unsigned int)seconds) * 1000000); - if (fraction_microseconds > 0) { - usleep(fraction_microseconds); - } - RETURN_LONG(php_sleep((unsigned int)seconds)); + const unsigned int fraction_microseconds = (unsigned int)((seconds - (unsigned int)seconds) * 1000000); + if (fraction_microseconds > 0) { + usleep(fraction_microseconds); + } + RETURN_LONG(php_sleep((unsigned int)seconds)); #else - // avoid -Werror=unreachable-code - RETURN_LONG(php_sleep((unsigned int)seconds)); -#endif - } - ZEND_ASSERT(Z_TYPE_P(num) == IS_LONG); // Z_PARAM_NUMBER(num) above guarantee that it's double or float or throw :) - zend_long seconds = Z_LVAL_P(num); - if (UNEXPECTED(seconds < 0)) { - zend_argument_value_error(1, "must be greater than or equal to 0"); - RETURN_THROWS(); - } - RETURN_LONG(php_sleep((unsigned int)seconds)); + // avoid -Werror=unreachable-code + RETURN_LONG(php_sleep((unsigned int)seconds)); +#endif + } + ZEND_ASSERT(Z_TYPE_P(num) == IS_LONG); // Z_PARAM_NUMBER(num) above guarantee that it's double or float or throw :) + zend_long seconds = Z_LVAL_P(num); + if (UNEXPECTED(seconds < 0)) { + zend_argument_value_error(1, "must be greater than or equal to 0"); + RETURN_THROWS(); + } + RETURN_LONG(php_sleep((unsigned int)seconds)); } /* }}} */ ``` huh look at that, seems PR #13401 isn't properly formatted, i should do something about that. This is script manual right now, but ideally it should be an automated part of CI tests, where a failed test can give a link to a diff file, so people can fix it by just running ``` curl 'link-to-diff-file' | git apply - ``` fwiw StyleCI does something similar (but doesn't support C): https://styleci.io/

« previous php.git-pulls (#25121) next »