Skip to content

Commit

Permalink
Merge cd9c885 into c2d42fb
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Jul 27, 2017
2 parents c2d42fb + cd9c885 commit 5544912
Show file tree
Hide file tree
Showing 15 changed files with 356 additions and 540 deletions.
103 changes: 59 additions & 44 deletions core/commands/checkout.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

from ..git_command import GitCommand
from ...common import util
from ..ui_mixins.quick_panel import show_branch_panel


NEW_BRANCH_PROMPT = "Branch name:"
NEW_BRANCH_INVALID = "`{}` is a invalid branch name.\nRead more on $(man git-check-ref-format)"


class GsCheckoutBranchCommand(WindowCommand, GitCommand):
Expand All @@ -16,32 +18,24 @@ class GsCheckoutBranchCommand(WindowCommand, GitCommand):
user selected.
"""

def run(self):
sublime.set_timeout_async(self.run_async, 0)

def run_async(self):
stdout = self.git("branch", "--no-color")
branch_entries = (branch.strip() for branch in stdout.split("\n") if branch)
def run(self, branch=None):
sublime.set_timeout_async(lambda: self.run_async(branch), 0)

# The line with the active branch will begin with an asterisk.
self.local_inactive_branches = [branch for branch in branch_entries if not branch[0] == "*"]

if not self.local_inactive_branches:
self.window.show_quick_panel(["There are no branches available."], None)
def run_async(self, branch):
if branch:
self.on_branch_selection(branch)
else:
self.window.show_quick_panel(
self.local_inactive_branches,
self.on_selection,
flags=sublime.MONOSPACE_FONT
)

def on_selection(self, branch_name_index):
if branch_name_index == -1:
show_branch_panel(
self.on_branch_selection,
local_branches_only=True,
ignore_current_branch=True)

def on_branch_selection(self, branch):
if not branch:
return

branch_name = self.local_inactive_branches[branch_name_index]
self.git("checkout", branch_name)
sublime.status_message("Checked out `{}` branch.".format(branch_name))
self.git("checkout", branch)
sublime.status_message("Checked out `{}` branch.".format(branch))
util.view.refresh_gitsavvy(self.window.active_view(), refresh_sidebar=True)


Expand All @@ -51,21 +45,25 @@ class GsCheckoutNewBranchCommand(WindowCommand, GitCommand):
Prompt the user for a new branch name, create it, and check it out.
"""

def run(self):
sublime.set_timeout_async(self.run_async)
def run(self, base_branch=None):
sublime.set_timeout_async(lambda: self.run_async(base_branch))

def run_async(self, name=""):
self.window.show_input_panel(NEW_BRANCH_PROMPT, name, self.on_done, None, None)
def run_async(self, base_branch=None):
self.base_branch = base_branch
self.window.show_input_panel(NEW_BRANCH_PROMPT, "", self.on_done, None, None)

def on_done(self, branch_name):
pattern = r"^(?!\.|.*\.\..*|.*@.*|\/)[a-zA-Z0-9\-\_\/\.\u263a-\U0001f645]+(?<!\.lock)(?<!\/)(?<!\.)$"
match = re.match(pattern, branch_name)
if not match:
sublime.error_message("`{}` is a invalid branch name.\nRead more on $(man git-check-ref-format)".format(branch_name))
sublime.error_message(NEW_BRANCH_INVALID.format(branch_name))
sublime.set_timeout_async(self.run_async(branch_name))
return None

self.git("checkout", "-b", branch_name)
self.git(
"checkout", "-b",
branch_name,
self.base_branch if self.base_branch else None)
sublime.status_message("Created and checked out `{}` branch.".format(branch_name))
util.view.refresh_gitsavvy(self.window.active_view())

Expand All @@ -78,25 +76,42 @@ class GsCheckoutRemoteBranchCommand(WindowCommand, GitCommand):
selected branch.
"""

def run(self):
sublime.set_timeout_async(self.run_async)

def run_async(self):
self.remote_branches = self.list_remote_branches()
self.window.show_quick_panel(
self.remote_branches,
self.on_selection,
flags=sublime.MONOSPACE_FONT
)

def on_selection(self, remote_branch_index):
if remote_branch_index == -1:
def run(self, remote_branch=None):
sublime.set_timeout_async(lambda: self.run_async(remote_branch))

def run_async(self, remote_branch):
if remote_branch:
self.on_branch_selection(remote_branch)
else:
show_branch_panel(
self.on_branch_selection,
remote_branches_only=True)

def on_branch_selection(self, remote_branch):
if not remote_branch:
return

remote_branch = self.remote_branches[remote_branch_index]
self.remote_branch = remote_branch
local_name = remote_branch.split("/", 1)[1]
self.git("checkout", "-b", local_name, "--track", remote_branch)
sublime.status_message("Checked out `{}` as local branch `{}`.".format(remote_branch, local_name))
v = self.window.show_input_panel(
NEW_BRANCH_PROMPT,
local_name,
self.on_enter_local_name,
None,
None)
v.run_command("select_all")

def on_enter_local_name(self, branch_name):
pattern = r"^(?!\.|.*\.\..*|.*@.*|\/)[a-zA-Z0-9\-\_\/\.\u263a-\U0001f645]+(?<!\.lock)(?<!\/)(?<!\.)$"
match = re.match(pattern, branch_name)
if not match:
sublime.error_message(NEW_BRANCH_INVALID.format(branch_name))
sublime.set_timeout_async(self.on_branch_selection(self.remote_branch))
return None

self.git("checkout", "-b", branch_name, "--track", self.remote_branch)
sublime.status_message(
"Checked out `{}` as local branch `{}`.".format(self.remote_branch, branch_name))
util.view.refresh_gitsavvy(self.window.active_view(), refresh_sidebar=True)


Expand Down
33 changes: 10 additions & 23 deletions core/commands/commit_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from ...common import util
from ..git_command import GitCommand
from ..ui_mixins.quick_panel import PanelActionMixin
from ..ui_mixins.quick_panel import PanelActionMixin, show_branch_panel


COMMIT_NODE_CHAR = "●"
Expand Down Expand Up @@ -146,35 +146,22 @@ def run(self, base_commit=None, target_commit=None, file_path=None):
self._target_commit = target_commit
sublime.set_timeout_async(self.run_async)

def run_async(self):
self.all_branches = [b.name_with_remote for b in self.get_branches()]
def run_async(self, **kwargs):
show_branch_panel(self.on_branch_selection)

if hasattr(self, '_selected_branch') and self._selected_branch in self.all_branches:
pre_selected_index = self.all_branches.index(self._selected_branch)
def on_branch_selection(self, branch):
if branch:
self.window.run_command("gs_compare_commit", {
"file_path": self._file_path,
"base_commit": self._base_commit if self._base_commit else branch,
"target_commit": self._target_commit if self._target_commit else branch
})
else:
pre_selected_index = self.all_branches.index(self.get_current_branch_name())

self.window.show_quick_panel(
self.all_branches,
self.on_branch_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index,
)

def on_branch_selection(self, index):
if index == -1:
self.window.run_command("gs_compare_against", {
"base_commit": self._base_commit,
"target_commit": self._target_commit,
"file_path": self._file_path
})
return
selected_branch = self.all_branches[index]
self.window.run_command("gs_compare_commit", {
"file_path": self._file_path,
"base_commit": self._base_commit if self._base_commit else selected_branch,
"target_commit": self._target_commit if self._target_commit else selected_branch
})


class GsCompareAgainstCommand(PanelActionMixin, WindowCommand, GitCommand):
Expand Down
39 changes: 10 additions & 29 deletions core/commands/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

from ..git_command import GitCommand
from ...common import util


ALL_REMOTES = "All remotes."
from ..ui_mixins.quick_panel import show_remote_panel


class GsFetchCommand(WindowCommand, GitCommand):
Expand All @@ -19,39 +17,22 @@ def run(self, remote=None):
if remote:
return self.do_fetch(remote)

self.remotes = list(self.get_remotes().keys())
show_remote_panel(self.on_remote_selection, show_all=True)

if not self.remotes:
self.window.show_quick_panel(["There are no remotes available."], None)
else:
if len(self.remotes) > 1:
self.remotes.append(ALL_REMOTES)

pre_selected_idx = (self.remotes.index(self.last_remote_used)
if self.last_remote_used in self.remotes
else 0)

self.window.show_quick_panel(
self.remotes,
self.on_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_idx
)

def on_selection(self, remotes_index):
# User cancelled.
if remotes_index == -1:
def on_remote_selection(self, remote):
if not remote:
return
remote = self.remotes[remotes_index]

if remote == ALL_REMOTES:
if remote is True:
sublime.set_timeout_async(lambda: self.do_fetch())
else:
self.last_remote_used = remote
sublime.set_timeout_async(lambda: self.do_fetch(remote))

def do_fetch(self, remote=None):
sublime.status_message("Starting fetch...")
if remote is None:
sublime.status_message("Starting fetch all remotes...")
else:
sublime.status_message("Starting fetch {}...".format(remote))

self.fetch(remote)
sublime.status_message("Fetch complete.")
util.view.refresh_gitsavvy(self.window.active_view())
30 changes: 9 additions & 21 deletions core/commands/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

from ...common import util
from ..git_command import GitCommand
from ..ui_mixins.quick_panel import PanelActionMixin, PanelCommandMixin, show_log_panel
from ..ui_mixins.quick_panel import PanelActionMixin, PanelCommandMixin
from ..ui_mixins.quick_panel import show_log_panel, show_branch_panel


class LogMixin(object):
Expand Down Expand Up @@ -74,16 +75,16 @@ def run_async(self, **kwargs):

self.window.show_quick_panel(
[entry[3] for entry in self._entries],
self.on_author_selection,
lambda index: self.on_author_selection(index, **kwargs),
flags=sublime.MONOSPACE_FONT,
selected_index=(list(line[2] for line in self._entries)).index(email)
)

def on_author_selection(self, index):
def on_author_selection(self, index, **kwargs):
if index == -1:
return
self._selected_author = self._entries[index][3]
super().run_async()
super().run_async(**kwargs)

def log(self, **kwargs):
return super().log(author=self._selected_author, **kwargs)
Expand All @@ -92,24 +93,11 @@ def log(self, **kwargs):
class GsLogByBranchCommand(LogMixin, WindowCommand, GitCommand):

def run_async(self, **kwargs):
self.all_branches = [b.name_with_remote for b in self.get_branches()]
show_branch_panel(lambda branch: self.on_branch_selection(branch, **kwargs))

if hasattr(self, '_selected_branch') and self._selected_branch in self.all_branches:
pre_selected_index = self.all_branches.index(self._selected_branch)
else:
pre_selected_index = self.all_branches.index(self.get_current_branch_name())

self.window.show_quick_panel(
self.all_branches,
self.on_branch_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index
)

def on_branch_selection(self, index):
if index == -1:
return
super().run_async(branch=self.all_branches[index])
def on_branch_selection(self, branch, **kwargs):
if branch:
super().run_async(branch=branch, **kwargs)


class GsLogCommand(PanelCommandMixin, WindowCommand, GitCommand):
Expand Down
29 changes: 9 additions & 20 deletions core/commands/log_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from .log import GsLogActionCommand, GsLogCommand
from .navigate import GsNavigate
from ...common import util
from ..ui_mixins.quick_panel import show_branch_panel


COMMIT_NODE_CHAR = "●"
COMMIT_NODE_CHAR_OPTIONS = "●*"
Expand Down Expand Up @@ -95,7 +97,7 @@ class GsLogGraphByAuthorCommand(LogGraphMixin, WindowCommand, GitCommand):
by the specified author.
"""

def run_async(self):
def run_async(self, **kwargs):
email = self.git("config", "user.email").strip()
self._entries = []

Expand Down Expand Up @@ -129,26 +131,13 @@ def get_graph_args(self):

class GsLogGraphByBranchCommand(LogGraphMixin, WindowCommand, GitCommand):

def run_async(self):
self.all_branches = [b.name_with_remote for b in self.get_branches()]

if hasattr(self, '_selected_branch') and self._selected_branch in self.all_branches:
pre_selected_index = self.all_branches.index(self._selected_branch)
else:
pre_selected_index = self.all_branches.index(self.get_current_branch_name())

self.window.show_quick_panel(
self.all_branches,
self.on_branch_selection,
flags=sublime.MONOSPACE_FONT,
selected_index=pre_selected_index
)
def run_async(self, **kwargs):
show_branch_panel(self.on_branch_selection)

def on_branch_selection(self, index):
if index == -1:
return
self._selected_branch = self.all_branches[index]
super().run_async()
def on_branch_selection(self, branch):
if branch:
self._selected_branch = branch
super().run_async()

def get_graph_args(self):
args = super().get_graph_args()
Expand Down
Loading

0 comments on commit 5544912

Please sign in to comment.