MEDIUM: Update plugin gitbacked to v2016-08-14

This commit is contained in:
Bertrand Jacquin 2017-10-15 04:52:26 +01:00
parent 0b228e56e8
commit 27153f9e6a
No known key found for this signature in database
GPG Key ID: 5534871F2E2E93DA
7 changed files with 100 additions and 38 deletions

View File

@ -1,4 +1,4 @@
gitbacked Plugin for DokuWiki # gitbacked Plugin for DokuWiki
Store/Sync pages and media files in a git repository Store/Sync pages and media files in a git repository
@ -6,13 +6,14 @@ All documentation for this plugin can be found at
http://www.dokuwiki.org/plugin:gitbacked http://www.dokuwiki.org/plugin:gitbacked
If you install this plugin manually, make sure it is installed in If you install this plugin manually, make sure it is installed in
lib/plugins/gitbacked/ - if the folder is called different it `lib/plugins/gitbacked/` - if the folder is called different it
will not work! will not work!
Please refer to http://www.dokuwiki.org/plugins for additional info Please refer to http://www.dokuwiki.org/plugins for additional info
on how to install plugins in DokuWiki. on how to install plugins in DokuWiki.
---- ## License
Copyright (C) Wolfgang Gassler <wolfgang@gassler.org> Copyright (C) Wolfgang Gassler <wolfgang@gassler.org>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify

View File

@ -24,7 +24,7 @@ class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
io_mkdir_p($this->temp_dir); io_mkdir_p($this->temp_dir);
} }
public function register(Doku_Event_Handler &$controller) { public function register(Doku_Event_Handler $controller) {
$controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write'); $controller->register_hook('IO_WIKIPAGE_WRITE', 'AFTER', $this, 'handle_io_wikipage_write');
$controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload'); $controller->register_hook('MEDIA_UPLOAD_FINISH', 'AFTER', $this, 'handle_media_upload');
@ -39,35 +39,56 @@ class action_plugin_gitbacked_editcommit extends DokuWiki_Action_Plugin {
} else { } else {
$repoPath = DOKU_INC.$this->getConf('repoPath'); $repoPath = DOKU_INC.$this->getConf('repoPath');
} }
//set the path to the git binary
$gitPath = trim($this->getConf('gitPath'));
if ($gitPath !== '') {
Git::set_bin($gitPath);
}
//init the repo and create a new one if it is not present //init the repo and create a new one if it is not present
io_mkdir_p($repoPath); io_mkdir_p($repoPath);
$repo = new GitRepo($repoPath, true, true); $repo = new GitRepo($repoPath, true, true);
//set git working directory (by default DokuWiki's savedir) //set git working directory (by default DokuWiki's savedir)
$repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir'); $repoWorkDir = DOKU_INC.$this->getConf('repoWorkDir');
$repo->git_path .= ' --work-tree '.escapeshellarg($repoWorkDir); Git::set_bin(Git::get_bin().' --work-tree '.escapeshellarg($repoWorkDir));
$params = str_replace( $params = str_replace(
array('%mail%','%user%'), array('%mail%','%user%'),
array($this->getAuthorMail(),$this->getAuthor()), array($this->getAuthorMail(),$this->getAuthor()),
$this->getConf('addParams')); $this->getConf('addParams'));
if ($params) { if ($params) {
$repo->git_path .= ' '.$params; Git::set_bin(Git::get_bin().' '.$params);
} }
return $repo; return $repo;
} }
private function isIgnored($filePath) {
$ignore = false;
$ignorePaths = trim($this->getConf('ignorePaths'));
if ($ignorePaths !== '') {
$paths = explode(',',$ignorePaths);
foreach($paths as $path) {
if (strstr($filePath,$path)) {
$ignore = true;
}
}
}
return $ignore;
}
private function commitFile($filePath,$message) { private function commitFile($filePath,$message) {
$repo = $this->initRepo(); if (!$this->isIgnored($filePath)) {
$repo = $this->initRepo();
//add the changed file and set the commit message //add the changed file and set the commit message
$repo->add($filePath); $repo->add($filePath);
$repo->commit($message); $repo->commit($message);
//if the push after Commit option is set we push the active branch to origin //if the push after Commit option is set we push the active branch to origin
if ($this->getConf('pushAfterCommit')) { if ($this->getConf('pushAfterCommit')) {
$repo->push('origin',$repo->active_branch()); $repo->push('origin',$repo->active_branch());
} }
}
} }

View File

@ -12,9 +12,11 @@ $conf['commitPageMsg'] = 'Wiki page %page% changed with summary [%summary%] by %
$conf['commitPageMsgDel'] = 'Wiki page %page% deleted with reason [%summary%] by %user%'; $conf['commitPageMsgDel'] = 'Wiki page %page% deleted with reason [%summary%] by %user%';
$conf['commitMediaMsg'] = 'Wiki media %media% uploaded by %user%'; $conf['commitMediaMsg'] = 'Wiki media %media% uploaded by %user%';
$conf['commitMediaMsgDel'] = 'Wiki media %media% deleted by %user%'; $conf['commitMediaMsgDel'] = 'Wiki media %media% deleted by %user%';
$conf['repoPath'] = $GLOBALS['conf']['savedir']; $conf['repoPath'] = $GLOBALS['conf']['savedir'];
$conf['repoWorkDir'] = $GLOBALS['conf']['savedir']; $conf['repoWorkDir'] = $GLOBALS['conf']['savedir'];
$conf['gitPath'] = '';
$conf['addParams'] = ''; $conf['addParams'] = '';
$conf['ignorePaths'] = '';
/* plugin/gitbacked: Define a nice author (until addParams is fixed) /* plugin/gitbacked: Define a nice author (until addParams is fixed)
* MUST NOT be defined in conf/local.php as $GLOBALS['USERINFO'] does not yet exist there * MUST NOT be defined in conf/local.php as $GLOBALS['USERINFO'] does not yet exist there

View File

@ -14,5 +14,6 @@ $meta['commitMediaMsg'] = array('string');
$meta['commitMediaMsgDel'] = array('string'); $meta['commitMediaMsgDel'] = array('string');
$meta['repoPath'] = array('string'); $meta['repoPath'] = array('string');
$meta['repoWorkDir'] = array('string'); $meta['repoWorkDir'] = array('string');
$meta['gitPath'] = array('string');
$meta['addParams'] = array('string'); $meta['addParams'] = array('string');
$meta['ignorePaths'] = array('string');

View File

@ -15,4 +15,6 @@ $lang['commitMediaMsg'] = 'Commit message for media files (%user%,%media% are re
$lang['commitMediaMsgDel'] = 'Commit message for deleted media files (%user%,%media% are replaced by the corresponding values)'; $lang['commitMediaMsgDel'] = 'Commit message for deleted media files (%user%,%media% are replaced by the corresponding values)';
$lang['repoPath'] = 'Path of the git repo (e.g. the savedir '.$GLOBALS['conf']['savedir'].')'; $lang['repoPath'] = 'Path of the git repo (e.g. the savedir '.$GLOBALS['conf']['savedir'].')';
$lang['repoWorkDir'] = 'Path of the git working tree, must contain "pages" and "media" directories (e.g. the savedir '.$GLOBALS['conf']['savedir'].')'; $lang['repoWorkDir'] = 'Path of the git working tree, must contain "pages" and "media" directories (e.g. the savedir '.$GLOBALS['conf']['savedir'].')';
$lang['gitPath'] = 'Path to the git binary (if empty, the default "/usr/bin/git" will be used)';
$lang['addParams'] = 'Additional git parameters (added to the git execution command) (%user% and %mail% are replaced by the corresponding values)'; $lang['addParams'] = 'Additional git parameters (added to the git execution command) (%user% and %mail% are replaced by the corresponding values)';
$lang['ignorePaths'] = 'Paths/files which are ignored and not added to git (comma-separated)';

View File

@ -48,7 +48,7 @@ class Git {
public static function get_bin() { public static function get_bin() {
return self::$bin; return self::$bin;
} }
/** /**
* Sets up library for use in a default Windows environment * Sets up library for use in a default Windows environment
*/ */
@ -82,20 +82,21 @@ class Git {
public static function open($repo_path) { public static function open($repo_path) {
return new GitRepo($repo_path); return new GitRepo($repo_path);
} }
/** /**
* Clones a remote repo into a directory and then returns a GitRepo object * Clones a remote repo into a directory and then returns a GitRepo object
* for the newly created local repo * for the newly created local repo
* *
* Accepts a creation path and a remote to clone from * Accepts a creation path and a remote to clone from
* *
* @access public * @access public
* @param string repository path * @param string repository path
* @param string remote source * @param string remote source
* @param string reference path
* @return GitRepo * @return GitRepo
**/ **/
public static function &clone_remote($repo_path, $remote) { public static function &clone_remote($repo_path, $remote, $reference = null) {
return GitRepo::create_new($repo_path, $remote, true); return GitRepo::create_new($repo_path, $remote, true, $reference);
} }
/** /**
@ -137,16 +138,23 @@ class GitRepo {
* @access public * @access public
* @param string repository path * @param string repository path
* @param string directory to source * @param string directory to source
* @param string reference path
* @return GitRepo * @return GitRepo
*/ */
public static function &create_new($repo_path, $source = null, $remote_source = false) { public static function &create_new($repo_path, $source = null, $remote_source = false, $reference = null) {
if (is_dir($repo_path) && file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) { if (is_dir($repo_path) && file_exists($repo_path."/.git") && is_dir($repo_path."/.git")) {
throw new Exception('"'.$repo_path.'" is already a git repository'); throw new Exception('"'.$repo_path.'" is already a git repository');
} else { } else {
$repo = new self($repo_path, true, false); $repo = new self($repo_path, true, false);
if (is_string($source)) { if (is_string($source)) {
if ($remote_source) { if ($remote_source) {
$repo->clone_remote($source); if (!is_dir($reference) || !is_dir($reference.'/.git')) {
throw new Exception('"'.$reference.'" is not a git repository. Cannot use as reference.');
} else if (strlen($reference)) {
$reference = realpath($reference);
$reference = "--reference $reference";
}
$repo->clone_remote($source, $reference);
} else { } else {
$repo->clone_from($source); $repo->clone_from($source);
} }
@ -229,6 +237,16 @@ class GitRepo {
} }
} }
/**
* Get the path to the git repo directory (eg. the ".git" directory)
*
* @access public
* @return string
*/
public function git_directory_path() {
return ($this->bare) ? $this->repo_path : $this->repo_path."/.git";
}
/** /**
* Tests if git is installed * Tests if git is installed
* *
@ -318,7 +336,7 @@ class GitRepo {
* Runs a 'git status' call * Runs a 'git status' call
* *
* Accept a convert to HTML bool * Accept a convert to HTML bool
* *
* @access public * @access public
* @param bool return string with <br /> * @param bool return string with <br />
* @return string * @return string
@ -346,7 +364,7 @@ class GitRepo {
} }
return $this->run("add $files -v"); return $this->run("add $files -v");
} }
/** /**
* Runs a `git rm` call * Runs a `git rm` call
* *
@ -372,10 +390,12 @@ class GitRepo {
* *
* @access public * @access public
* @param string commit message * @param string commit message
* @param boolean should all files be committed automatically (-a flag)
* @return string * @return string
*/ */
public function commit($message = "") { public function commit($message = "", $commit_all = true) {
return $this->run("commit -av -m ".escapeshellarg($message)); $flags = $commit_all ? '-av' : '-v';
return $this->run("commit ".$flags." -m ".escapeshellarg($message));
} }
/** /**
@ -414,10 +434,11 @@ class GitRepo {
* *
* @access public * @access public
* @param string source url * @param string source url
* @param string reference path
* @return string * @return string
*/ */
public function clone_remote($source) { public function clone_remote($source, $reference) {
return $this->run("clone $source ".$this->repo_path); return $this->run("clone $reference $source ".$this->repo_path);
} }
/** /**
@ -569,7 +590,7 @@ class GitRepo {
if ($message === null) { if ($message === null) {
$message = $tag; $message = $tag;
} }
return $this->run("tag -a $tag -m $message"); return $this->run("tag -a $tag -m " . escapeshellarg($message));
} }
/** /**
@ -619,13 +640,27 @@ class GitRepo {
return $this->run("pull $remote $branch"); return $this->run("pull $remote $branch");
} }
/**
* List log entries.
*
* @param strgin $format
* @return string
*/
public function log($format = null) {
if ($format === null)
return $this->run('log');
else
return $this->run('log --pretty=format:"' . $format . '"');
}
/** /**
* Sets the project description. * Sets the project description.
* *
* @param string $new * @param string $new
*/ */
public function set_description($new) { public function set_description($new) {
file_put_contents($this->repo_path."/.git/description", $new); $path = $this->git_directory_path();
file_put_contents($path."/description", $new);
} }
/** /**
@ -634,7 +669,8 @@ class GitRepo {
* @return string * @return string
*/ */
public function get_description() { public function get_description() {
return file_get_contents($this->repo_path."/.git/description"); $path = $this->git_directory_path();
return file_get_contents($path."/description");
} }
/** /**
@ -650,4 +686,3 @@ class GitRepo {
} }
/* End of file */ /* End of file */

View File

@ -1,7 +1,7 @@
base gitbacked base gitbacked
author Wolfgang Gassler author Wolfgang Gassler (@woolfg), Carsten Teibes (@carstene1ns)
email wolfgang@gassler.org email wolfgang@gassler.org
date 2012-10-31 date 2016-08-14
name gitbacked plugin name gitbacked plugin
desc Pages and Media are stored in Git desc Pages and Media are stored in Git
url https://github.com/woolfg/gitbacked url https://github.com/woolfg/dokuwiki-plugin-gitbacked