Skip to content

Read conditional include #1054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Sep 4, 2020
Merged
Prev Previous commit
Next Next commit
Add method to retrieve all possible paths to include
  • Loading branch information
buddly27 committed Sep 2, 2020
commit 8e263b8f972f78c673f36f2bbc1f8563ce6acb10
37 changes: 36 additions & 1 deletion git/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import logging
import os
import re
import glob
import fnmatch
from collections import OrderedDict

from git.compat import (
Expand Down Expand Up @@ -455,6 +457,39 @@ def _has_includes(self):
for section in self.sections()
)

def _included_paths(self):
"""Return all paths that must be included to configuration.
"""
paths = []

for section in self.sections():
if section == "include":
paths += self.items(section)

match = CONDITIONAL_INCLUDE_REGEXP.search(section)
if match is not None and self._repo is not None:
keyword = match.group(1)
value = match.group(2).strip()

if keyword in ["gitdir", "gitdir/i"]:
value = osp.expanduser(value)
flags = [re.IGNORECASE] if keyword == "gitdir/i" else []
regexp = re.compile(fnmatch.translate(value), *flags)

if any(
regexp.match(path) is not None
and self._repo.git_dir.startswith(path)
for path in glob.glob(value)
):
paths += self.items(section)


elif keyword == "onbranch":
if value == self._repo.active_branch.name:
paths += self.items(section)

return paths

def read(self):
"""Reads the data stored in the files we have been initialized with. It will
ignore files that cannot be read, possibly leaving an empty configuration
Expand Down Expand Up @@ -492,7 +527,7 @@ def read(self):
# Read includes and append those that we didn't handle yet
# We expect all paths to be normalized and absolute (and will assure that is the case)
if self._has_includes():
for _, include_path in self.items('include'):
for _, include_path in self._included_paths():
if include_path.startswith('~'):
include_path = osp.expanduser(include_path)
if not osp.isabs(include_path):
Expand Down