tree2md 0.13.0

Like the tree command, but outputs in Markdown. Optimized for AI agents.
# tree2md

[![Crates.io](https://img.shields.io/crates/v/tree2md.svg)](https://crates.io/crates/tree2md)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)

**Visualize your codebase structure for humans and AI agents.**

```bash
tree2md                        # Pretty tree in terminal
tree2md | pbcopy               # Pipe-friendly tree for clipboard
tree2md -c -I "*.rs" -L 2     # Tree + file contents for AI context
tree2md -c --max-chars 30000   # Fit contents within token budget
```

---

## Output Modes

Output format is auto-detected based on TTY:

| Mode | When | What |
|------|------|------|
| **TTY** | Terminal | Emoji, LOC bars, stats, tree characters |
| **Pipe** | `\| pbcopy`, redirect, etc. | Plain tree + `(N lines)` |
| **Pipe + `-c`** | AI context | Tree + code-fenced file contents |

### TTY (terminal)

```
โ”œโ”€โ”€ ๐Ÿ“ src/
โ”‚   โ”œโ”€โ”€ ๐Ÿฆ€ cli.rs          [โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ]    156 (M)
โ”‚   โ”œโ”€โ”€ ๐Ÿฆ€ main.rs         [โ–ˆโ–ˆโ–ˆโ–ˆยทยทยทยทยทยท]     65 (S)
โ”‚   โ””โ”€โ”€ ๐Ÿ“ render/
โ”‚       โ”œโ”€โ”€ ๐Ÿฆ€ pipe.rs     [โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆยท]    148 (M)
โ”‚       โ””โ”€โ”€ ๐Ÿฆ€ terminal.rs [โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ]    247 (M) โ˜…
โ””โ”€โ”€ Cargo.toml              [โ–ˆยทยทยทยทยทยทยทยทยท]     36 (S)

**Totals**: ๐Ÿ“‚ 3 dirs โ€ข ๐Ÿ“„ 5 files โ€ข ๐Ÿงพ ~652 LOC
```

### Pipe

```
.
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ cli.rs  (156 lines)
โ”‚   โ”œโ”€โ”€ main.rs  (65 lines)
โ”‚   โ””โ”€โ”€ render/
โ”‚       โ”œโ”€โ”€ pipe.rs  (148 lines)
โ”‚       โ””โ”€โ”€ terminal.rs  (247 lines)
โ””โ”€โ”€ Cargo.toml  (36 lines)
```

### Pipe + `-c` (AI context)

```
.
โ”œโ”€โ”€ src/
โ”‚   โ””โ”€โ”€ main.rs  (65 lines)
โ””โ”€โ”€ Cargo.toml  (36 lines)

## src/main.rs

```rust
fn main() {
    println!("Hello, world!");
}
```

## Cargo.toml

```toml
[package]
name = "example"
```
```

---

## Installation

```bash
cargo install tree2md
```

Or from source:

```bash
git clone https://github.com/zawakin/tree2md
cd tree2md
cargo install --path .
```

Prebuilt binaries are on [GitHub Releases](https://github.com/zawakin/tree2md/releases). From v0.12.1 on, each archive has a signed build provenance attestation. Verify a download before running it:

```bash
gh attestation verify tree2md-aarch64-apple-darwin.tar.gz --repo zawakin/tree2md
```

This checks that the file was built by this repository's release workflow from the tagged commit. A replaced archive fails, even if its `.sha256` was replaced too.

---

## CLI Options

### Filtering

| Flag | Description |
|------|-------------|
| `-L, --level <N>` | Limit traversal depth |
| `-I, --include <GLOB>` | Include patterns (repeatable) |
| `-X, --exclude <GLOB>` | Exclude patterns (repeatable) |
| `--use-gitignore {auto\|never\|always}` | Respect `.gitignore` |
| `-l, --follow-links` | Follow symbolic links (default: symlinks are skipped) |
| `--paths-from <FILE>` | Include only the paths listed in FILE, one per line (`-` = stdin). Literal paths, no globs; a listed directory includes its contents |
| `--files0-from <FILE>` | Same as `--paths-from`, NUL-separated (`find -print0`, `git ls-files -z`, `fd -0`) |

`-I`/`-X` can be combined with a path list to refine it โ€” rules are applied on top with the usual last-match-wins order (`--paths-from list -X build` prunes `build/` inside listed directories).

### Contents

| Flag | Description |
|------|-------------|
| `-c, --contents` | Append file contents as code blocks |
| `--max-chars <N>` | Limit total content to N characters (requires `-c`) |
| `--contents-mode {head\|nest}` | Truncation strategy (default: `head`) |

### Statistics

| Flag | Description |
|------|-------------|
| `--stats {off\|min\|full}` | Statistics display (default: `full`) |
| `--loc {off\|fast\|accurate}` | Line counting mode (default: `fast`) |

### Hot spots

| Flag | Description |
|------|-------------|
| `--hot` | Mark the files that changed most often in git history |
| `--hot-since <SPAN>` | History window: `30d`, `12w`, `6m`, `1y` (default: `90d`). Implies `--hot` |

See [Hot spots](#hot-spots) below.

### Fun & Style

| Flag | Description |
|------|-------------|
| `--fun {auto\|on\|off}` | Emojis and animations (default: `auto`) |
| `--emoji <MAPPING>` | Custom emoji (e.g., `--emoji ".rs=๐Ÿš€"`) |
| `--emoji-map <FILE>` | Load emoji mappings from TOML file |
| `--no-anim` | Disable animations |

### Safety

| Flag | Description |
|------|-------------|
| `--safe` | Apply safety filters (default) |
| `--unsafe` | Disable all safety filters |

---

## Hot spots

`--hot` reads the target's git history and shows where the code is being edited:

```bash
tree2md --hot                  # last 90 days
tree2md src --hot-since 6m     # a subdirectory, last 6 months
```

Each changed file gets its commit count, and a ranked list follows the stats:

```
โ”œโ”€โ”€ cli.rs  (427 lines, 9 commits/1y)
โ””โ”€โ”€ main.rs  (140 lines, 3 commits/1y)

**Hot spots** (commits in the last 1y):
1. cli.rs: 9 commits, +251/-305, 3mo ago
2. fs_tree/build.rs: 8 commits, +233/-130, 1mo ago
```

In the terminal, the ten hottest files are marked with ๐Ÿ”ฅ and their commit count.

- Only files shown in the tree are counted and ranked: `-I`/`-X`, `-L`, gitignore and the safety filter apply as usual, so an excluded secret file never appears however often it changed.
- Lockfiles (`Cargo.lock`, `package-lock.json`, `go.sum`, โ€ฆ) and changelogs are left out: they change with every dependency bump or release.
- Commits that touch more than 50 files (mass reformatting, renames, vendoring) are skipped and the count of skipped commits is shown in the header.
- Merge commits are not counted. A renamed file's history before the rename is not carried over.
- Outside a git repository `--hot` prints a warning and the tree is shown without it. In a shallow clone only the fetched history is counted.

---

## Safety Defaults

Excluded by default (at any depth):

- `.env`, `.env.*`, `.envrc`, `.ssh/**`, `*.pem`, `*.key`, `credentials`, `secrets`
- Tokens and passwords: `.npmrc`, `.pypirc`, `.netrc`, `.git-credentials`, `.docker/config.json`, `.kube/config`, `*.tfstate`, `.pgpass`, `.my.cnf`, `*.ppk`, `.gnupg/**`
- PHP / web hosting: Composer `auth.json`, `wp-config.php`, `.htpasswd`
- `node_modules/`, `target/`, `dist/`, `build/`
- `.git/**`, `.DS_Store`, `Thumbs.db`

Use `-I` to selectively include, or `--unsafe` to disable filters.

---

## Use Cases

**Copy structure to clipboard**

```bash
tree2md . -L 3 | pbcopy
```

**Feed codebase to AI agent**

```bash
tree2md . -c -I "*.rs" -I "*.toml" | pbcopy
```

**Fit contents within token budget**

```bash
tree2md . -c --max-chars 30000 | pbcopy
```

**Find where the code is being edited**

```bash
tree2md . --hot-since 30d
```

**Quick project overview**

```bash
tree2md . -L 2 --stats min
```

**Rust files only, 3 levels deep**

```bash
tree2md src/ -L 3 -I "*.rs"
```

**Select directories by a condition tree2md can't express** โ€” compute the
path set with your favourite tool and pipe it in:

```bash
# only projects whose project.json says "active"
for d in projects/*/; do
  jq -e '.status == "active"' "$d/project.json" >/dev/null && printf '%s\0' "$d"
done | tree2md -c --files0-from -

# tracked files only
git ls-files -z | tree2md --files0-from -

# files changed on this branch, with contents, for a review prompt
git diff --name-only main... | tree2md -c --paths-from - | pbcopy
```

---

## Build from Source

```bash
git clone https://github.com/zawakin/tree2md
cd tree2md
cargo build --release
cargo test
```

---

## Contributing

PRs welcome! See [CONTRIBUTING.md](CONTRIBUTING.md).

```bash
mise run verify   # fmt + clippy + tests
mise run fix      # auto-format + clippy fix
```

---

## License

MIT License โ€” see [LICENSE](LICENSE).