How to Generate Git LFS Extension Patterns in Python

This script builds mock Git LFS file patterns for common geospatial extensions and filters them based on compression suffixes.

Easy Python 3.10+ Aug 9, 2026 Git + Python 12 views 0 copies

Python code

20 lines
Python 3.10+
import itertools
import re

LFS_EXTENSIONS = {".csv", ".geojson", ".tif", ".shp", ".gpkg"}

def build_mock_lfs_pattern(base_name="data_usgs_lidar"):
    patterns = []
    for ext in sorted(LFS_EXTENSIONS):
        for variant in (("", ".lz4"), (".compressed",), (".b", ".a"), ("_v1", ".zip")):
            full_pattern = base_name + "".join(variant) + ext
            patterns.append(full_pattern)
    return patterns

def match_lfs_candidates(base_name):
    return [p for p in build_mock_lfs_pattern(base_name) if re.search(r"\.(lz4|zip|compressed)", p)]

if __name__ == "__main__":
    results = match_lfs_candidates()
    for item in results:
        print(item)

Output

stdout
data_usgs_lidar.compressed.csv
data_usgs_lidar.compressed.geojson
data_usgs_lidar.compressed.gpkg
data_usgs_lidar.compressed.shp
data_usgs_lidar.compressed.tif
data_usgs_lidar.lz4.csv
data_usgs_lidar.lz4.geojson
data_usgs_lidar.lz4.gpkg
data_usgs_lidar.lz4.shp
data_usgs_lidar.lz4.tif
data_usgs_lidar_v1.zip.csv
data_usgs_lidar_v1.zip.geojson
data_usgs_lidar_v1.zip.gpkg
data_usgs_lidar_v1.zip.shp
data_usgs_lidar_v1.zip.tif

How it works

The build_mock_lfs_pattern function uses itertools.product implicitly by nesting loops to generate combinations of base names, variants, and extensions. Since each extension is combined with every variant, the result is a comprehensive list of possible file names that might be tracked by Git LFS. The match_lfs_candidates function then uses a regular expression to filter only those patterns containing .lz4, .zip, or .compressed, which are common compression indicators in LFS rules. Sorting the extensions ensures deterministic output, which is useful for testing and automation scripts.

Common mistakes

  • Forgetting to sort the extensions, leading to non-deterministic output order.
  • Using a single regex pattern that also matches compressed variants like `.compress` accidentally.
  • Not escaping dots in the extension patterns when building regex, causing unexpected matches.

Variations

  1. Use `itertools.product` to generate the combinations more explicitly: `for base, variant, ext in itertools.product([base_name], variants, extensions)`.
  2. Instead of a list comprehension with regex, you can use `any(ext in p for ext in ['.lz4', '.zip', '.compressed'])` for readability.

Real-world use cases

  • Generating test fixtures that simulate Git LFS-tracked file patterns in a geospatial data pipeline.
  • Creating a list of file patterns to configure a .gitattributes file automatically from a set of extensions.
  • Filtering large file listings to identify which files might require special handling in a Git repository.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Git + Python

Related tutorials and quizzes for this topic.