/**
  *  Add new gitolite repository
  * @throws ValidationErrors
  */
 function add_git_repo()
 {
     $is_gitolite = GitoliteAdmin::is_gitolite();
     if (!ProjectSourceRepositories::canAdd($this->logged_user, $this->active_project)) {
         $this->response->forbidden();
     }
     // if
     $project = $this->active_project;
     $project_id = $project->getId();
     $logged_user = $this->logged_user;
     $user_id = $logged_user->getId();
     $no_key_warning = FALSE;
     $view_url = "";
     if (AngieApplication::isModuleLoaded("source") && $this->getControllerName() == 'project_tracking_gitolite') {
         $do_continue = true;
     }
     if ($do_continue) {
         $project_users = Projects::findById($project_id);
         // Prepare users map
         $users_details = $this->active_project->users()->describe($this->logged_user, true, true, STATE_VISIBLE);
         $user_detail_permissions = array();
         $repoobj = new ProjectSourceRepositories();
         if (is_foreachable($users_details)) {
             foreach ($users_details as $key => $value) {
                 // check key exists
                 $user_keys = GitoliteAc::check_keys_added($value['user']['id']);
                 if ($user_keys > 0) {
                     $objuser = new rtmUser($value['user']['id']);
                     $user_detail_permissions[$value['user']['id']] = array('readaccess' => $repoobj->canAccess($objuser, $project), 'writeaccess' => $repoobj->canAdd($objuser, $project), 'writeaccessplus' => $repoobj->canManage($objuser, $project), 'user_keys' => $user_keys);
                     $allowed_users[$value['user']['id']] = $value['user']['name'];
                 }
             }
         }
         // Add Administrator , Leaders and Project Manager in allowed people list
         if ($this->logged_user->isAdministrator() || $this->logged_user->isProjectManager()) {
             $objuser = new rtmUser($user_id);
             $user_keys = GitoliteAc::check_keys_added($user_id);
             if ($user_keys) {
                 $user_detail_permissions[$user_id] = array('readaccess' => $repoobj->canAccess($objuser, $project), 'writeaccess' => $repoobj->canAdd($objuser, $project), 'writeaccessplus' => $repoobj->canManage($objuser, $project), 'user_keys' => $user_keys);
                 $allowed_users[$user_id] = $logged_user->getName();
             } else {
                 $no_key_warning = TRUE;
                 $view_url = $this->logged_user->getViewUrl();
             }
         }
         $this->response->assign(array('curr_users' => $allowed_users, 'user_detail_permissions' => $user_detail_permissions, 'form_action' => Router::assemble('add_git_repository', array('project_slug' => $project->getSlug())), 'noaccess' => GITOLITE_NOACCESS, 'readaccess' => GITOLITE_READACCESS, 'manageaccess' => GITOLITE_MANAGEACCESS, 'is_gitolite' => $is_gitolite, 'no_key_warning' => $no_key_warning, 'view_url' => $view_url));
     } else {
         $this->response->assign(array('add_error' => TRUE));
     }
     if ($this->request->isSubmitted()) {
         // check for form submission
         try {
             /* Check form with validation error */
             $repository_data = $this->request->post('repository');
             /* print_r($repository_data);
                die(); */
             $errors = new ValidationErrors();
             $post_data = $this->request->post();
             /* print_r($post_data['access']);
                die(); */
             $settings = GitoliteAdmin::get_admin_settings();
             $is_remote = !isset($settings["git_server_location"]) || $settings["git_server_location"] != "remote" ? FALSE : TRUE;
             if ($is_remote == FALSE) {
                 $sever_user_path = GitoliteAdmin::get_server_user_path();
                 if (!$sever_user_path) {
                     $errors->addError('Repository path on server invalid');
                 }
             }
             $repo_name = trim($repository_data['name']);
             $access = $post_data['access'];
             if ($repo_name == "") {
                 $errors->addError('Please enter repository name', 'repo_name');
             }
             if (!is_array($access) && count($access) == 0) {
                 $errors->addError('Select access levels for user', 'access');
             }
             /* Check for duplications repository name and Key */
             if (!$errors->hasErrors()) {
                 if (!preg_match("/^[A-Za-z0-9-]+\$/", $repo_name)) {
                     $errors->addError('Please enter valid repository name.', 'repo_name');
                 }
                 $dup_cnt = ProjectGitolite::check_duplication($project_id, $repository_data);
                 if (count($dup_cnt) == 0) {
                     $errors->addError('Problem occured while saving data, please try again.');
                 } elseif (count($dup_cnt) > 0) {
                     if ($dup_cnt[0]['dup_name_cnt'] > 0) {
                         $errors->addError('Repository name already used');
                     }
                 }
             }
             // if errors found throw error exception
             if ($errors->hasErrors()) {
                 throw $errors;
             }
             /** save gitolite details in database * */
             // save reponame
             try {
                 DB::beginWork('Creating a new repository @ ' . __CLASS__);
                 /**
                  * if gitolite is setup on remote, change repo path
                  */
                 if (!$is_remote) {
                     $repo_path = $sever_user_path . "/repositories/" . $repository_data['name'] . ".git";
                 } elseif ($is_remote) {
                     $repo_path = GIT_FILES_PATH . "/" . $repo_name;
                 }
                 if (is_array($post_data)) {
                     $repository_path_url = array('repository_path_url' => $repo_path);
                     $notif_setting = isset($repository_data["disable_notifications"]) ? "yes" : "no";
                     $repo_notification_setting = array('repo_notification_setting' => $notif_setting);
                 }
                 $repository_data = array_merge($repository_data, $repository_path_url);
                 $repository_data = array_merge($repository_data, $repo_notification_setting);
                 $this->active_repository = new GitRepository();
                 $this->active_repository->setAttributes($repository_data);
                 $this->active_repository->setCreatedBy($this->logged_user);
                 $this->active_repository->save();
                 $repo_fk = $this->active_repository->getId();
                 if ($repo_fk) {
                     $clone_url = $settings['git_clone_url'] . $repo_name;
                     $body = $clone_url;
                     $this->project_object_repository->setName($this->active_repository->getName());
                     $this->project_object_repository->setBody($body);
                     $this->project_object_repository->setParentId($this->active_repository->getId());
                     $this->project_object_repository->setVisibility($repository_data['visibility']);
                     $this->project_object_repository->setProjectId($this->active_project->getId());
                     $this->project_object_repository->setCreatedBy($this->logged_user);
                     $this->project_object_repository->setState(STATE_VISIBLE);
                     $this->project_object_repository->save();
                     $repo_id = ProjectGitolite::add_repo_details($repo_fk, $project_id, $user_id, $repo_path, $repository_data, $clone_url);
                     if ($repo_id) {
                         $add_access = ProjectGitolite::add_access_levels($repo_id, serialize($post_data['access']), $user_id, 1);
                         if ($add_access) {
                             $res = ProjectGitolite::render_conf_file();
                             $dir = $settings['gitoliteadminpath'] . "gitolite-admin";
                             $command = "cd " . $dir . " && git add * && git commit -am 'render conf file' && git push  || echo 'Not found'";
                             exec($command, $output, $return_var);
                             if ($is_remote) {
                                 $git_server = $settings['git_clone_url'];
                                 //$command = "cd ".$settings['gitoliteadminpath']." && git clone ".$git_server.":".$repo_name;
                                 chdir(GIT_FILES_PATH);
                                 $command = "git clone " . $git_server . $repo_name;
                                 exec($command, $output, $return_var);
                                 /* @set_time_limit(0);
                                    $pull_all_branches = ProjectGitolite::pull_branches($repo_path);
                                    if(!$pull_all_branches)
                                    {
                                    @ProjectGitolite::remove_directory($work_git_path);
                                    $errors->addError('Error while saving branches.');
                                    throw $errors;
                                    } */
                             }
                         } else {
                             $errors->addError('Error while saving access levels.');
                             throw $errors;
                         }
                     } else {
                         $errors->addError('Error while saving repository.');
                         throw $errors;
                     }
                 } else {
                     $errors->addError('Error while saving repository.');
                     throw $errors;
                 }
                 DB::commit('Repository created @ ' . __CLASS__);
                 $this->response->respondWithData($this->active_repository);
             } catch (Exception $e) {
                 DB::rollback('Failed to create a repository @ ' . __CLASS__);
                 $this->response->exception($e);
             }
         } catch (Exception $e) {
             DB::rollback('Failed to create a repository @ ' . __CLASS__);
             $this->response->exception($e);
         }
     }
 }