Merge pull request #2067 from riyad/diff-and-patch-for-commits-and-merge-requests
Diff and patch for commits and merge requests
This commit is contained in:
commit
e750efd9fc
14 changed files with 238 additions and 37 deletions
|
@ -26,7 +26,8 @@ class CommitController < ProjectResourceController
|
|||
end
|
||||
end
|
||||
|
||||
format.patch
|
||||
format.diff { render text: @commit.to_diff }
|
||||
format.patch { render text: @commit.to_patch }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
class MergeRequestsController < ProjectResourceController
|
||||
before_filter :module_enabled
|
||||
before_filter :merge_request, only: [:edit, :update, :destroy, :show, :commits, :diffs, :automerge, :automerge_check, :raw]
|
||||
before_filter :validates_merge_request, only: [:show, :diffs, :raw]
|
||||
before_filter :merge_request, only: [:edit, :update, :destroy, :show, :commits, :diffs, :automerge, :automerge_check]
|
||||
before_filter :validates_merge_request, only: [:show, :diffs]
|
||||
before_filter :define_show_vars, only: [:show, :diffs]
|
||||
|
||||
# Allow read any merge_request
|
||||
|
@ -16,7 +16,6 @@ class MergeRequestsController < ProjectResourceController
|
|||
# Allow destroy merge_request
|
||||
before_filter :authorize_admin_merge_request!, only: [:destroy]
|
||||
|
||||
|
||||
def index
|
||||
@merge_requests = MergeRequestsLoadContext.new(project, current_user, params).execute
|
||||
end
|
||||
|
@ -25,11 +24,10 @@ class MergeRequestsController < ProjectResourceController
|
|||
respond_to do |format|
|
||||
format.html
|
||||
format.js
|
||||
end
|
||||
end
|
||||
|
||||
def raw
|
||||
send_file @merge_request.to_raw
|
||||
format.diff { render text: @merge_request.to_diff }
|
||||
format.patch { render text: @merge_request.to_patch }
|
||||
end
|
||||
end
|
||||
|
||||
def diffs
|
||||
|
|
|
@ -150,4 +150,19 @@ class Commit
|
|||
def parents_count
|
||||
parents && parents.count || 0
|
||||
end
|
||||
|
||||
# Shows the diff between the commit's parent and the commit.
|
||||
#
|
||||
# Cuts out the header and stats from #to_patch and returns only the diff.
|
||||
def to_diff
|
||||
# see Grit::Commit#show
|
||||
patch = to_patch
|
||||
|
||||
# discard lines before the diff
|
||||
lines = patch.split("\n")
|
||||
while !lines.first.start_with?("diff --git") do
|
||||
lines.shift
|
||||
end
|
||||
lines.join("\n")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -202,20 +202,22 @@ class MergeRequest < ActiveRecord::Base
|
|||
false
|
||||
end
|
||||
|
||||
def to_raw
|
||||
FileUtils.mkdir_p(Rails.root.join("tmp", "patches"))
|
||||
patch_path = Rails.root.join("tmp", "patches", "merge_request_#{self.id}.patch")
|
||||
|
||||
from = commits.last.id
|
||||
to = source_branch
|
||||
|
||||
project.repo.git.run('', "format-patch" , " > #{patch_path.to_s}", {}, ["#{from}..#{to}", "--stdout"])
|
||||
|
||||
patch_path
|
||||
end
|
||||
|
||||
def mr_and_commit_notes
|
||||
commit_ids = commits.map(&:id)
|
||||
Note.where("(noteable_type = 'MergeRequest' AND noteable_id = :mr_id) OR (noteable_type = 'Commit' AND noteable_id IN (:commit_ids))", mr_id: id, commit_ids: commit_ids)
|
||||
end
|
||||
|
||||
# Returns the raw diff for this merge request
|
||||
#
|
||||
# see "git diff"
|
||||
def to_diff
|
||||
project.repo.git.native(:diff, {timeout: 30, raise: true}, "#{target_branch}...#{source_branch}")
|
||||
end
|
||||
|
||||
# Returns the commit as a series of email patches.
|
||||
#
|
||||
# see "git format-patch"
|
||||
def to_patch
|
||||
project.repo.git.format_patch({timeout: 30, raise: true, stdout: true}, "#{target_branch}..#{source_branch}")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1 +0,0 @@
|
|||
<%= @commit.to_patch %>
|
|
@ -5,9 +5,14 @@
|
|||
%span.btn.disabled.grouped
|
||||
%i.icon-comment
|
||||
= @notes_count
|
||||
= link_to project_commit_path(@project, @commit, format: :patch), class: "btn small grouped" do
|
||||
%i.icon-download-alt
|
||||
Get Patch
|
||||
.left.btn-group
|
||||
%a.btn.small.grouped.dropdown-toggle{ data: {toggle: :dropdown} }
|
||||
%i.icon-download-alt
|
||||
Download as
|
||||
%span.caret
|
||||
%ul.dropdown-menu
|
||||
%li= link_to "Email Patches", project_commit_path(@project, @commit, format: :patch)
|
||||
%li= link_to "Plain Diff", project_commit_path(@project, @commit, format: :diff)
|
||||
= link_to project_tree_path(@project, @commit), class: "browse-button primary grouped" do
|
||||
%strong Browse Code »
|
||||
%h3.commit-title.page_title
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
- if @merge_request.valid_diffs?
|
||||
= render "commits/diffs", diffs: @diffs
|
||||
- elsif @merge_request.broken_diffs?
|
||||
%h4.nothing_here_message
|
||||
%h4.nothing_here_message
|
||||
Can't load diff.
|
||||
You can #{link_to "download MR patch", raw_project_merge_request_path(@project, @merge_request), class: "vlink"} instead.
|
||||
You can
|
||||
= link_to "download it", project_merge_request_path(@project, @merge_request), format: :diff, class: "vlink"
|
||||
instead.
|
||||
- else
|
||||
%h4.nothing_here_message Nothing to merge
|
||||
|
|
|
@ -13,9 +13,14 @@
|
|||
= "MERGED"
|
||||
- if can?(current_user, :modify_merge_request, @merge_request)
|
||||
- if @merge_request.open?
|
||||
= link_to raw_project_merge_request_path(@project, @merge_request), class: "btn grouped" do
|
||||
%i.icon-download-alt
|
||||
Get Patch
|
||||
.left.btn-group
|
||||
%a.btn.grouped.dropdown-toggle{ data: {toggle: :dropdown} }
|
||||
%i.icon-download-alt
|
||||
Download as
|
||||
%span.caret
|
||||
%ul.dropdown-menu
|
||||
%li= link_to "Email Patches", project_merge_request_path(@project, @merge_request, format: :patch)
|
||||
%li= link_to "Plain Diff", project_merge_request_path(@project, @merge_request, format: :diff)
|
||||
|
||||
= link_to 'Close', project_merge_request_path(@project, @merge_request, merge_request: {closed: true }, status_only: true), method: :put, class: "btn grouped danger", title: "Close merge request"
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue