Skip to content

script to format pull requests #13414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

divinity76
Copy link
Contributor

@divinity76 divinity76 commented Feb 17, 2024

a script to format pull requests.
related conversation: https://github.com/php/php-src/pull/13401/files/d64a8ccdc1d21576827059ee86c0fa073c95ffcc#r1492699756

requirements: git, clang-format, php-cli.
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 --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c
index 320cbcc743..db68b727e5 100644
--- a/ext/standard/basic_functions.c
+++ b/ext/standard/basic_functions.c
@@ -1128,58 +1128,58 @@ PHP_FUNCTION(flush)
 /* }}} */
 
 /* {{{ 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));
+		// 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));
+	}
+	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 script is manual right now, but ideally it should be an automated part of a CI test, 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 does not support C): https://styleci.io/

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
@divinity76 divinity76 marked this pull request as draft February 17, 2024 02:10
... that was a lot harder than it should have been x.x
@divinity76 divinity76 marked this pull request as ready for review February 17, 2024 02:44
divinity76 added a commit to divinity76/php-src that referenced this pull request Feb 17, 2024
formatted with the script from php#13414
@divinity76
Copy link
Contributor Author

divinity76 commented Feb 17, 2024

have a better idea: #13417

@divinity76 divinity76 closed this Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant