diff --git a/gitium/inc/class-git-wrapper.php b/gitium/inc/class-git-wrapper.php index 8863658..355dd21 100644 --- a/gitium/inc/class-git-wrapper.php +++ b/gitium/inc/class-git-wrapper.php @@ -652,6 +652,11 @@ function rm_cached( $path ) { return ( $return == 0 ); } + function revert_commit( $commit_hash ) { + list( $return, ) = $this->_call( 'revert', '--no-edit', $commit_hash ); + return ( $return === 0 ); + } + function remove_wp_content_from_version_control() { $process = proc_open( 'rm -rf ' . ABSPATH . '/wp-content/.git', diff --git a/gitium/inc/class-gitium-submenu-commits.php b/gitium/inc/class-gitium-submenu-commits.php index 53c7837..cdc5be9 100644 --- a/gitium/inc/class-gitium-submenu-commits.php +++ b/gitium/inc/class-gitium-submenu-commits.php @@ -26,6 +26,33 @@ class Gitium_Submenu_Commits extends Gitium_Menu { public function __construct() { parent::__construct( $this->gitium_menu_slug, $this->commits_menu_slug ); add_action( GITIUM_ADMIN_MENU_ACTION, array( $this, 'admin_menu' ) ); + if ( current_user_can( GITIUM_MANAGE_OPTIONS_CAPABILITY ) ) { + add_action( 'admin_init', array( $this, 'revert_last_commit' ) ); + } + } + + public function revert_last_commit() { + if ( ! filter_input( INPUT_POST, 'GitiumSubmitRevertLastCommit', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) ) { + return; + } + check_admin_referer( 'gitium-admin' ); + + $commits = $this->git->get_last_commits( 1 ); + if ( empty( $commits ) ) { + $this->redirect( 'No commits to revert.', false, $this->commits_menu_slug ); + } + + $commit_hash = key( $commits ); + + if ( ! $this->git->revert_commit( $commit_hash ) ) { + $this->redirect( 'Could not revert commit: ' . $this->git->get_last_error(), false, $this->commits_menu_slug ); + } + + if ( ! $this->git->push() ) { + $this->redirect( 'Revert commit created locally but push failed: ' . $this->git->get_last_error(), false, $this->commits_menu_slug ); + } + + $this->success_redirect( 'Commit `' . $commit_hash . '` reverted and pushed successfully.', $this->commits_menu_slug ); } public function admin_menu() { @@ -69,6 +96,7 @@ public function page() { table_head(); ?>
git->get_last_commits( GITIUM_LAST_COMMITS ) as $commit_id => $data ) { unset( $committer_name ); extract( $data ); @@ -114,9 +142,21 @@ public function page() {