Class: GitProc::GitRemote
- Inherits:
-
Object
- Object
- GitProc::GitRemote
- Defined in:
- lib/git-process/git_remote.rb
Overview
Git Remote configuration
Class Method Summary collapse
-
.hostname_and_user_from_ssh_config(host_alias, config_file) ⇒ Object
noinspection RubyClassMethodNamingConvention.
Instance Method Summary collapse
- #add_remote(remote_name, url) ⇒ void (also: #add)
- #config ⇒ GitProc::GitConfig
-
#exists? ⇒ Boolean
Does this have a remote defined?.
-
#expanded_url(server_name = 'origin', raw_url = nil, opts = {}) ⇒ Object
Expands the git configuration server name to a url.
-
#initialize(gitconfig) ⇒ GitRemote
constructor
A new instance of GitRemote.
- #logger ⇒ #info, ...
- #master_branch_name ⇒ Object
- #name ⇒ Object
- #remote_names ⇒ Object
- #repo_name ⇒ Object
- #server_name ⇒ Object deprecated Deprecated.
Constructor Details
#initialize(gitconfig) ⇒ GitRemote
Returns a new instance of GitRemote.
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 |
#config ⇒ GitProc::GitConfig
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?.
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
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 (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, ...
52 53 54 |
# File 'lib/git-process/git_remote.rb', line 52 def logger @logger ||= @gitconfig.logger end |
#master_branch_name ⇒ Object
109 110 111 |
# File 'lib/git-process/git_remote.rb', line 109 def master_branch_name "#{self.name}/#{config.master_branch}" end |
#name ⇒ Object
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_names ⇒ Object
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_name ⇒ Object
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_name ⇒ Object
Deprecated.
TODO: Remove
65 66 67 |
# File 'lib/git-process/git_remote.rb', line 65 def server_name @server_name ||= self.remote_name end |