Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
/.yarn/releases/* binary
/.yarn/plugins/**/* binary
/.pnp.* binary linguist-generated
/.githooks/* text eol=lf
/.github/scripts/*.sh text eol=lf
18 changes: 18 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/sh

set -eu

cd "$(git rev-parse --show-toplevel)"

if git diff --cached --quiet -- readme.txt changelog.txt; then
exit 0
fi

if ! git diff --quiet -- changelog.txt; then
echo "Stage or restore changes in changelog.txt before committing." >&2
exit 1
fi

php_binary=$(git config --get hcaptcha.phpPath || echo php)
"$php_binary" .github/scripts/update-changelog.php --staged
git add -- changelog.txt
9 changes: 0 additions & 9 deletions .githooks/pre-push

This file was deleted.

35 changes: 35 additions & 0 deletions .githooks/setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Install repository hooks for local checkouts and worktrees.
*
* @package HCaptcha
*/

$root = dirname( __DIR__ );

// A worktree has a .git file instead of a directory.
if ( ! file_exists( $root . '/.git' ) ) {
echo "Skipping Git hooks outside a Git checkout.\n";
exit( 0 );
}

chdir( $root );

exec( 'git config --local core.hooksPath .githooks', $output, $exit_code );

if ( 0 !== $exit_code ) {
exit( $exit_code );
}

exec( 'git config --local hcaptcha.phpPath ' . escapeshellarg( str_replace( '\\', '/', PHP_BINARY ) ), $output, $exit_code );

if ( 0 !== $exit_code ) {
exit( $exit_code );
}

if ( '\\' !== DIRECTORY_SEPARATOR && ! chmod( $root . '/.githooks/pre-commit', 0755 ) ) {
fwrite( STDERR, "Could not make the pre-commit hook executable.\n" );
exit( 1 );
}

echo "Git hooks installed.\n";
6 changes: 4 additions & 2 deletions .githooks/setup.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/bin/bash
#!/bin/sh

git config core.hooksPath .githooks
set -eu

php "$(dirname "$0")/setup.php"
118 changes: 118 additions & 0 deletions .github/scripts/update-changelog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php
/**
* Generate changelog.txt from the latest readme.txt release notes.
*
* @package HCaptcha
*/

/**
* Replace the latest release section while preserving the archived history.
*
* @param string $readme Readme content.
* @param string $changelog Changelog content.
*
* @return string
* @throws RuntimeException When the readme has no release notes.
*/
function hcaptcha_generate_changelog( string $readme, string $changelog ): string {
$readme = str_replace( [ "\r\n", "\r" ], "\n", $readme );
$changelog = str_replace( [ "\r\n", "\r" ], "\n", $changelog );

if ( ! preg_match( '/^== Changelog ==[ \t]*$\n(.*?)(?=^== [^=\n]+ ==[ \t]*$|\z)/ms', $readme, $section ) ) {
throw new RuntimeException( 'No Changelog section found in readme.txt.' );
}

$heading = '^= ([0-9]+\.[0-9]+\.[0-9]+(?:-[^\s=]+)?) =[ \t]*$';

if ( ! preg_match( '/' . $heading . '\n(.*?)(?=^= [^=\n]+ =[ \t]*$|^\[See changelog for all versions\]|\z)/ms', $section[1], $release ) ) {
throw new RuntimeException( 'No release notes found in readme.txt.' );
}

$history = preg_replace(
'/^= ' . preg_quote( $release[1], '/' ) . ' =[ \t]*$\n.*?(?=^= [^=\n]+ =[ \t]*$|\z)/ms',
'',
$changelog
);

return rtrim( '= ' . $release[1] . " =\n" . trim( $release[2] ) . "\n\n" . ltrim( $history, "\n" ), "\n" ) . "\n";
}

/**
* Read a file from the Git index, including partially staged changes.
*
* @param string $file Tracked file name.
*
* @return string
* @throws RuntimeException When the file cannot be read.
*/
function hcaptcha_read_staged_changelog_file( string $file ): string {
exec( 'git show ' . escapeshellarg( ':' . $file ), $lines, $exit_code );

if ( 0 !== $exit_code ) {
throw new RuntimeException( 'Could not read the staged ' . $file . '.' );
}

return implode( "\n", $lines ) . "\n";
}

/**
* Run the generator or check that the committed changelog is current.
*
* @param string[] $arguments Command arguments.
*
* @return int
*/
function hcaptcha_update_changelog( array $arguments ): int {
if ( array_diff( $arguments, [ '--check', '--staged', '--generate-only' ] ) ) {
fwrite( STDERR, "Usage: php .github/scripts/update-changelog.php [--check|--staged]\n" );

return 1;
}

$root = dirname( __DIR__, 2 );
chdir( $root );

try {
if ( in_array( '--staged', $arguments, true ) ) {
$readme = hcaptcha_read_staged_changelog_file( 'readme.txt' );
$changelog = hcaptcha_read_staged_changelog_file( 'changelog.txt' );
} else {
$readme = file_get_contents( $root . '/readme.txt' );
$changelog = file_get_contents( $root . '/changelog.txt' );
}

if ( false === $readme || false === $changelog ) {
throw new RuntimeException( 'Could not read readme.txt or changelog.txt.' );
}

$generated = hcaptcha_generate_changelog( $readme, $changelog );

if ( in_array( '--check', $arguments, true ) ) {
if ( $generated !== str_replace( [ "\r\n", "\r" ], "\n", $changelog ) ) {
fwrite( STDERR, "changelog.txt is out of date. Run composer changelog and commit the result.\n" );

return 1;
}

echo "Changelog is up to date.\n";

return 0;
}

if ( false === file_put_contents( $root . '/changelog.txt', $generated ) ) {
throw new RuntimeException( 'Could not write changelog.txt.' );
}
} catch ( RuntimeException $exception ) {
fwrite( STDERR, $exception->getMessage() . "\n" );

return 1;
}

echo "Changelog generated from readme.txt.\n";

return 0;
}

if ( isset( $argv[0] ) && realpath( $argv[0] ) === __FILE__ ) {
exit( hcaptcha_update_changelog( array_slice( $argv, 1 ) ) );
}
56 changes: 4 additions & 52 deletions .github/scripts/update-changelog.sh
Original file line number Diff line number Diff line change
@@ -1,54 +1,6 @@
#!/bin/bash
#!/bin/sh

# Check if readme.txt exists
if [ ! -f readme.txt ]; then
echo "readme.txt file not found!"
exit 1
fi
set -eu

# Determine the latest changelog version in readme.txt
latest_version=$(sed -n 's/^= \([0-9]\+\.[0-9]\+\.[0-9]\+\) =$/\1/p' readme.txt | head -n 1)

if [ -z "$latest_version" ]; then
echo "No latest version found in readme.txt!"
exit 0
fi

# Get the latest changelog section from readme.txt
latest_section=$(sed -n "/^= $latest_version =$/,/^= [0-9]\+\.[0-9]\+\.[0-9]\+ =$/ { /^= [0-9]\+\.[0-9]\+\.[0-9]\+ =$/!p; }" readme.txt)
latest_section="= ${latest_version} =\n${latest_section}\n"

# Remove the latest changelog section from changelog.txt
sed -i "/^= $latest_version =$/,/^= [0-9]\+\.[0-9]\+\.[0-9]\+ =$/ {/^= $latest_version =$/d; /^= [0-9]\+\.[0-9]\+\.[0-9]\+ =$/!d;}" changelog.txt

# Append the latest changelog section from readme.txt to changelog.txt
{ echo -e "$latest_section"; cat changelog.txt; } > temp.txt && mv temp.txt changelog.txt

# Generate the file without changing Git configuration, committing, or pushing.
if [ "${1:-}" = "--generate-only" ]; then
echo "Changelog generated from readme.txt."
exit 0
fi

# Check if there are changes in changelog.txt
if git diff --quiet changelog.txt; then
echo "No changes in changelog.txt."
exit 0
fi

# Configure Git commit information
git config --global user.email "gha@hcaptcha.com"
git config --global user.name "hCaptcha GHA"

# Add, commit, and push changes
git add changelog.txt
git commit -m "Update changelog from readme.txt"

# Check if running in CI environment
if [ -n "$CI" ]; then
# Push changes if in CI environment
git push
echo "Changelog successfully updated and pushed to the repository."
else
echo "Changelog updated locally. Skipping push."
fi
php_binary=$(git config --get hcaptcha.phpPath || echo php)
"$php_binary" "$(dirname "$0")/update-changelog.php" "$@"
Loading