22/**
33 * Brings a clone up to date with its remote, without ever touching work in
44 * progress: a clone with uncommitted changes, or one on another branch than
5- * the default one, is only fetched.
5+ * the default one, is only fetched. Exits with an error code when git fails.
66 */
77
88if ( ! isset ( $ argv [1 ] ) || ! is_dir ( $ argv [1 ] ) ) {
99 exit ( "Usage: php refresh-repository.php <repository-folder> \n" );
1010}
1111
1212$ repository = $ argv [1 ];
13- $ git = 'git -C ' . escapeshellarg ( (string ) realpath ( $ repository ) );
13+ $ path = (string ) realpath ( $ repository );
14+ $ git = 'git -C ' . escapeshellarg ( $ path );
1415
1516/**
1617 * Runs a git command and returns its output, or null when it fails.
@@ -26,9 +27,30 @@ function git_output( $git, $command ) {
2627 return 0 === $ exit_code ? trim ( implode ( "\n" , $ output ) ) : null ;
2728}
2829
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+ }
40+
2941printf ( "--- Refreshing repository \033[32m%s \033[0m --- \n" , $ repository );
3042
31- passthru ( "{$ git } fetch --quiet origin " );
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+ }
3254
3355$ default_branch = git_output ( $ git , 'symbolic-ref --short refs/remotes/origin/HEAD ' );
3456if ( null === $ default_branch ) {
@@ -38,8 +60,13 @@ function git_output( $git, $command ) {
3860}
3961$ default_branch = null === $ default_branch ? null : preg_replace ( '#^origin/# ' , '' , $ default_branch );
4062$ current_branch = git_output ( $ git , 'rev-parse --abbrev-ref HEAD ' );
63+ $ changes = git_output ( $ git , 'status --porcelain ' );
4164
42- if ( '' !== (string ) git_output ( $ git , 'status --porcelain ' ) ) {
65+ if ( null === $ changes ) {
66+ fail ( $ repository , 'its status could not be read. ' );
67+ }
68+
69+ if ( '' !== $ changes ) {
4370 printf ( "Leaving \033[33m%s \033[0m alone, it has uncommitted changes. \n" , $ repository );
4471 exit ;
4572}
@@ -50,4 +77,7 @@ function git_output( $git, $command ) {
5077}
5178
5279printf ( "Pulling latest changes into %s... \n" , $ default_branch );
53- passthru ( "{$ git } pull --quiet --ff-only " );
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