Class: GitHttpsable::Push::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/git_httpsable/push/repository.rb

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Repository

Returns a new instance of Repository.



4
5
6
7
# File 'lib/git_httpsable/push/repository.rb', line 4

def initialize(path, options = {})
  @path = path
  @options = { log: logger }.merge(options)
end

Instance Method Details

#absolute_path(path) ⇒ Object



75
76
77
# File 'lib/git_httpsable/push/repository.rb', line 75

def absolute_path(path)
  path.start_with?('/') ? path : '/' + path
end

#env_git_access_tokenObject



49
50
51
# File 'lib/git_httpsable/push/repository.rb', line 49

def env_git_access_token
  ENV['GIT_SERVER_ACCESS_TOKEN']
end

#env_github_access_tokenObject



45
46
47
# File 'lib/git_httpsable/push/repository.rb', line 45

def env_github_access_token
  ENV['GITHUB_ACCESS_TOKEN']
end

#gitObject



37
38
39
# File 'lib/git_httpsable/push/repository.rb', line 37

def git
  @git ||= ::Git.open(@path, @options)
end

#host(host) ⇒ Object



67
68
69
# File 'lib/git_httpsable/push/repository.rb', line 67

def host(host)
  ENV['GIT_SERVER_HOST'] || host
end

#loggerObject



33
34
35
# File 'lib/git_httpsable/push/repository.rb', line 33

def logger
  ::GitHttpsable::Push.logger
end

#portObject



71
72
73
# File 'lib/git_httpsable/push/repository.rb', line 71

def port
  ENV['GIT_SERVER_PORT'] || nil
end

#push(remote = 'origin', branch = 'master', options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/git_httpsable/push/repository.rb', line 9

def push(remote = 'origin', branch = 'master', options = {})
  logger.debug(remote: remote, branch: branch, options: options)
  # check current branch's upstream remote and branch?
  url = remote_url(remote)
  fail NotExistRemoteUrlError, %(no "#{remote}" url) unless url
  parsed = GitCloneUrl.parse(url)

  converted_url = \
    URI::Generic.build(
      scheme: scheme,
      userinfo: userinfo,
      host: host(parsed.host),
      port: port,
      path: absolute_path(parsed.path)
    )
  git.push(converted_url.to_s, branch, options)
rescue StandardError => e
  raise e if e.is_a?(GitHttpsablePushError)

  exception = GitHttpsablePushError.new('(' + e.class.name + ') ' + e.message)
  exception.set_backtrace(e.backtrace)
  raise exception
end

#remote_url(remote) ⇒ Object



41
42
43
# File 'lib/git_httpsable/push/repository.rb', line 41

def remote_url(remote)
  git.remote(remote) && git.remote(remote).url
end

#schemeObject



63
64
65
# File 'lib/git_httpsable/push/repository.rb', line 63

def scheme
  ENV['GIT_SERVER_SCHEME'] || 'https'
end

#userinfoObject



53
54
55
56
57
58
59
60
61
# File 'lib/git_httpsable/push/repository.rb', line 53

def userinfo
  if env_git_access_token
    env_git_access_token
  elsif env_github_access_token
    "#{env_github_access_token}:x-oauth-basic"
  else
    fail NoAuthDataError
  end
end