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|
  if 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
  else
    cache[remote] = { }
  end
end
LGHCONF =
"http://github.com/guides/local-github-config"

Instance Method Summary collapse

Instance Method Details

#current_branchObject



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

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

#current_remoteObject



77
78
79
80
81
82
83
84
85
# File 'lib/hub/context.rb', line 77

def current_remote
  return if remotes.empty?

  if current_branch
    remote_for(current_branch)
  else
    default_remote
  end
end

#default_remoteObject



87
88
89
# File 'lib/hub/context.rb', line 87

def default_remote
  remotes.first
end

#github_token(fatal = true) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/hub/context.rb', line 50

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/hub/context.rb', line 115

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
      url = '[email protected]:%s/%s.git'
    elsif http_clone?
      url = 'http://github.com/%s/%s.git'
    else
      url = 'git://github.com/%s/%s.git'
    end

    url % [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.



42
43
44
45
46
47
48
# File 'lib/hub/context.rb', line 42

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)


103
104
105
# File 'lib/hub/context.rb', line 103

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

#is_repo?Boolean

Core.repositoryformatversion should exist for all git repositories, and be blank for all non-git repositories. If there’s a better config setting to check here, this can be changed without breaking anything.

Returns:

  • (Boolean)


111
112
113
# File 'lib/hub/context.rb', line 111

def is_repo?
  GIT_CONFIG['config core.repositoryformatversion']
end

#normalize_branch(branch) ⇒ Object



91
92
93
# File 'lib/hub/context.rb', line 91

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

#remote_for(branch) ⇒ Object



95
96
97
# File 'lib/hub/context.rb', line 95

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

#remotesObject



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

def remotes
  list = GIT_CONFIG['remote'].to_s.split("\n")
  main = list.delete('origin') and list.unshift(main)
  list
end

#remotes_group(name) ⇒ Object



73
74
75
# File 'lib/hub/context.rb', line 73

def remotes_group(name)
  GIT_CONFIG["config remotes.#{name}"]
end

#repo_nameObject



36
37
38
# File 'lib/hub/context.rb', line 36

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

#repo_ownerObject



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

def repo_owner
  REMOTES[default_remote][:user]
end

#repo_userObject



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

def repo_user
  REMOTES[current_remote][:user]
end

#tracked_branchObject



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

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

#tracked_for(branch) ⇒ Object



99
100
101
# File 'lib/hub/context.rb', line 99

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