Skip to content

Commit

Permalink
Fix: use check-ref-format to check branch name validity
Browse files Browse the repository at this point in the history
  • Loading branch information
randy3k committed Mar 7, 2018
1 parent 6ef51db commit 7ddd7cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
15 changes: 7 additions & 8 deletions core/commands/checkout.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import re
import sublime
from sublime_plugin import WindowCommand

from ..git_command import GitCommand
from ..exceptions import GitSavvyError
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)"
_is_valid_branch_name = re.compile(
r"^(?!\.|.*\.\..*|.*@.*|\/)[a-zA-Z0-9\-\_\/\.#\u263a-\U0001f645]+(?<!\.lock)(?<!\/)(?<!\.)$")


class GsCheckoutBranchCommand(WindowCommand, GitCommand):
Expand Down Expand Up @@ -57,8 +55,9 @@ def run_async(self, base_branch=None):
v.run_command("select_all")

def on_done(self, branch_name):
match = _is_valid_branch_name.match(branch_name)
if not match:
try:
self.validate_branch_name(branch_name)
except GitSavvyError:
sublime.error_message(NEW_BRANCH_INVALID.format(branch_name))
sublime.set_timeout_async(self.run_async(branch_name))
return None
Expand Down Expand Up @@ -109,9 +108,9 @@ def on_branch_selection(self, remote_branch, local_name=None):
v.run_command("select_all")

def on_enter_local_name(self, branch_name):

match = _is_valid_branch_name.match(branch_name)
if not match:
try:
self.validate_branch_name(branch_name)
except GitSavvyError:
sublime.error_message(NEW_BRANCH_INVALID.format(branch_name))
sublime.set_timeout_async(self.on_branch_selection(self.remote_branch, branch_name))
return None
Expand Down
4 changes: 4 additions & 0 deletions core/git_mixins/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,7 @@ def branches_containing_commit(self, commit_hash, local_only=True, remote_only=F
commit_hash
).strip().split("\n")
return [branch.strip() for branch in branches]

def validate_branch_name(self, branch):
ref = "refs/heads/{}".format(branch)
self.git("check-ref-format", "--branch", ref)

0 comments on commit 7ddd7cb

Please sign in to comment.