Module: Hub::Context

Included in:
Commands
Defined in:
lib/hub/context.rb,
lib/hub/commands.rb

Overview

See context.rb

Constant Summary collapse

GIT_CONFIG =

Caches output when shelling out to git

Hash.new do |cache, cmd|
  result = %x{git #{cmd}}.chomp
  cache[cmd] = $?.success? && !result.empty? ? result : nil
end
REMOTES =

Parses URLs for git remotes and stores info

Hash.new do |cache, remote|
  url = GIT_CONFIG["config remote.#{remote}.url"]

  if url && url.to_s =~ %r{\bgithub\.com[:/](.+)/(.+).git$}
    cache[remote] = { :user => $1, :repo => $2 }
  else
    cache[remote] = { }
  end
end
LGHCONF =
"http://github.com/guides/local-github-config"

Instance Method Summary collapse

Instance Method Details

#current_branchObject



54
55
56
# File 'lib/hub/context.rb', line 54

def current_branch
  GIT_CONFIG['symbolic-ref -q HEAD']
end

#current_remoteObject



63
64
65
# File 'lib/hub/context.rb', line 63

def current_remote
  (current_branch && remote_for(current_branch)) || default_remote
end

#default_remoteObject



67
68
69
# File 'lib/hub/context.rb', line 67

def default_remote
  'origin'
end

#github_token(fatal = true) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/hub/context.rb', line 46

def github_token(fatal = true)
  if token = GIT_CONFIG['config github.token']
    token
  elsif fatal
    abort("** No GitHub token set. See #{LGHCONF}")
  end
end

#github_url(options = {}) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/hub/context.rb', line 87

def github_url(options = {})
  repo = options[:repo]
  user, repo = repo.split('/') if repo && repo.index('/')
  user ||= options[:user] || github_user
  repo ||= repo_name
  secure = options[:private]

  if options[:web] == 'wiki'
    scheme = secure ? 'https:' : 'http:'
    '%s//wiki.github.com/%s/%s/' % [scheme, user, repo]
  elsif options[:web]
    scheme = secure ? 'https:' : 'http:'
    path = options[:web] == true ? '' : options[:web].to_s
    '%s//github.com/%s/%s%s' % [scheme, user, repo, path]
  else
    if secure
      '[email protected]:%s/%s.git'
    elsif http_clone?
      'http://github.com/%s/%s.git'
    else
      'git://github.com/%s/%s.git'
    end % [user, repo]
  end
end

#github_user(fatal = true) ⇒ Object

Either returns the GitHub user as set by git-config(1) or aborts with an error message.



38
39
40
41
42
43
44
# File 'lib/hub/context.rb', line 38

def github_user(fatal = true)
  if user = GIT_CONFIG['config github.user']
    user
  elsif fatal
    abort("** No GitHub user set. See #{LGHCONF}")
  end
end

#http_clone?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/hub/context.rb', line 83

def http_clone?
  GIT_CONFIG['config --bool hub.http-clone'] == 'true'
end

#normalize_branch(branch) ⇒ Object



71
72
73
# File 'lib/hub/context.rb', line 71

def normalize_branch(branch)
  branch.sub('refs/heads/', '')
end

#remote_for(branch) ⇒ Object



75
76
77
# File 'lib/hub/context.rb', line 75

def remote_for(branch)
  GIT_CONFIG['config branch.%s.remote' % normalize_branch(branch)]
end

#repo_nameObject



32
33
34
# File 'lib/hub/context.rb', line 32

def repo_name
  REMOTES[default_remote][:repo] || File.basename(Dir.pwd)
end

#repo_ownerObject



24
25
26
# File 'lib/hub/context.rb', line 24

def repo_owner
  REMOTES[default_remote][:user]
end

#repo_userObject



28
29
30
# File 'lib/hub/context.rb', line 28

def repo_user
  REMOTES[current_remote][:user]
end

#tracked_branchObject



58
59
60
61
# File 'lib/hub/context.rb', line 58

def tracked_branch
  branch = current_branch && tracked_for(current_branch)
  normalize_branch(branch) if branch
end

#tracked_for(branch) ⇒ Object



79
80
81
# File 'lib/hub/context.rb', line 79

def tracked_for(branch)
  GIT_CONFIG['config branch.%s.merge' % normalize_branch(branch)]
end