Skip to content

Commit

Permalink
Item11267: Changes to support git submodules
Browse files Browse the repository at this point in the history
The "working directory" is different when a git hook is called from a
submodule,  so the lib path for the hooks needs to be adjusted.

Also, the githooks are installed into two different locations:
  Normal repo:     .git/hooks
  submodule repo:  .git/modules/<module-name>/hooks


Conflicts:
	core/pseudo-install.pl
	core/tools/develop/githooks/commit-msg
	core/tools/develop/githooks/pre-commit

git-svn-id: http://svn.foswiki.org/branches/Release01x01@17832 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Aug 1, 2014
1 parent 47de95d commit 450a3ec
Show file tree
Hide file tree
Showing 3 changed files with 523 additions and 2 deletions.
59 changes: 57 additions & 2 deletions core/pseudo-install.pl
Expand Up @@ -324,7 +324,7 @@ sub error {

sub trace {

#warn "...",@_,"\n";
#warn "...", @_, "\n";

return;
}
Expand Down Expand Up @@ -441,9 +441,10 @@ sub installModuleByName {
);
$libDir = 'TWiki';
}
if ( -e $manifest ) {
if ( $manifest && -e $manifest ) {
installFromMANIFEST( $module, $moduleDir, $manifest, $ignoreBlock );
update_gitignore_file($moduleDir);
update_githooks_dir( $moduleDir, $module );
}
else {
$libDir = undef;
Expand Down Expand Up @@ -1477,6 +1478,60 @@ sub update_gitignore_file {
return;
}

# install the githooks. If called with a module name (ie. "CommentPlugin")
# then we might be in a .git "superproject" structure, so look for a .git/modules/$module/hooks
# directory. otherwise a final call at the end will install into the primary .git/hooks location

sub update_githooks_dir {
my ( $moduleDir, $module ) = @_;
$module ||= '';
use Cwd;

my $hooks_src = File::Spec->catdir( 'tools', 'develop', 'githooks' );

# Check for .git directories, and copy in hooks if needed
foreach my $gitdir ( '.', '..' ) {
my $hooks_tgt = File::Spec->catdir( $gitdir, '.git', 'hooks' );
my $target_dir =
File::Spec->catdir( $moduleDir, $gitdir, '.git', 'hooks' );
my $gitmodule_target_dir =
File::Spec->catdir( $gitdir, '.git', 'modules', $module, 'hooks' );
my $gitmodule_hooks_tgt =
File::Spec->catdir( $gitdir, '.git', 'modules', $module, 'hooks' );

foreach my $hook (
qw( applypatch-msg commit-msg post-commit post-update pre-applypatch pre-commit pre-rebase prepare-commit-msg post-receive update)
)
{
next unless ( -f File::Spec->catfile( $hooks_src, $hook ) );

if ($module) {
if ( -d $gitmodule_target_dir ) {
unlink File::Spec->catfile( $gitmodule_target_dir, $hook )
if (
-e File::Spec->catfile( $gitmodule_target_dir, $hook )
);
linkOrCopy '.',
File::Spec->catfile( $hooks_src, $hook ),
File::Spec->catfile( $gitmodule_hooks_tgt, $hook ),
$CAN_LINK;
}
}
else {
if ( -d $target_dir ) {
unlink _cleanPath(
File::Spec->catfile( $target_dir, $hook ) )
if ( -e File::Spec->catfile( $target_dir, $hook ) );
linkOrCopy $moduleDir,
File::Spec->catfile( $hooks_src, $hook ),
File::Spec->catfile( $hooks_tgt, $hook ),
$CAN_LINK;
}
}
}
}
}

init();
exec_opts();
init_config();
Expand Down
154 changes: 154 additions & 0 deletions core/tools/develop/githooks/commit-msg
@@ -0,0 +1,154 @@
#!/usr/bin/perl
# See bottom of file for license and copyright information

use strict;
use warnings;

BEGIN {
# Look in the current and parent directories for libraries
# With git submodules, the current directory is one lib lower than
# with a normal git repo during the commit

if ( -d './core' ) {
unshift @INC, './core/lib'; # Pick up foswiki libs
}
elsif ( -d '../core' ) {
unshift @INC, '../core/lib'; # Pick up foswiki libs
}
else {
print STDERR
"Unable to find the location of the foswiki core directory\n";
print STDERR
"Be sure that foswiki core has been installed before attempting to commit changes\n";
die "The pre-commit-msg exit won't work - commit aborted";
}
if ( -d './BuildContrib' ) {
unshift @INC,
'./BuildContrib/lib'; # Pick up BuildContrib version of PerlTidy
}
elsif ( -d '../BuildContrib' ) {
unshift @INC,
'../BuildContrib/lib'; # Pick up BuildContrib version of PerlTidy
}
else {
print STDERR "Unable to find the location of BuildContrib\n";
print STDERR
"Be sure that BuildContrib has been pseudo-installed before attempting to commit changes\n";
die "The pre-commit-msg exit won't work - commit aborted";
}
}

use LWP::Simple;

# COMMIT-MSG CLIENT HOOK for Foswiki git
#
# The commit-msg hook tests that the message identifies one or more
# tasks, and that each task is in a state to accept checkins.
#
# STDERR ends up on the users' terminal

# These are not used here except to keep the error message in sync with pre-commit
my $WINDOW_DAYS = 3; # window for date in %META
my $WINDOW = $WINDOW_DAYS * 24 * 60 * 60;

my $failmsg = '';
my $logmsg;

{
local $/ = undef;
open my $fh, "<", $ARGV[0]
or die "could not open $ARGV[0]: $!";
$logmsg = <$fh>;
close $fh;
};

fail("No Bug item in log message\n") unless ( $logmsg =~ /^Item\d+\s*:/ );

my @items;
$logmsg =~ s/\b(Item\d+)\s*:/push(@items, $1); '';/gem;
foreach my $item (@items) {
my $url = "http://foswiki.org/Tasks/ItemStatusQuery?item=$item;skin=text";
my $state = get $url;

unless ( defined $state ) {
$failmsg .=
"ERROR: GET on Tasks.ItemStatusQuery failed. Check http://foswiki.org/Tasks/ItemStatusQuery\n";
fail($failmsg);
}

$failmsg .= "$item does not exist\n" unless $state;

if ( $state =~
/^(Waiting for Release|Closed|No Action Required|Proposal Required)$/ )
{
$failmsg .= "$item is in $state state; cannot check in\n";
}

}

fail($failmsg) if $failmsg;

exit 0;

# PLEASE keep this message in sync with
# http://foswiki.org/Development/SvnRepository#RulesForCheckins
sub fail {
my $message = shift;
print STDERR <<"EOF";
--------------------------------------------------------------
Illegal checkin to Foswiki git repo found in pre-commit:
=======
$message
=======
http://foswiki.org/Development/GitRepository#RulesForCheckins
Rules - files being checked in must:
1. Have a comment...
2. ...with relevant ItemNNN task topics in the first line, e.g.
Item12345: Item12346: fixed foo, updated release notes
3. Refer to ItemNNN task topics which are open at the time of
checkin, i.e. *not* one of: Closed, Waiting For Release,
No Action or Proposal Required
4. .pl and .pm files must be "tidied" if the TIDY control file
in the root of the extension calls for it, see:
http://foswiki.org/Development/TIDY
5. .txt files in web directories must have META:TOPICINFO with
the author "ProjectContributor", a version of 1 and a date
within $WINDOW_DAYS days of the checkin. Any FILEATTACHMENTs must
has the "ProjectContributor" author, a version of 1 and a date
with $WINDOW_DAYS days of the checkin.
Getting rejected commits with perltidy? We are checking using
version $Perl::Tidy::VERSION
See http://foswiki.org/Development/PerlTidy#Versions
------------TRUNCATED COMMIT MESSAGE FOLLOWS------------------------
EOF
$logmsg =~ s/^# Untracked files:.*//ms;
print STDERR $logmsg;
exit 1;
}

__END__
Foswiki - The Free and Open Source Wiki, http://foswiki.org/
Copyright (C) 2014 Foswiki Contributors. Foswiki Contributors
are listed in the AUTHORS file in the root of this distribution.
NOTE: Please extend that file, not this notice.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version. For
more details read LICENSE in the root of this distribution.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
As per the GPL, removal of this notice is prohibited.

0 comments on commit 450a3ec

Please sign in to comment.