Class: Hub::Context::LocalRepo

Inherits:
Struct
  • Object
show all
Extended by:
Forwardable
Includes:
GitReaderMethods
Defined in:
lib/hub/context.rb

Constant Summary collapse

ORIGIN_NAMES =
%w[ upstream github origin ]

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GitReaderMethods

extended

Class Method Details

.default_hostObject



235
236
237
# File 'lib/hub/context.rb', line 235

def self.default_host
  ENV['GITHUB_HOST'] || main_host
end

.main_hostObject



239
240
241
# File 'lib/hub/context.rb', line 239

def self.main_host
  'github.com'
end

Instance Method Details

#branch_at_ref(*parts) ⇒ Object



168
169
170
171
172
173
174
175
176
# File 'lib/hub/context.rb', line 168

def branch_at_ref(*parts)
  begin
    head = file_read(*parts)
  rescue Errno::ENOENT
    return nil
  else
    Branch.new(self, head.rstrip) if head.sub!('ref: ', '')
  end
end

#current_branchObject



164
165
166
# File 'lib/hub/context.rb', line 164

def current_branch
  @current_branch ||= branch_at_ref('HEAD')
end

#file_exist?(*parts) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/hub/context.rb', line 182

def file_exist?(*parts)
  File.exist?(File.join(git_dir, *parts))
end

#file_read(*parts) ⇒ Object



178
179
180
# File 'lib/hub/context.rb', line 178

def file_read(*parts)
  File.read(File.join(git_dir, *parts))
end

#known_host?(host) ⇒ Boolean

Returns:

  • (Boolean)


229
230
231
232
233
# File 'lib/hub/context.rb', line 229

def known_host?(host)
  default = default_host
  default == host || "ssh.#{default}" == host ||
    git_config('hub.host', :all).to_s.split("\n").include?(host)
end

#main_projectObject



151
152
153
# File 'lib/hub/context.rb', line 151

def main_project
  remote = origin_remote and remote.project
end

#master_branchObject



186
187
188
189
190
191
# File 'lib/hub/context.rb', line 186

def master_branch
  if remote = origin_remote
    default_branch = branch_at_ref("refs/remotes/#{remote}/HEAD")
  end
  default_branch || Branch.new(self, 'refs/heads/master')
end

#nameObject



133
134
135
136
137
138
139
# File 'lib/hub/context.rb', line 133

def name
  if project = main_project
    project.name
  else
    File.basename(dir)
  end
end

#origin_remoteObject



221
222
223
# File 'lib/hub/context.rb', line 221

def origin_remote
  remotes.detect {|r| r.urls.any? }
end

#remote_branch_and_project(username_fetcher, prefer_upstream = false) ⇒ Object



155
156
157
158
159
160
161
162
# File 'lib/hub/context.rb', line 155

def remote_branch_and_project(username_fetcher, prefer_upstream = false)
  project = main_project
  if project and branch = current_branch
    branch = branch.push_target(username_fetcher.call(project.host), prefer_upstream)
    project = remote_by_name(branch.remote_name).project if branch && branch.remote?
  end
  [branch, project]
end

#remote_by_name(remote_name) ⇒ Object



225
226
227
# File 'lib/hub/context.rb', line 225

def remote_by_name(remote_name)
  remotes.find {|r| r.name == remote_name }
end

#remotesObject



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/hub/context.rb', line 195

def remotes
  @remotes ||= begin
    names = []
    url_memo = Hash.new {|h,k| names << k; h[k]=[] }
    git_command('remote -v').to_s.split("\n").map do |line|
      next if line !~ /^(.+?)\t(.+) \(/
      name, url = $1, $2
      url_memo[name] << url
    end
    ((ORIGIN_NAMES + names) & names).map do |name|
      urls = url_memo[name].uniq
      Remote.new(self, name, urls)
    end
  end
end

#remotes_for_publish(owner_name) ⇒ Object



211
212
213
214
215
# File 'lib/hub/context.rb', line 211

def remotes_for_publish(owner_name)
  list = ORIGIN_NAMES.map {|n| remote_by_name(n) }
  list << remotes.find {|r| p = r.project and p.owner == owner_name }
  list.compact.uniq.reverse
end

#remotes_group(name) ⇒ Object



217
218
219
# File 'lib/hub/context.rb', line 217

def remotes_group(name)
  git_config "remotes.#{name}"
end

#repo_hostObject



147
148
149
# File 'lib/hub/context.rb', line 147

def repo_host
  project = main_project and project.host
end

#repo_ownerObject



141
142
143
144
145
# File 'lib/hub/context.rb', line 141

def repo_owner
  if project = main_project
    project.owner
  end
end

#ssh_configObject



246
247
248
# File 'lib/hub/context.rb', line 246

def ssh_config
  @ssh_config ||= SshConfig.new
end