Skip to main content

Merger is a tool that scans a directory, filters files using customizable patterns, and merges readable content into a single output file.

Project description

Merger CLI

Python License: GPLv3 PyPI

Merger is a command-line utility for developers that scans a directory, filters files using customizable ignore patterns, and merges all readable content into a single output file, suitable both for human reading and for use by AI models. It supports multiple output formats (e.g., JSON, directory tree, plain text with file delimiters), and can be extended with custom file parsers (e.g., .pdf) and custom exporters (e.g., .xml, .md).


TLDR

  1. Install the package: pip install merger-cli
  2. Navigate to your project folder: cd path/to/your/project
  3. Create a merger ignore file: Manually or with merger -c [TEMPLATE] (Where the template is optional)
  4. Execute merger-cli: merger . (to create a single combined file called merger.txt)

For more options, refer to the Usage section below.


Summary

  1. Core Features
  2. Dependencies
  3. Installation
  4. Usage
  5. Ignore Pattern Syntax
  6. Output Formats
  7. Custom Parsers
  8. Custom Exporters
  9. CLI Options
  10. License

Core Features

  • Recursive merge of all readable files under a root directory.
  • Custom glob-like ignore patterns for filtering.
  • Automatic file encoding detection.
  • Modular parser & exporter system for custom formats and outputs with easy CLI management.
  • Multiple export formats (built-in and custom).
  • Modern CLI interface.

Dependencies

Component Version Notes
Python ≥ 3.8 Required
Pydantic ≥ 2.0 Required
Rich ≥ 13.0 Required

All dependencies are listed in requirements.txt.


Installation

pip install merger-cli

Usage

Basic merge

merger .

This writes a file named merger.txt in the current directory.


Save output to a specific directory

merger ./project ./out

This writes ./out/merger.txt (or ./out/merger.json, depending on the exporter).


Pick an output format

Use -e or --exporter to select the output format:

merger ./src --exporter JSON
merger ./src --exporter DIRECTORY_TREE
merger ./src --exporter PLAIN_TEXT
merger ./src --exporter TREE_PLAIN_TEXT

Custom ignore patterns

Provide one or more ignore patterns with --ignore (see Ignore Pattern Syntax):

merger ./project --ignore "*.log" "__pycache__/**" "*.tmp"

Custom ignore file

Provide a file containing ignore patterns (one per line) with --merger-ignore (see Ignore Pattern Syntax):

merger . --merger-ignore "C:\Users\USER\Desktop\ignore.txt"

Custom ignore templates

Quickly create a merger.ignore file using built-in templates:

merger -c PYTHON

Supported templates: DEFAULT, PYTHON, JAVASCRIPT, TYPESCRIPT, JAVA, GO, RUST, CPP, CSHARP, RUBY, PHP, KOTLIN.


Custom modules (Parsers & Exporters)

List all installed custom modules (parsers and exporters):

merger --list

Verbose output

merger ./src --log-level DEBUG

Ignore Pattern Syntax

Ignore patterns are evaluated relative to the input directory (the directory you ask merger to scan). If a path is not located under that root, it will not match.

Segment matching

The pattern is split into segments and matched against the scanned path’s relative segments.

Supported segments:

  • Literal segments (e.g. src, tests, README.md)
  • * matches exactly one path segment
  • ** matches zero or more path segments
  • Embedded * inside a segment matches prefix*suffix (e.g. foo*.py, *cache*)

Anchoring

  • Leading / anchors the pattern to the scan root

    • Example: /src/*.py matches src/main.py but not project/src/main.py
  • Leading ./ anchors the pattern to the start of the relative path (equivalent anchoring behavior)

    • Example: ./src/*.py matches src/main.py but not project/src/main.py
  • Without anchoring, the pattern may match starting at any segment boundary within the relative path

    • Example: src/*.py matches both src/main.py and project/src/main.py

Type qualifiers

  • Trailing / requires the matched path to be a directory

    • Example: build/ matches the build directory entry
  • Trailing : requires the matched path to be a file

    • Example: README.md: matches the README.md file
  • Trailing !:

    • This is a special escape suffix that disables type qualification and preserves any trailing / or : as literal characters in the final path segment

      • Examples:
        • data:! matches any file or directory literally named data:
        • data:: matches any file literally named data:
        • data:/ matches any directory literally named data:
        • data!! matches any file or directory literally named data!
        • data!/ matches any directory literally named data!
        • data!: matches any file literally named data!

Examples

Ignore all files or directories that end with .log:

  • *.log

Ignore all dist directories:

  • dist/

Ignore a file named config.json at the scan root:

  • /config.json:

Ignore all .py files directly under any src directory (but not deeper):

  • src/*.py:

Ignore all files or directories that contain cache and are only one level deep inside any directory named src:

  • src/*/*cache*

Ignore all __pycache__ directories inside the src directory at the scan root:

  • ./src/**/__pycache__/

Ignore all files data::

  • data::

Ignore all directories data::

  • data:/

Ignore all files or directories data::

  • data:!

Output Formats

Merger writes one output file to the output directory, named merger.<extension> based on the selected exporter.

Exporter Name File Extension Description
TREE_PLAIN_TEXT .txt Directory tree + plain-text merged file contents (default).
PLAIN_TEXT .txt Plain-text merged file contents with <<FILE_START>> / <<FILE_END>> file delimiter.
TREE .txt Directory tree only.
JSON .json JSON mapping file paths to parsed file contents (path: content).
JSON_TREE .json Structured JSON representing the directory tree and file contents with hierarchy and metadata.

Custom Parsers

Merger uses parser strategies to support parsing of non-text file formats (e.g., PDF, images with OCR, etc.).


Parser Abstract Class

All parsers must inherit from Parser:

from merger.parsing.parser import Parser

Required structure:

  • EXTENSIONS: Set[str] (e.g., {".pdf"})
  • MAX_BYTES_FOR_VALIDATION: Optional[int]
  • validate(cls, file_chunk_bytes, *, file_path=None, logger=None) -> bool
  • parse(cls, file_bytes, *, file_path=None, logger=None) -> str

Managing Custom Parsers

To install a module:

merger --install path/to/parser.py

To uninstall a module (* removes all modules including parsers and exporters):

merger --uninstall <module_id>

To list installed modules:

merger --list

Custom Parser Example (PDF)

import logging
from pathlib import Path
from typing import Union, Optional, Set, Type

import pymupdf
from merger.parsing.parser import Parser


class PdfParser(Parser):
    EXTENSIONS: Set[str] = {".pdf"}
    MAX_BYTES_FOR_VALIDATION: Optional[int] = None

    @classmethod
    def validate(
        cls,
        file_chunk_bytes: Union[bytes, bytearray],
        *,
        file_path: Optional[Path] = None,
        logger: Optional[logging.Logger] = None
    ) -> bool:
        """
        Validate that the given file represents a readable PDF document.

        Args:
            file_chunk_bytes: Binary contents of the file being validated, sufficient to perform validation.
            file_path: Path of the file being validated.
            logger: Optional logger instance for logging.

        Returns:
            bool: True if the file is a readable PDF, False otherwise.
        """
        try:
            with pymupdf.open(file_path) as doc:
                _ = doc[0]
            return True

        except Exception:
            return False

    @classmethod
    def parse(
        cls,
        file_bytes: Union[bytes, bytearray],
        *,
        file_path: Optional[Path] = None,
        logger: Optional[logging.Logger] = None,
    ) -> str:
        """
        Extracts and concatenates text from all pages of a PDF file.

        Args:
            file_bytes: Binary contents of the file being parsed.
            file_path: Path of the file being parsed.
            logger: optional logger instance for logging.

        Returns:

        """
        texts = []
        with pymupdf.open(stream=file_bytes) as doc:
            for page in doc:
                text = page.get_text()
                if text:
                    text = text.replace("\n\n", "")
                    texts.append(text)

        full_text = " ".join(texts)
        return full_text


parser_cls: Type[Parser] = PdfParser

Available at examples/custom_parsers/pdf_parser.py.

The module must expose a parser_cls object referencing the parser class.


Custom Exporters

You can also extend Merger with custom export strategies to output data in any format (e.g., XML, Markdown, CSV).


Exporter Abstract Class

All exporters must inherit from TreeExporter:

from merger.exporters.tree_exporter import TreeExporter

Required structure:

  • NAME: str (The name used in --exporter)
  • FILE_EXTENSION: str (The output file extension)
  • export(cls, tree: FileTree) -> bytes

Managing Custom Exporters

To install an exporter:

merger --install path/to/exporter.py

To uninstall an exporter (* removes all modules including parsers and exporters):

merger --uninstall <exporter_id>

To list installed exporters:

merger --list

Custom Exporter Example (XML)

import xml.etree.ElementTree as ET

from merger.file_tree.entries import FileEntry, DirectoryEntry, FileTreeEntry
from merger.exporters.tree_exporter import TreeExporter
from merger.file_tree.tree import FileTree


class XmlExporter(TreeExporter):
    """
    A custom exporter that generates an XML representation of the file tree.
    """
    NAME = "XML"
    FILE_EXTENSION = ".xml"

    @classmethod
    def export(cls, tree: FileTree) -> bytes:
        root = ET.Element("filetree")
        cls._to_xml(tree.root, root)

        cls._indent(root)

        return ET.tostring(root, encoding="utf-8", xml_declaration=True)

    @classmethod
    def _to_xml(cls, entry: FileTreeEntry, parent: ET.Element):
        if isinstance(entry, FileEntry):
            file_el = ET.SubElement(parent, "file", {
                "name": entry.name,
                "path": entry.path.as_posix()
            })
            content_el = ET.SubElement(file_el, "content")
            content_el.text = entry.content

        elif isinstance(entry, DirectoryEntry):
            dir_el = ET.SubElement(parent, "directory", {
                "name": entry.name,
                "path": entry.path.as_posix()
            })
            for child in sorted(entry.children.values(), key=lambda e: e.name.lower()):
                cls._to_xml(child, dir_el)

    @classmethod
    def _indent(cls, elem: ET.Element, level: int = 0):
        """
        Recursive function to indent XML elements while preserving text content.
        """
        i = "\n" + level * "  "
        if len(elem):
            if not elem.text or not elem.text.strip():
                elem.text = i + "  "

            if not elem.tail or not elem.tail.strip():
                elem.tail = i

            for child in elem:
                cls._indent(child, level + 1)

            if len(elem) > 0:
                last_child = elem[-1]
                if not last_child.tail or not last_child.tail.strip():
                    last_child.tail = i

        else:
            if level and (not elem.tail or not elem.tail.strip()):
                elem.tail = i

exporter_cls = XmlExporter

Available at examples/custom_exporters/xml_exporter.py.

The module must expose an exporter_cls object referencing the exporter class.


CLI Options

Option Description
input_dir Root directory to scan for files.
output_path Output directory where the tool writes merger.<ext> (default: current directory).
-e, --exporter Output exporter strategy (e.g., TREE_PLAIN_TEXT, PLAIN_TEXT, JSON, XML).
-i, --install Install a custom module (parser or exporter).
-u, --uninstall Uninstall a module by ID (* removes all modules including parsers and exporters).
-l, --list List all installed custom modules.
--ignore One or more ignore patterns (see Ignore Pattern Syntax).
--merger-ignore File containing ignore patterns (default: ./merger.ignore).
-c, --create-ignore Create a merger.ignore file using a built-in template (e.g., DEFAULT, PYTHON).
--version Show installed version.
--log-level Set logging verbosity.

License

This project is licensed under the GPLv3 License — see LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

merger_cli-3.1.0.tar.gz (72.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

merger_cli-3.1.0-py3-none-any.whl (58.3 kB view details)

Uploaded Python 3

File details

Details for the file merger_cli-3.1.0.tar.gz.

File metadata

  • Download URL: merger_cli-3.1.0.tar.gz
  • Upload date:
  • Size: 72.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for merger_cli-3.1.0.tar.gz
Algorithm Hash digest
SHA256 428c68103fc8bc45080a695d2a5eeef05a16e7c16eca8ad4ee2b05c4d79c270d
MD5 33f1ee0336a6d9447690840063685330
BLAKE2b-256 efe4fb8de03331ee0019048f1c7e5694cc191f913d84e88fed86ffd243e905ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for merger_cli-3.1.0.tar.gz:

Publisher: publish.yml on diogotoporcov/merger-cli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file merger_cli-3.1.0-py3-none-any.whl.

File metadata

  • Download URL: merger_cli-3.1.0-py3-none-any.whl
  • Upload date:
  • Size: 58.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for merger_cli-3.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7799009dd470a1d0c25a3670f376b230336514b5bdd2f7d3eefd143ab0b06266
MD5 15c9fa973cac2a15874ab0b90677ecbe
BLAKE2b-256 81a03864ae64207a5dca9c20eb358e99d1c92557a79314b77e93d0c1bfc16bbc

See more details on using hashes here.

Provenance

The following attestation bundles were made for merger_cli-3.1.0-py3-none-any.whl:

Publisher: publish.yml on diogotoporcov/merger-cli

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page