| 1 | package Git::MoreHooks::GitRepoAdmin; | ||
| 2 | |||
| 3 | ## no critic (InputOutput::RequireCheckedSyscalls) | ||
| 4 | ## no critic (ValuesAndExpressions::ProhibitMagicNumbers) | ||
| 5 | ## no critic (ValuesAndExpressions::RequireInterpolationOfMetachars) | ||
| 6 | ## no critic (ControlStructures::ProhibitPostfixControls) | ||
| 7 | |||
| 8 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use feature qw( say ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | # ABSTRACT: Integrate with .git-repo-admin | ||
| 14 | |||
| 15 | our $VERSION = '0.019'; # VERSION: generated by DZP::OurPkgVersion | ||
| 16 | |||
| 17 | |||
| 18 | |||
| 19 | 1 | ▶ | use Git::Hooks 3.000000; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 20 | use English qw( -no_match_vars ); | ||
| 21 | use Path::Tiny; | ||
| 22 | use Cwd; | ||
| 23 | use File::Spec; | ||
| 24 | use File::Temp qw/ tempfile tempdir /; | ||
| 25 | use Log::Any qw{$log}; | ||
| 26 | |||
| 27 | my $PKG = __PACKAGE__; | ||
| 28 | my ($CFG) = __PACKAGE__ =~ /::([^:]+)$/msx; | ||
| 29 | $CFG = 'githooks.' . $CFG; | ||
| 30 | |||
| 31 | ############# | ||
| 32 | # Grok hook configuration, check it and set defaults. | ||
| 33 | |||
| 34 | sub _setup_config { | ||
| 35 | my ($git) = @_; | ||
| 36 | $log->debugf( __PACKAGE__ . '::_setup_config(%s):', '$git' ); | ||
| 37 | |||
| 38 | my $config = $git->get_config(); | ||
| 39 | $log->tracef( __PACKAGE__ . '::_setup_config(): Current Git config:\n%s.', $config ); | ||
| 40 | |||
| 41 | $config->{ lc $CFG } //= {}; | ||
| 42 | |||
| 43 | my $default = $config->{ lc $CFG }; | ||
| 44 | $default->{'ref'} //= [ '^refs/heads/main$', '^refs/heads/master$' ]; | ||
| 45 | |||
| 46 | return; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub _current_version { | ||
| 50 | my ( $git, $dir ) = @_; | ||
| 51 | $log->debug( ( caller 0 )[3], '( ', $git, ', ', $dir, ')' ); | ||
| 52 | my $ver_file_cnt = path( File::Spec->catdir( $dir, 'VERSION' ) )->slurp_utf8; | ||
| 53 | $log->debug( ( caller 0 )[3], ': ver_file_cnt=', $ver_file_cnt ); | ||
| 54 | my ($hooks_cfg_ver) = $ver_file_cnt =~ m/^([[:digit:]]+)$/msx; | ||
| 55 | return if !$hooks_cfg_ver; | ||
| 56 | return $hooks_cfg_ver; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub _new_version { | ||
| 60 | my ( $git, $ref, $dir ) = @_; | ||
| 61 | $log->debug( ( caller 0 )[3], '( ', $git, ', ', $ref, ', ', $dir, ')' ); | ||
| 62 | |||
| 63 | my $filepath = File::Spec->catdir( $dir, 'VERSION' ); | ||
| 64 | my $object = "$ref:$filepath"; | ||
| 65 | my $ver_file_cnt = $git->run( | ||
| 66 | 'cat-file', | ||
| 67 | '-p', $object, | ||
| 68 | { | ||
| 69 | env => { | ||
| 70 | |||
| 71 | # Eliminate the effects of system wide and global configuration. | ||
| 72 | GIT_CONFIG_NOSYSTEM => 1, | ||
| 73 | XDG_CONFIG_HOME => undef, | ||
| 74 | HOME => undef, | ||
| 75 | }, | ||
| 76 | }, | ||
| 77 | ); | ||
| 78 | |||
| 79 | $log->debug( ( caller 0 )[3], ': ver_file_cnt=', $ver_file_cnt ); | ||
| 80 | my ($hooks_cfg_ver) = $ver_file_cnt =~ m/^([[:digit:]]+)$/msx; | ||
| 81 | return if !$hooks_cfg_ver; | ||
| 82 | return $hooks_cfg_ver; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub _ref_matches { | ||
| 86 | my ( $git, $our_refs, $branches ) = @_; | ||
| 87 | $log->debug( ( caller 0 )[3], '( ', ( join q{:}, @{$our_refs} ), ', ', ( join q{:}, @{$branches} ), ')' ); | ||
| 88 | |||
| 89 | my @matches; | ||
| 90 | foreach my $our_ref ( @{$our_refs} ) { | ||
| 91 | my @m = grep { m/$our_ref/msx } @{$branches}; | ||
| 92 | push @matches, @m; | ||
| 93 | } | ||
| 94 | |||
| 95 | return @matches; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _update_server_side { | ||
| 99 | my ( $git, $ref, $dir ) = @_; | ||
| 100 | $log->debug( ( caller 0 )[3], '( ', $git, ', ', $ref, ', ', $dir, ')' ); | ||
| 101 | |||
| 102 | my $tmp = File::Temp->new(); | ||
| 103 | |||
| 104 | # Hook is always run with cdw as repo root dir. | ||
| 105 | my $filepath = File::Spec->catdir( $dir, 'initialize-server.sh' ); | ||
| 106 | my $object = "$ref:$filepath"; | ||
| 107 | my $exe_file_cnt = $git->run( | ||
| 108 | 'cat-file', | ||
| 109 | '-p', $object, | ||
| 110 | { | ||
| 111 | env => { | ||
| 112 | |||
| 113 | # Eliminate the effects of system wide and global configuration. | ||
| 114 | GIT_CONFIG_NOSYSTEM => 1, | ||
| 115 | XDG_CONFIG_HOME => undef, | ||
| 116 | HOME => undef, | ||
| 117 | }, | ||
| 118 | }, | ||
| 119 | ); | ||
| 120 | $log->debug( ( caller 0 )[3], ': exe_file_cnt=', $exe_file_cnt ); | ||
| 121 | print {$tmp} $exe_file_cnt; | ||
| 122 | my $tmp_filename = $tmp->filename; | ||
| 123 | $log->debug( ( caller 0 )[3], ': tmp_filename=', $tmp_filename ); | ||
| 124 | local $OUTPUT_AUTOFLUSH = 1; | ||
| 125 | system 'bash', $tmp_filename, $ref; | ||
| 126 | |||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | ########## | ||
| 131 | |||
| 132 | sub check_affected_refs_client_side { | ||
| 133 | my ( $git, $is_squash_merge ) = @_; | ||
| 134 | $log->debug( ( caller 0 )[3], '( ', $git, '( ', ( $is_squash_merge // 'undef' ), ')' ); | ||
| 135 | $is_squash_merge = 0 if ( !defined $is_squash_merge ); | ||
| 136 | |||
| 137 | _setup_config($git); | ||
| 138 | $log->debug( ( caller 0 )[3], ': cwd=', cwd ); | ||
| 139 | |||
| 140 | # Hook is always run with cdw as repo root dir. | ||
| 141 | my $curr_ver = _current_version( $git, '.git/.git-repo-admin' ); | ||
| 142 | $log->debug( ( caller 0 )[3], ': curr_ver=', $curr_ver ); | ||
| 143 | |||
| 144 | # We cannot use $git->get_affected_refs() because post-merge hook | ||
| 145 | # does not get information about which branches are affected. | ||
| 146 | # Don't know why!!! | ||
| 147 | # So instead we just work with the only branch in the config. | ||
| 148 | my @our_refs = $git->get_config( $CFG => 'ref' ); | ||
| 149 | $log->debug( ( caller 0 )[3], ': our_refs=', ( join q{:}, @our_refs ) ); | ||
| 150 | my $branches_raw = $git->run( | ||
| 151 | 'for-each-ref', | ||
| 152 | '--format', | ||
| 153 | '%(refname)', | ||
| 154 | { | ||
| 155 | env => { | ||
| 156 | |||
| 157 | # Eliminate the effects of system wide and global configuration. | ||
| 158 | GIT_CONFIG_NOSYSTEM => 1, | ||
| 159 | XDG_CONFIG_HOME => undef, | ||
| 160 | HOME => undef, | ||
| 161 | }, | ||
| 162 | }, | ||
| 163 | ); | ||
| 164 | my @branches = split qr{\n}msx, $branches_raw; | ||
| 165 | $log->debug( ( caller 0 )[3], ': branches=', ( join q{:}, @branches ) ); | ||
| 166 | |||
| 167 | my @matches = _ref_matches( $git, \@our_refs, \@branches ); | ||
| 168 | if ( @matches > 1 ) { | ||
| 169 | $git->fault('Config variable \'ref\' matches with more than one branch.'); | ||
| 170 | return scalar @matches; | ||
| 171 | } | ||
| 172 | elsif ( @matches == 0 ) { | ||
| 173 | $git->fault('Config variable \'ref\' does not match with any branch.'); | ||
| 174 | return 1; | ||
| 175 | } | ||
| 176 | |||
| 177 | # Okay, no errors in the config. | ||
| 178 | # We always proceed to read the VERSION from the hook config branch. | ||
| 179 | my $new_ver = _new_version( $git, $matches[0], '.git-repo-admin' ); | ||
| 180 | $log->debug( ( caller 0 )[3], ': new_ver=', $new_ver ); | ||
| 181 | if ( $new_ver > $curr_ver ) { | ||
| 182 | $log->debug( ( caller 0 )[3], 'Newer version detected. Update config.' ); | ||
| 183 | say '********************************************************************************'; | ||
| 184 | say '* UPDATE CLIEND SIDE HOOKS *'; | ||
| 185 | say '* Run .git-repo-admin/install-client-hooks.sh *'; | ||
| 186 | say '* ATTN. Switch to right branch first! *'; | ||
| 187 | say '********************************************************************************'; | ||
| 188 | } | ||
| 189 | |||
| 190 | return 0; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub check_affected_refs_server_side { | ||
| 194 | my ($git) = @_; | ||
| 195 | $log->debug( ( caller 0 )[3], '( ', $git, ')' ); | ||
| 196 | |||
| 197 | _setup_config($git); | ||
| 198 | my $curr_ver = _current_version( $git, '.git-repo-admin' ); | ||
| 199 | $log->debug( ( caller 0 )[3], ': curr_ver=', $curr_ver ); | ||
| 200 | |||
| 201 | # We're only interested in branches | ||
| 202 | my @refs = grep { m{^refs/heads/}msx } $git->get_affected_refs(); | ||
| 203 | return 1 unless @refs; | ||
| 204 | |||
| 205 | my @our_refs = $git->get_config( $CFG => 'ref' ); | ||
| 206 | $log->debug( ( caller 0 )[3], ': our_refs=', ( join q{:}, @our_refs ) ); | ||
| 207 | |||
| 208 | # my ($r, $match) = _ref_matches( $git, \@our_refs, \@refs ); | ||
| 209 | my @matches = _ref_matches( $git, \@our_refs, \@refs ); | ||
| 210 | $log->debug( ( caller 0 )[3], ': matches=', ( join q{:}, @matches ) ); | ||
| 211 | if ( @matches > 1 ) { | ||
| 212 | $git->fault('Config variable \'ref\' matches with more than one branch.'); | ||
| 213 | $log->debug( ( caller 0 )[3], '(): ' . scalar @matches ); | ||
| 214 | return scalar @matches; | ||
| 215 | } | ||
| 216 | elsif ( @matches == 1 ) { | ||
| 217 | my $new_ver = _new_version( $git, $matches[0], '.git-repo-admin' ); | ||
| 218 | $log->debug( ( caller 0 )[3], ': new_ver=', $new_ver ); | ||
| 219 | if ( $new_ver > $curr_ver ) { | ||
| 220 | $log->debug( ( caller 0 )[3], 'Newer version detected. Update config.' ); | ||
| 221 | _update_server_side( $git, $matches[0], '.git-repo-admin' ); | ||
| 222 | } | ||
| 223 | $log->debug( ( caller 0 )[3], '(): 0' ); | ||
| 224 | return 0; | ||
| 225 | } | ||
| 226 | |||
| 227 | # On server side we get the affected refs via the hook, and the main/master | ||
| 228 | # doesn't need to be one of them, if it wasn't updated. | ||
| 229 | |||
| 230 | } | ||
| 231 | |||
| 232 | # Install hooks | ||
| 233 | POST_MERGE \&check_affected_refs_client_side; | ||
| 234 | POST_RECEIVE \&check_affected_refs_server_side; | ||
| 235 | |||
| 236 | 1; | ||
| 237 | |||
| 238 | __END__ | ||