Class: Mxrb::TeamServer::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/mxrb/team_server.rb

Overview

Git transport for Team Server. PATs are passed to a short-lived GIT_ASKPASS process and are never embedded in a URL or git config.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials: Credentials.new, runner: CommandRunner.new, authenticator: GitAuthenticator.new(credentials)) ⇒ Repository

Returns a new instance of Repository.



165
166
167
168
169
# File 'lib/mxrb/team_server.rb', line 165

def initialize(credentials: Credentials.new, runner: CommandRunner.new,
               authenticator: GitAuthenticator.new(credentials))
  @runner = runner
  @authenticator = authenticator
end

Class Method Details

.repository_url(source) ⇒ Object



215
216
217
218
219
220
221
222
223
224
# File 'lib/mxrb/team_server.rb', line 215

def self.repository_url(source)
  value = source.to_s.strip
  value = "https://#{HOST}/#{value}.git" if value.match?(APP_ID)
  uri = URI.parse(value)
  raise TeamServerError, "invalid Team Server Git URL: #{source.inspect}" unless valid_repository_uri?(uri)

  uri.to_s
rescue URI::InvalidURIError
  raise TeamServerError, "invalid Team Server Git URL: #{source.inspect}"
end

Instance Method Details

#clone(source, target:, branch: nil, depth: nil) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/mxrb/team_server.rb', line 171

def clone(source, target:, branch: nil, depth: nil)
  url = self.class.repository_url(source)
  root = File.expand_path(target)
  raise TeamServerError, "#{root}: destination already exists" if File.exist?(root)

  capture!(clone_arguments(url, root, branch:, depth:))
  mprs = postprocess_clone(root)
  CloneResult.new(root:, repository_url: url, branch:, mpr_files: mprs)
rescue StandardError
  FileUtils.rm_rf(root) if defined?(root) && root && File.directory?(root) && !File.exist?(File.join(root,
                                                                                                     '.git'))
  raise
end

#fetch(root, prune: true) ⇒ Object



185
186
187
188
189
190
191
# File 'lib/mxrb/team_server.rb', line 185

def fetch(root, prune: true)
  repository = repository_root(root)
  args = %w[git fetch]
  args << '--prune' if prune
  output = capture!(args, chdir: repository)
  GitResult.new(output:, repository_url: remote_url(repository))
end

#pull(root, ff_only: true) ⇒ Object



193
194
195
196
197
198
199
200
# File 'lib/mxrb/team_server.rb', line 193

def pull(root, ff_only: true)
  repository = repository_root(root)
  args = %w[git pull]
  args << '--ff-only' if ff_only
  output = capture!(args, chdir: repository)
  validate_mprs!(repository)
  GitResult.new(output:, repository_url: remote_url(repository))
end

#push(root, remote: 'origin', branch: nil) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/mxrb/team_server.rb', line 202

def push(root, remote: 'origin', branch: nil)
  repository = repository_root(root)
  args = ['git', 'push', '--', remote.to_s]
  args << branch.to_s if branch
  output = capture!(args, chdir: repository)
  GitResult.new(output:, repository_url: remote_url(repository, remote:))
end

#status(root) ⇒ Object



210
211
212
213
# File 'lib/mxrb/team_server.rb', line 210

def status(root)
  repository = repository_root(root)
  capture!(['git', 'status', '--short', '--branch'], chdir: repository)
end