gitprep / templates / branches.html.ep /
2720305 7 years ago
2 contributor
308 lines | 11.821kb
<%
  # API
  my $api = gitprep_api;

  # Parameters
  my $user_id = param('user');
  my $project_id = param('project');
  my $op = param('op') || '';
  my $page = param('page') || 1;
  
  # Git
  my $git = $self->app->git;

  # Delete
  my $errors;
  if ($op eq 'delete' && lc $self->req->method eq 'post') {
    
    # Forbbiden
    unless ($api->logined($user_id)) {
      $self->redirect_to('/');
      return;    
    }
    
    # Parameters
    my $branch = param('branch');
    
    # Validator
    my $vc = app->vc;
    
    # Validation result
    my $validation = $vc->validation;
    
    # "branch"
    if (!(defined $branch && length $branch)) {
      $validation->add_failed(branch => 'Branch name is empty');
    }

    if ($validation->is_valid) {
      # Delete branch
      eval { $git->delete_branch(app->rep_info($user_id, $project_id), $branch) };
      if (my $e = $@) {
        app->log->error(url_with . ": $e");
        $errors = ['Internal Error'];
      }
      else {
        $self->flash(message => "Branch $branch is deleted.");
        $self->redirect_to;
        return;
      }
    }
    else { $errors = $validation->messages }
  }
  
  # Default branch
  my $default_branch_name = app->manager->default_branch($user_id, $project_id);
  my $default_branch = $git->branch($self->app->rep_info($user_id, $project_id), $default_branch_name);
  
  # Branches
  my $branches = [];

  # Pagenation
  my $page_count = 20;
  my $skip = $page_count * ($page - 1);
  
  my $all_branches = $git->branches($self->app->rep_info($user_id, $project_id));
  my $max = 0;
  my $all_count = 0;
  for my $branch (@$all_branches) {
    $branch->{status} = $git->branch_status(
      $self->app->rep_info($user_id, $project_id),
      $default_branch->{name},
      $branch->{name}
    );
    $max = $branch->{status}{ahead} if $max < $branch->{status}{ahead};
    $max = $branch->{status}{behind} if $max < $branch->{status}{behind};
    
    if ($branch->{status}{ahead} > 100) {
      $branch->{status}{ahead_bar} = 100;
    }
    else {
      $branch->{status}{ahead_bar} = $branch->{status}{ahead};
    }
    if ($branch->{status}{behind} > 100) {
      $branch->{status}{behind_bar} = 100;
    }
    else {
      $branch->{status}{behind_bar} = $branch->{status}{behind};
    }
    
    $max = 100 if $max > 100;
    
    if ($all_count >= $skip && $all_count < $skip + $page_count) {
      push @$branches, $branch;
    }
    $all_count++;
  }
  
  my $original_project_row_id = app->dbi->model('project')->select(
    'original_project',
    where => {'user.id' => $user_id, 'project.id' => $project_id}
  )->value;
  
  my $original_project;
  if ($original_project_row_id) {
    $original_project = app->dbi->model('project')->select(
      [
        {__MY__ => '*'},
        {user => ['id']}
      ],
      where => {'project.row_id' => $original_project_row_id}
    )->one;
  }
%>

% layout 'common', title => "branches  \x{30fb} $user_id/$project_id";

  %= include '/include/header';
  
  <div class="container" style="padding-bottom:30px">
    %= include '/include/errors', errors => $errors;
    %= include '/include/message', message => flash('message');
    
    <!-- Branches (for tests) -->
    
    <h1 class="topic1">Branches</h1>
      % if (@$branches) {
        <ul class="branches">
          % if (@$branches) {
            % my $branches_count;
            % for (my $i = 0; $i < @$branches; $i++) {
              <%
                my $branch = $branches->[$i];
                my $branch_name = $branch->{name};

                my $commit_author_email = $branch->{commit}{author_email};
                my $commit_author_id = app->dbi->model('user')->select(
                  'id',
                  where => {email => $commit_author_email}
                )->value;

                my $pull_request = app->dbi->model('pull_request')->select(
                  {__MY__ => ['row_id']},
                  where => {
                    'base_project.id' => $project_id,
                    'base_project__user.id' => $user_id,
                    base_branch => $default_branch_name,
                    target_branch => $branch_name
                  }
                )->one;
                
                my $issue;
                if ($pull_request) {
                  $issue = app->dbi->model('issue')->select(
                    where => {pull_request => $pull_request->{row_id}}
                  )->one;
                }
                
              %>

              <li>
                <ul>
                  <li>
                    <div class="branches-name">
                      <a href="<%= url_for("/$user_id/$project_id/tree/$branch_name") %>">
                        <%= $branch_name %>
                      </a>
                    </div>
                    <div class="branches-age" title="<%= $branch->{commit}{age_string_datetime_local} %>">
                      Updated <%= $branch->{commit}{age_string} %> by
                    </div>
                    <div class="branches-author"  title="<%= $branch->{commit}{author_email} %>">
                      % if (defined $commit_author_id) {
                        <a href="<%= url_for("/$commit_author_id") %>"><%= $commit_author_id %></a>
                      % } else {
                        <%= $branch->{commit}{author_name} %>
                      % }
                    </div>
                  </li>
                  <li class="second-child">
                    % if ($branch_name eq $default_branch_name) {
                      <div style="padding-left:80px;padding-top:3px">
                        <div class="branches-default">
                          Default
                        </div>
                      </div>
                    % } else {
                      <table class="ahead-behind">
                        <tr>
                          <td class="ahead-behind-behind-count">
                            <%= $branch->{status}{behind} %>
                          </td>
                          <td class="ahead-behind-separate">
                          </td>
                          <td style="padding-left:3px">
                            <%= $branch->{status}{ahead} %>
                          </td>
                        </tr>
                        <tr>
                          <td style="width:100px">
                            <div style="margin-left:auto;margin-right:0;background:#dcdcdc;width:<%= $branch->{status}{behind_bar} %>%;height:4px"></div>
                          </td>
                          <td class="ahead-behind-separate">
                          </td>
                          <td style="width:100px">
                            <div style="background:#dcdcdc;width:<%= $branch->{status}{ahead_bar} %>%;height:4px"></div>
                          </td>
                        </tr>
                      </table>
                    % }
                  </li>
                  <li class="last-child">
                    % if ($branch_name eq $default_branch_name) {
                      % if ($api->logined($user_id)) {
                        <a href="<%= url_for("/$user_id/$project_id/settings#form-default-branch") %>" type="submit" class="btn btn-small">Change default branch</a>
                      % }
                    % } else {
                      % if ($api->logined) {
                        % if ($pull_request) {
                          #<%= $issue->{number} %>
                          % if ($issue->{open}) {
                            <a class="branches-open" href="<%= url_for("/$user/$project/pull/$issue->{number}") %>">
                              open
                            </a>
                          % } else {
                            <a class="branches-close" href="<%= url_for("/$user/$project/pull/$issue->{number}") %>">
                              closed
                            </a>
                          % }
                        % } else {
                          <% 
                            my $compare_url;
                            if ($original_project) {
                              my $original_user_id = $original_project->{'user.id'};
                              my $original_project_id = $original_project->{id};
                              
                              my $exists_original_branch_name = app->git->exists_branch(
                                app->rep_info($original_user_id, $original_project_id),
                                $branch_name
                              );
                              if ($exists_original_branch_name) {
                                $compare_url = url_for("/$original_user_id/$original_project_id/compare/$branch_name...$user_id:$branch_name")->query(expand => 1);
                              }
                              else {
                                my $original_project_default_branch = app->manager->default_branch($original_user_id, $original_project_id);
                                $compare_url = url_for("/$original_user_id/$original_project_id/compare/$original_project_default_branch...$user_id:$branch_name")->query(expand => 1);
                              }
                            }
                            else {
                              $compare_url = url_for("/$user_id/$project_id/compare/$branch_name")->query(expand => 1);
                            }
                          %>
                          <a class="btn btn-small" href="<%= $compare_url %>">
                            New pull request
                          </a>
                        % }
                      % } else {
                        <a class="btn btn-small" href="<%= url_for("/$user_id/$project_id/compare/$branch_name")->query(expand => 1) %>">
                          Compare
                        </a>
                      % }
                      % if ($api->logined($user_id)) {
                        % if ($pull_request && $issue->{open}) {
                          <button class="btn btn-small disabled delete-branch"value="Delete">Delete</button>
                        % } else {
                          <form action="<%= url_for->query(op => 'delete') %>" method="post" style="display:inline-block">
                            <input type="submit" class="btn btn-small delete-branch" style="color:#900;" value="Delete">
                            %= hidden_field branch => $branch_name;
                          </form>
                        % }
                      % }
                    % }
                  </li>
                </ul>
              </li>
              % $branches_count++;
            % }


            <div class="pagenation-container" style="margin-top:20px">
              <ul class="pagenation">
                % if ($page == 1) {
                  <li><span>Newer</span></li>
                % } else {
                  % my $newer_page = $page - 1;
                  <li>
                    <a href="<%= url_for("/$user_id/$project_id/branches?page=$newer_page") %>">Newer</a>
                  </li>
                % }
                % if ($branches_count < $page_count) {
                  <li><span>Older</span></li>
                % } else {
                  % my $older_page = $page + 1;
                  <li>
                    <a href="<%= url_for("/$user_id/$project_id/branches?page=$older_page") %>">Older</a>
                  </li>
                % }
              </ul>
            </div>
          % } else {
            <li style="text-align:center;text-color:#767676">
              There aren’t any branches.
            </li>
          % }
        </ul>
      % }
  </div>
  
  %= include '/include/footer';