Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
d3303176
Commit
d3303176
authored
Mar 04, 2020
by
Alex Pooley
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Handle multiple digit auto snippet naming
parent
ed7ae583
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
37 additions
and
31 deletions
+37
-31
app/models/snippet_repository.rb
app/models/snippet_repository.rb
+8
-10
spec/models/snippet_repository_spec.rb
spec/models/snippet_repository_spec.rb
+29
-21
No files found.
app/models/snippet_repository.rb
View file @
d3303176
...
...
@@ -4,7 +4,7 @@ class SnippetRepository < ApplicationRecord
include
Shardable
DEFAULT_EMPTY_FILE_NAME
=
'snippetfile'
EMPTY_FILE_PATTERN
=
/^
#{
DEFAULT_EMPTY_FILE_NAME
}
(\d)\.txt$/
.
freeze
EMPTY_FILE_PATTERN
=
/^
#{
DEFAULT_EMPTY_FILE_NAME
}
(\d
+
)\.txt$/
.
freeze
CommitError
=
Class
.
new
(
StandardError
)
...
...
@@ -51,14 +51,14 @@ class SnippetRepository < ApplicationRecord
end
def
transform_file_entries
(
files
)
last_index
=
get_last_empty_file_index
next_index
=
get_last_empty_file_index
+
1
files
.
each
do
|
file_entry
|
file_entry
[
:action
]
=
infer_action
(
file_entry
)
unless
file_entry
[
:action
]
if
file_entry
[
:file_path
].
blank?
file_entry
[
:file_path
]
=
build_empty_file_name
(
las
t_index
)
las
t_index
+=
1
file_entry
[
:file_path
]
=
build_empty_file_name
(
nex
t_index
)
nex
t_index
+=
1
end
end
end
...
...
@@ -70,12 +70,10 @@ class SnippetRepository < ApplicationRecord
end
def
get_last_empty_file_index
last_file
=
repository
.
ls_files
(
nil
)
.
map!
{
|
file
|
file
.
match
(
EMPTY_FILE_PATTERN
)
}
.
compact
.
max_by
{
|
element
|
element
[
1
]
}
last_file
?
(
last_file
[
1
].
to_i
+
1
)
:
1
repository
.
ls_files
(
nil
).
inject
(
0
)
do
|
max
,
file
|
idx
=
file
[
EMPTY_FILE_PATTERN
,
1
].
to_i
[
idx
,
max
].
max
end
end
def
build_empty_file_name
(
index
)
...
...
spec/models/snippet_repository_spec.rb
View file @
d3303176
...
...
@@ -168,34 +168,42 @@ describe SnippetRepository do
end
end
context
'when files are not named'
do
let
(
:data
)
do
[
{
file_path:
''
,
content:
'foo'
,
action: :create
},
{
file_path:
''
,
content:
'bar'
,
action: :create
},
{
file_path:
'foo.txt'
,
content:
'bar'
,
action: :create
}
]
shared_examples
'snippet repository with file names'
do
|*
filenames
|
it
'sets a name for unnamed files'
do
ls_files
=
snippet
.
repository
.
ls_files
(
nil
)
expect
(
ls_files
).
to
include
(
*
filenames
)
end
end
let_it_be
(
:named_snippet
)
{
{
file_path:
'fee.txt'
,
content:
'bar'
,
action: :create
}
}
let_it_be
(
:unnamed_snippet
)
{
{
file_path:
''
,
content:
'dummy'
,
action: :create
}
}
it
'sets a name for non named files'
do
context
'when some files are not named'
do
let
(
:data
)
{
[
named_snippet
]
+
Array
.
new
(
2
)
{
unnamed_snippet
.
clone
}
}
before
do
expect
do
snippet_repository
.
multi_files_action
(
user
,
data
,
commit_opts
)
end
.
not_to
raise_error
end
it_behaves_like
'snippet repository with file names'
,
'snippetfile1.txt'
,
'snippetfile2.txt'
end
expect
(
snippet
.
repository
.
ls_files
(
nil
)).
to
include
(
'snippetfile1.txt'
,
'snippetfile2.txt'
,
'foo.txt'
)
context
'repository already has 10 unnamed snippets'
do
let
(
:pre_populate_data
)
{
Array
.
new
(
10
)
{
unnamed_snippet
.
clone
}
}
let
(
:data
)
{
[
named_snippet
]
+
Array
.
new
(
2
)
{
unnamed_snippet
.
clone
}
}
before
do
# Pre-populate repository with 9 unnamed snippets.
snippet_repository
.
multi_files_action
(
user
,
pre_populate_data
,
commit_opts
)
expect
do
snippet_repository
.
multi_files_action
(
user
,
data
,
commit_opts
)
end
.
not_to
raise_error
end
it_behaves_like
'snippet repository with file names'
,
'snippetfile10.txt'
,
'snippetfile11.txt'
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment