Skip to content

Commit ae1c230

Browse files
Added regex support for selecting and excluding branches and tags (#80)
* added regex support for selecting and excluding branches and tags * added changelog * added tests for regex branch selection * documentation * changelog
1 parent 24bd344 commit ae1c230

File tree

7 files changed

+106
-9
lines changed

7 files changed

+106
-9
lines changed

CHANGELOG.rst

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
1.3.2.dev8+gd45a443.d20240722 (2024-07-23)
2+
==========================================
3+
4+
Deprecations and Removals
5+
-------------------------
6+
7+
- The theme specific versions selector menu/badge is deprecated in favour of consistent experience
8+
across themes. Now, every theme will have the selector menu either in its sidebar or in its
9+
footer(if the theme supports it). (`#47 <https://github.com/devanshshukla99/sphinx-versioned-docs/pull/47>`__)
10+
- The CLI option ``--branches`` is removed in favour of ``--branch`` and ``-b``. (`#67 <https://github.com/devanshshukla99/sphinx-versioned-docs/pull/67>`__)
11+
12+
13+
Features
14+
--------
15+
16+
- Added a feature to either have the vanilla versions selector menu or have it as a floating badge via
17+
using either ``--floating-badge`` or ``--badge`` option available through command-line. (`#47 <https://github.com/devanshshukla99/sphinx-versioned-docs/pull/47>`__)
18+
- Modified the ``--branch``/ ``-b`` to accomodate branch selection/exclusion. Now, any branch can be selected
19+
by mentioning it in ``--branch``/``--b`` and any can be excluded by adding a ``-`` infront of the branch/tag
20+
name in the cli argument.
21+
Like ``--branch main,-v1.0,v2.0`` will select ``main``, ``v2.0`` and will exclude ``v1.0``. (`#69 <https://github.com/devanshshukla99/sphinx-versioned-docs/pull/69>`__)
22+
- Added regex support for selecting and excluding branches and tags.
23+
Now, any branch can be selected by mentioning it in ``--branch``/ ``--b`` and any can be excluded by adding a ``-``
24+
infront of the branch/tag name in the argument.
25+
26+
Suppose there are 3 branches and tags: ``main, v1.0, v2.0``.
27+
The argument ``--branch main,-v*`` will select ``main`` and will exclude ``v1.0`` and ``v2.0``.
28+
Similarly, the argument ``--branch -main,v*`` will select ``v1.0`` and ``v2.0`` and will exclude ``main``.
29+
30+
Note: selecting a branch takes presidence over excluding one. (`#80 <https://github.com/devanshshukla99/sphinx-versioned-docs/pull/80>`__)
31+
32+
133
1.3.1 (2024-02-28)
234
==================
335

docs/changes/80.feature.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Added regex support for selecting and excluding branches and tags.
2+
Now, any branch can be selected by mentioning it in ``--branch``/ ``--b`` and any can be excluded by adding a ``-``
3+
infront of the branch/tag name in the argument.
4+
5+
Suppose there are 3 branches and tags: ``main, v1.0, v2.0``.
6+
The argument ``--branch main,-v*`` will select ``main`` and will exclude ``v1.0`` and ``v2.0``.
7+
Similarly, the argument ``--branch -main,v*`` will select ``v1.0`` and ``v2.0`` and will exclude ``main``.
8+
9+
Note: selecting a branch takes presidence over excluding one.

docs/settings.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,12 @@ These command line options must be specified when executing the ``sphinx-version
5757

5858
.. option:: -b <branch names>, --branch <branch names>
5959

60-
Build docs and the version selector menu only for certain tags and branches.
61-
The branch names can be separated by ``,`` or ``|``.
60+
Build documentation for selected branches and tags.
61+
The branch/tag names can be separated by ``,`` or ``|`` and supports regex.
6262

6363
Example: ``sphinx-versioned --branch="main, v1.0, v2.0"``
64+
65+
``sphinx-versioned --branch="main, -v*"``
6466

6567
.. option:: -m <branch name>, --main-branch <branch name>
6668

docs/tutorial.rst

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ some particular branch(s) and tag(s), they can be specified in the ``--branch``
9595
9696
Either of the two options above will select ``main``, ``v2.0`` and will only build these.
9797

98+
.. code-block:: console
99+
100+
$ sphinx-versioned --branch "main,v*"
101+
102+
The above argument will select ``main``, ``v1.0`` and ``v2.0`` and will only build these.
103+
98104
#. **For excluding a branch:** mention the branch/tag name with ``-`` in the CLI argument like:
99105

100106
.. code-block:: console
@@ -103,7 +109,13 @@ some particular branch(s) and tag(s), they can be specified in the ``--branch``
103109
104110
The above command will build all available branches and tags except ``main``, ``v2.0``
105111

106-
#. **For selecting and excluding simultaneously:** mention the branch/tag name with ``-`` in the CLI argument like:
112+
.. code-block:: console
113+
114+
$ sphinx-versioned --branch "-v*"
115+
116+
The above command will build all available branches and tags except ``v1.0``, ``v2.0``
117+
118+
#. **For selecting and excluding simultaneously:** mention the branch/tag name with ``-`` in the CLI argument like (mind you selecting takes presidence over excluding one):
107119

108120
.. code-block:: console
109121

sphinx_versioned/build.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import fnmatch
23
import shutil
34
import pathlib
45
from sphinx import application
@@ -26,7 +27,7 @@ class VersionedDocs:
2627
config : :class:`dict`
2728
"""
2829

29-
def __init__(self, config: dict) -> None:
30+
def __init__(self, config: dict, debug: bool = False) -> None:
3031
self.config = config
3132
self._parse_config(config)
3233
self._handle_paths()
@@ -48,6 +49,9 @@ def __init__(self, config: dict) -> None:
4849
else:
4950
self.main_branch = "main"
5051

52+
if debug:
53+
return
54+
5155
self.prebuild()
5256

5357
# Adds our extension to the sphinx-config
@@ -97,8 +101,9 @@ def _select_branches(self) -> None:
97101
return
98102

99103
for tag in self.select_branches:
100-
if tag in self._lookup_branch.keys():
101-
self._versions_to_pre_build.append(self._lookup_branch.get(tag))
104+
filtered_tags = fnmatch.filter(self._lookup_branch.keys(), tag)
105+
if filtered_tags:
106+
self._versions_to_pre_build.extend([self._lookup_branch.get(x) for x in filtered_tags])
102107
elif self.force_branches:
103108
log.warning(f"Forcing build for branch `{tag}`, be careful, it may or may not exist!")
104109
self._versions_to_pre_build.append(PseudoBranch(tag))
@@ -113,8 +118,9 @@ def _exclude_branches(self) -> None:
113118

114119
_names_versions_to_pre_build = [x.name for x in self._versions_to_pre_build]
115120
for tag in self.exclude_branches:
116-
if tag in _names_versions_to_pre_build:
117-
self._versions_to_pre_build.remove(self._lookup_branch.get(tag))
121+
filtered_tags = fnmatch.filter(_names_versions_to_pre_build, tag)
122+
for x in filtered_tags:
123+
self._versions_to_pre_build.remove(self._lookup_branch.get(x))
118124

119125
return
120126

sphinx_versioned/lib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def parse_branch_selection(branches: str) -> tuple:
103103
----------
104104
branches : :class:`str`
105105
Input CLI-argument.
106-
106+
107107
Returns
108108
-------
109109
select_branches, exclude_branches : :class:`list`, :class:`list`

tests/test_branch_selection.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
import pytest
33
import pathlib
44
from bs4 import BeautifulSoup as bs
5+
from sphinx_versioned.build import VersionedDocs
56
from sphinx_versioned.lib import parse_branch_selection
67

8+
79
VERSIONS_SUPPOSED = {
810
"v1.0": [
911
"index.html",
@@ -29,6 +31,40 @@ def test_parse_branch_selection(branches, select, exclude):
2931
assert parsed_exclude == exclude
3032

3133

34+
@pytest.mark.parametrize(
35+
"branches, select, exclude",
36+
[
37+
("main,v*", ["main", "v1.0", "v2.0"], []),
38+
("-v2.*", ["main", "v1.0"], ["v2.0"]),
39+
("-v*", ["main"], ["v1.0", "v2.0"]),
40+
],
41+
)
42+
def test_parse_branch_selection_regex(branches, select, exclude):
43+
parsed_select, parsed_exclude = parse_branch_selection(branches)
44+
45+
ver = VersionedDocs(
46+
{
47+
"chdir": ".",
48+
"output_dir": OUTPATH,
49+
"git_root": BASEPATH.parent,
50+
"local_conf": "docs/conf.py",
51+
"select_branches": parsed_select,
52+
"exclude_branches": parsed_exclude,
53+
"main_branch": "main",
54+
"quite": False,
55+
"verbose": True,
56+
"force_branches": True,
57+
},
58+
debug=True,
59+
)
60+
_names_versions_to_pre_build = [x.name for x in ver._versions_to_pre_build]
61+
for tag in select:
62+
assert tag in _names_versions_to_pre_build
63+
for tag in exclude:
64+
assert tag not in _names_versions_to_pre_build
65+
return
66+
67+
3268
def test_top_level_index():
3369
assert OUTPATH.exists()
3470
assert (OUTPATH / "index.html").is_file()

0 commit comments

Comments
 (0)