---
phase: 07-log-output-emulation
plan: 01
type: execute
wave: 1
depends_on: []
files_modified:
  - cmd_log.py
  - tests/conftest.py
  - tests/test_cmd_log.py
autonomous: true

must_haves:
  truths:
    - "git log --oneline outputs hash and subject per line"
    - "git log -N limits output to N commits"
    - "git log --oneline -5 combines both flags correctly"
    - "Basic git log (no flags) still works unchanged"
  artifacts:
    - path: "cmd_log.py"
      provides: "Flag translation for --oneline and -N"
      contains: "ONELINE_TEMPLATE"
    - path: "tests/conftest.py"
      provides: "sl_repo_with_commits fixture"
      contains: "sl_repo_with_commits"
    - path: "tests/test_cmd_log.py"
      provides: "E2E tests for log flags"
      contains: "TestLogOneline"
  key_links:
    - from: "cmd_log.py"
      to: "sl log"
      via: "run_sl with -T and -l flags"
      pattern: "run_sl.*-T.*-l"
    - from: "tests/test_cmd_log.py"
      to: "cmd_log.py"
      via: "run_gitsl log --oneline"
      pattern: 'run_gitsl.*"log".*"--oneline"'
---

<objective>
Implement git log flag translation for --oneline and -N formats.

Purpose: Enable git workflow tools to use git log with common formatting flags when working with Sapling repos.

Output: Updated cmd_log.py with flag parsing, new test fixture for multi-commit repos, comprehensive E2E tests.
</objective>

<context>
@.planning/PROJECT.md
@.planning/ROADMAP.md
@.planning/phases/07-log-output-emulation/07-RESEARCH.md

# Source files
@cmd_log.py
@cmd_add.py (flag translation pattern reference)
@tests/conftest.py
@tests/test_cmd_log.py
@tests/helpers/commands.py
</context>

<tasks>

<task type="auto">
  <name>Task 1: Update cmd_log.py with flag translation</name>
  <files>cmd_log.py</files>
  <action>
Update cmd_log.py to parse and translate git log flags to sl equivalents:

1. Add import for `re` module
2. Define ONELINE_TEMPLATE constant: `"{node|short} {desc|firstline}\n"`
3. Update handle() function to:
   - Parse `--oneline` flag -> add `-T ONELINE_TEMPLATE` to sl args
   - Parse `-N` format (e.g., `-5`) -> add `-l N` to sl args
   - Parse `-n N` format (space between) -> add `-l N` to sl args
   - Parse `-nN` format (attached) -> add `-l N` to sl args
   - Parse `--max-count=N` format -> add `-l N` to sl args
   - Pass all other arguments through unchanged

Use regex `r'^-(\d+)$'` to match -N format. Process args in a while loop with index to handle `-n N` consuming two args.

When neither flag is present, behavior remains unchanged (passthrough).

Reference: 07-RESEARCH.md has complete implementation example.
  </action>
  <verify>
`python -c "import cmd_log; print(cmd_log.ONELINE_TEMPLATE)"` outputs the template string.
  </verify>
  <done>
cmd_log.py contains flag parsing for --oneline and all -N variants, with proper sl argument translation.
  </done>
</task>

<task type="auto">
  <name>Task 2: Add sl_repo_with_commits fixture and E2E tests</name>
  <files>tests/conftest.py, tests/test_cmd_log.py</files>
  <action>
**In tests/conftest.py:**

Add new fixture `sl_repo_with_commits` that:
- Takes sl_repo as dependency
- Creates 10 commits, each with a file (file0.txt through file9.txt)
- Uses run_command for sl add and sl commit
- Returns the repo path

**In tests/test_cmd_log.py:**

Add new test classes after existing tests:

1. `TestLogOneline` (FLAG-04):
   - `test_oneline_returns_hash_and_subject`: Verify each line has format `<hex_hash> <subject>`
   - Use sl_repo_with_commit fixture

2. `TestLogLimit` (FLAG-05):
   - `test_limit_with_dash_n`: `-3` limits to 3 commits
   - `test_limit_with_dash_n_space`: `-n 3` limits to 3 commits
   - `test_limit_with_dash_n_attached`: `-n3` limits to 3 commits
   - `test_limit_with_max_count`: `--max-count=3` limits to 3 commits
   - Use sl_repo_with_commits fixture (10 commits)

3. `TestLogCombined`:
   - `test_oneline_with_limit_either_order`: `--oneline -3` and `-3 --oneline` produce same result
   - `test_limit_greater_than_available`: `-100` on 10-commit repo shows all 10
   - Use sl_repo_with_commits fixture

Import run_gitsl from conftest and shutil for sl availability check.

Use semantic matching: verify hash is valid hex, subject is non-empty. Do NOT compare exact hash values.
  </action>
  <verify>
`cd /Users/vmakaev/NonWork/gitsl && python -m pytest tests/test_cmd_log.py -v` passes all tests including new ones.
  </verify>
  <done>
conftest.py has sl_repo_with_commits fixture. test_cmd_log.py has comprehensive tests for --oneline, -N variants, and combined flags. All tests pass.
  </done>
</task>

</tasks>

<verification>
1. All existing tests still pass: `python -m pytest tests/ -v`
2. New log flag tests pass: `python -m pytest tests/test_cmd_log.py -v`
3. Manual verification:
   - Create sl repo with commits
   - Run `python gitsl.py log --oneline` - should show hash and subject per line
   - Run `python gitsl.py log -3` - should show 3 commits
   - Run `python gitsl.py log --oneline -3` - should show 3 commits in oneline format
</verification>

<success_criteria>
1. FLAG-04 satisfied: `git log --oneline` outputs `<hash> <subject>` format via sl template
2. FLAG-05 satisfied: `git log -N` limits output to N commits via `sl log -l N`
3. Combined flags work: `git log --oneline -5` produces 5 commits in oneline format
4. Passthrough preserved: `git log` without flags still works as before
5. All E2E tests pass
</success_criteria>

<output>
After completion, create `.planning/phases/07-log-output-emulation/07-01-SUMMARY.md`
</output>
