Skip to content

Commit

Permalink
fix: Handle copies in automatic commit messages
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 8, 2024
1 parent 956244d commit fccc759
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
1 change: 1 addition & 0 deletions assets/templates/COMMIT_MESSAGE.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

{{- range .RenamedOrCopied -}}
{{ if and (eq .X 'R') (eq .Y '.') }}Change attributes of {{ .Path | targetRelPath }}
{{ else if and (eq .X 'C') (eq .Y '.') -}}Copy {{ .OrigPath | targetRelPath }} to {{ .Path | targetRelPath }}
{{ else }}{{with (printf "unsupported XY: %q" (printf "%c%c" .X .Y)) }}{{ fail . }}{{ end }}
{{ end }}
{{- end -}}
Expand Down
41 changes: 41 additions & 0 deletions internal/chezmoigit/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"testing"

"github.com/alecthomas/assert/v2"

"github.com/twpayne/chezmoi/v2/internal/chezmoitest"
)

func TestParseStatusPorcelainV2(t *testing.T) {
Expand Down Expand Up @@ -56,6 +58,45 @@ func TestParseStatusPorcelainV2(t *testing.T) {
},
},
},
{
name: "copied",
outputStr: chezmoitest.JoinLines(
"2 C. N... 100644 100644 100644 4a58007052a65fbc2fc3f910f2855f45a4058e74 4a58007052a65fbc2fc3f910f2855f45a4058e74 C100 c\tb",
"2 R. N... 100644 100644 100644 4a58007052a65fbc2fc3f910f2855f45a4058e74 4a58007052a65fbc2fc3f910f2855f45a4058e74 R100 d\tb",
),
expectedStatus: &Status{
RenamedOrCopied: []RenamedOrCopiedStatus{
{
X: 'C',
Y: '.',
Sub: "N...",
MH: 0o100644,
MI: 0o100644,
MW: 0o100644,
HH: "4a58007052a65fbc2fc3f910f2855f45a4058e74",
HI: "4a58007052a65fbc2fc3f910f2855f45a4058e74",
RC: 'C',
Score: 100,
Path: "c",
OrigPath: "b",
},
{
X: 'R',
Y: '.',
Sub: "N...",
MH: 0o100644,
MI: 0o100644,
MW: 0o100644,
HH: "4a58007052a65fbc2fc3f910f2855f45a4058e74",
HI: "4a58007052a65fbc2fc3f910f2855f45a4058e74",
RC: 'R',
Score: 100,
Path: "d",
OrigPath: "b",
},
},
},
},
{
name: "update",
outputStr: "1 .M N... 100644 100644 100644 353dbbb3c29a80fb44d4e26dac111739d25294db 353dbbb3c29a80fb44d4e26dac111739d25294db cmd/git.go\n",
Expand Down
29 changes: 19 additions & 10 deletions internal/cmd/testdata/scripts/autocommit.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,47 @@ exec chezmoi init

# test that chezmoi add creates and pushes a commit
exec chezmoi add $HOME${/}.file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'Add \.file'

# test that chezmoi edit creates and pushes a commit
exec chezmoi edit $HOME${/}.file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'Update \.file'

# test that chezmoi chattr creates and pushes a commit
exec chezmoi chattr +executable $HOME${/}.file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'Change attributes of \.file'

# test that chezmoi add on a directory creates and pushes a commit
exec chezmoi add $HOME${/}.dir
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'Add \.dir/file'

# test that copying a file creates a valid commit message
cp $CHEZMOISOURCEDIR/executable_dot_file $CHEZMOISOURCEDIR/executable_dot_file2
mv $CHEZMOISOURCEDIR/executable_dot_file $CHEZMOISOURCEDIR/executable_dot_file3
exec git -C $CHEZMOISOURCEDIR config --local diff.renames copies
exec git -C $CHEZMOISOURCEDIR add .
exec chezmoi edit $HOME${/}.dir${/}file
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'Copy \.file to \.file2'

# test that chezmoi chattr on a file in a directory creates and pushes a commit
exec chezmoi chattr --debug +private $HOME${/}.dir/file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'Change attributes of \.dir'

# test that chezmoi forget creates and pushes a commit
exec chezmoi forget --force $HOME${/}.file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
stdout 'Remove \.file'
exec chezmoi forget --force $HOME${/}.file2
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'Remove \.file2'

# test that chezmoi edit uses a custom commit message template
appendline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplate = "{{ .prefix }}my commit message"'
exec chezmoi edit $HOME${/}.dir${/}file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'feat: my commit message'

# test that only one of git.commitMessageTemplate and git.commitMessageTemplateFile can be set
Expand All @@ -50,7 +59,7 @@ removeline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplate = "{{ .pref

# test that chezmoi edit uses a custom commit message template file
exec chezmoi edit $HOME${/}.dir${/}file
exec git --git-dir=$CHEZMOISOURCEDIR/.git show HEAD
exec git -C $CHEZMOISOURCEDIR show HEAD
stdout 'feat: my commit message file'
removeline $CHEZMOICONFIGDIR/chezmoi.toml ' commitMessageTemplateFile = ".COMMIT_MESSAGE.tmpl"'

Expand Down

0 comments on commit fccc759

Please sign in to comment.