Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Gitium Heroku/Dokku friendly #162

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,67 @@ Gitium requires git command line tool with a minimum version of 1.7 installed on

For more details about Gitium, head here: https://www.presslabs.org/gitium/docs/usage/

### Working with Heroku / Dokku

We recommend using a separate 'gitium' branch for automatic pushes from Gitium.

Add a composer.json file to your root source folder, to save the original .gitignore that would otherwise be deleted by herokuish as
part of deployment:
```
{
"scripts": {
"post-install-cmd": [
"cp .gitignore .gitignore_gitium"
]
}
}
```

Modify your .gitignore, to have at least the following lines:
```
*.log
*.swp
*.back
*.bak
*.sql
*.sql.gz
~*
.maintenance
wp-content/blogs.dir/
wp-content/upgrade/
wp-content/backup-db/
wp-content/cache/
wp-content/backups/
wp-content/uploads/
secret/
**/node_modules/
!wp-content/**/vendor/

.heroku/
.profile.d/
vendor/
.composer/
.builders_run
.bash_history
.rnd
.release
.env
*log.txt
```

Add this to your wp_config.php:
```
define( 'GITIUM_REMOTE_URL', <YOUR_REPO_URL>);
define( 'GITIUM_REMOTE_TRACKING_BRANCH', 'gitium' );
```

The deployment process would consist of:

1) Merge contents of 'gitium' branch into 'master', making sure merged contents are correct.

2) Deploy with ```git push dokku``` as usual.


### Contributing

We’ve built this to make our lives easier and we’re happy to do that for other developers, too. We’d really appreciate it if you could contribute with code, tests, documentation or just share your experience with Gitium.
Expand Down
48 changes: 44 additions & 4 deletions gitium/gitium.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* Plugin Name: Gitium
* Version: 1.0.1
* Version: 1.0.3
* Author: Presslabs
* Author URI: https://www.presslabs.com
* License: GPL2
Expand Down Expand Up @@ -51,6 +51,44 @@
require_once __DIR__ . '/inc/class-gitium-submenu-commits.php';
require_once __DIR__ . '/inc/class-gitium-submenu-settings.php';

function gitium_auto_init( ) {
global $git;

// error_log( __FUNCTION__ . ' - in auto_init_all ' );

if ( ! $git->is_status_working() || ! $git->get_remote_url() || ! ! $git->get_remote_tracking_branch() ) {
$remote_url = get_option( 'gitium_remote_url', false );
$branch = get_transient( 'gitium_remote_tracking_branch' );

// error_log( __FUNCTION__ . ' - $remote_url = ' . $remote_url );
// error_log( __FUNCTION__ . ' - $branch = ' . $branch );

if ( $remote_url && $branch ) {
$git->init();
$git->add_remote_url( $remote_url );
$git->fetch_ref();
$git->add();
$current_user = wp_get_current_user();
$commit = $git->commit( __( 'Merged existing code from ', 'gitium' ) . get_home_url(), $current_user->display_name, $current_user->user_email );
if ( ! $commit ) {
//$git->cleanup();
//error_log( __FUNCTION__ . ' - Error commiting existing code: ' . $git->get_last_error() );
return;
}
if ( ! $git->merge_initial_commit( $commit, $branch ) ) {
//error_log( __FUNCTION__ . ' - Error merging existing code: ' . $git->get_last_error());
//$git->cleanup();
return;
}
$git->push( $branch );

_gitium_status( true );
gitium_update_is_status_working();
}
}
}
add_action( 'wp_loaded', 'gitium_auto_init' );

function gitium_load_textdomain() {
load_plugin_textdomain( 'gitium', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
Expand Down Expand Up @@ -78,7 +116,7 @@ function gitium_deactivation() {
}
register_deactivation_hook( __FILE__, 'gitium_deactivation' );

function gitium_uninstall_hook() {
function gitium_uninstall_hook( $delete_options = true ) {
delete_transient( 'gitium_remote_tracking_branch' );
delete_transient( 'gitium_remote_disconnected' );
delete_transient( 'gitium_uncommited_changes' );
Expand All @@ -87,8 +125,10 @@ function gitium_uninstall_hook() {
delete_transient( 'gitium_menu_bubble' );
delete_transient( 'gitium_is_status_working' );

delete_option( 'gitium_keypair' );
delete_option( 'gitium_webhook_key' );
if ( $delete_options ) {
delete_option( 'gitium_keypair' );
delete_option( 'gitium_webhook_key' );
}
}
register_uninstall_hook( __FILE__, 'gitium_uninstall_hook' );

Expand Down
61 changes: 59 additions & 2 deletions gitium/inc/class-git-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,17 @@
/wp-signup.php
/wp-trackback.php
/xmlrpc.php

.heroku/
.profile.d/
vendor/
.composer/
.builders_run
.bash_history
.rnd
.release
.env

EOF
);

Expand All @@ -96,6 +107,23 @@ class Git_Wrapper {
function __construct( $repo_dir ) {
$this->repo_dir = $repo_dir;
$this->private_key = '';

//error_log( __FUNCTION__ . ' - $remote_url = ' . get_option('gitium_remote_url', '') );
//error_log( __FUNCTION__ . ' - GITIUM_REMOTE_URL = ' . GITIUM_REMOTE_URL );
if(defined('GITIUM_REMOTE_URL') && GITIUM_REMOTE_URL) {
update_option('gitium_remote_url', GITIUM_REMOTE_URL);
//error_log( __FUNCTION__ . ' - $remote_url = ' . get_option('gitium_remote_url', '') );
}
if(defined('GITIUM_REMOTE_TRACKING_BRANCH') && GITIUM_REMOTE_TRACKING_BRANCH) {
set_transient('gitium_remote_tracking_branch', GITIUM_REMOTE_TRACKING_BRANCH);
//error_log( __FUNCTION__ . ' - $gitium_remote_tracking_branch = ' .get_transient('gitium_remote_tracking_branch', '') );
}
if( gitium_get_keypair() ) {
list( $git_public_key, $git_private_key ) = gitium_get_keypair();
$this->set_key( $git_private_key );
//error_log( __FUNCTION__ . ' - $git_public_key = ' . $git_public_key );
}
$this->init();
}

function _rrmdir( $dir ) {
Expand Down Expand Up @@ -231,11 +259,36 @@ function get_behind_commits() {
}

function init() {
file_put_contents( "$this->repo_dir/.gitignore", $this->gitignore );
list( $return, ) = $this->_call( 'init' );
if( file_exists( "$this->repo_dir/.gitignore_gitium" ) ) {
copy("$this->repo_dir/.gitignore_gitium", "$this->repo_dir/.gitignore");
}
if( ! file_exists( "$this->repo_dir/.gitignore" ) ) {
file_put_contents( "$this->repo_dir/.gitignore", $this->gitignore );
}
$remote_url = get_option( 'gitium_remote_url', "" );
if(empty($remote_url)) {
if( ! $this->is_dot_git_dir( "$this->repo_dir/.git" ) ) {
list( $return, ) = $this->_call( 'init' );
}
}
if( !file_exists("$this->repo_dir/.git") && !file_exists("$this->repo_dir/.temp_cloned_repo") ) {
$this->_call( 'clone', $remote_url, "$this->repo_dir/.temp_cloned_repo" );
if($this->get_last_error()) {
error_log( __FUNCTION__ . ' - GitClassWrapper error: ' . $this->get_last_error() );
}
elseif(!file_exists("$this->repo_dir/.temp_cloned_repo/.git")) {
error_log( __FUNCTION__ . ' - Finished cloning, but $this->repo_dir/.temp_cloned_repo/.git does not exist.');
}
`cp -r $this->repo_dir/.temp_cloned_repo/.git $this->repo_dir`;
`rm -rf $this->repo_dir/.temp_cloned_repo`;
if(!file_exists("$this->repo_dir/.git")) {
error_log( __FUNCTION__ . " - GitClassWrapper error: $this->repo_dir/.git does not exist" );
}
}
$this->_call( 'config', 'user.email', '[email protected]' );
$this->_call( 'config', 'user.name', 'Gitium' );
$this->_call( 'config', 'push.default', 'matching' );

return ( 0 == $return );
}

Expand All @@ -250,6 +303,10 @@ function is_dot_git_dir( $dir ) {
}

function cleanup() {
if(file_exists("$this->repo_dir/.git")) {
error_log( "GitWrapper->cleanup(), deleting existing .git folder" );
}

$dot_git_dir = realpath( $this->repo_dir . '/.git' );
if ( $this->is_dot_git_dir( $dot_git_dir ) && $this->_rrmdir( $dot_git_dir ) ) {
if ( WP_DEBUG ) {
Expand Down
2 changes: 1 addition & 1 deletion gitium/inc/class-gitium-menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function disconnect_repository() {
return;
}
check_admin_referer( 'gitium-admin' );
gitium_uninstall_hook();
gitium_uninstall_hook(false);
if ( ! $this->git->remove_remote() ) {
$this->redirect( 'Could not remove remote.' );
}
Expand Down
7 changes: 6 additions & 1 deletion gitium/inc/class-gitium-submenu-configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ public function init_process( $remote_url ) {

public function init_repo() {
$remote_url = filter_input(INPUT_POST, 'remote_url', FILTER_SANITIZE_STRING);
if ( $remote_url != get_option( 'gitium_remote_url', '' ) ) {
update_option( 'gitium_remote_url', $remote_url);
error_log( __FUNCTION__ . ' - $remote_url = ' . $remote_url );
}
$gitium_submit_fetch = filter_input(INPUT_POST, 'GitiumSubmitFetch', FILTER_SANITIZE_STRING);
if ( ! isset( $gitium_submit_fetch ) || ! isset( $remote_url ) ) {
return;
Expand Down Expand Up @@ -133,11 +137,12 @@ public function choose_branch() {
}

private function setup_step_1_remote_url() {
$remote_url = get_option( 'gitium_remote_url', "" );
?>
<tr>
<th scope="row"><label for="remote_url"><?php _e( 'Remote URL', 'gitium' ); ?></label></th>
<td>
<input type="text" class="regular-text" name="remote_url" id="remote_url" placeholder="[email protected]:user/example.git" value="">
<input type="text" class="regular-text" name="remote_url" id="remote_url" placeholder="[email protected]:user/example.git" value="<?php echo esc_attr( $remote_url ); ?>">
<p class="description"><?php _e( 'This URL provide access to a Git repository via SSH, HTTPS, or Subversion.', 'gitium' ); ?><br />
<?php _e( 'If you need to authenticate over "https://" instead of SSH use: <code>https://user:[email protected]/user/example.git</code>', 'gitium' ); ?></p>
</td>
Expand Down
6 changes: 6 additions & 0 deletions gitium/inc/ssh-git
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#!/bin/sh
SSH_AUTH_SOCK=''
SSH="ssh -q -F /dev/null -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"

if [ "$GIT_SSH_KEY" ] ; then
echo "$GIT_SSH_KEY" > /tmp/gitium.key
export GIT_KEY_FILE="/tmp/gitium.key"
fi

if [ -z "$GIT_KEY_FILE" ] ; then
exec $SSH "$@"
else
Expand Down