Skip to content

Commit

Permalink
Merge pull request #258 from frostming/gu-branches-wildcard
Browse files Browse the repository at this point in the history
Allow wildcard matching for "branches" command
  • Loading branch information
frostming authored Sep 26, 2019
2 parents c8ac1d4 + 7778529 commit 697f4b4
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ The Interface
``undo``
Un-does the last commit in git history. (alias: ``un``)

``branches``
``branches [<wildcard pattern>]``
Display a list of available branches.
Allows wildcard pattern matching of branch name.


The Installation
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h2>Git Workflow for Humans</h2>
<span class=ig>$ </span>git unpublish &lt;branch&gt;
<span class=ig># Removes branch from remote server.</span>

<span class=ig>$ </span>git branches
<span class=ig>$ </span>git branches [wildcard pattern]
<span class=ig># Nice &amp; pretty list of branches + publication status.</span>

<span class=ig>$ </span>git undo [--hard]
Expand Down
5 changes: 3 additions & 2 deletions extra/man/legit.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.
.TH LEGIT: GIT FOR HUMANS "" "" ""
.SH NAME
Legit: Git for Humans \-
Legit: Git for Humans \-
.
.nr rst2man-indent-level 0
.
Expand Down Expand Up @@ -50,8 +50,9 @@ Why not bring this innovation back to the command line?
.SH THE INTERFACE
.INDENT 0.0
.TP
.B \fBbranches\fP
.B \fBbranches [<wildcard pattern>]\fP
Get a nice pretty list of available branches.
Allows wildcard branch name matching.
.TP
.B \fBsync [<branch>]\fP
Synchronizes the given branch. Defaults to current branch.
Expand Down
2 changes: 1 addition & 1 deletion extra/zsh-completion/_legit
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ _legit ()
local -a subcommands
subcommands=(
'settings: Display and edit the current Legit settings.'
'branches: Get a nice pretty list of available branches.'
'branches: Get a nice pretty list of available branches. Allows wildcard branch name matching.'
'sync: Synchronizes the given branch. Defaults to current branch. Stash, Fetch, Auto-Merge/Rebase, Push, and Unstash. You can only sync published branches.'
'switch: Switches to specified branch. Defaults to current branch. Automatically stashes and unstashes any changes.'
'publish: Publishes specified branch to the remote.'
Expand Down
8 changes: 6 additions & 2 deletions legit/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,16 @@ def undo(scm, verbose, fake, hard):


@cli.command()
@click.argument('wildcard_pattern', required=False)
@pass_scm
def branches(scm):
def branches(scm, wildcard_pattern):
"""Displays a list of branches."""
scm.repo_check()

scm.display_available_branches()
if wildcard_pattern:
scm.display_available_branches(wildcard_pattern)
else:
scm.display_available_branches()


def do_install(ctx, verbose, fake):
Expand Down
16 changes: 12 additions & 4 deletions legit/scm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
This module provides the main interface to Git.
"""

import fnmatch
import os
import sys
from collections import namedtuple
Expand Down Expand Up @@ -286,8 +287,8 @@ def branch_fuzzy_match(b):

return branch

def get_branches(self, local=True, remote_branches=True):
"""Returns a list of local and remote branches."""
def get_branches(self, local=True, remote_branches=True, wildcard_pattern='*'):
"""Returns a list of local and remote branches matching wildcard_pattern."""

if not self.repo.remotes:
remote_branches = False
Expand Down Expand Up @@ -315,6 +316,11 @@ def get_branches(self, local=True, remote_branches=True):
if b not in legit_settings.forbidden_branches:
branches.append(Branch(b, is_published=False))

matching_branch_names = fnmatch.filter(
[branch.name for branch in branches], wildcard_pattern
)
branches = [branch for branch in branches if branch.name in matching_branch_names]

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

def get_branch_names(self, local=True, remote_branches=True):
Expand All @@ -323,15 +329,17 @@ def get_branch_names(self, local=True, remote_branches=True):

return [b.name for b in branches]

def display_available_branches(self):
def display_available_branches(self, wildcard_pattern='*'):
"""Displays available branches."""

if not self.repo.remotes:
remote_branches = False
else:
remote_branches = True
branches = self.get_branches(local=True, remote_branches=remote_branches)

branches = self.get_branches(
local=True, remote_branches=remote_branches, wildcard_pattern=wildcard_pattern
)
if not branches:
click.echo(crayons.red('No branches available'))
return
Expand Down
9 changes: 8 additions & 1 deletion legit/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ def test_config(runner):

@pytest.mark.cli
def test_branches(runner):
"""Test undo alias un"""
"""Test branches command"""
result = runner.invoke(cli, ["branches"])
assert result.exit_code == 0


@pytest.mark.cli
def test_branches_with_wildcard(runner):
"""Test branches command with wildcard filename pattern"""
result = runner.invoke(cli, ["branches", "ma*"])
assert result.exit_code == 0
4 changes: 4 additions & 0 deletions legit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,17 @@ def format_help(help):
Unpublish a specific branch from remote:
$ {5}
List branches matching wildcard pattern:
$ {6}
Commands:""".format(
crayons.red('legit switch <branch>'),
crayons.red('legit sync'),
crayons.red('legit sync <branch>'),
crayons.red('legit publish'),
crayons.red('legit publish <branch>'),
crayons.red('legit unpublish <branch>'),
crayons.red('legit branches [<wildcard pattern>]'),
)

help = help.replace('Commands:', additional_help)
Expand Down

0 comments on commit 697f4b4

Please sign in to comment.