You are here

function _git_deploy_get_upstream in Git Deploy 7

Same name and namespace in other branches
  1. 8.2 git_deploy.module \_git_deploy_get_upstream()
  2. 6.2 git_deploy.module \_git_deploy_get_upstream()
  3. 6 git_deploy.module \_git_deploy_get_upstream()
  4. 7.2 git_deploy.module \_git_deploy_get_upstream()

Gets upstream info.

Parameters

Git $git: A Git instance.

string $pattern: Pattern for matching branch names, without trailing ".x". Must not include repository name. Also used to check for release tags.

array|false $git_config: Git configuration if found.

Return value

string[] Array with the following keys, if found:

  • branch: Best matching remote branch.
  • remote: Remote repository containing best matching branch.
  • tag: Release tag from last common commit in matching branch.
  • datestamp: Unix timestamp of last common commit.
1 call to _git_deploy_get_upstream()
git_deploy_system_info_alter in ./git_deploy.module
Implements hook_system_info_alter().

File

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

Code

function _git_deploy_get_upstream(Git $git, $pattern = '*', $git_config = FALSE) {
  $pattern = str_replace(array(
    '.',
    '*',
  ), array(
    '\\.',
    '\\d+',
  ), $pattern);
  $upstream = array();

  // List references in descending version order.
  $refs = $git
    ->getrefs();
  uksort($refs, 'version_compare');
  $refs = array_reverse($refs);

  // List remote branches matching pattern. Make special exception for master.
  // If origin exists, don't check any other remote repositories.
  $remotes = preg_grep("/^refs\\/remotes\\/origin\\/(?:{$pattern}\\.x|master)\$/", array_keys($refs));
  if (empty($remotes)) {
    $remotes = preg_grep("/^refs\\/remotes\\/.+\\/(?:{$pattern}\\.x|master)\$/", array_keys($refs));
  }
  $remotes = array_intersect_key($refs, array_flip($remotes));
  if (!empty($remotes)) {
    list(, , $upstream['remote']) = explode('/', key($remotes), 4);

    // List tags matching pattern.
    $tags = array_intersect_key($refs, array_flip(preg_grep("/^refs\\/tags\\/{$pattern}\\.\\d+.*/", array_keys($refs))));

    // Check for tag on current commit.
    $githash = _git_deploy_tag($git, $tags, array(
      $git
        ->revParse(),
    ), $upstream);
    if (!isset($upstream['tag'])) {

      // Check if we are tracking a valid remote branch.
      $head = trim(current(file($git->dir . '/HEAD')));
      if (strpos($head, 'ref: refs/heads/') === 0) {

        // Get local branch name.
        $head = str_replace('ref: refs/heads/', '', $head);
        if (!empty($git_config) && isset($git_config["branch {$head}"]['remote']) && isset($git_config["branch {$head}"]['merge'])) {

          // Get remote branch name.
          $ref = str_replace('refs/heads/', 'refs/remotes/' . $git_config["branch {$head}"]['remote'] . '/', $git_config["branch {$head}"]['merge']);
          if (isset($remotes[$ref])) {
            $local = _git_deploy_base($git, array(
              $ref => $remotes[$ref],
            ), $githash, $upstream);

            // If most recent common commit is not the current commit, check for
            // tag on most recent common commit.
            if (reset($local) != $githash) {
              $githash = _git_deploy_tag($git, $tags, $local, $upstream);
            }
          }
        }
      }

      // If we are not tracking a valid remote branch, find best best matching
      // remote branch.
      if (!isset($upstream['branch'])) {
        $local = _git_deploy_base($git, $remotes, $githash, $upstream);

        // If most recent common commit is not the current commit, check for tag
        // on most recent common commit.
        if (reset($local) != $githash) {
          $githash = _git_deploy_tag($git, $tags, $local, $upstream);
        }
      }
    }

    // Find the timestamp for the current commit.
    $upstream['datestamp'] = @$git
      ->getObject($githash)->committer->time;
  }
  return $upstream;
}