Skip to content
Browse files
Merge pull request #216 from TJKresch/checkout_remote_branch
Add new command to checkout branch of remote repository
  • Loading branch information
sbuttgereit committed Jun 7, 2017
2 parents 18dd351 + c541f87 commit 4f12f2534507bacc5bc70d840c097cfdf79de99e
Showing with 54 additions and 2 deletions.
  1. +1 −0 Default.sublime-commands
  2. +1 −1 sgit/
  3. +52 −1 sgit/
@@ -33,6 +33,7 @@

{ "caption": "Git: Checkout", "command": "git_checkout_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 Tag", "command": "git_checkout_tag"},
{ "caption": "Git: Checkout Current File", "command": "git_checkout_current_file"},
@@ -53,7 +53,7 @@

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

from .merge import GitMergeCommand

@@ -6,12 +6,13 @@

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

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):
@@ -179,6 +180,56 @@ def on_done(self, repo, branch):
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:

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

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'})
self.window.run_command('git_status', {'refresh_only': True})

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

0 comments on commit 4f12f25

Please sign in to comment.