exec git init
exec git config user.email "you@example.com"
exec git config user.name "Your Name"
exec lefthook install
exec git add -A
exec git commit --no-verify -m 'initial'

# Keep an unrelated unstaged change in the worktree
exec echo "unrelated unstaged change\n"
cp stdout c.txt
grep 'unrelated unstaged change' c.txt

# Stage first change
exec echo "line1\nlineStaged\nline3\n"
cp stdout a.txt
grep lineStaged a.txt
exec git add a.txt

# Add unstaged change on same line
exec echo "line1\nlineUnstaged\nline3\n"
cp stdout a.txt
grep lineUnstaged a.txt

# Change that line in the hook
! exec git commit -m 'test'
stderr '\s*conflict while merging unstaged changes\s*'

# Must restore the state before running the hook
grep lineUnstaged a.txt
! grep line42 a.txt
grep lineCommitted b.txt
grep 'unrelated unstaged change' c.txt
! exists .git/info/lefthook-unstaged.patch
! exists .git/info/lefthook-unstaged-all.patch
exec git stash list
! stdout 'lefthook auto backup'

-- lefthook.yml --
pre-commit:
  jobs:
    - run: echo "line1\nline42\nline3\n" > a.txt
    - run: echo "line1\nline42\nline3\n" > b.txt

-- a.txt --
line1
line2
line3

-- b.txt --
line1
lineCommitted
line3

-- c.txt --
committed content
