File: diff_file_component_preview.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (154 lines) | stat: -rw-r--r-- 5,001 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# frozen_string_literal: true

module RapidDiffs
  class DiffFileComponentPreview < ViewComponent::Preview
    layout 'lookbook/rapid_diffs'

    # @!group Code

    def added_and_removed_lines
      hunk = "
        --- a/app/views/layouts/preview/rapid_diffs.html.haml	(revision eaba934a0bc6eed56cfd1f082e9fa3f5409f2938)
        +++ b/app/views/layouts/preview/rapid_diffs.html.haml	(date 1718119822001)
        @@ -1,7 +1,6 @@
        -= universal_stylesheet_link_tag 'application'
        -= universal_stylesheet_link_tag 'application_utilities'
         = universal_stylesheet_link_tag 'preview/rapid_diffs'
         = webpack_bundle_tag 'javascripts/entrypoints/preview/rapid_diffs'
        += webpack_bundle_tag 'javascripts/entrypoints/preview'

        -%div{ style: 'padding: 20px' }
        +%div{ style: 'padding: 20px', class: 'container-fluid' }
           = yield
      "
      render(::RapidDiffs::DiffFileComponent.new(diff_file: diff_file(hunk)))
    end

    def added_lines
      hunk = "
        --- a/app/views/layouts/preview/rapid_diffs.html.haml	(revision eaba934a0bc6eed56cfd1f082e9fa3f5409f2938)
        +++ b/app/views/layouts/preview/rapid_diffs.html.haml	(date 1718118441569)
        @@ -2,6 +2,7 @@
         = universal_stylesheet_link_tag 'application_utilities'
         = universal_stylesheet_link_tag 'preview/rapid_diffs'
         = webpack_bundle_tag 'javascripts/entrypoints/preview/rapid_diffs'
        += webpack_bundle_tag 'javascripts/entrypoints/preview/rapid_diffs'

         %div{ style: 'padding: 20px' }
           = yield
      "
      render(::RapidDiffs::DiffFileComponent.new(diff_file: diff_file(hunk)))
    end

    def removed_lines
      hunk = "
        --- a/app/views/layouts/preview/rapid_diffs.html.haml	(revision eaba934a0bc6eed56cfd1f082e9fa3f5409f2938)
        +++ b/app/views/layouts/preview/rapid_diffs.html.haml	(date 1718119765262)
        @@ -1,7 +1,6 @@
         = universal_stylesheet_link_tag 'application'
         = universal_stylesheet_link_tag 'application_utilities'
         = universal_stylesheet_link_tag 'preview/rapid_diffs'
        -= webpack_bundle_tag 'javascripts/entrypoints/preview/rapid_diffs'

         %div{ style: 'padding: 20px' }
           = yield
      "
      render(::RapidDiffs::DiffFileComponent.new(diff_file: diff_file(hunk)))
    end

    def added_file
      hunk = "
        --- /dev/null
        +++ b/app/views/layouts/preview/rapid_diffs.html.haml	(date 1718119765262)
        @@ -0,0 +1,7 @@
        += universal_stylesheet_link_tag 'application'
        += universal_stylesheet_link_tag 'application_utilities'
        += universal_stylesheet_link_tag 'preview/rapid_diffs'
        += webpack_bundle_tag 'javascripts/entrypoints/preview/rapid_diffs'
        +
        +%div{ style: 'padding: 20px' }
        +  = yield
      "
      render(::RapidDiffs::DiffFileComponent.new(diff_file: diff_file(hunk)))
    end

    def removed_file
      hunk = "
        --- a/app/views/layouts/preview/rapid_diffs.html.haml	(revision eaba934a0bc6eed56cfd1f082e9fa3f5409f2938)
        +++ /dev/null
        @@ -1,7 +1,0 @@
        -= universal_stylesheet_link_tag 'application'
        -= universal_stylesheet_link_tag 'application_utilities'
        -= universal_stylesheet_link_tag 'preview/rapid_diffs'
        -= webpack_bundle_tag 'javascripts/entrypoints/preview/rapid_diffs'
        -
        -%div{ style: 'padding: 20px' }
        -  = yield
      "
      render(::RapidDiffs::DiffFileComponent.new(diff_file: diff_file(hunk)))
    end

    # @!endgroup

    private

    def diff_file(hunk)
      diff = raw_diff(diff_content(hunk))
      diff_refs = ::Gitlab::Diff::DiffRefs.new(base_sha: 'a', head_sha: 'b')
      ::Gitlab::Diff::File.new(diff, repository: FakeRepository.new, diff_refs: diff_refs).tap do |file|
        file.instance_variable_set(:@new_blob, Blob.decorate(raw_blob(diff_content(hunk))))
      end
    end

    def diff_content(hunk)
      hunk.split("\n").filter_map(&:lstrip).reject(&:empty?).join("\n")
    end

    def raw_diff(hunk)
      ::Gitlab::Git::Diff.new(
        {
          diff: hunk,
          new_path: new_path(hunk),
          old_path: old_path(hunk),
          a_mode: '0',
          b_mode: '100644',
          new_file: true,
          renamed_file: false,
          deleted_file: false,
          too_large: false
        })
    end

    def raw_blob(hunk)
      ::Gitlab::Git::Blob.new(
        id: 'bba46076dd3e6a406b45ad98ef3b8194fde8b568',
        commit_id: 'master',
        size: 264,
        name: new_path(hunk),
        path: new_path(hunk),
        data: "",
        mode: '100644'
      )
    end

    def old_path(hunk)
      hunk[%r{--- a/([^\s\n]*)}, 1]
    end

    def new_path(hunk)
      hunk[%r{\+\+\+ b/([^\s\n]*)}, 1]
    end

    class FakeRepository
      def initialize
        @project = FactoryBot.build_stubbed(:project)
      end

      def attributes(_)
        {}
      end

      attr_reader :project
    end
  end
end