Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ resolve each path, run `minigit add <path>`, then run

`minigit diff` shows line changes between HEAD and working files, including
untracked files, binary changes, executable modes, and missing final newlines.
`minigit status` retains its staged / not staged / untracked sections. Both
commands are read-only. Object storage also supports local
[packfiles](docs/packfiles.md) without changing object hashes.
`minigit status` starts with the current branch, then lists staged (HEAD vs index), not staged (index vs working files), and untracked paths, plus an unresolved (merge conflict) section while a merge is pending. It compares content, never timestamps. Object storage also supports local [packfiles](docs/packfiles.md) without changing object hashes.

To test push, initialize another scratch repository and run
`minigit serve --port 9418 --token demo-token` there. From the first repository,
Expand Down Expand Up @@ -189,6 +187,10 @@ minigit pull 127.0.0.1:9418 main --token demo-token # fetch, then integra
rehearsal; the full project demo remains in progress.
- [Module 1 writeup](docs/writeups/module-1.md) - object formats, validation,
packfiles, measured tradeoffs, and remaining limitations.
- [Working-tree demo](docs/demo.md) - status, diff, safe branch switching, and
conflict resolution rehearsal.
- [Module 2 writeup](docs/writeups/module-2.md) - index snapshots, nested trees,
the status model, Myers diff, checkout safety, and measured results.
- [Onboarding guide](https://docs.google.com/document/d/1D77Ncmtunxj7GVADytHo1An6BKzaV-Ep7AxAYfL405k/edit?tab=t.0) -
start here if you are new: setup, branching, workflow, pull requests, code
review, and coding standards.
Expand Down
224 changes: 224 additions & 0 deletions docs/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,227 @@ revision and record their name, revision, environment, and any blockers in
#60/#67. This local validation does not claim that teammate rehearsal or the
full project demo is complete. Pruning, garbage collection, delta compression,
and a pack CLI remain outside this segment.


# Working-tree demo

This is the Module 2 segment for the shared demo: staging, status, diff, safe branch switching, and explicit conflict resolution. It uses the real CLI in disposable folders, and every command is a separate process.

## Run the working-tree segment

From the repository root after installing the project environment:

```
export PYTHONPATH="$(pwd)"
alias minigit='python3 -m minigit.cli'
```

Use `.venv/bin/python -m minigit.cli` in place of `python3 -m minigit.cli` if you run the project's virtual environment. The segment uses a POSIX shell (`printf`, `chmod`, `ls -l`), so on Windows run it in Git Bash.

Start each part below in a new empty folder, for example:

```
mkdir /tmp/wt-demo-1 && cd /tmp/wt-demo-1
minigit init
```

`init` and `add` print nothing when they succeed. Commit and branch hashes differ on every run.

## 1. Three states: committed, staged, edited again

```
$ printf 'one\n' > notes.txt
$ minigit add notes.txt
$ minigit commit -m first
[main 84bc7e1] first
$ printf 'two\n' > notes.txt
$ minigit add notes.txt
$ printf 'three\n' > notes.txt
$ minigit status
On branch main
staged:
notes.txt
not staged:
notes.txt
$ minigit diff
diff --minigit a/notes.txt b/notes.txt
--- a/notes.txt
+++ b/notes.txt
@@ -1,1 +1,1 @@
-one
+three
$ minigit commit -m second
[main a305708] second
$ minigit status
On branch main
not staged:
notes.txt
$ minigit diff
diff --minigit a/notes.txt b/notes.txt
--- a/notes.txt
+++ b/notes.txt
@@ -1,1 +1,1 @@
-two
+three
```

At the first `status`, three versions of `notes.txt` exist:

| Place | Content | Compared with | Result |
| --- | --- | --- | --- |
| Last commit (HEAD) | `one` | the index | differs, so `staged:` |
| Index (staged) | `two` | the file on disk | differs, so `not staged:` |
| File on disk | `three` | | |

`diff` compares HEAD with the file on disk and does not look at the index, so the first `diff` shows `one` against `three`. The commit records the index, not the disk, so it saves `two`. The last `diff` proves it: HEAD now holds `two` and the disk still holds `three`.

## 2. Safe branch switching

```
$ printf 'A\n' > a.txt
$ printf '#!/bin/sh\necho hi\n' > run.sh
$ chmod +x run.sh
$ minigit add a.txt
$ minigit add run.sh
$ minigit commit -m base
[main 472cfa2] base
$ minigit branch feature
Created branch 'feature' at 472cfa2.
$ minigit checkout feature
Switched to branch 'feature'.
$ printf 'B\n' > a.txt
$ printf 'x\n' > extra.txt
$ minigit add a.txt
$ minigit add extra.txt
$ minigit commit -m feature-work
[feature 29faa57] feature-work
$ minigit checkout main
Switched to branch 'main'.
$ cat a.txt
A
$ ls
a.txt run.sh
$ ls -l run.sh
-rwxr-xr-x 1 … run.sh
```

A tracked file with local edits blocks the switch, and nothing is lost:

```
$ printf 'local\n' > a.txt
$ minigit checkout feature
minigit: local changes would be lost: a.txt; commit or undo your edits first
$ cat a.txt
local
$ minigit status
On branch main
not staged:
a.txt
```

An untracked file that the target branch would overwrite also blocks it:

```
$ printf 'A\n' > a.txt
$ printf 'mine\n' > extra.txt
$ minigit status
On branch main
untracked:
extra.txt
$ minigit checkout feature
minigit: untracked path would be overwritten: extra.txt; move or delete it first
$ cat extra.txt
mine
```

Once the blocker is gone, the switch restores the bytes, the executable bit, and the index:

```
$ rm extra.txt
$ minigit checkout feature
Switched to branch 'feature'.
$ cat a.txt
B
$ ls -l run.sh
-rwxr-xr-x 1 … run.sh
$ cat extra.txt
x
$ minigit status
On branch feature
clean
```

Rewriting `a.txt` with its committed bytes (`A`) counts as no change, because status compares content, not whether a file was touched.

## 3. Conflict resolution

```
$ printf 'x\n' > file.txt
$ minigit add file.txt
$ minigit commit -m base
[main c66dfab] base
$ minigit branch feature
Created branch 'feature' at c66dfab.
$ printf 'ours\n' > file.txt
$ minigit add file.txt
$ minigit commit -m ours
[main 398a1f9] ours
$ minigit checkout feature
Switched to branch 'feature'.
$ printf 'theirs\n' > file.txt
$ minigit add file.txt
$ minigit commit -m theirs
[feature ee4d829] theirs
$ minigit checkout main
Switched to branch 'main'.
$ minigit merge feature
minigit: merge conflict in: file.txt
resolve conflicts, run 'minigit add <path>', then 'minigit commit -m <message>'
$ cat file.txt
<<<<<<< HEAD
ours
=======
theirs
>>>>>>> feature
$ minigit status
On branch main
unresolved (merge conflict):
file.txt
$ minigit commit -m early
minigit: unresolved merge paths: file.txt
$ printf 'resolved\n' > file.txt
$ minigit status
On branch main
unresolved (merge conflict):
file.txt
$ minigit add file.txt
$ minigit status
On branch main
staged:
file.txt
$ minigit commit -m "resolved by hand"
[main 4051f66] resolved by hand
$ minigit status
On branch main
clean
```

Editing the file does not resolve the conflict. Only `add` does, and the final commit is the two-parent merge commit. Run the branch setup on `main` first and the conflicting edit on `feature` second; if `feature` is simply ahead of `main`, `merge` fast-forwards and there is nothing to resolve.

## What this shows

| Guarantee | Where it is visible |
| --- | --- |
| `add` snapshots the bytes at the moment it runs | part 1: the commit saves `two`, not `three` |
| `status` keeps staged, not staged, and untracked separate | parts 1 and 2 |
| `status` and `diff` only read | no command in this segment changes a file, the index, or a ref because of them |
| `checkout` validates before it writes | part 2: both refusals leave every file as it was |
| A merge conflict stays visible until `add` | part 3 |

Resolving a conflict by deleting the file, mode-only changes, binary files, and unusual paths are covered by the automated tests (`tests/test_index.py`, `tests/test_checkout_safety.py`, and the integration tests), not by this walkthrough.

## Validation record and remaining rehearsal

This segment was run by hand on 2026-11-19 at `2e87586` (`main`), Python 3.13.5, macOS on Apple silicon. Its output matches the blocks above apart from commit hashes and the `ls -l` details. The combined quality check passes all 445 tests.

Another teammate must still follow this segment without setup help at the final shared-checkpoint revision.
79 changes: 79 additions & 0 deletions docs/writeups/module-2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Module 2: index and working tree

## What was built

Module 2 owns the index and everything that compares the three places a file can live: the last commit (HEAD), the index (`.minigit/index`), and the files on disk. In `minigit/index.py` it provides:

- `add`, which snapshots a file's bytes and mode into the index.
- `status`, which reports staged, not staged, untracked, and unresolved (merge conflict) paths.
- `diff`, which shows line changes between HEAD and the working files.
- Safe `checkout`, which switches branches only when no local work can be lost.

Tests are in `tests/test_index.py` and `tests/test_checkout_safety.py`, plus the integration tests that run the CLI.

## Index snapshots

The index is a text file with one line per tracked path: `<mode> <hash> <path>`. `add` writes the file's bytes as a blob through Module 1's `ObjectStore` and records the hash, so the index holds the content as it was when `add` ran. Editing the file afterwards does not change what is staged. `commit` builds its tree from the index, not from the disk.

## Building nested trees

Index paths are flat (`src/util/a.py`), but commits need nested trees. The index entries are grouped by folder and written bottom-up: blobs, then each subtree, then the root tree. Entries are sorted by name so the same index always produces the same tree hash.

## Three-state status model

`status` makes two comparisons and one scan:

- staged: HEAD tree against the index.
- not staged: the index against the files on disk.
- untracked: files on disk that are not in the index.
- unresolved (merge conflict): paths from the pending merge state.

Comparisons use content hash and mode only. Timestamps and sizes are never trusted, so a same-size edit with the original timestamp is still found (`test_same_size_edit_with_preserved_timestamp_is_still_found`). A path can be in both staged and not staged at once, which is what the demo's first part shows.

## Myers diff and its reuse by merge

Line diffs use a handwritten Myers O(ND) algorithm. It returns `LineEdit(start, old_count, new_lines)` values in coordinates of the original text, and `apply_edits` replays them. Text is split on `\n` only. A file is binary if it contains a NUL byte or is not valid UTF-8, and binary files get a summary instead of hunks.

Module 3's merge reuses this: it diffs each side against the base and combines the edit lists, reporting a conflict when two edits overlap. Because both modules share one diff, a conflict is reported on exactly the lines `diff` would show as changed.

## Diff output

`diff` compares HEAD with the working files and ignores the index. Changes are grouped into hunks with three lines of context, and each hunk header is `@@ -old,count +new,count @@`. The output also handles a missing final newline (`\ No newline at end of file`), mode changes, binary files, and paths with unusual characters (`display_path` quotes them).

## Checkout safety

`checkout` validates before it writes:

1. Every path in the target tree is checked: absolute paths, `.` or `..` parts, backslashes, NUL, CR/LF, `.minigit`, and symlinks are rejected.
2. Tracked files with local edits or missing files block the switch.
3. Untracked files that the target would overwrite, including a file or folder blocking a path, block the switch.
4. Only after all checks pass are files written, and the index is written last.

Each error ends with the next step after a semicolon, for example `local changes would be lost: a.txt; commit or undo your edits first`. During a merge, conflicted paths keep their pre-merge index entry so they are not mistaken for local edits.

## Boundary with Module 3

Module 3 owns merge state. `add` asks it whether a path is part of a pending merge and tells it when the path has been resolved; a deleted path counts as resolved when it is listed as conflicted. `status` and `commit` read the unresolved list from the same state. Module 2 currently reads this through the private `_read_pending_merge()`.

## Measured results

The full tables, machine, and method are in `docs/performance.md`. On a generated repository of 3000 small files plus one 20000-line file (best of 5, Python 3.13.5, MacBook Air with Apple silicon), `status` and `diff` fell from about 0.12 s to about 0.066 s. A profile showed about 6000 `os.getcwd` calls from computing a relative path for every file. `walk_working_files()` now computes it once per folder. Every file is still read and hashed, nothing is cached, and the commands still write nothing. A 500-edit diff improved less (0.223 s to 0.166 s) because most of its time is the Myers diff.

These numbers come from a Module 2 fixture, not the shared project benchmark in #47/#58. The benchmark script itself is not yet checked in; that is tracked for the final handoff.

## Tradeoffs

- Reading and hashing every file keeps `status` correct but means its cost grows with repository size. There is no stat cache.
- The index is rewritten on each `add`, which is simple and atomic but not cheap for very large indexes.
- Diffs are line-based only, with no rename detection and no word-level output.
- Checkout refuses rather than merges local edits, which is safe but less convenient.

## Lessons learned

This module introduced a lot of concepts I had not worked with before, and the Myers diff was the biggest of them. I knew what a diff looks like, but not how a program finds the smallest set of changes. Working out how it walks along diagonals, and how it records each step so the path can be traced back was really interesting.

The second lesson was to measure before optimizing. The profile showed something I would not have guessed: the same function, `os.getcwd`, was called about 6000 times because a relative path was recomputed for every file. Computing it once per folder removed that cost with a small change, and kept `status` correct with no cache to go stale.

## What I would change next

First, replace `_read_pending_merge()` with Module 3's public `get_merge_status()` so Module 2 no longer depends on a private method. Second, make `add` refuse, or warn about, a file that still contains conflict markers.
Loading