[!exec:git] stop

mkgitconfig
mkhomedir golden
mkhomedir

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

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

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

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

# test that chezmoi init --apply --depth 1 --force --purge clones, applies, and purges
chhome home4/user
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

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

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

# test chezmoi init --data=true
chhome home7/user
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

# test that chezmoi init fails if the generated config is not valid
chhome home8/user
mkgitconfig
! chezmoi init
stderr 'parsing error'
! exists .config/chezmoi

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