|
1 | 1 | <?php |
| 2 | +/** |
| 3 | + * Brings a clone up to date with its remote, without ever touching work in |
| 4 | + * progress: a clone with uncommitted changes, or one on another branch than |
| 5 | + * the default one, is only fetched. Exits with an error code when git fails. |
| 6 | + */ |
2 | 7 |
|
3 | 8 | if ( ! isset( $argv[1] ) || ! is_dir( $argv[1] ) ) { |
4 | 9 | exit( "Usage: php refresh-repository.php <repository-folder>\n" ); |
5 | 10 | } |
6 | 11 |
|
7 | 12 | $repository = $argv[1]; |
8 | | -$path = realpath( getcwd() . "/{$repository}" ); |
| 13 | +$path = (string) realpath( $repository ); |
| 14 | +$git = 'git -C ' . escapeshellarg( $path ); |
9 | 15 |
|
10 | | -printf( "--- Refreshing repository \033[32m{$repository}\033[0m ---\n" ); |
| 16 | +/** |
| 17 | + * Runs a git command and returns its output, or null when it fails. |
| 18 | + * |
| 19 | + * @param string $git Git executable with its working directory. |
| 20 | + * @param string $command Git command and arguments. |
| 21 | + * |
| 22 | + * @return string|null |
| 23 | + */ |
| 24 | +function git_output( $git, $command ) { |
| 25 | + exec( "{$git} {$command} 2>/dev/null", $output, $exit_code ); |
11 | 26 |
|
12 | | -printf( "Switching to latest \033[33mdefault\033[0m branch...\n" ); |
13 | | -system( "git --git-dir={$path}/.git --work-tree={$path} checkout $(git --git-dir={$path}/.git remote show origin | grep 'HEAD branch' | cut -d' ' -f5)" ); |
| 27 | + return 0 === $exit_code ? trim( implode( "\n", $output ) ) : null; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Prints an error and exits with an error code. |
| 32 | + * |
| 33 | + * @param string $repository Repository folder. |
| 34 | + * @param string $message What went wrong. |
| 35 | + */ |
| 36 | +function fail( $repository, $message ) { |
| 37 | + fwrite( STDERR, sprintf( "Could not refresh \033[31m%s\033[0m: %s\n", $repository, $message ) ); |
| 38 | + exit( 1 ); |
| 39 | +} |
14 | 40 |
|
15 | | -printf( "Pulling latest changes...\n" ); |
16 | | -system( "git --git-dir={$path}/.git --work-tree={$path} pull" ); |
| 41 | +printf( "--- Refreshing repository \033[32m%s\033[0m ---\n", $repository ); |
| 42 | + |
| 43 | +// Git looks for a repository in parent folders too, so a folder that is not a |
| 44 | +// clone would otherwise refresh the repository around it. |
| 45 | +$toplevel = git_output( $git, 'rev-parse --show-toplevel' ); |
| 46 | +if ( null === $toplevel || realpath( $toplevel ) !== $path ) { |
| 47 | + fail( $repository, 'it is not a Git clone.' ); |
| 48 | +} |
| 49 | + |
| 50 | +passthru( "{$git} fetch --quiet origin", $exit_code ); |
| 51 | +if ( 0 !== $exit_code ) { |
| 52 | + fail( $repository, 'fetching from origin failed.' ); |
| 53 | +} |
| 54 | + |
| 55 | +$default_branch = git_output( $git, 'symbolic-ref --short refs/remotes/origin/HEAD' ); |
| 56 | +if ( null === $default_branch ) { |
| 57 | + // Clones made before the remote HEAD was recorded need it looked up once. |
| 58 | + git_output( $git, 'remote set-head origin --auto' ); |
| 59 | + $default_branch = git_output( $git, 'symbolic-ref --short refs/remotes/origin/HEAD' ); |
| 60 | +} |
| 61 | +$default_branch = null === $default_branch ? null : preg_replace( '#^origin/#', '', $default_branch ); |
| 62 | +$current_branch = git_output( $git, 'rev-parse --abbrev-ref HEAD' ); |
| 63 | +$changes = git_output( $git, 'status --porcelain' ); |
| 64 | + |
| 65 | +if ( null === $changes ) { |
| 66 | + fail( $repository, 'its status could not be read.' ); |
| 67 | +} |
| 68 | + |
| 69 | +if ( '' !== $changes ) { |
| 70 | + printf( "Leaving \033[33m%s\033[0m alone, it has uncommitted changes.\n", $repository ); |
| 71 | + exit; |
| 72 | +} |
| 73 | + |
| 74 | +if ( null === $default_branch || $current_branch !== $default_branch ) { |
| 75 | + printf( "Leaving \033[33m%s\033[0m alone, it is on branch %s instead of %s.\n", $repository, $current_branch, $default_branch ?: 'the default branch' ); |
| 76 | + exit; |
| 77 | +} |
| 78 | + |
| 79 | +printf( "Pulling latest changes into %s...\n", $default_branch ); |
| 80 | +passthru( "{$git} pull --quiet --ff-only", $exit_code ); |
| 81 | +if ( 0 !== $exit_code ) { |
| 82 | + fail( $repository, 'pulling the latest changes failed.' ); |
| 83 | +} |
0 commit comments