You are here

function _git_deploy_base in Git Deploy 7

Same name and namespace in other branches
  1. 6 git_deploy.module \_git_deploy_base()

Checks for most recent local commit in a set of remote branches.

Parameters

Git $git: A Git instance.

string[] $remotes: Remote branch commit hashes keyed by reference, in descending order.

string $githash: Local commit hash.

array $upstream: Things we need to know to determine status.

Return value

string[] Local history starting with most recent common commit hash.

1 call to _git_deploy_base()
_git_deploy_get_upstream in ./git_deploy.module
Gets upstream info.

File

./git_deploy.module, line 242
Adds project, version and date information to projects checked out with Git.

Code

function _git_deploy_base(Git $git, array $remotes, $githash, array &$upstream) {
  $local_queue = array(
    @$git
      ->getObject($githash),
  );
  $local = $remote = array();
  foreach ($remotes as $ref => $hash) {
    if ($hash == $githash) {

      // If remote branch matches local branch, it is the best match.
      $local = array(
        $githash,
      );
      list(, , $upstream['remote'], $upstream['branch']) = explode('/', $ref, 4);

      // Local history contains last remote commit.
      $upstream['synced'] = TRUE;
      break;
    }
    elseif (in_array($hash, $remote)) {

      // Last remote branch was more recent. For performance, stop looking.
      break;
    }
    elseif (in_array($hash, $local)) {

      // This remote branch is more recent.
      $githash = $hash;
      list(, , $upstream['remote'], $upstream['branch']) = explode('/', $ref, 4);

      // Local history contains last remote commit.
      $upstream['synced'] = TRUE;
    }
    else {

      // Find most recent common commit.
      $remote_queue = array(
        @$git
          ->getObject($hash),
      );
      $remote = array();

      // See if remote commit has been found in local history.
      while ($commit = array_pop($remote_queue)) {

        // Add remote commit to remote history.
        array_unshift($remote, $commit
          ->getName());
        if (!in_array($commit
          ->getName(), $local)) {

          // Add parent commits to remote queue to be checked.
          foreach ($commit->parents as $parent) {

            // Only check parent if not already in remote history.
            if (!in_array($parent, $remote)) {
              $remote_queue[] = @$git
                ->getObject($parent);
            }
          }

          // See if local commit has been found in remote history.
          $commit = array_pop($local_queue);

          // Add local commit to local history.
          array_unshift($local, $commit
            ->getName());
          if (!in_array($commit
            ->getName(), $remote)) {

            // Add parent commits to local queue to be checked.
            foreach ($commit->parents as $parent) {

              // Only check parent if not already in local history.
              if (!in_array($parent, $local)) {
                $local_queue[] = @$git
                  ->getObject($parent);
              }
            }
          }
          elseif (!isset($last_commit) || in_array($last_commit, $remote)) {

            // Found most recent common commit in local history.
            list(, , $upstream['remote'], $upstream['branch']) = explode('/', $ref, 4);
            $last_commit = $githash = $commit
              ->getName();
            break;
          }
        }
        elseif (!isset($last_commit) || in_array($last_commit, $remote)) {

          // Found most recent common commit in remote history.
          list(, , $upstream['remote'], $upstream['branch']) = explode('/', $ref, 4);
          $last_commit = $githash = $commit
            ->getName();
          break;
        }
      }

      // If we did not find a common commit, check rest of remote queue.
      if (!in_array($githash, $remote)) {
        while ($commit = array_pop($remote_queue)) {

          // Add remote commit to remote history.
          array_unshift($remote, $commit
            ->getName());
          if (!in_array($commit
            ->getName(), $local)) {

            // Add parent commits to remote queue to be checked.
            foreach ($commit->parents as $parent) {

              // Only check parent if not already in remote history.
              if (!in_array($parent, $remote)) {
                $remote_queue[] = @$git
                  ->getObject($parent);
              }
            }
          }
          elseif (!isset($last_commit) || in_array($last_commit, $remote)) {

            // Found most recent common commit in remote history.
            list(, , $upstream['remote'], $upstream['branch']) = explode('/', $ref, 4);
            $last_commit = $githash = $commit
              ->getName();
            break;
          }
        }
      }
      elseif (!in_array($githash, $local)) {
        while ($commit = array_pop($local_queue)) {

          // Add local commit to local history.
          array_unshift($local, $commit
            ->getName());
          if (!in_array($commit
            ->getName(), $remote)) {

            // Add parent commits to local queue to be checked.
            foreach ($commit->parents as $parent) {

              // Only check parent if not already in local history.
              if (!in_array($parent, $local)) {
                $local_queue[] = @$git
                  ->getObject($parent);
              }
            }
          }
          elseif (!isset($last_commit) || in_array($last_commit, $remote)) {

            // Found most recent common commit in local history.
            list(, , $upstream['remote'], $upstream['branch']) = explode('/', $ref, 4);
            $last_commit = $githash = $commit
              ->getName();
            break;
          }
        }
      }

      // Look for last commit in local history for update status.
      $upstream['synced'] = in_array($hash, $local);
    }
  }
  return array_slice($local, array_search($githash, $local));
}