Class: GitProc::GitRemote

Inherits:
Object
  • Object
show all
Defined in:
lib/git-process/git_remote.rb

Overview

Git Remote configuration

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gitconfig) ⇒ GitRemote

Returns a new instance of GitRemote.

Parameters:



43
44
45
# File 'lib/git-process/git_remote.rb', line 43

def initialize(gitconfig)
  @gitconfig = gitconfig
end

Class Method Details

.hostname_and_user_from_ssh_config(host_alias, config_file) ⇒ Object

TODO:

use the netrc gem

noinspection RubyClassMethodNamingConvention



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/git-process/git_remote.rb', line 217

def self.hostname_and_user_from_ssh_config(host_alias, config_file)
  if File.exists?(config_file)
    config_lines = File.new(config_file).readlines

    in_host_section = false
    host_name = nil
    user_name = nil

    config_lines.each do |line|
      line.chop!
      if /^\s*Host\s+#{host_alias}\s*$/ =~ line
        in_host_section = true
        next
      end

      if in_host_section and (/^\s*HostName\s+\S+\s*$/ =~ line)
        host_name = line.sub(/^\s*HostName\s+(\S+)\s*$/, '\1')
        break unless user_name.nil?
      elsif in_host_section and (/^\s*User\s+\S+\s*$/ =~ line)
        user_name = line.sub(/^\s*User\s+(\S+)\s*$/, '\1')
        break unless host_name.nil?
      elsif in_host_section and (/^\s*Host\s+.*$/ =~ line)
        break
      end
    end
    host_name.nil? ? nil : [host_name, user_name]
  else
    nil
  end
end

Instance Method Details

#add_remote(remote_name, url) ⇒ void Also known as: add

This method returns an undefined value.



207
208
209
# File 'lib/git-process/git_remote.rb', line 207

def add_remote(remote_name, url)
  config.gitlib.command(:remote, ['add', remote_name, url])
end

#configGitProc::GitConfig

Returns:



55
56
57
# File 'lib/git-process/git_remote.rb', line 55

def config
  @gitconfig
end

#exists?Boolean

Returns does this have a remote defined?.

Returns:

  • (Boolean)

    does this have a remote defined?



68
69
70
71
72
73
74
# File 'lib/git-process/git_remote.rb', line 68

def exists?
  if @has_remote.nil?
    @has_remote = (config.gitlib.command(:remote) != '')
  end
  logger.debug { "Does a remote exist? #{@has_remote}" }
  @has_remote
end

#expanded_url(server_name = 'origin', raw_url = nil, opts = {}) ⇒ Object

TODO:

use the netrc gem

Expands the git configuration server name to a url.

Takes into account further expanding an SSH uri that uses SSH aliasing in .ssh/config

Parameters:

  • server_name (String) (defaults to: 'origin')

    the git configuration server name; defaults to ‘origin’

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :ssh_config_file (String)

    the SSH config file to use; defaults to ~/.ssh/config

Returns:

  • the fully expanded URL; never nil

Raises:



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/git-process/git_remote.rb', line 166

def expanded_url(server_name = 'origin', raw_url = nil, opts = {})
  if raw_url.nil?
    raise ArgumentError.new('Need server_name') unless server_name

    conf_key = "remote.#{server_name}.url"
    url = config[conf_key]

    raise GitHubService::NoRemoteRepository.new("There is no value set for '#{conf_key}'") if url.nil? or url.empty?
  else
    raise GitHubService::NoRemoteRepository.new("There is no value set for '#{raw_url}'") if raw_url.nil? or raw_url.empty?
    url = raw_url
  end

  if /^\S+@/ =~ url
    url.sub(/^(\S+@\S+?):(.*)$/, "ssh://\\1/\\2")
  else
    uri = URI.parse(url)
    host = uri.host
    scheme = uri.scheme

    raise URI::InvalidURIError.new("Need a scheme in URI: '#{url}'") unless scheme

    if scheme == 'file'
      url
    elsif host.nil?
      # assume that the 'scheme' is the named configuration in ~/.ssh/config
      rv = GitRemote.hostname_and_user_from_ssh_config(scheme, opts[:ssh_config_file] ||= "#{ENV['HOME']}/.ssh/config")

      raise GitHubService::NoRemoteRepository.new("Could not determine a host from #{url}") if rv.nil?

      host = rv[0]
      user = rv[1]
      url.sub(/^\S+:(\S+)$/, "ssh://#{user}@#{host}/\\1")
    else
      url
    end
  end
end

#logger#info, ...

Returns:

  • (#info, #warn, #debug, #error)


49
50
51
# File 'lib/git-process/git_remote.rb', line 49

def logger
  @logger ||= @gitconfig.logger
end

#master_branch_nameString Also known as: remote_integration_branch_name

Takes #remote_name and combines it with GitProc::GitConfig#master_branch.

Examples:

master_branch_name #=> origin/master

Returns:

  • (String)

    the complete remote name of the integration branch



131
132
133
# File 'lib/git-process/git_remote.rb', line 131

def master_branch_name
  "#{self.name}/#{config.master_branch}"
end

#remote_nameString? Also known as: name

Returns the “remote name” to use. By convention the most common name is “origin”.

If the Git configuration “gitProcess.remoteName” is set, that will always be used. Otherwise this simple returns the first name it finds in the list of remotes.

Returns:

  • (String, nil)

    the remote name, or nil if there are none defined



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/git-process/git_remote.rb', line 102

def remote_name
  unless @remote_name
    @remote_name = config['gitProcess.remoteName']
    if @remote_name.nil? or @remote_name.empty?
      remotes = self.remote_names
      if remotes.empty?
        @remote_name = nil
      else
        @remote_name = remotes[0]
        raise "remote name is not a String: #{@remote_name.inspect}" unless @remote_name.is_a? String
      end
    end
    logger.debug { "Using remote name of '#{@remote_name}'" }
  end
  @remote_name
end

#remote_namesObject



139
140
141
142
143
144
145
146
# File 'lib/git-process/git_remote.rb', line 139

def remote_names
  remote_str = config.gitlib.command(:remote, [:show])
  if remote_str.nil? or remote_str.empty?
    []
  else
    remote_str.split(/\n/)
  end
end

#repo_nameString

The name of the repository

Examples:

repo_name #=> "jdigger/git-process"

Returns:

  • (String)

    the name of the repository



84
85
86
87
88
89
90
91
92
# File 'lib/git-process/git_remote.rb', line 84

def repo_name
  unless @repo_name
    url = config["remote.#{name}.url"]
    raise GitProcessError.new("There is no #{name} url set up.") if url.nil? or url.empty?
    uri = Addressable::URI.parse(url)
    @repo_name = uri.path.sub(/\.git/, '').sub(/^\//, '')
  end
  @repo_name
end

#server_nameObject

Deprecated.
TODO:

Remove



62
63
64
# File 'lib/git-process/git_remote.rb', line 62

def server_name
  @server_name ||= self.remote_name
end