gitprep / templates / branches.html.ep /
2c1f53f 8 years ago
2 contributor
345 lines | 11.714kb
<%

  # API
  my $api = gitprep_api;

  # Parameters
  my $user = param('user');
  my $project = param('project');
  my $op = param('op') || '';
  my $display = param('display') || 'overview';
  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)) {
      $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($user, $project, $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, $project);
  my $default_branch = $git->branch($user, $project, $default_branch_name);
  
  # Branches
  my $branch_types;
  my $branches_h = {
    default => [],
    active => [],
    stale => [],
    all => []
  };

  # Pagenation
  my $page_count = 20;
  my $skip = $page_count * ($page - 1);
  
  my $branches = $git->branches($user, $project);
  my $max = 0;
  my $active_count = 0;
  my $stale_count = 0;
  my $all_count = 0;
  for my $branch (@$branches) {
    $branch->{status} = $git->branch_status($user, $project, $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;
    
    my $branch_type;
    if ($branch->{name} eq $default_branch_name) {
      $branch_type = 'default';
    }
    elsif ($branch->{commit}{age} < 60 * 60 * 24 * (365 * 3)) {
      $branch_type = 'active';
    }
    else {
      $branch_type = 'stale';
    }
    
    if ($display eq 'overview') {
      if ($branch_type eq 'default') {
        push @{$branches_h->{default}}, $branch;
      }
      elsif ($branch_type eq 'active') {
        push @{$branches_h->{active}}, $branch;
      }
      elsif ($branch_type eq 'stale') {
        push @{$branches_h->{stale}}, $branch;
      }
    }
    elsif ($display eq 'active') {
      if ($branch_type eq 'active') {
        if ($active_count >= $skip && $active_count < $skip + $page_count) {
          push @{$branches_h->{active}}, $branch;
        }
        $active_count++;
      }
    }
    elsif ($display eq 'stale') {
      if ($branch_type eq 'stale') {
        if ($stale_count >= $skip && $stale_count < $skip + $page_count) {
          push @{$branches_h->{stale}}, $branch;
        }
        $stale_count++;
      }
    }
    elsif ($display eq 'all') {
      if ($all_count >= $skip && $all_count < $skip + $page_count) {
        push @{$branches_h->{all}}, $branch;
      }
      $all_count++;
    }
  }
  
%>

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

  %= include '/include/header';
  
  <div class="container" style="padding-bottom:30px">
    %= include '/include/errors', errors => $errors;
    %= include '/include/message', message => flash('message');
    
    <!-- Branches (for tests) -->
    
    <ul class="branches-select">
      <li class="<%= $display eq 'overview' ? 'active' : '' %>">
        <a href="<%= url_for("/$user/$project/branches") %>">Overview</a>
      </li>
      <li class="<%= $display eq 'active' ? 'active' : '' %>">
        <a href="<%= url_for("/$user/$project/branches/active") %>">Active</a>
      </li>
      <li class="<%= $display eq 'stale' ? 'active' : '' %>">
        <a href="<%= url_for("/$user/$project/branches/stale") %>">Stale</a>
      </li>
      <li class="<%= $display eq 'all' ? 'active' : '' %>">
        <a href="<%= url_for("/$user/$project/branches/all") %>">All branches</a>
      </li>
    </ul>
    <%
      if ($display eq 'overview') {
        $branch_types = ['default', 'active', 'stale'];
      }
      elsif ($display eq 'active') {
        $branch_types = ['active'];
      }
      elsif ($display eq 'stale') {
        $branch_types = ['stale'];
      }
      elsif ($display eq 'all') {
        $branch_types = ['all'];
      }
    %>
    
    % for my $branch_type (@$branch_types) {
      % my $branches = $branches_h->{$branch_type};
      
      % if (@$branches || $display ne 'overview') {
        <ul class="branches">
          <li>
            % if ($branch_type eq 'default') {
              Default branch
            % } elsif ($branch_type eq 'active') {
              Active branch
            % } elsif ($branch_type eq 'stale') {
              Stale branch
            % } elsif ($branch_type eq 'all') {
              All branches
            % }
          </li>
          % if (@$branches) {
            % my $branches_count;
            % for (my $i = 0; $i < @$branches; $i++) {
              <%
                my $branch = $branches->[$i];
                my $bname = $branch->{name};

                my $commit_author_mail = $branch->{commit}{author_email};
                my $commit_author_id = app->dbi->model('user')->select(
                  'id',
                  where => {mail => $commit_author_mail}
                )->value;
                
              %>
              % if ($display eq 'overview' && $i > 4) {
                <li class="branches-overview-more">
                  <a href="<%= url_for("/$user/$project/branches/$branch_type") %>">
                    View more <%= $branch_type %> branches
                  </a>
                </li>
                % last;
              % } else {

                <li>
                  <ul>
                    <li>
                      <div class="branches-name">
                        <a href="<%= url_for("/$user/$project/tree/$bname") %>">
                          <%= $bname %>
                        </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 ($bname 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 ($bname eq $default_branch_name) {
                        % if ($api->logined($user)) {
                          <a href="<%= url_for("/$user/$project/settings") %>" type="submit" class="btn btn-small">Change default branch</a>
                        % }
                      % } else {
                        <a class="btn btn-small" href="<%= url_for("/$user/$project/compare/$bname") %>">
                          % if ($api->logined) {
                            New pull request
                          % } else {
                            Compare
                          % }
                        </a>
                        % if ($api->logined($user)) {
                          <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 => $bname;
                          </form>
                        % }
                      % }
                    </li>
                  </ul>
                </li>
              % }
              % $branches_count++;
            % }


            % if ($display ne 'overview') {
              <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/$project/branches/$display?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/$project/branches/$display?page=$older_page") %>">Older</a>
                    </li>
                  % }
                </ul>
              </div>
            % }
          % } else {
            <li style="text-align:center;text-color:#767676">
              <%
                my $branch_type_name;
                if ($branch_type eq 'active' || $branch_type eq 'stale') {
                  $branch_type_name = $branch_type;
                }
                else {
                  $branch_type_name = '';
                }
              %>
              
              There aren’t any <%= $branch_type_name %> branches.
            </li>
          % }
        </ul>
      % }
    % }
  </div>
  
  %= include '/include/footer';