Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ExtractsPath.html « classes « code « doc - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b8c0c26f9905c80f6b13d924dd2a8d90e824a4e (plain)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ExtractsPath</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../css/github.css" type="text/css" media="screen" />
<script src="../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
        <h1>
            <span class="type">Module</span> 
            ExtractsPath 
            
        </h1>
        <ul class="files">
            
            <li><a href="../files/lib/extracts_path_rb.html">lib/extracts_path.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<p>Module providing methods for dealing with separating a tree-ish string and
a file path string when combined in a request parameter</p>

    </div>
  


  


  
  


  
    <!-- Namespace -->
    <div class="sectiontitle">Namespace</div>
    <ul>
      
        <li>
          <span class="type">CLASS</span>
          <a href="ExtractsPath/InvalidPathError.html">ExtractsPath::InvalidPathError</a>
        </li>
      
    </ul>
  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>A</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="#method-i-assign_ref_vars">assign_ref_vars</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>E</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="#method-i-extract_ref">extract_ref</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-assign_ref_vars">
            
              <b>assign_ref_vars</b>()
            
            <a href="../classes/ExtractsPath.html#method-i-assign_ref_vars" name="method-i-assign_ref_vars" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Assigns common instance variables for views working with Git tree-ish
objects</p>

<p>Assignments are:</p>
<ul><li>
<p>@id     - A string representing the joined ref and path</p>
</li><li>
<p>@ref    - A string representing the ref (e.g., the branch, tag, or commit
SHA)</p>
</li><li>
<p>@path   - A string representing the filesystem path</p>
</li><li>
<p>@commit - A <a href="CommitDecorator.html">CommitDecorator</a> representing
the commit from the given ref</p>
</li><li>
<p>@tree   - A <a href="TreeDecorator.html">TreeDecorator</a> representing the
tree at the given ref/path</p>
</li></ul>

<p>If the :id parameter appears to be requesting a specific response format,
that will be handled as well.</p>

<p>Automatically renders `not_found!` if a valid tree path could not be
resolved (e.g., when a user inserts an invalid path or ref).</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-assign_ref_vars_source')" id="l_method-i-assign_ref_vars_source">show</a>
                
              </p>
              <div id="method-i-assign_ref_vars_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/extracts_path.rb, line 104</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">assign_ref_vars</span>
  <span class="ruby-comment"># Handle formats embedded in the id</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">params</span>[<span class="ruby-value">:id</span>].<span class="ruby-identifier">ends_with?</span>(<span class="ruby-string">'.atom'</span>)
    <span class="ruby-identifier">params</span>[<span class="ruby-value">:id</span>].<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">%r\.atom$/</span>, <span class="ruby-string">''</span>)
    <span class="ruby-identifier">request</span>.<span class="ruby-identifier">format</span> = <span class="ruby-value">:atom</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-ivar">@ref</span>, <span class="ruby-ivar">@path</span> = <span class="ruby-identifier">extract_ref</span>(<span class="ruby-identifier">request</span>.<span class="ruby-identifier">fullpath</span>)

  <span class="ruby-ivar">@id</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span>(<span class="ruby-ivar">@ref</span>, <span class="ruby-ivar">@path</span>)

  <span class="ruby-ivar">@commit</span> = <span class="ruby-constant">CommitDecorator</span>.<span class="ruby-identifier">decorate</span>(<span class="ruby-ivar">@project</span>.<span class="ruby-identifier">commit</span>(<span class="ruby-ivar">@ref</span>))

  <span class="ruby-ivar">@tree</span> = <span class="ruby-constant">Tree</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@commit</span>.<span class="ruby-identifier">tree</span>, <span class="ruby-ivar">@project</span>, <span class="ruby-ivar">@ref</span>, <span class="ruby-ivar">@path</span>)
  <span class="ruby-ivar">@tree</span> = <span class="ruby-constant">TreeDecorator</span>.<span class="ruby-identifier">new</span>(<span class="ruby-ivar">@tree</span>)

  <span class="ruby-identifier">raise</span> <span class="ruby-constant">InvalidPathError</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@tree</span>.<span class="ruby-identifier">invalid?</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">NoMethodError</span>, <span class="ruby-constant">InvalidPathError</span>
  <span class="ruby-identifier">not_found!</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-extract_ref">
            
              <b>extract_ref</b>(input)
            
            <a href="../classes/ExtractsPath.html#method-i-extract_ref" name="method-i-extract_ref" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Given a string containing both a Git tree-ish, such as a branch or tag, and
a filesystem path joined by forward slashes, attempts to separate the two.</p>

<p>Expects a @project instance variable to contain the active project. This is
used to check the input against a list of valid repository refs.</p>

<p>Examples</p>

<pre class="ruby"><span class="ruby-comment"># No @project available</span>
<span class="ruby-identifier">extract_ref</span>(<span class="ruby-string">'master'</span>)
<span class="ruby-comment"># =&gt; ['', '']</span>

<span class="ruby-identifier">extract_ref</span>(<span class="ruby-string">'master'</span>)
<span class="ruby-comment"># =&gt; ['master', '']</span>

<span class="ruby-identifier">extract_ref</span>(<span class="ruby-string">&quot;f4b14494ef6abf3d144c28e4af0c20143383e062/CHANGELOG&quot;</span>)
<span class="ruby-comment"># =&gt; ['f4b14494ef6abf3d144c28e4af0c20143383e062', 'CHANGELOG']</span>

<span class="ruby-identifier">extract_ref</span>(<span class="ruby-string">&quot;v2.0.0/README.md&quot;</span>)
<span class="ruby-comment"># =&gt; ['v2.0.0', 'README.md']</span>

<span class="ruby-identifier">extract_ref</span>(<span class="ruby-string">'/gitlab/vagrant/tree/master/app/models/project.rb'</span>)
<span class="ruby-comment"># =&gt; ['master', 'app/models/project.rb']</span>

<span class="ruby-identifier">extract_ref</span>(<span class="ruby-string">'issues/1234/app/models/project.rb'</span>)
<span class="ruby-comment"># =&gt; ['issues/1234', 'app/models/project.rb']</span>

<span class="ruby-comment"># Given an invalid branch, we fall back to just splitting on the first slash</span>
<span class="ruby-identifier">extract_ref</span>(<span class="ruby-string">'non/existent/branch/README.md'</span>)
<span class="ruby-comment"># =&gt; ['non', 'existent/branch/README.md']</span>
</pre>

<p>Returns an Array where the first value is the tree-ish and the second is
the path</p>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-extract_ref_source')" id="l_method-i-extract_ref_source">show</a>
                
              </p>
              <div id="method-i-extract_ref_source" class="dyn-source">
                <pre><span class="ruby-comment"># File lib/extracts_path.rb, line 48</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">extract_ref</span>(<span class="ruby-identifier">input</span>)
  <span class="ruby-identifier">pair</span> = [<span class="ruby-string">''</span>, <span class="ruby-string">''</span>]

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">pair</span> <span class="ruby-keyword">unless</span> <span class="ruby-ivar">@project</span>

  <span class="ruby-comment"># Remove project, actions and all other staff from path</span>
  <span class="ruby-identifier">input</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-node">&quot;/#{@project.path_with_namespace}&quot;</span>, <span class="ruby-string">&quot;&quot;</span>)
  <span class="ruby-identifier">input</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">%r^\/(tree|commits|blame|blob)\//</span>, <span class="ruby-string">&quot;&quot;</span>) <span class="ruby-comment"># remove actions</span>
  <span class="ruby-identifier">input</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">%r\?.*$/</span>, <span class="ruby-string">&quot;&quot;</span>) <span class="ruby-comment"># remove stamps suffix</span>
  <span class="ruby-identifier">input</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">%r.atom$/</span>, <span class="ruby-string">&quot;&quot;</span>) <span class="ruby-comment"># remove rss feed</span>
  <span class="ruby-identifier">input</span>.<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">%r\/edit$/</span>, <span class="ruby-string">&quot;&quot;</span>) <span class="ruby-comment"># remove edit route part</span>

  <span class="ruby-keyword">if</span> <span class="ruby-identifier">input</span>.<span class="ruby-identifier">match</span>(<span class="ruby-regexp">%r^([[:alnum:]]{40})(.+)/</span>)
    <span class="ruby-comment"># If the ref appears to be a SHA, we're done, just split the string</span>
    <span class="ruby-identifier">pair</span> = <span class="ruby-identifier">$~</span>.<span class="ruby-identifier">captures</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-comment"># Otherwise, attempt to detect the ref using a list of the project's</span>
    <span class="ruby-comment"># branches and tags</span>

    <span class="ruby-comment"># Append a trailing slash if we only get a ref and no file path</span>
    <span class="ruby-identifier">id</span> = <span class="ruby-identifier">input</span>
    <span class="ruby-identifier">id</span> <span class="ruby-operator">+=</span> <span class="ruby-string">'/'</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">id</span>.<span class="ruby-identifier">ends_with?</span>(<span class="ruby-string">'/'</span>)

    <span class="ruby-identifier">valid_refs</span> = <span class="ruby-ivar">@project</span>.<span class="ruby-identifier">ref_names</span>
    <span class="ruby-identifier">valid_refs</span>.<span class="ruby-identifier">select!</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">id</span>.<span class="ruby-identifier">start_with?</span>(<span class="ruby-node">&quot;#{v}/&quot;</span>) }

    <span class="ruby-keyword">if</span> <span class="ruby-identifier">valid_refs</span>.<span class="ruby-identifier">length</span> <span class="ruby-operator">!=</span> <span class="ruby-number">1</span>
      <span class="ruby-comment"># No exact ref match, so just try our best</span>
      <span class="ruby-identifier">pair</span> = <span class="ruby-identifier">id</span>.<span class="ruby-identifier">match</span>(<span class="ruby-regexp">%r([^\/]+)(.*)/</span>).<span class="ruby-identifier">captures</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-comment"># Partition the string into the ref and the path, ignoring the empty first value</span>
      <span class="ruby-identifier">pair</span> = <span class="ruby-identifier">id</span>.<span class="ruby-identifier">partition</span>(<span class="ruby-identifier">valid_refs</span>.<span class="ruby-identifier">first</span>)[<span class="ruby-number">1</span><span class="ruby-operator">..</span><span class="ruby-number">-1</span>]
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-comment"># Remove ending slashes from path</span>
  <span class="ruby-identifier">pair</span>[<span class="ruby-number">1</span>].<span class="ruby-identifier">gsub!</span>(<span class="ruby-regexp">%r^\/|\/$/</span>, <span class="ruby-string">''</span>)

  <span class="ruby-identifier">pair</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>