<% # API my $api = gitprep_api; # Parameters my $user = param('user'); my $project = param('project'); my $from_rev = param('rev1'); my $rev = param('rev2'); my $page = param('page') || 0; my $expand = param('expand'); unless ($from_rev) { $from_rev = app->manager->default_branch($user, $project); } # Git my $git = $self->app->git; if (lc $self->req->method eq 'post') { my $op = param('op'); if ($op eq 'create-pull-request') { my $title = param('title'); my $message = param('message'); my $project_row_id = app->dbi->model('project')->select( 'row_id', where => {user_id => $user, name => $project} )->value; my $pull_request = app->dbi->model('pull_request')->select( where => {project => $project_row_id, branch1 => $from_rev, branch2 => $rev} )->one; if ($pull_request) { $self->redirect_to("/$user/$project/pulls/$pull_request->{row_id}"); return; } else { my $new_pull_request_params = { project => $project_row_id, branch1 => $from_rev, branch2 => $rev, title => $title, message => $message, open => 1 }; app->dbi->model('pull_request')->insert($new_pull_request_params); my $new_pull_request_row_id = app->dbi->model('pull_request')->select( 'row_id', where => { project => $project_row_id, branch1 => $from_rev, branch2 => $rev } )->value; $self->redirect_to("/$user/$project/pulls/$new_pull_request_row_id"); return; } } } # Commits my $commits = $git->forward_commits(app->rep_info($user, $project), $from_rev, $rev); my $commits_count = @$commits; my $commits_date = {}; my $authors = {}; for my $commit (@$commits) { my $date = $commit->{age_string_date_local}; $commits_date->{$date} ||= []; $authors->{$commit->{author}} = 1; push @{$commits_date->{$date}}, $commit; } my $authors_count = keys %$authors; # Start commit my $start_commit = $git->separated_commit(app->rep_info($user, $project), $from_rev, $rev); # End commit my $end_commit = $git->get_commit(app->rep_info($user, $project), $rev); if (!$start_commit || !$end_commit) { $self->reply->not_found; return; } # Branches my $branches = $git->branches(app->rep_info($user, $project)); @$branches = sort { $a->{commit}{age} <=> $b->{commit}{age} } @$branches; # Variables stash id => $end_commit->{id}; stash from_id => $start_commit->{id}; stash rev => $end_commit->{id}; stash from_rev => $start_commit->{id}; # Can open pull request my $can_open_pull_request; if (keys %$commits_date && $expand) { $can_open_pull_request = 1; } # Can merge my $merge_automatical; if ($can_open_pull_request) { # Create working repository if it don't exist $self->app->manager->create_work_rep($user, $project); # Fetch repository my $work_rep_info = app->work_rep_info($user, $project); my @git_fetch_cmd = $self->app->git->cmd($work_rep_info, 'fetch'); Gitprep::Util::run_command(@git_fetch_cmd) or Carp::croak "Can't execute git fetch: @git_fetch_cmd"; # Lock repository my $lock = $self->app->manager->lock_rep($work_rep_info); # Checkout tmp branch and git reset --hard from my remote branch my $gitprep_tmp_branch_name = '__gitprep_tmp_branch__'; my @git_checkout_tmp_branch = $self->app->git->cmd( $work_rep_info, 'checkout', $gitprep_tmp_branch_name, ); Gitprep::Util::run_command(@git_checkout_tmp_branch) or Carp::croak "Can't execute git checkout: @git_checkout_tmp_branch"; # git reset --hard my @git_reset_hard_cmd = $self->app->git->cmd( $work_rep_info, 'reset', '--hard', "origin/$from_rev" ); Gitprep::Util::run_command(@git_reset_hard_cmd) or Carp::croak "Can't execute git reset --hard: @git_reset_hard_cmd"; # Check merge automatically $merge_automatical = $self->app->manager->check_merge_automatical( app->work_rep_info($user, $project), $gitprep_tmp_branch_name, "origin/$rev" ); } layout 'common', title => "Comparing $from_rev...$rev \x{30fb} $user/$project"; %> %= javascript begin $(document).ready(function () { // Change base branch $('#base-branch-btn').on('click', function () { $('#base-branch-popup') .css('display', 'block') .css('top', '40px') .css('left', '10px') ; }); $('#base-branch-close').on('click', function () { $('#base-branch-popup').css('display', 'none'); }); $('[name=base-branch]').on('keypress', function (e) { // Enter if (e.which == 13) { var href = '<%= url_for("/$user/$project/compare/") %>' + $(this).val() + '...<%= $rev %>'; if (<%= $expand ? 1 : 0 %>) { href = href + '?expand=1'; } location.href = href; } }); // Change compare branch $('#compare-branch-btn').on('click', function () { $('#compare-branch-popup') .css('display', 'block') .css('top', '40px') .css('left', '96px') ; }); $('#compare-branch-close').on('click', function () { $('#compare-branch-popup').css('display', 'none'); }); $('[name=compare-branch]').on('keypress', function (e) { // Enter if (e.which == 13) { var href = '<%= url_for("/$user/$project/compare/") %>' + '<%= $from_rev %>...' + $(this).val(); if (<%= $expand ? 1 : 0 %>) { href = href + '?expand=1'; } location.href = href; } }); }); % end %= include '/include/header';
% if ($can_open_pull_request) { Open a pull request % } else { Comparing changes % }
... % if ($can_open_pull_request) { % if ($merge_automatical) { <%= "\x{2714}" %>Able to merge. These branches can be automatically merged. % } else { <%= "×" %>Not able to merge. These branches can't be automatically merged. % } % }
% if ($merge_automatical) {
<%= hidden_field op => 'create-pull-request' %>
<%= text_field 'title' => $commits->[0]{title_short} %>
<%= text_area 'message' %>
<%= submit_button 'Create pull request', class => 'btn btn-success' %>
% } % if (keys %$commits_date) {
% for my $date (reverse sort keys %$commits_date) { % my $commits = $commits_date->{$date};
Commits on <%= $date %>
% }
%= include '/include/commit_body'; % } else {
There isn't anything to compare.
<%= $from_rev %> is up to date with all commits from <%= $rev %>. Try ">switching the base for your comparison.
% }
%= include '/include/footer';