diff --git a/versioncontrol_project_git/versioncontrol_project_git.info b/versioncontrol_project_git/versioncontrol_project_git.info new file mode 100644 index 0000000..7b4f7f5 --- /dev/null +++ b/versioncontrol_project_git/versioncontrol_project_git.info @@ -0,0 +1,6 @@ +name = "Version Control Git / Project" +description = "Provides additional Git-specific integration between the Version Control API suite and the Project suite." +core = 6.x +package = Version Control +dependencies[] = versioncontrol_git +dependencies[] = versioncontrol_project diff --git a/versioncontrol_project_git/versioncontrol_project_git.module b/versioncontrol_project_git/versioncontrol_project_git.module new file mode 100644 index 0000000..e3000ad --- /dev/null +++ b/versioncontrol_project_git/versioncontrol_project_git.module @@ -0,0 +1,60 @@ + 'Default branch', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('versioncontrol_project_git_default_branch_form', 1), + 'access callback' => 'versioncontrol_project_git_default_branch_access', + 'access arguments' => array(1), + 'type' => MENU_LOCAL_TASK, + 'weight' => 3, + 'file' => 'versioncontrol_project_git.pages.inc', + ); + + return $items; +} + +/** + * Access callback: Checks for a user's access to a valid Git repository. + * + * Path: node/%project_node/edit/default-branch + * + * @param $project + * The project object that should have an attached repository. + * + * @return + * TRUE if the project has an attached Git repository with at least one + * branch that the user has write access to, FALSE otherwise. + * + * @see versioncontrol_project_git_menu() + */ +function versioncontrol_project_git_default_branch_access($project) { + // Display only on nodes that have a repository attached. + if (empty($project->versioncontrol_project)) { + return FALSE; + } + + // Ensure it is a Git repository. + $repo = $project->versioncontrol_project['repo']; + if (!$repo instanceof VersioncontrolGitRepository) { + return FALSE; + } + + // Ensure at least one branch exists. + if (!count($repo->loadBranches())) { + return FALSE; + } + + // Check the permissions. + return project_user_access($project, 'write to vcs'); +} diff --git a/versioncontrol_project_git/versioncontrol_project_git.pages.inc b/versioncontrol_project_git/versioncontrol_project_git.pages.inc new file mode 100644 index 0000000..3163738 --- /dev/null +++ b/versioncontrol_project_git/versioncontrol_project_git.pages.inc @@ -0,0 +1,82 @@ +versioncontrol_project['repo']; + $form['#repo'] = $repo; + $form['#nid'] = $project->nid; + + // Add the radios widget for default branch selection. + $form['default_branch'] = array( + '#title' => t('The default repository branch'), + '#description' => t('This branch will be checked out by default when someone clones the project.'), + '#type' => 'radios', + '#options' => array(), + ); + + // Make all branches available as options. + $branches = $repo->loadBranches(); + foreach ($branches as $id => $branch) { + $form['default_branch']['#options'][$id] = check_plain($branch->name); + } + uasort($form['default_branch']['#options'], '_versioncontrol_project_git_inverse_version_compare'); + + // Make the current default branch the default selection. + if ($repo->getDefaultBranch()) { + $default_branch = $repo->loadBranches(NULL, array('name' => $repo->getDefaultBranch())); + foreach ($default_branch as $id => $branch) { + $form['default_branch']['#default_value'] = $id; + } + } + + // Add a submit button. + $form['submit'] = array( + '#type' => 'submit', + '#value' => t('Save'), + ); + + return $form; +} + +/** + * Form submission handler for versioncontrol_project_git_default_branch_form(). + */ +function versioncontrol_project_git_default_branch_form_submit($form, &$form_state) { + $repo = $form['#repo']; + $default_branch = reset($repo->loadBranches(array($form_state['values']['default_branch']))); + + if ($default_branch) { + try { + $repo->setDefaultBranch($default_branch->name); + $form_state['redirect'] = 'node/' . $form['#nid']; + drupal_set_message(t('The default branch has been changed to %branch.', array('%branch' => $default_branch->name))); + } + catch (Exception $e) { + drupal_set_message(filter_xss($e->getMessage()), 'error'); + } + } +} + +/** + * Compares two branch names. + * + * Callback for uasort() within versioncontrol_project_git_default_branch_form(). + * + * @param $a + * The first branch name. + * @param $b + * The secound branch name. + * + * @return + * The inverse of version_compare(). + */ +function _versioncontrol_project_git_inverse_version_compare($a, $b) { + return version_compare($b, $a); +}