Skip to content

Commit

Permalink
Merge pull request #216 from TJKresch/checkout_remote_branch
Browse files Browse the repository at this point in the history
Add new command to checkout branch of remote repository
  • Loading branch information
sbuttgereit committed Jun 7, 2017
2 parents 18dd351 + c541f87 commit 4f12f25
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
1 change: 1 addition & 0 deletions Default.sublime-commands
Expand Up @@ -33,6 +33,7 @@


{ "caption": "Git: Checkout", "command": "git_checkout_branch"}, { "caption": "Git: Checkout", "command": "git_checkout_branch"},
{ "caption": "Git: Checkout New Branch", "command": "git_checkout_new_branch"}, { "caption": "Git: Checkout New Branch", "command": "git_checkout_new_branch"},
{ "caption": "Git: Checkout Remote Branch", "command": "git_checkout_remote_branch"},
{ "caption": "Git: Checkout Commit", "command": "git_checkout_commit"}, { "caption": "Git: Checkout Commit", "command": "git_checkout_commit"},
{ "caption": "Git: Checkout Tag", "command": "git_checkout_tag"}, { "caption": "Git: Checkout Tag", "command": "git_checkout_tag"},
{ "caption": "Git: Checkout Current File", "command": "git_checkout_current_file"}, { "caption": "Git: Checkout Current File", "command": "git_checkout_current_file"},
Expand Down
2 changes: 1 addition & 1 deletion sgit/__init__.py
Expand Up @@ -53,7 +53,7 @@


from .checkout import (GitCheckoutBranchCommand, GitCheckoutCommitCommand, from .checkout import (GitCheckoutBranchCommand, GitCheckoutCommitCommand,
GitCheckoutNewBranchCommand, GitCheckoutCurrentFileCommand, GitCheckoutNewBranchCommand, GitCheckoutCurrentFileCommand,
GitCheckoutTagCommand) GitCheckoutTagCommand, GitCheckoutRemoteBranchCommand)


from .merge import GitMergeCommand from .merge import GitMergeCommand


Expand Down
53 changes: 52 additions & 1 deletion sgit/checkout.py
Expand Up @@ -6,12 +6,13 @@


from .util import noop from .util import noop
from .cmd import GitCmd from .cmd import GitCmd
from .helpers import GitStatusHelper, GitBranchHelper, GitErrorHelper, GitLogHelper from .helpers import GitStatusHelper, GitBranchHelper, GitErrorHelper, GitLogHelper, GitRemoteHelper
from .helpers import GitTagHelper from .helpers import GitTagHelper




GIT_BRANCH_EXISTS_MSG = "The branch %s already exists. Do you want to overwrite it?" GIT_BRANCH_EXISTS_MSG = "The branch %s already exists. Do you want to overwrite it?"


NO_REMOTES = u"No remotes have been configured. Remotes can be added with the Git: Add Remote command. Do you want to add a remote now?"


class GitCheckoutWindowCmd(GitCmd, GitBranchHelper, GitLogHelper, GitErrorHelper): class GitCheckoutWindowCmd(GitCmd, GitBranchHelper, GitLogHelper, GitErrorHelper):
pass pass
Expand Down Expand Up @@ -179,6 +180,56 @@ def on_done(self, repo, branch):
self.window.run_command('git_status', {'refresh_only': True}) self.window.run_command('git_status', {'refresh_only': True})




class GitCheckoutRemoteBranchCommand(WindowCommand, GitCheckoutWindowCmd, GitRemoteHelper):
"""Checkout a remote branch."""
def run(self, repo=None):
repo = self.get_repo()
if not repo:
return

remotes = self.get_remotes(repo)
if not remotes:
if sublime.ok_cancel_dialog(NO_REMOTES, 'Add Remote'):
self.window.run_command('git_remote_add')
return

choices = self.format_quick_remotes(remotes)
self.window.show_quick_panel(choices, partial(self.remote_panel_done, repo, choices))

def remote_panel_done(self, repo, choices, idx):
if idx != -1:
remote = choices[idx][0]

remote_branches = self.get_remote_branches(repo, remote)
if not remote_branches:
return sublime.error_message("No branches on remote %s" % remote)

formatted_remote_branches = self.format_quick_branches(remote_branches)
local_branches = [b for _, b in self.get_branches(repo)]
remote_only_branches = [b for b in formatted_remote_branches if b[0] not in frozenset(local_branches)]

if not remote_only_branches:
return sublime.error_message("All remote branches are already present locally")

def on_remote():
self.window.show_quick_panel(remote_only_branches, partial(self.remote_branch_panel_done, repo, remote_only_branches))

sublime.set_timeout(on_remote, 50)

def remote_branch_panel_done(self, repo, branches, idx):
if idx != -1:
branch = branches[idx][0]

exit, stdout, stderr = self.git(['checkout', branch], cwd=repo)
if exit == 0:
panel = self.window.get_output_panel('git-checkout')
panel.run_command('git_panel_write', {'content': stderr})
self.window.run_command('show_panel', {'panel': 'output.git-checkout'})
else:
sublime.error_message(self.format_error_message(stderr))
self.window.run_command('git_status', {'refresh_only': True})


class GitCheckoutCurrentFileCommand(TextCommand, GitCmd, GitStatusHelper): class GitCheckoutCurrentFileCommand(TextCommand, GitCmd, GitStatusHelper):
""" """
Documentation coming soon. Documentation coming soon.
Expand Down

0 comments on commit 4f12f25

Please sign in to comment.