diff --git a/git_deploy.module b/git_deploy.module index 731a5bd..3cb905e 100644 --- a/git_deploy.module +++ b/git_deploy.module @@ -22,19 +22,19 @@ function git_deploy_system_info_alter(array &$info, Extension $file, $type) { static $projects = []; if ($file->origin == 'core' || empty($info['version'])) { // Make sure Git operates in the right directory. - chdir($file->getPath()); + $git = 'git -C ' . $file->getPath(); // Verify that we are in a Git repository. - $directory = exec('git rev-parse --show-toplevel 2> ' . GIT_DEPLOY_ERROR_DUMP); + $directory = exec("$git rev-parse --show-toplevel 2> " . GIT_DEPLOY_ERROR_DUMP); if (!empty($directory)) { // Only check Git once per repository. if (!isset($projects[$directory])) { $projects[$directory] = []; // Get upstream info. list($core) = explode('.', $info['core']); - $upstream = _git_deploy_get_upstream($file->origin == 'core' ? "$core*" : "$info[core]-*"); + $upstream = _git_deploy_get_upstream($git, $file->origin == 'core' ? "$core*" : "$info[core]-*"); // Find the project name based on fetch URL. if (isset($upstream['remote'])) { - $fetch_url = exec("git config --get remote.$upstream[remote].url 2> " . GIT_DEPLOY_ERROR_DUMP); + $fetch_url = exec("$git config --get remote.$upstream[remote].url 2> " . GIT_DEPLOY_ERROR_DUMP); if (!empty($fetch_url)) { $projects[$directory]['project'] = basename($fetch_url, '.git'); } @@ -54,16 +54,14 @@ function git_deploy_system_info_alter(array &$info, Extension $file, $type) { } $info = $projects[$directory] + $info; } - // Switch back to Drupal root folder. - chdir(DRUPAL_ROOT); } } /** * Gets upstream info. * - * This function must be run from within a Git repository. - * + * @param string $git + * Git formatted for command line. * @param string $pattern * Pattern for matching branch names, without trailing ".x". Must not include * remote. Also used to check for release tags. @@ -75,49 +73,49 @@ function git_deploy_system_info_alter(array &$info, Extension $file, $type) { * - tag: Release tag from last common commit in matching branch. * - datestamp: Unix timestamp of last common commit. */ -function _git_deploy_get_upstream($pattern = '*') { +function _git_deploy_get_upstream($git, $pattern = '*') { $upstream = []; // Get tracked upstream branch. - $remote = exec('git rev-parse --abbrev-ref @{upstream} 2> ' . GIT_DEPLOY_ERROR_DUMP); + $remote = exec("$git rev-parse --abbrev-ref @{upstream} 2> " . GIT_DEPLOY_ERROR_DUMP); if (fnmatch("*/$pattern.x", $remote)) { // Set remote. list($upstream['branch'], $upstream['remote']) = array_reverse(explode('/', $remote)); // Find last common commit in both local and upstream. - $last_base = exec("git merge-base HEAD $remote 2> " . GIT_DEPLOY_ERROR_DUMP); + $last_base = exec("$git merge-base HEAD $remote 2> " . GIT_DEPLOY_ERROR_DUMP); // Get time of last common commit. - $upstream['datestamp'] = exec("git log -1 --pretty=format:%at $last_base 2> " . GIT_DEPLOY_ERROR_DUMP); + $upstream['datestamp'] = exec("$git log -1 --pretty=format:%at $last_base 2> " . GIT_DEPLOY_ERROR_DUMP); } else { // If local does not track an upstream branch, find best matching remote // branch. - exec('git remote 2> ' . GIT_DEPLOY_ERROR_DUMP, $remotes); + exec("$git remote 2> " . GIT_DEPLOY_ERROR_DUMP, $remotes); if (!empty($remotes)) { // Only consider the origin remote, if it exists. $branch_pattern = in_array('origin', $remotes) ? "origin/$pattern.x" : "*/$pattern.x"; // List matching branches in descending order. - exec("git branch -r --sort=-refname --list --format='%(refname:short)' $branch_pattern 2> " . GIT_DEPLOY_ERROR_DUMP, $branches); + exec("$git branch -r --sort=-refname --list --format='%(refname:short)' $branch_pattern 2> " . GIT_DEPLOY_ERROR_DUMP, $branches); if (!empty($branches)) { $remote = array_shift($branches); // Find last common commit in both local and upstream. - $last_base = exec("git merge-base HEAD $remote 2> " . GIT_DEPLOY_ERROR_DUMP); + $last_base = exec("$git merge-base HEAD $remote 2> " . GIT_DEPLOY_ERROR_DUMP); if (!empty($branches)) { // There's no reason to check other branches for a more recent common // commit if they do not contain the base commit, so filter the branch // list. - exec("git branch -r --sort=-refname --contains $last_base --no-contains $remote --format='%(refname:short)' $branch_pattern 2> " . GIT_DEPLOY_ERROR_DUMP, $branches); + exec("$git branch -r --sort=-refname --contains $last_base --no-contains $remote --format='%(refname:short)' $branch_pattern 2> " . GIT_DEPLOY_ERROR_DUMP, $branches); while (!empty($branches)) { $branch = array_shift($branches); // Find last common commit in both local and upstream. - $base = exec("git merge-base HEAD $branch 2> " . GIT_DEPLOY_ERROR_DUMP); + $base = exec("$git merge-base HEAD $branch 2> " . GIT_DEPLOY_ERROR_DUMP); // Count commits since last base commit. - $difference = exec("git rev-list --count $base ^$last_base 2> " . GIT_DEPLOY_ERROR_DUMP); + $difference = exec("$git rev-list --count $base ^$last_base 2> " . GIT_DEPLOY_ERROR_DUMP); // If base contains new commits, it is a better match. if (!empty($difference)) { $remote = $branch; $last_base = $base; if (!empty($branches)) { // Filter the branch list. - exec("git branch -r --sort=-refname --contains $last_base --no-contains $remote --format='%(refname:short)' $branch_pattern 2> " . GIT_DEPLOY_ERROR_DUMP, $branches); + exec("$git branch -r --sort=-refname --contains $last_base --no-contains $remote --format='%(refname:short)' $branch_pattern 2> " . GIT_DEPLOY_ERROR_DUMP, $branches); } } } @@ -125,13 +123,13 @@ function _git_deploy_get_upstream($pattern = '*') { // Set remote. list($upstream['branch'], $upstream['remote']) = array_reverse(explode('/', $remote)); // Check for latest release tag from last common commit. - exec("git tag --points-at $last_base $pattern.* 2> " . GIT_DEPLOY_ERROR_DUMP, $tags); + exec("$git tag --points-at $last_base $pattern.* 2> " . GIT_DEPLOY_ERROR_DUMP, $tags); if (!empty($tags)) { usort($tags, 'version_compare'); $upstream['tag'] = end($tags); } // Get time of last common commit. - $upstream['datestamp'] = exec("git log -1 --pretty=format:%at $last_base 2> " . GIT_DEPLOY_ERROR_DUMP); + $upstream['datestamp'] = exec("$git log -1 --pretty=format:%at $last_base 2> " . GIT_DEPLOY_ERROR_DUMP); } } } @@ -146,19 +144,19 @@ function git_deploy_update_status_alter(&$projects) { // Override the update status on dev branches. if ($project_info['install_type'] == 'dev' && in_array($project_info['status'], [UpdateManagerInterface::NOT_SECURE, UpdateManagerInterface::NOT_CURRENT])) { // Make sure Git operates in the right directory. - chdir(drupal_get_path(preg_replace('/-disabled$/', '', $project_info['project_type']), key($project_info['includes']))); + $git = 'git -C ' . drupal_get_path(preg_replace('/-disabled$/', '', $project_info['project_type']), key($project_info['includes'])); // Verify that we are in a Git repository. - $directory = exec('git rev-parse --show-toplevel 2> ' . GIT_DEPLOY_ERROR_DUMP); + $directory = exec("$git rev-parse --show-toplevel 2> " . GIT_DEPLOY_ERROR_DUMP); if (!empty($directory)) { $version = $project_info['existing_version']; if (isset($project_info['releases'][$version])) { $release = $project_info['releases'][$version]; if (isset($release['tag'])) { // Fetch latest commit from origin. - exec('git fetch origin 2> ' . GIT_DEPLOY_ERROR_DUMP); - $last_commit = exec("git log -1 --pretty=format:%H origin/$release[tag] 2> " . GIT_DEPLOY_ERROR_DUMP); + exec("$git fetch origin 2> " . GIT_DEPLOY_ERROR_DUMP); + $last_commit = exec("$git log -1 --pretty=format:%H origin/$release[tag] 2> " . GIT_DEPLOY_ERROR_DUMP); // See if local branch contains latest commit. - exec("git merge-base --is-ancestor $last_commit HEAD 2> " . GIT_DEPLOY_ERROR_DUMP, $output, $return_val); + exec("$git merge-base --is-ancestor $last_commit HEAD 2> " . GIT_DEPLOY_ERROR_DUMP, $output, $return_val); // We still need to compare commit time to release time because this // repository may have been cloned from a copy of the upstream // repository. Allow a 12 hour time difference between release and @@ -172,6 +170,4 @@ function git_deploy_update_status_alter(&$projects) { } } } - // Switch back to Drupal root folder. - chdir(DRUPAL_ROOT); }