Skip to content

Commit

Permalink
Merge pull request #51 from tallowen/remote-config
Browse files Browse the repository at this point in the history
Configurable Remote
  • Loading branch information
Kenneth Reitz committed Apr 3, 2012
2 parents 9e64b1d + 547e070 commit 6ad8bad
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 19 deletions.
8 changes: 4 additions & 4 deletions legit/cli.py
Expand Up @@ -222,8 +222,8 @@ def cmd_graft(args):
if not into_branch:
into_branch = repo.head.ref.name

branch_names = get_branch_names(local=True, remote=False)
remote_branch_names = get_branch_names(local=False, remote=True)
branch_names = get_branch_names(local=True, remote_branches=False)
remote_branch_names = get_branch_names(local=False, remote_branches=True)

if branch not in branch_names:
print "{0} doesn't exist. Use a branch that does.".format(
Expand Down Expand Up @@ -306,7 +306,7 @@ def cmd_harvest(args):
else:
is_external = False

branch_names = get_branch_names(local=True, remote=False)
branch_names = get_branch_names(local=True, remote_branches=False)

if from_branch not in branch_names:
print "{0} isn't an available branch. Use a branch that is.".format(
Expand Down Expand Up @@ -520,4 +520,4 @@ def handle_abort(aborted):
hv='harvest',
har='harvest',
h='help'
)
)
48 changes: 33 additions & 15 deletions legit/scm.py
Expand Up @@ -11,9 +11,10 @@
import sys
import subprocess
from collections import namedtuple
from exceptions import ValueError
from operator import attrgetter

from git import Repo, Git
from git import Repo
from git.exc import GitCommandError

from .settings import settings
Expand Down Expand Up @@ -103,20 +104,19 @@ def fetch():

repo_check()

return repo.git.execute([git, 'fetch', repo.remotes[0].name])
return repo.git.execute([git, 'fetch', remote.name])


def smart_pull():
'git log --merges origin/master..master'

repo_check()

remote = repo.remotes[0].name
branch = repo.head.ref.name

fetch()

return smart_merge('{0}/{1}'.format(remote, branch))
return smart_merge('{0}/{1}'.format(remote.name, branch))


def smart_merge(branch, allow_rebase=True):
Expand Down Expand Up @@ -148,7 +148,7 @@ def push(branch=None):
if branch is None:
return repo.git.execute([git, 'push'])
else:
return repo.git.execute([git, 'push', repo.remotes[0].name, branch])
return repo.git.execute([git, 'push', remote.name, branch])


def checkout_branch(branch):
Expand Down Expand Up @@ -193,7 +193,7 @@ def unpublish_branch(branch):
repo_check()

return repo.git.execute([git,
'push', repo.remotes[0].name, ':{0}'.format(branch)])
'push', remote.name, ':{0}'.format(branch)])


def publish_branch(branch):
Expand All @@ -202,7 +202,7 @@ def publish_branch(branch):
repo_check()

return repo.git.execute([git,
'push', repo.remotes[0].name, branch])
'push', remote.name, branch])


def get_repo():
Expand All @@ -218,48 +218,66 @@ def get_repo():
return None


def get_branches(local=True, remote=True):
def get_remote():
reader = repo.config_reader()

# If there is no legit section return the default remote.
if not reader.has_section('legit'):
return repo.remotes[0]

# If there is no remote option in the legit section return the default.
if not any('legit' in s and 'remote' in s for s in reader.sections()):
return repo.remotes[0]

remote_name = reader.get('legit', 'remote')
if not remote_name in [r.name for r in repo.remotes]:
raise ValueError('Remote "{0}" does not exist! Please update your git '
'configuration.'.format(remote_name))

return repo.remote(remote_name)


def get_branches(local=True, remote_branches=True):
"""Returns a list of local and remote branches."""

repo_check()

# print local
branches = []

if remote:
if remote_branches:

# Remote refs.
try:
for b in repo.remotes[0].refs:
for b in remote.refs:
name = '/'.join(b.name.split('/')[1:])

if name not in settings.forbidden_branches:
branches.append(Branch(name, True))
except IndexError:
pass


if local:

# Local refs.
for b in [h.name for h in repo.heads]:

if b not in [br.name for br in branches] or not remote:
if b not in [br.name for br in branches] or not remote_branches:
if b not in settings.forbidden_branches:
branches.append(Branch(b, False))


return sorted(branches, key=attrgetter('name'))


def get_branch_names(local=True, remote=True):
def get_branch_names(local=True, remote_branches=True):

repo_check()

branches = get_branches(local=local, remote=remote)
branches = get_branches(local=local, remote_branches=remote_branches)

return [b.name for b in branches]



repo = get_repo()
remote = get_remote()

0 comments on commit 6ad8bad

Please sign in to comment.