Skip to content

Commit

Permalink
Item9238: Create new RepositoryPlugin
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.foswiki.org/trunk/RepositoryPlugin@10469 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
  • Loading branch information
GeorgeClark authored and GeorgeClark committed Jan 2, 2011
0 parents commit 99af3d9
Show file tree
Hide file tree
Showing 6 changed files with 417 additions and 0 deletions.
9 changes: 9 additions & 0 deletions data/System/RepoPerlDoc.txt
@@ -0,0 +1,9 @@
%META:TOPICINFO{author="ProjectContributor" date="1290819795" format="1.1" version="$Rev$"}%
%META:TOPICPARENT{name="DeveloperDocumentationCategory"}%
%IF{
"'%URLPARAM{"module"}%'=''"
then="<form class='foswikiForm' ><div class='foswikiFormSteps'><div class='foswikiFormStep'><label for='module'>Perl Module</label> <input type='text' name='module' id='module' value='%URLPARAM{module}%' class='foswikiInputField foswikiFocus' /> <input type='submit' class='foswikiSubmit' /></div></div></form>No module was specified. Check [[DevelopingPlugins][Developing plugins]] and [[Foswiki:Development.DevelopersBible][Developer's Bible]] for more information."
}%
---+!! %URLPARAM{module}%
%REPO{module="%URLPARAM{module}%"}%
%INCLUDE{"doc:%URLPARAM{module}%" level="1"}%
73 changes: 73 additions & 0 deletions data/System/RepositoryPlugin.txt
@@ -0,0 +1,73 @@
---+!! !RepositoryPlugin
<!--
One line description, required for extensions repository catalog.
BuildContrib will fill in the SHORTDESCRIPTION with the value of
$SHORTDESCRIPTION from the .pm module, or you can redefine it here if you
prefer.
* Set SHORTDESCRIPTION = %$SHORTDESCRIPTION%
-->
%SHORTDESCRIPTION%

<blockquote class="foswikiHelp">
%X% *Caution!* This macro can expose file system path information that would not normally be revealed in a production site.<br /> This plugin is a *Developer Tool* and should be used with caution on production systems.
</blockquote>

%TOC%

The REPO macro is used to report the status of a module or topic in the Source
Repository. The following information is reported:

* The real file name of the file, dereferencing any symbolic links. (Supports pseudo-install)
* Interpreted git status of the file (Modified, Added, Renamed, Deleted, Copied)
* The commit message from the most recent commit on the file.

---++ Usage

| =%<nop>REPO{}%= | Provide a git report on the topic expanding the macro |
| =%<nop>REPO{module="Foswiki::Module"}%= | Provide a git report on the specified Foswiki module. |
| =%<nop>REPO{web="Someweb" topic="SomeTopic"}%= | Provide a git report on the specified Web.Topic |

---++ Examples

---+++ Report for this topic
If installed, the REPO macro will expand to the status of this topic file.
<verbatim>
%REPO{}%
</verbatim>

%REPO{}%

---+++ Report for Main.WebHome
<verbatim>
%REPO{web="Main" topic="WebHome"}%
</verbatim>

%REPO{web="Main" topic="WebHome"}%

---+++ Report for =Foswiki::Plugins::RepositoryPlugin=
<verbatim>
%REPO{module="Foswiki::Plugins::RepositoryPlugin"}%
</verbatim>

%REPO{module="Foswiki::Plugins::RepositoryPlugin"}%

---++ Installation Instructions

%$INSTALL_INSTRUCTIONS%

---++ Info

Many thanks to the following sponsors for supporting this work:
* Acknowledge any sponsors here

| Author(s): | |
| Copyright: | &copy; |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| Dependencies: | %$DEPENDENCIES% |
| Home page: | http://foswiki.org/bin/view/Extensions/RepositoryPlugin |
| Support: | http://foswiki.org/bin/view/Support/RepositoryPlugin |

<!-- Do _not_ attempt to edit this topic; it is auto-generated. -->
301 changes: 301 additions & 0 deletions lib/Foswiki/Plugins/RepositoryPlugin.pm
@@ -0,0 +1,301 @@
# See bottom of file for default license and copyright information

=begin TML
---+ package RepositoryPlugin
The RepositoryPlugin is part of the BuildContrib which provides the other tools
to run from a repository checkout.
Repo Plugin will determine if a Foswiki install is running from a SVN or git
checkout, and will parse out information about the repo, including current
revision, for reporting in a topic.
For this initial version:
* Assumes that the git and svn commands are on the default path
* Assumes that the repo root is one level up from the current directory
=cut

package Foswiki::Plugins::RepositoryPlugin;

# Always use strict to enforce variable scoping
use strict;

require Foswiki::Func; # The plugins API
require Foswiki::Plugins; # For the API version

# $VERSION is referred to by Foswiki, and is the only global variable that
# *must* exist in this package.
# This should always be $Rev$ so that Foswiki can determine the checked-in
# status of the plugin. It is used by the build automation tools, so
# you should leave it alone.
our $VERSION = '$Rev$';

# This is a free-form string you can use to "name" your own plugin version.
# It is *not* used by the build automation tools, but is reported as part
# of the version number in PLUGINDESCRIPTIONS.
our $RELEASE = '0.1';

# Short description of this plugin
# One line description, is shown in the %SYSTEMWEB%.TextFormattingRules topic:
our $SHORTDESCRIPTION =
'Repo Plugin - Displays information about the repository if running from a git or svn checkout';

# You must set $NO_PREFS_IN_TOPIC to 0 if you want your plugin to use
# preferences set in the plugin topic. This is required for compatibility
# with older plugins, but imposes a significant performance penalty, and
# is not recommended. Instead, leave $NO_PREFS_IN_TOPIC at 1 and use
# =$Foswiki::cfg= entries set in =LocalSite.cfg=, or if you want the users
# to be able to change settings, then use standard Foswiki preferences that
# can be defined in your %USERSWEB%.SitePreferences and overridden at the web
# and topic level.
our $NO_PREFS_IN_TOPIC = 1;
our $REPO_TYPE = ''; # Global - can be cached by persistent perl

my $svnbin;
my $gitbin;
my $rootdir;
my %repoInfo;

=begin TML
---++ initPlugin($topic, $web, $user) -> $boolean
* =$topic= - the name of the topic in the current CGI query
* =$web= - the name of the web in the current CGI query
* =$user= - the login name of the user
* =$installWeb= - the name of the web the plugin topic is in
(usually the same as =$Foswiki::cfg{SystemWebName}=)
=cut

sub initPlugin {
my ( $topic, $web, $user, $installWeb ) = @_;

# check for Plugins.pm versions
if ( $Foswiki::Plugins::VERSION < 2.0 ) {
Foswiki::Func::writeDebug( 'Version mismatch between ',
__PACKAGE__, ' and Plugins.pm' );
return 0;
}

$svnbin = $Foswiki::cfg{Plugins}{RepositoryPlugin}{svnProgram}
|| 'svn';

$gitbin = $Foswiki::cfg{Plugins}{RepositoryPlugin}{gitProgram}
|| 'git';

$rootdir = $Foswiki::cfg{Plugins}{RepositoryPlugin}{rootDir}
|| '../';


Foswiki::Func::registerTagHandler( 'REPO', \&_REPO );

# Plugin correctly initialized
return 1;
}

=begin TML
---++ _REPO
Primary macro for the REPO Plugin. Returns basic information about the current repository
%<nop>REPO{}%
%<nop>REPO{module="Foswiki::Plugins::MyPlugin"}% Get information on the specified Foswiki module.
%<nop>REPO{web="Web" topic="topic" attachment="attach"}% Get information on a specific topic or attachment.
Note. In the case of pseudo-installed files, the file is dereferenced from the symlink to the absolute file name. Information on that file is reported.
=cut

sub _REPO {
my ( $session, $params, $theTopic, $theWeb ) = @_;

my $module = $params->{module} || '';

my $web = $params->{web} || $theWeb;
my $topic = $params->{topic} || $theTopic;

if ( Scalar::Util::tainted($web) ) {
$web = Foswiki::Sandbox::untaint( $web,
\&Foswiki::Sandbox::validateWebName );
}

if ( Scalar::Util::tainted($topic) ) {
$topic = Foswiki::Sandbox::untaint( $topic,
\&Foswiki::Sandbox::validateTopicName );
}

return '<blockquote class="foswikiAlert"> %X% Error: =module= and =web / topic= parameters cannot be combined. </blockquote>' if ($module && ($params->{web} || $params->{topic}) );

my $thisfile;

if ($module) {
return '<blockquote class="foswikiAlert"> %X% Error: Only Foswiki modules can be queried </blockquote>' unless ($module =~ m/^Foswiki/);
$module =~ s/[^\w:]//g;

$module =~ s#::#/#g;
foreach my $inc (@INC) {
if ( -f "$inc/$module.pm" ) {
$thisfile = "$inc/$module.pm";
last;
}
}
}
else {
$thisfile = $Foswiki::cfg{DataDir}.'/'.$web.'/'.$topic.'.txt';
}

my $absfile;

# Deference symbolic links - typically used in pseudo-install
if ( -l $thisfile ) {
$absfile = readlink $thisfile;
$absfile ||= '';
}

$absfile ||= $thisfile;
my $texit;

(my $topicStatus, $texit) =
Foswiki::Sandbox->sysCommand( "$gitbin status --porcelain $absfile ",
);

$topicStatus = substr( $topicStatus, 0, 2);

return "*File not known to git:* =$absfile= " if ($topicStatus eq '??');

my $status = $topicStatus =~ m/\ [MD]/ ? 'Modified, not updated in index'
: $topicStatus =~ m/M[\ MD]/ ? 'updated in index'
: $topicStatus =~ m/A[\ MD]/ ? 'added to index'
: $topicStatus =~ m/D[\ M]/ ? 'deleted from index'
: $topicStatus =~ m/R[\ MD]/ ? 'renamed in index'
: $topicStatus =~ m/C[\ MD]/ ? 'copied in index'
: $topicStatus eq '' ? 'up to date'
: 'unknown';

(my $topicinfo, $texit) =
Foswiki::Sandbox->sysCommand( "$gitbin log -1 $absfile ",
);

my $report =
"*Repo file:* =$absfile= \n\n";

$report .= "*File Status:* ($topicStatus) - $status\n\n";

$report .= "*Last Commit:* \n"
. '<verbatim>'
. $topicinfo
. '</verbatim>' unless $texit;

return $report;


my ( $gitinfo, $gexit ) =
Foswiki::Sandbox->sysCommand( "$gitbin svn info ",
);

$repoInfo{type} = 'git' unless ($gexit || $gitinfo =~ m/Not a git repository/);

my ( $svninfo, $sexit ) =
Foswiki::Sandbox->sysCommand( "$svnbin info ",
);
$repoInfo{type} = 'svn' unless ($sexit || $svninfo =~ m/not a working copy/);


# if ($repoInfo->{type} eq 'svn') {
# $svninfo =~ /Path: (
# Path: .
# URL: http://svn.twiki.org/svn/twiki/trunk/core
# Repository Root: http://svn.twiki.org/svn
# Repository UUID: a00a5322-12db-0310-a70b-8735589c885e
# Revision: 19150
# Node Kind: directory
# Schedule: normal
# Last Changed Author: PeterThoeny
# Last Changed Rev: 19140
# Last Changed Date: 2010-06-27 02:42:03 -0400 (Sun, 27 Jun 2010)
#
# gac@cardinal: /data/gac/SVN/twiki/core $ cd -
# /data/gac/SVN/twiki
# gac@cardinal: /data/gac/SVN/twiki $ cd /var/www/foswiki/trunk/
# gac@cardinal: /var/www/foswiki/trunk (Item9238-BuildContrib)$ git svn info
# Path: .
# URL: http://svn.foswiki.org/trunk
# Repository Root: http://svn.foswiki.org
# Repository UUID: 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
# Revision: 8030
# Node Kind: directory
# Schedule: normal
# Last Changed Author: CrawfordCurrie
# Last Changed Rev: 8030
# Last Changed Date: 2010-07-05 05:21:49 -0400 (Mon, 05 Jul 2010)
#



my $cmd = $params->{_DEFAULT} || '';

if ( $cmd eq 'date' ) {
my ( $output, $exit ) =
Foswiki::Sandbox->sysCommand( "$gitbin log -1 ",
);
($output) = $output =~ /^Date:\s+(.*)$/m;
return "<verbatim>($output)</verbatim>";
}
elsif ( $cmd eq 'author' ) {
my ( $output, $exit ) =
Foswiki::Sandbox->sysCommand( "$gitbin log -1 ",
);
($output) = $output =~ /^Author:\s+(.*)$/m;
return "<verbatim>($output)</verbatim>";
}
elsif ( $cmd eq 'log' ) {
my ( $output, $exit ) =
Foswiki::Sandbox->sysCommand( "$gitbin log -5 ",
);
return "<verbatim>($output)</verbatim>";
}
elsif ( $cmd eq 'branch') {
my ( $output, $exit ) =
Foswiki::Sandbox->sysCommand( "$gitbin branch",
);
($output) = $output =~ /^\*(.*)$/m;
return "$output";
}
else {
return $repoInfo{type};
}

}

1;
__END__
This copyright information applies to the RepositoryPlugin:
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# RepositoryPlugin is Copyright (C) 2010 Foswiki Contributors. Foswiki Contributors
# are listed in the AUTHORS file in the root of this distribution.
# NOTE: Please extend that file, not this notice.
# Additional copyrights apply to some or all of the code as follows:
# Copyright (C) 2000-2003 Andrea Sterbini, a.sterbini@flashnet.it
# Copyright (C) 2001-2006 Peter Thoeny, peter@thoeny.org
# and TWiki Contributors. All Rights Reserved. Foswiki Contributors
# are listed in the AUTHORS file in the root of this distribution.
#
# This license applies to RepositoryPlugin *and also to any derivatives*
#
# 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.
#
# For licensing info read LICENSE file in the Foswiki root.
5 changes: 5 additions & 0 deletions lib/Foswiki/Plugins/RepositoryPlugin/DEPENDENCIES
@@ -0,0 +1,5 @@
# Dependencies for RepositoryPlugin
# Example:
# Time::ParseDate,>=2003.0211,cpan,Required.
# Foswiki::Plugins,>=1.2,perl,Requires version 1.2 of handler API.

5 changes: 5 additions & 0 deletions lib/Foswiki/Plugins/RepositoryPlugin/MANIFEST
@@ -0,0 +1,5 @@
# Release manifest for RepositoryPlugin
data/System/RepositoryPlugin.txt 0644 Documentation
data/System/RepoPerlDoc.txt 0644 Version of PerlDoc topic with repository information
lib/Foswiki/Plugins/RepositoryPlugin.pm 0644 Perl module

0 comments on commit 99af3d9

Please sign in to comment.