RepoTree is a fast, context-aware Python utility designed to generate plain-text representations of directory structures for Git repositories.
Unlike standard tree commands, RepoTree is built for developers. Its primary feature is context-awareness: it automatically parses .gitignore rules to hide build artifacts, caches, and temporary files, ensuring the output represents only the source code and relevant files.
-
Git-Aware: Automatically prioritizes
git check-ignoreto filter files exactly as Git sees them. -
Smart Colors: Blue for directories, Green for files, Yellow for configs (automatically disabled when writing to files to keep text clean).
-
Depth Control: Limit recursion depth to get a high-level overview of massive projects.
-
Fallback Logic: Includes a manual
.gitignoreparser if Git is not installed or the directory is not initialized. -
Visualization Modes: Supports visual ASCII trees (
├──), flat file lists (pre-order traversal), and raw modes. -
Flexible Navigation: Works with absolute paths, relative paths, or the current directory.
-
Zero Dependencies: Written in pure Python 3.6+ (standard library only).
To use rtree globally from any terminal window, clone the repo and install it using pip.
This allows you to keep the source code and makes updating the tool easy.
# Clone the repository
git clone https://github.com/qtremors/file-tree-generator.git
# Navigate to the project folder
cd file-tree-generator
# Install the CLI in editable mode
pip install -e .The -e flag stands for "editable". It links the global command to your local file, so any changes you make to rtree.py apply immediately.
If you do not wish to install it, you can simply run the script using Python:
python rtree.py [arguments]Once installed, use the command rtree . Below are the supported arguments and scenarios.
| Argument | Short | Description |
|---|---|---|
--repo |
-r |
Target Directory. Can be relative, absolute, or subdirectory. Defaults to CWD if omitted. |
--list |
Scans the current directory and lists all valid Git repositories found. |
| Argument | Short | Description |
|---|---|---|
--depth |
Depth Limit. Stop scanning after N levels (e.g., --depth 2). |
|
--flat |
Flat Mode. Output a flat list of paths instead of a tree. | |
--ascii |
Force ASCII. Force standard tree characters (├──) (default behavior). |
|
--raw |
Raw Mode. Ignore .gitignore rules and show everything (including .git/ contents). |
| Argument | Short | Description |
|---|---|---|
--out |
-o |
Save to file. If no filename is given, auto-generates one (e.g., [repo]_tree.txt). |
--no-color |
Plain Text. Disable ANSI colors in the terminal. |
Generates a clean tree, respecting .gitignore.
rtree -r my-projectExpected Output:
my-project/
├── src/
│ ├── main.py
│ └── utils.py
├── tests/
│ └── test_main.py
├── .gitignore
├── .git/
└── README.md
(Note: __pycache__ or .env files are hidden automatically.)
Inspect only the top-level folders.
rtree -r my-project --depth 1Useful for copying file paths for use in other scripts or LLM prompts.
rtree -r my-project --flatExpected Output:
.git/
.gitignore
README.md
src/
src/main.py
tests/
Saves the output to a file. Note: Colors are automatically disabled when saving to files.
rtree -r my-project --outConsole Output:
my-project_ascii_tree.txt
Debug mode to see everything, including ignored files, virtual environments, and git internals.
rtree -r my-project --rawThe script relies on the RepoTreeVisualizer class to handle scanning and filtering.
-
Initialization: The script accepts a target repository path (
--repo). If omitted, it defaults to the current working directory. It immediately locates the.gitignorefile. -
Hybrid Ignore Calculation:
- Phase 1 (Eager Pruning): As the script walks the directory, it immediately skips known heavy folders (like
node_modulesorvenv) using internal pattern matching. This prevents unnecessary scanning of thousands of files. - Phase 2 (Git Precision): For the remaining files, it queries
git check-ignoreto ensure 100% parity with your.gitignorerules (handling negations and complex overrides correctly).
- Phase 1 (Eager Pruning): As the script walks the directory, it immediately skips known heavy folders (like
-
Traversal:
-
It uses
os.walkto traverse the directory. -
Depth Logic: If
--depthis set, it stops recursion when the specified level is reached. -
Filtering: For every file/folder, it checks against the computed
ignored_set. Special care is taken to always show the root.gitignoreand.gitfolder.
-
-
Rendering:
-
ASCII Tree: Uses a recursive function to draw
├──,│, and└──characters. -
Color Logic: Wraps strings in ANSI escape codes (e.g.,
\033[94mfor directories) unless--no-coloror--outis used.
-
# --- Basic Usage ---
# Scan the current directory
rtree
python rtree.py
# Scan a specific subdirectory
rtree -r my-project
python rtree.py -r my-project
# Scan an absolute path
rtree -r "C:/Projects/App"
python rtree.py -r "C:/Projects/App"
# --- Depth Control ---
# Limit tree to 2 levels deep (great for large repos)
rtree --depth 2
python rtree.py --depth 2
# Combine specific target with depth limit
rtree -r src --depth 3
python rtree.py -r src --depth 3
# --- Output to File ---
# Auto-name the output file (e.g., 'folder_tree.txt')
rtree -o
python rtree.py -o
# Save to a specific filename
rtree -o structure.txt
python rtree.py -o structure.txt
# Scan 'src' and save to 'src.txt'
rtree -r src -o src.txt
python rtree.py -r src -o src.txt
# --- Flat List Mode ---
# Output a flat list of file paths instead of a tree
rtree --flat
python rtree.py --flat
# Save the flat list to a file
rtree --flat -o list.txt
python rtree.py --flat -o list.txt
# --- Raw / Debug Mode ---
# Ignore .gitignore rules (shows .git, venv, etc.)
rtree --raw
python rtree.py --raw
# See top-level hidden files only
rtree --raw --depth 1
python rtree.py --raw --depth 1
# Flat list of every file on disk (ignoring rules)
rtree --raw --flat
python rtree.py --raw --flat
# --- Utilities ---
# List all git repositories found in the current directory
rtree --list
python rtree.py --list
# Force disable colored output in the terminal
rtree --no-color
python rtree.py --no-color-
Python 3.6+ (Uses
typingandpathliblogic). -
Git (Recommended for most accurate ignore-checking, but not strictly required due to fallback logic).
-
"Command not found": Ensure you ran
pip install -e .inside the folder. If it still fails, make sure your PythonScripts/(Windows) orbin/(Mac/Linux) folder is in your system PATH. -
Error: 'repo' not found: Ensure the directory you provide via
--repoexists. -
Git ignore not working: If
gitis not in your system PATH, the script relies on Python matching. Complex gitignore patterns (like negation!) might not be perfectly emulated in the fallback mode. -
Colors look weird in output files: The script auto-detects when you use
--outand disables colors. If you pipe output manually (e.g.,rtree > file.txt), use the--no-colorflag to prevent garbage characters.