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

fix maintenance problem when changes are only on remote #201

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions gitium/inc/class-git-wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,20 @@ function commit( $message, $author_name = '', $author_email = '' ) {
return ( $return === 0 ) ? $response[0] : false;
}

function pull() {
// Perform a git pull
list( $status, $output ) = $this->_call('pull');

// Check if the pull was successful
if ( $status !== 0 ) {
// Log or handle error if the pull fails
$this->log_error('Git pull failed: ' . $output);
return false;
}

return true;
}

function push( $branch = '' ) {
if ( ! empty( $branch ) ) {
list( $return, ) = $this->_call( 'push', '--porcelain', '-u', 'origin', $branch );
Expand Down
36 changes: 33 additions & 3 deletions gitium/inc/class-gitium-submenu-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,59 @@ public function save_ignorelist() {
}

public function save_changes() {
$gitium_save_changes = filter_input(INPUT_POST, 'GitiumSubmitSaveChanges', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$gitium_save_changes = filter_input(INPUT_POST, 'GitiumSubmitSaveChanges', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$gitium_commit_msg = filter_input(INPUT_POST, 'commitmsg', FILTER_SANITIZE_FULL_SPECIAL_CHARS);
if ( ! isset( $gitium_save_changes ) ) {
if ( ! isset( $gitium_save_changes ) ) {
return;
}
check_admin_referer( 'gitium-admin' );

gitium_enable_maintenance_mode() or wp_die( __( 'Could not enable the maintenance mode!', 'gitium' ) );

// Check if the local repository is behind the remote
$behind_commits = $this->git->get_behind_commits();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_behind_commits runs git rev-list which comparse only commits that exists localy. In order to check comits on remote you have to run a git fetch first, there is a function that I think does that fetch_ref().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can inspire from gitium_merge_and_push function and call merge_with_accept_mine but there is a lock that it should be aquired. You can try caling gitium_merge_and_push directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I see that gitium_merge_and_push is called below at line 143.

What is then issue that you try to fix? Do you have some STR, or maybe some errors messages ?
Maybe only calling gitium_disable_maintenace_mode is enough?

I see that this function save_changes only when Save changes button is pressed? Where problems with this?

if ( $behind_commits !== false && count($behind_commits) > 0 ) {
// Perform a git pull if the local branch is behind
if ( ! $this->git->pull() ) {
gitium_disable_maintenance_mode();
$this->redirect( __( 'Could not pull the latest changes from the repository!', 'gitium' ) );
return;
}
}

// Check if there are any changes in the working directory after pulling
list( $branch_status, $new_response ) = $this->git->status(true);
if (empty($new_response)) {
// No changes to commit, exit maintenance mode and redirect with success
gitium_disable_maintenance_mode();
$this->success_redirect( __( 'No changes to commit, repository is up-to-date.', 'gitium' ) );
return;
}

// Add all changes in the working directory
$this->git->add();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$this->git->add();
if ($this->add() == 0) {
gitium_disable_maintenance_mode();
$this->success_redirect( __( 'No changes to commit, repository is up-to-date.', 'gitium' ) );
return;
}


// Prepare the commit message
$commitmsg = sprintf( __( 'Merged changes from %s on %s', 'gitium' ), get_site_url(), date( 'm.d.Y' ) );
if ( isset( $gitium_commit_msg ) && ! empty( $gitium_commit_msg ) ) {
$commitmsg = $gitium_commit_msg;
}

$current_user = wp_get_current_user();
$commit = $this->git->commit( $commitmsg, $current_user->display_name, $current_user->user_email );
if ( ! $commit ) {
gitium_disable_maintenance_mode();
$this->redirect( __( 'Could not commit!', 'gitium' ) );
return;
}

$merge_success = gitium_merge_and_push( $commit );
gitium_disable_maintenance_mode();
if ( ! $merge_success ) {
$this->redirect( __( 'Merge failed: ', 'gitium' ) . $this->git->get_last_error() );
return;
}

$this->success_redirect( sprintf( __( 'Pushed commit: `%s`', 'gitium' ), $commitmsg ) );
}

Expand Down