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:



46
47
48
# File 'lib/git-process/git_remote.rb', line 46

def initialize(gitconfig)
  @gitconfig = gitconfig
end

Class Method Details

.hostname_and_user_from_ssh_config(host_alias, config_file) ⇒ Object

noinspection RubyClassMethodNamingConvention



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/git-process/git_remote.rb', line 189

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.



180
181
182
# File 'lib/git-process/git_remote.rb', line 180

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

#configGitProc::GitConfig

Returns:



58
59
60
# File 'lib/git-process/git_remote.rb', line 58

def config
  @gitconfig
end

#exists?Boolean

Returns does this have a remote defined?.

Returns:

  • (Boolean)

    does this have a remote defined?



71
72
73
74
75
76
77
# File 'lib/git-process/git_remote.rb', line 71

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

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:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/git-process/git_remote.rb', line 139

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)


52
53
54
# File 'lib/git-process/git_remote.rb', line 52

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

#master_branch_nameObject



109
110
111
# File 'lib/git-process/git_remote.rb', line 109

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

#nameObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/git-process/git_remote.rb', line 91

def 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_a? String' unless @remote_name.is_a? String
      end
    end
    logger.debug { "Using remote name of '#{@remote_name}'" }
  end
  @remote_name
end

#remote_namesObject



114
115
116
117
118
119
120
121
# File 'lib/git-process/git_remote.rb', line 114

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_nameObject



80
81
82
83
84
85
86
87
88
# File 'lib/git-process/git_remote.rb', line 80

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



65
66
67
# File 'lib/git-process/git_remote.rb', line 65

def server_name
  @server_name ||= self.remote_name
end