Skip to content

Commit

Permalink
Use .gitattributes to work around Windows line ending differences
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Sep 1, 2020
1 parent f85708c commit 2ee5ef6
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* -text
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,3 @@ require (
gopkg.in/ini.v1 v1.60.0 // indirect
gopkg.in/yaml.v2 v2.3.0
)

// Temporary while waiting for https://github.com/rogpeppe/go-internal/pull/106 to be merged.
replace github.com/rogpeppe/go-internal => github.com/twpayne/go-internal v1.5.3-0.20200706163000-4426ab554b0a
7 changes: 3 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
Expand Down Expand Up @@ -345,8 +348,6 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/twpayne/go-internal v1.5.3-0.20200706163000-4426ab554b0a h1:Vq/UMXTdWUDhrmgvcioHYtz/IavukmThI08zxYektl4=
github.com/twpayne/go-internal v1.5.3-0.20200706163000-4426ab554b0a/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/twpayne/go-shell v0.3.0 h1:nhDM9cQTqWwA0jGPOnqzsw8lke2jcKpvxDgnDO8Lq9o=
github.com/twpayne/go-shell v0.3.0/go.mod h1:H/gzux0DOH5jsjQSHXs6rs2Onxy+V4j6ycZTOulC0l8=
github.com/twpayne/go-vfs v1.0.1/go.mod h1:OIXA6zWkcn7Jk46XT7ceYqBMeIkfzJ8WOBhGJM0W4y8=
Expand Down Expand Up @@ -461,8 +462,6 @@ golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200821140526-fda516888d29 h1:mNuhGagCf3lDDm5C0376C/sxh6V7fy9WbdEu/YDNA04=
golang.org/x/sys v0.0.0-20200821140526-fda516888d29/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200828194041-157a740278f4 h1:kCCpuwSAoYJPkNc6x0xT9yTtV4oKtARo4RGBQWOfg9E=
golang.org/x/sys v0.0.0-20200828194041-157a740278f4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down
45 changes: 29 additions & 16 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -42,6 +43,7 @@ func TestScript(t *testing.T) {
"mkfile": cmdMkFile,
"mkhomedir": cmdMkHomeDir,
"mksourcedir": cmdMkSourceDir,
"unix2dos": cmdUNIX2DOS,
},
Condition: func(cond string) (bool, error) {
switch cond {
Expand Down Expand Up @@ -178,6 +180,8 @@ func cmdMkHomeDir(ts *testscript.TestScript, neg bool, args []string) {
},
".exists": "# contents of .exists\n",
".gitconfig": "" +
"[core]\n" +
" autocrlf = false\n" +
"[user]\n" +
" email = you@example.com\n" +
" name = Your Name\n",
Expand Down Expand Up @@ -219,6 +223,8 @@ func cmdMkSourceDir(ts *testscript.TestScript, neg bool, args []string) {
"executable_dot_binary": "#!/bin/sh\n",
"dot_bashrc": "# contents of .bashrc\n",
"dot_gitconfig.tmpl": "" +
"[core]\n" +
" autocrlf = false\n" +
"[user]\n" +
" email = {{ \"you@example.com\" }}\n" +
" name = Your Name\n",
Expand All @@ -233,6 +239,28 @@ func cmdMkSourceDir(ts *testscript.TestScript, neg bool, args []string) {
}
}

// cmdUNIX2DOS converts files from UNIX line endings to DOS line endings.
func cmdUNIX2DOS(ts *testscript.TestScript, neg bool, args []string) {
if neg {
ts.Fatalf("unsupported: ! unix2dos")
}
if len(args) < 1 {
ts.Fatalf("usage: unix2dos paths...")
}
for _, arg := range args {
filename := ts.MkAbs(arg)
data, err := ioutil.ReadFile(filename)
if err != nil {
ts.Fatalf("%s: %v", filename, err)
}
data = bytes.Join(bytes.Split(data, []byte{'\n'}), []byte{'\r', '\n'})
//nolint:gosec
if err := ioutil.WriteFile(filename, data, 0o666); err != nil {
ts.Fatalf("%s: %v", filename, err)
}
}
}

func setup(env *testscript.Env) error {
var (
binDir = filepath.Join(env.WorkDir, "bin")
Expand All @@ -256,28 +284,13 @@ func setup(env *testscript.Env) error {
env.Setenv("SHELL", filepath.Join(binDir, "shell"))
}

if runtime.GOOS != "windows" {
// Fix permissions on any files in the bin directory.
infos, err := ioutil.ReadDir(binDir)
if err == nil {
for _, info := range infos {
if err := os.Chmod(filepath.Join(binDir, info.Name()), 0o755); err != nil {
return err
}
}
}
}

root := make(map[string]interface{})
switch runtime.GOOS {
case "windows":
root["/bin"] = map[string]interface{}{
// editor.cmd a non-interactive script that appends "# edited\n" to
// the end of each file.
"editor.cmd": &vfst.File{
Perm: 0o755,
Contents: []byte(`@for %%x in (%*) do echo # edited >> %%x`),
},
"editor.cmd": "@for %%x in (%*) do echo # edited >> %%x\r\n",
}
default:
root["/bin"] = map[string]interface{}{
Expand Down
3 changes: 3 additions & 0 deletions testdata/scripts/secretgeneric.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[!windows] chmod 755 bin/secret
[windows] unix2dos bin/secret.cmd

chezmoi secret generic examplepassword
stdout examplepassword

Expand Down
3 changes: 3 additions & 0 deletions testdata/scripts/secretgopass.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[!windows] chmod 755 bin/gopass
[windows] unix2dos bin/gopass.cmd

chezmoi -v secret gopass show misc/example.com
stdout examplepassword

Expand Down
3 changes: 3 additions & 0 deletions testdata/scripts/secretkeepassxc.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[!windows] chmod 755 bin/keepass-test
[windows] unix2dos bin/keepass-test.cmd

stdin $HOME/input
chezmoi secret keepassxc -- show --show-protected secrets.kdbx example.com
stdout examplelogin
Expand Down
3 changes: 3 additions & 0 deletions testdata/scripts/secretlastpass.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[!windows] chmod 755 bin/lpass
[windows] unix2dos bin/lpass.cmd

chezmoi secret lastpass -- show --json example.com
stdout examplelogin

Expand Down
3 changes: 3 additions & 0 deletions testdata/scripts/secretonepassword.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[!windows] chmod 755 bin/op
[windows] unix2dos bin/op.cmd

chezmoi execute-template '{{ (onepassword "ExampleLogin").uuid }}'
stdout '^wxcplh5udshnonkzg2n4qx262y$'

Expand Down
3 changes: 3 additions & 0 deletions testdata/scripts/secretpass.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[!windows] chmod 755 bin/pass
[windows] unix2dos bin/pass.cmd

chezmoi -v secret pass show misc/example.com
stdout examplepassword

Expand Down
7 changes: 5 additions & 2 deletions testdata/scripts/updategit.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ chezmoi git -- commit -m 'Add dot_bashrc'
# test chezmoi init --apply
chhome home2${/}user
chezmoi init --apply file://$WORK/home/user/.local/share/chezmoi
cmp -text $HOME${/}.bashrc $WORK${/}home${/}user${/}.bashrc
cmp $HOME${/}.bashrc $WORK${/}home${/}user${/}.bashrc

# create a new commit
chhome home${/}user
Expand All @@ -25,4 +25,7 @@ chezmoi update
grep '# edited' $HOME${/}.bashrc

-- home/user/.bashrc --
# contents of .bashrc
# contents of .bashrc
-- home2/user/.gitconfig --
[core]
autocrlf = false

0 comments on commit 2ee5ef6

Please sign in to comment.