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
ae88355b
Commit
ae88355b
authored
Apr 05, 2017
by
Rémy Coutable
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Retrieve Git-specific env in Gitlab::Git::RevList and add a new #new_refs method
Signed-off-by:
Rémy Coutable
<
remy@rymai.me
>
parent
cb20cfa2
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
72 additions
and
79 deletions
+72
-79
lib/gitlab/checks/force_push.rb
lib/gitlab/checks/force_push.rb
+3
-7
lib/gitlab/git/rev_list.rb
lib/gitlab/git/rev_list.rb
+25
-24
spec/lib/gitlab/git/rev_list_spec.rb
spec/lib/gitlab/git/rev_list_spec.rb
+44
-48
No files found.
lib/gitlab/checks/force_push.rb
View file @
ae88355b
...
@@ -8,13 +8,9 @@ module Gitlab
...
@@ -8,13 +8,9 @@ module Gitlab
if
Gitlab
::
Git
.
blank_ref?
(
oldrev
)
||
Gitlab
::
Git
.
blank_ref?
(
newrev
)
if
Gitlab
::
Git
.
blank_ref?
(
oldrev
)
||
Gitlab
::
Git
.
blank_ref?
(
newrev
)
false
false
else
else
missed_ref
,
exit_status
=
Gitlab
::
Git
::
RevList
.
new
(
oldrev
,
newrev
,
project:
project
,
env:
env
).
execute
Gitlab
::
Git
::
RevList
.
new
(
path_to_repo:
project
.
repository
.
path_to_repo
,
if
exit_status
==
0
oldrev:
oldrev
,
newrev:
newrev
).
missed_ref
.
present?
missed_ref
.
present?
else
raise
"Got a non-zero exit code while calling out to `git rev-list` in the force-push check."
end
end
end
end
end
end
end
...
...
lib/gitlab/git/rev_list.rb
View file @
ae88355b
module
Gitlab
module
Gitlab
module
Git
module
Git
class
RevList
class
RevList
attr_reader
:
project
,
:env
attr_reader
:
oldrev
,
:newrev
,
:path_to_repo
ALLOWED_VARIABLES
=
%w[GIT_OBJECT_DIRECTORY GIT_ALTERNATE_OBJECT_DIRECTORIES]
.
freeze
def
initialize
(
path_to_repo
:,
newrev
:,
oldrev:
nil
)
@oldrev
=
oldrev
def
initialize
(
oldrev
,
newrev
,
project
:,
env:
nil
)
@newrev
=
newrev
@project
=
project
@path_to_repo
=
path_to_repo
@env
=
env
.
presence
||
{}
@args
=
[
Gitlab
.
config
.
git
.
bin_path
,
"--git-dir=
#{
project
.
repository
.
path_to_repo
}
"
,
"rev-list"
,
"--max-count=1"
,
oldrev
,
"^
#{
newrev
}
"
]
end
end
def
execute
# This method returns an array of new references
Gitlab
::
Popen
.
popen
(
@args
,
nil
,
parse_environment_variables
)
def
new_refs
execute
([
*
base_args
,
newrev
,
'--not'
,
'--all'
])
end
end
def
valid?
# This methods returns an array of missed references
environment_variables
.
all?
do
|
(
name
,
value
)
|
def
missed_ref
value
.
to_s
.
start_with?
(
project
.
repository
.
path_to_repo
)
execute
([
*
base_args
,
'--max-count=1'
,
oldrev
,
"^
#{
newrev
}
"
])
end
end
end
private
private
def
parse_environment_variables
def
execute
(
args
)
return
{}
unless
valid?
output
,
status
=
Gitlab
::
Popen
.
popen
(
args
,
nil
,
Gitlab
::
Git
::
Env
.
all
.
stringify_keys
)
unless
status
.
zero?
raise
"Got a non-zero exit code while calling out `
#{
args
.
join
(
' '
)
}
`."
end
environment_variables
output
.
split
(
"
\n
"
)
end
end
def
environment_variables
def
base_args
@environment_variables
||=
env
.
slice
(
*
ALLOWED_VARIABLES
).
compact
[
Gitlab
.
config
.
git
.
bin_path
,
"--git-dir=
#{
path_to_repo
}
"
,
'rev-list'
]
end
end
end
end
end
end
...
...
spec/lib/gitlab/git/rev_list_spec.rb
View file @
ae88355b
...
@@ -3,58 +3,54 @@ require 'spec_helper'
...
@@ -3,58 +3,54 @@ require 'spec_helper'
describe
Gitlab
::
Git
::
RevList
,
lib:
true
do
describe
Gitlab
::
Git
::
RevList
,
lib:
true
do
let
(
:project
)
{
create
(
:project
,
:repository
)
}
let
(
:project
)
{
create
(
:project
,
:repository
)
}
context
"validations"
do
before
do
described_class
::
ALLOWED_VARIABLES
.
each
do
|
var
|
expect
(
Gitlab
::
Git
::
Env
).
to
receive
(
:all
).
and_return
({
context
var
do
GIT_OBJECT_DIRECTORY
:
'foo'
,
it
"accepts values starting with the project repo path"
do
GIT_ALTERNATE_OBJECT_DIRECTORIES
:
'bar'
env
=
{
var
=>
"
#{
project
.
repository
.
path_to_repo
}
/objects"
}
})
rev_list
=
described_class
.
new
(
'oldrev'
,
'newrev'
,
project:
project
,
env:
env
)
end
expect
(
rev_list
).
to
be_valid
context
"#new_refs"
do
end
let
(
:rev_list
)
{
Gitlab
::
Git
::
RevList
.
new
(
newrev:
'newrev'
,
path_to_repo:
project
.
repository
.
path_to_repo
)
}
it
"rejects values starting not with the project repo path"
do
it
'calls out to `popen`'
do
env
=
{
var
=>
"/some/other/path"
}
expect
(
Gitlab
::
Popen
).
to
receive
(
:popen
).
with
([
rev_list
=
described_class
.
new
(
'oldrev'
,
'newrev'
,
project:
project
,
env:
env
)
Gitlab
.
config
.
git
.
bin_path
,
"--git-dir=
#{
project
.
repository
.
path_to_repo
}
"
,
expect
(
rev_list
).
not_to
be_valid
'rev-list'
,
end
'newrev'
,
'--not'
,
it
"rejects values containing the project repo path but not starting with it"
do
'--all'
env
=
{
var
=>
"/some/other/path/
#{
project
.
repository
.
path_to_repo
}
"
}
],
rev_list
=
described_class
.
new
(
'oldrev'
,
'newrev'
,
project:
project
,
env:
env
)
nil
,
{
expect
(
rev_list
).
not_to
be_valid
'GIT_OBJECT_DIRECTORY'
=>
'foo'
,
end
'GIT_ALTERNATE_OBJECT_DIRECTORIES'
=>
'bar'
}).
and_return
([
"sha1
\n
sha2"
,
0
])
it
"ignores nil values"
do
env
=
{
var
=>
nil
}
expect
(
rev_list
.
new_refs
).
to
eq
(
%w[sha1 sha2]
)
rev_list
=
described_class
.
new
(
'oldrev'
,
'newrev'
,
project:
project
,
env:
env
)
end
end
expect
(
rev_list
).
to
be_valid
end
context
"#missed_ref"
do
end
let
(
:rev_list
)
{
Gitlab
::
Git
::
RevList
.
new
(
oldrev:
'oldrev'
,
newrev:
'newrev'
,
path_to_repo:
project
.
repository
.
path_to_repo
)
}
end
end
it
'calls out to `popen`'
do
expect
(
Gitlab
::
Popen
).
to
receive
(
:popen
).
with
([
context
"#execute"
do
Gitlab
.
config
.
git
.
bin_path
,
let
(
:env
)
{
{
"GIT_OBJECT_DIRECTORY"
=>
project
.
repository
.
path_to_repo
}
}
"--git-dir=
#{
project
.
repository
.
path_to_repo
}
"
,
let
(
:rev_list
)
{
Gitlab
::
Git
::
RevList
.
new
(
'oldrev'
,
'newrev'
,
project:
project
,
env:
env
)
}
'rev-list'
,
'--max-count=1'
,
it
"calls out to `popen` without environment variables if the record is invalid"
do
'oldrev'
,
allow
(
rev_list
).
to
receive
(
:valid?
).
and_return
(
false
)
'^newrev'
],
expect
(
Open3
).
to
receive
(
:popen3
).
with
(
hash_excluding
(
env
),
any_args
)
nil
,
{
rev_list
.
execute
'GIT_OBJECT_DIRECTORY'
=>
'foo'
,
end
'GIT_ALTERNATE_OBJECT_DIRECTORIES'
=>
'bar'
}).
and_return
([
"sha1
\n
sha2"
,
0
])
it
"calls out to `popen` with environment variables if the record is valid"
do
allow
(
rev_list
).
to
receive
(
:valid?
).
and_return
(
true
)
expect
(
rev_list
.
missed_ref
).
to
eq
(
%w[sha1 sha2]
)
expect
(
Open3
).
to
receive
(
:popen3
).
with
(
hash_including
(
env
),
any_args
)
rev_list
.
execute
end
end
end
end
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