Skip to content

Commit

Permalink
improve detecting upstream information
Browse files Browse the repository at this point in the history
  • Loading branch information
mislav committed Oct 24, 2011
1 parent ca16038 commit f6603e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
22 changes: 11 additions & 11 deletions lib/hub/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,17 @@ def current_branch
GIT_CONFIG['symbolic-ref -q HEAD']
end

def upstream_ref(branch)
GIT_CONFIG["name-rev #{branch}@{upstream} --name-only --refs='refs/remotes/*' --no-undefined"]
end

def upstream_remote(branch)
upstream_ref(branch) =~ %r{^remotes/(.+?)/} and $1
end

def tracked_branch
branch = current_branch && tracked_for(current_branch)
normalize_branch(branch) if branch
branch = current_branch && upstream_ref(current_branch)
branch.sub(%r{^remotes/(.+?)/}, '') if branch
end

def remotes
Expand All @@ -98,7 +106,7 @@ def remotes_group(name)

def current_remote
return if remotes.empty?
(current_branch && remote_for(current_branch)) || default_remote
(current_branch && upstream_remote(current_branch)) || default_remote
end

def default_remote
Expand All @@ -109,14 +117,6 @@ def normalize_branch(branch)
branch.sub('refs/heads/', '')
end

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

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

def http_clone?
GIT_CONFIG['config --bool hub.http-clone'] == 'true'
end
Expand Down
13 changes: 6 additions & 7 deletions test/hub_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ def setup
'config github.token' => 'abc123',
'config --get-all remote.origin.url' => 'git://github.com/defunkt/hub.git',
'config --get-all remote.mislav.url' => 'git://github.com/mislav/hub.git',
'config branch.master.remote' => 'origin',
'config branch.master.merge' => 'refs/heads/master',
"name-rev refs/heads/master@{upstream} --name-only --refs='refs/remotes/*' --no-undefined" => 'remotes/origin/master',
'config --bool hub.http-clone' => 'false',
'rev-parse --git-dir' => '.git'
)
Expand Down Expand Up @@ -700,7 +699,7 @@ def test_hub_compare_tracking_nothing

def test_hub_compare_tracking_branch
stub_branch('refs/heads/feature')
stub_tracking('feature', 'mislav', 'refs/heads/experimental')
stub_tracking('feature', 'mislav', 'experimental')

assert_command "compare",
"open https://github.com/mislav/hub/compare/experimental"
Expand Down Expand Up @@ -774,7 +773,7 @@ def test_hub_browse_subpage

def test_hub_browse_on_branch
stub_branch('refs/heads/feature')
stub_tracking('feature', 'mislav', 'refs/heads/experimental')
stub_tracking('feature', 'mislav', 'experimental')

assert_command "browse resque", "open https://github.com/tpw/resque"
assert_command "browse resque commits",
Expand Down Expand Up @@ -804,7 +803,7 @@ def test_hub_browse_no_tracking

def test_hub_browse_no_tracking_on_branch
stub_branch('refs/heads/feature')
stub_tracking('feature', nil, nil)
stub_tracking_nothing('feature')
assert_command "browse", "open https://github.com/defunkt/hub"
end

Expand Down Expand Up @@ -909,8 +908,8 @@ def stub_branch(value)
end

def stub_tracking(from, remote_name, remote_branch)
@git["config branch.#{from}.remote"] = remote_name
@git["config branch.#{from}.merge"] = remote_branch
value = remote_branch ? "remotes/#{remote_name}/#{remote_branch}" : nil
@git["name-rev refs/heads/#{from}@{upstream} --name-only --refs='refs/remotes/*' --no-undefined"] = value
end

def stub_tracking_nothing
Expand Down

0 comments on commit f6603e9

Please sign in to comment.