gitprep / templates / import-branch.html.ep /
19db03f 8 years ago
1 contributor
182 lines | 5.343kb
<%
  
  my $api = gitprep_api;
  
  my $user = param('user');
  my $project = param('project');
  my $remote_user = param('remote_user');
  my $remote_project = param('remote_project');
  my $remote_branch = param('remote-branch');
  
  # Authentication
  unless ($api->logined($user)) {
    $self->redirect_to('/');
    return;
  }
  
  # Branches
  my $git = app->git;
  my $remote_branches = $git->branches($self->app->rep_info($remote_user, $remote_project));
  my $remote_branch_names = [map { $_->{name} } @$remote_branches];
  
  my $op = param('op') || '';
  my $errors;
  if ($op eq 'import' && lc $self->req->method eq 'post') {
    # Parameters
    my $user = param('user');
    my $project = param('project');
    my $branch = param('branch');
    my $remote_user = param('remote_user');
    my $remote_project = param('remote_project');
    my $remote_branch = param('remote-branch');
    my $force = param('force');
    
    # Validator
    my $vc = app->vc;
    
    # Validation result
    my $validation = $vc->validation;
    
    # "user"
    if (!$vc->check($user, 'user_name')) {
      $validation->add_failed('User name is invalid.');
    }
    
    # "project"
    if (!$vc->check($project, 'project_name')) {
      $validation->add_failed('Repository name is invalid.');
    }
    
    # "branch"
    if (!(defined $branch && length $branch)) {
      $validation->add_failed('Branch name is empty.');
    }
    
    # "remote_user"
    if (!$vc->check($remote_user, 'user_name')) {
      $validation->add_failed('Remote User name is invalid.');
    }
    
    # "remote_project"
    if (!$vc->check($remote_project, 'project_name')) {
      $validation->add_failed('Remote repository is invalid.');
    }
    
    # "remote-branch"
    if (!(defined $remote_branch && length $remote_branch)) {
      $validation->add_failed('Remote branch name is empty.');
    }
    
    # "force"
    $force = $force ? 1 : 0;
    
    if ($validation->is_valid) {
      
      # Check branch name
      my $branches = $git->branches($self->app->rep_info($user, $project));
      
      if (!$force && grep { $branch eq $_->{name} } @$branches) {
        $errors = ["Branch \"$branch\" is already exists. If you want to import this branch, check force option."];
      }
      else {
        eval {
          $git->import_branch(
            app->rep_info($user, $project),
            $branch,
            app->rep_info($remote_user, $remote_project),
            $remote_branch,
            {force => $force}
          );
        };
        
        if (my $e = $@) {
          $errors = ['Internal Error'];
          app->log->error(url_for . ": $e");
        }
        else {
          flash(message => "Success: " . ($force ? 'force ' : '') . "import \"$remote_user / $remote_project / $remote_branch\" into \"$user / $project / $branch\"");
          $self->redirect_to('current');
          return;
        }
      }
    }
    else {
      $errors = $validation->messages;
    }
  }

  layout 'common', title => "Import branch";
%>

  %= include 'include/header';

  %= javascript begin
    $('document').ready(function () {
      
      // Select remote branch
      $('[name=copy-branch-name]').on('click', function () {
        $('[name=branch]').val($('[name=remote-branch]').val());
        return false;
      });
    });
  % end
  
  <div class="container">
    % if (my $message = flash('message')) {
      <div class="alert alert-success">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <%= $message %>
      </div>
    % }
    % if ($errors) {
      <div class="alert alert-error">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        % for my $error (@$errors) {
          <p><%= $error %></p>
        % }
      </div>
    % }
    <h3 class="topic1">Import branch</h3>
    <form action="<%= url_for("/$user/$project/import-branch/$remote_user/$remote_project")->query(op => 'import') %>" method="post">
      <div class="import-branch">
        <div class="left">
          <div class="import-branch-to">
            <div>
              %= "$user / $project";
            </div>
            <div>
              %= text_field 'branch', placeholder => "Branch name", style => "width:250px";
              <button name="copy-branch-name", class="btn" style="font-size:12px; padding-left:3px;padding-right:3px;color:#666">Copy Branch Name</button>
            </div>
            <div class="import-branch-button-panel">
              <div>
                %= submit_button 'Import', class => "btn btn-info";
              </div>
              <div>
                <%= check_box force => 1 %> 
              </div>
              <div>
                Force
              </div>
            </div>
          </div>
        </div>
        <div class="center">
          &lArr;
        </div>
        <div class="right">
          <div  class="import-branch-from">
            <div>
              %= "$remote_user / $remote_project";
            </div>
            <div>
              % param('remote-branch' => $remote_branch);
              %= select_field 'remote-branch' => $remote_branch_names, style => "width:250px";
            </div>
          </div>
        </div>
      </div>
    </form>
  </div>
  
  %= include '/include/footer';