[!exec:git] skip 'git not found in $PATH'

mkgitconfig
mkhomedir golden
mkhomedir

# test that chezmoi init creates a git repo
chezmoi init
exists $CHEZMOISOURCEDIR/.git

# create a commit
cp golden/chezmoi.toml $CHEZMOISOURCEDIR/.chezmoi.toml.tmpl
cp golden/.file $CHEZMOISOURCEDIR/dot_file
chezmoi git add .
chezmoi git commit -- --message 'Initial commit'

chhome home2/user

# test that chezmoi init fetches git repo but does not apply
mkgitconfig
chezmoi init file://$WORK/home/user/.local/share/chezmoi
exists $CHEZMOISOURCEDIR/.git
! exists $HOME/.file

chhome home3/user

# test that chezmoi init --apply fetches a git repo and runs chezmoi apply
mkgitconfig
chezmoi init --apply --force file://$WORK/home/user/.local/share/chezmoi
exists $CHEZMOISOURCEDIR/.git
cmp $HOME/.file golden/.file

chhome home4/user

# test that chezmoi init --apply --depth 1 --force --purge clones, applies, and purges
mkgitconfig
exists $CHEZMOICONFIGDIR
! exists $CHEZMOISOURCEDIR
chezmoi init --apply --depth 1 --force --purge file://$WORK/home/user/.local/share/chezmoi
cmp $HOME/.file golden/.file
! exists $CHEZMOICONFIGDIR
! exists $CHEZMOISOURCEDIR

chhome home5/user

# test that chezmoi init does not clone the repo if it is already checked out but does create the config file
mkgitconfig
chezmoi init --source=$HOME/dotfiles file://$WORK/nonexistentrepo
exists $CHEZMOICONFIGDIR/chezmoi.toml

chhome home6/user

# test chezmoi init --one-shot
mkgitconfig
chezmoi init --one-shot file://$WORK/home/user/.local/share/chezmoi
cmp $HOME/.file golden/.file
! exists $CHEZMOICONFIGDIR
! exists $CHEZMOISOURCEDIR

chhome home7/user

# test chezmoi init --data=true
mkgitconfig
chezmoi init --data=true file://$WORK/home/user/.local/share/chezmoi
cmp $CHEZMOICONFIGDIR/chezmoi.toml golden/chezmoi.toml

# test chezmoi init --data=false
chezmoi init --data=false file://$WORK/home/user/.local/share/chezmoi
cmp $CHEZMOICONFIGDIR/chezmoi.toml golden/chezmoi.toml-no-data

chhome home8/user

# test that chezmoi init fails if the generated config is not valid
mkgitconfig
! chezmoi init
stderr 'While parsing config:'
! exists .config/chezmoi

chhome home/user

# create a new branch
chezmoi git checkout -- -b new-branch
edit $CHEZMOISOURCEDIR/dot_file
chezmoi git add dot_file
chezmoi git commit -- --message 'Edit .file'
chezmoi git checkout master

chhome home9/user

# test chezmoi init --branch
mkgitconfig
chezmoi init --apply --branch=new-branch file://$WORK/home/user/.local/share/chezmoi
grep '# edited' $HOME/.file

chhome home10/user

# test chezmoi init --config-path
mkgitconfig
chezmoi init --config-path=$HOME/.chezmoi.toml file://$WORK/home/user/.local/share/chezmoi
cmp $HOME/.chezmoi.toml golden/chezmoi.toml
! exists $CHEZMOICONFIGDIR/chezmoi.toml

chhome home11/user

# test chezmoi init when the source dir is already in a git working copy
mkgitconfig
exec git init $HOME/.local/share
chezmoi init
! exists $CHEZMOISOURCEDIR/.git

-- golden/chezmoi.toml --
[data]
    email = "firstname.lastname@company.com"
-- golden/chezmoi.toml-no-data --
[data]
    email = "me@home.org"
-- home4/user/.config/chezmoi/chezmoi.toml --
-- home5/user/dotfiles/.git/.keep --
-- home5/user/dotfiles/.chezmoi.toml.tmpl --
[data]
    email = "me@home.org"
-- home7/user/.config/chezmoi/chezmoi.toml --
[data]
    email = "firstname.lastname@company.com"
-- home7/user/.local/share/chezmoi/.git/.keep --
-- home7/user/.local/share/chezmoi/.chezmoi.toml.tmpl --
{{- $email := get . "email" -}}
{{- if not $email -}}
{{-   $email = "me@home.org" -}}
{{- end -}}
[data]
    email = {{ $email | quote }}
-- home8/user/.local/share/chezmoi/.chezmoi.toml.tmpl --
[diff]
exclude: ["scripts"]
