pre-receive hook: undefined local variable or method 'repo_path'

Error message

Upon push:

$ git push devel HEAD
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: hooks/pre-receive:17:in `rescue in increase_reference_counter': undefined local variable or method `repo_path' for main:Object (NameError)
remote:     from hooks/pre-receive:13:in `increase_reference_counter'
remote:     from hooks/pre-receive:31:in `<main>'
To gitlab-devel.example.com:jreinhart/test-project.git
! [remote rejected] HEAD -> junk (pre-receive hook declined)
error: failed to push some refs to 'git@gitlab-devel.example.com:jreinhart/test-project.git'

Version

  • gitlab-shell: 5.9.1 (47924c46)
  • gitlab: v9.5.2

Description

refs = $stdin.read
key_id = ENV.delete('GL_ID')
protocol = ENV.delete('GL_PROTOCOL')
repo_path = Dir.pwd
gl_repository = ENV['GL_REPOSITORY']

def increase_reference_counter(gl_repository)
  result = GitlabNet.new.pre_receive(gl_repository)

  result['reference_counter_increased']
rescue GitlabNet::NotFound
  GitlabReferenceCounter.new(repo_path).increase       # <<<<<< repo_path not available
end

If gitlab-shell is newer than gitlab and the /pre_receive API doesn't exist yet, then GitlabNet::NotFound is raised. The problem is, this rescue statement will attempt to access repo_path, which as far as I can tell is not in scope.

Fixes

Option A

Make the variable global:

$repo_path = Dir.pwd
$gl_repository = ENV['GL_REPOSITORY']

Option B

Pass repo_path as a parameter:

def increase_reference_counter(gl_repository, repo_path)