diff --git a/server/hooks/hooks.go b/server/hooks/hooks.go index adf185d3c8efdb43e7fcb4ce326b5e1339d9675f..c625769f27b31039fe3657969b1e37af6791d61b 100644 --- a/server/hooks/hooks.go +++ b/server/hooks/hooks.go @@ -31,6 +31,7 @@ const ( // This function should be called by the backend when a repository is created. // TODO: support context. func GenerateHooks(_ context.Context, cfg *config.Config, repo string) error { + // TODO: support git hook tests. if flag.Lookup("test.v") != nil { log.WithPrefix("backend.hooks").Warn("refusing to set up hooks when in test") return nil diff --git a/server/server.go b/server/server.go index 690c9650ba37c47e50780704225c0b05ab625331..1c0470af365ea9a8ba70158b298262dd26073f7a 100644 --- a/server/server.go +++ b/server/server.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "net/http" "github.com/charmbracelet/log" @@ -158,6 +159,9 @@ func (s *Server) Shutdown(ctx context.Context) error { s.Cron.Stop() return nil }) + if closer, ok := s.Backend.(io.Closer); ok { + defer closer.Close() // nolint: errcheck + } return errg.Wait() } @@ -172,5 +176,8 @@ func (s *Server) Close() error { s.Cron.Stop() return nil }) + if closer, ok := s.Backend.(io.Closer); ok { + defer closer.Close() // nolint: errcheck + } return errg.Wait() } diff --git a/server/utils/utils.go b/server/utils/utils.go index ca54ce2717031906766a3209b17942e791e7e958..e0f3143f1b18e2a42c8d982a5f54f9262e29f20a 100644 --- a/server/utils/utils.go +++ b/server/utils/utils.go @@ -2,7 +2,7 @@ package utils import ( "fmt" - "path/filepath" + "path" "strings" "unicode" ) @@ -10,7 +10,9 @@ import ( // SanitizeRepo returns a sanitized version of the given repository name. func SanitizeRepo(repo string) string { repo = strings.TrimPrefix(repo, "/") - repo = filepath.Clean(repo) + // We're using path instead of filepath here because this is not OS dependent + // looking at you Windows + repo = path.Clean(repo) repo = strings.TrimSuffix(repo, ".git") return repo } @@ -40,8 +42,6 @@ func ValidateRepo(repo string) error { return fmt.Errorf("repo cannot be empty") } - fmt.Println("ACTUAL REPO NAME IS", repo) - for _, r := range repo { if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '-' && r != '_' && r != '.' && r != '/' { fmt.Println("INVALID CHAR", r, string(r)) diff --git a/testscript/script_test.go b/testscript/script_test.go index e0ca3b047dba4c2d3076714db235439a45b77359..58a71b52dad1f271de66205d599484b26a2d227a 100644 --- a/testscript/script_test.go +++ b/testscript/script_test.go @@ -8,6 +8,7 @@ import ( "net" "os" "path/filepath" + "runtime" "strings" "sync" "testing" @@ -47,7 +48,7 @@ func TestScript(t *testing.T) { "soft": cmdSoft(admin1.Signer()), "git": cmdGit(key), "mkreadme": cmdMkReadme, - "unix2dos": cmdUnix2Dos, + "dos2unix": cmdDos2Unix, }, Setup: func(e *testscript.Env) error { sshPort := test.RandomPort() @@ -153,14 +154,13 @@ func cmdSoft(key ssh.Signer) func(ts *testscript.TestScript, neg bool, args []st } } -func cmdUnix2Dos(ts *testscript.TestScript, neg bool, args []string) { - // now this should not be needed indeed - return +// P.S. Windows sucks! +func cmdDos2Unix(ts *testscript.TestScript, neg bool, args []string) { if neg { - ts.Fatalf("unsupported: ! unix2dos") + ts.Fatalf("unsupported: ! dos2unix") } if len(args) < 1 { - ts.Fatalf("usage: unix2dos paths...") + ts.Fatalf("usage: dos2unix paths...") } for _, arg := range args { filename := ts.MkAbs(arg) @@ -169,11 +169,8 @@ func cmdUnix2Dos(ts *testscript.TestScript, neg bool, args []string) { ts.Fatalf("%s: %v", filename, err) } - // First ensure we don't have any `\r\n` there already then replace all - // `\n` with `\r\n`. - // This should prevent creating `\r\r\n`. + // Replace all '\r\n' with '\n'. data = bytes.ReplaceAll(data, []byte{'\r', '\n'}, []byte{'\n'}) - data = bytes.ReplaceAll(data, []byte{'\n'}, []byte{'\r', '\n'}) if err := os.WriteFile(filename, data, 0o644); err != nil { ts.Fatalf("%s: %v", filename, err) @@ -184,13 +181,25 @@ func cmdUnix2Dos(ts *testscript.TestScript, neg bool, args []string) { func cmdGit(key string) func(ts *testscript.TestScript, neg bool, args []string) { return func(ts *testscript.TestScript, neg bool, args []string) { sshArgs := []string{ - "-F", "/dev/null", "-o", "StrictHostKeyChecking=no", - "-o", "UserKnownHostsFile=/dev/null", "-o", "IdentityAgent=none", "-o", "IdentitiesOnly=yes", "-o", "ServerAliveInterval=60", - "-i", key, + // Escape the key path for Windows. + "-i", strings.ReplaceAll(key, `\`, `\\`), + } + // Windows null device + // https://stackoverflow.com/a/36746090/10913628 + if runtime.GOOS == "windows" { + sshArgs = append(sshArgs, []string{ + "-F", `$nul`, + "-o", `UserKnownHostsFile=$nul`, + }...) + } else { + sshArgs = append(sshArgs, []string{ + "-F", "/dev/null", + "-o", "UserKnownHostsFile=/dev/null", + }...) } ts.Setenv( "GIT_SSH_COMMAND", diff --git a/testscript/testdata/mirror.txtar b/testscript/testdata/mirror.txtar index ea3dbc67db14fa4958bb30835725e269e1fa3df5..7dcdb6c59b40aa11edc15f6d7f90d1414f4aabc5 100644 --- a/testscript/testdata/mirror.txtar +++ b/testscript/testdata/mirror.txtar @@ -1,7 +1,7 @@ # vi: set ft=conf -# convert crlf on windows -[windows] unix2dos info1.txt info2.txt tree.txt +# convert crlf to lf on windows +[windows] dos2unix info1.txt info2.txt tree.txt # import a repo soft repo import --mirror charmbracelet/catwalk https://github.com/charmbracelet/catwalk.git diff --git a/testscript/testdata/repo-create.txtar b/testscript/testdata/repo-create.txtar index d1d1b98613137496e3f98a3ecfb9ec9e30c8bb5f..108790cada62f112e21f385da48fe9d6d816465f 100644 --- a/testscript/testdata/repo-create.txtar +++ b/testscript/testdata/repo-create.txtar @@ -1,7 +1,7 @@ # vi: set ft=conf -# convert crlf on windows -[windows] unix2dos tree.txt readme.md branch_list.1.txt +# convert crlf to lf on windows +[windows] dos2unix tree.txt readme.md branch_list.1.txt # create a repo soft repo create repo1 -d 'description' -H -p -n 'repo11' diff --git a/testscript/testdata/repo-import.txt b/testscript/testdata/repo-import.txt index df54b0024ad5c6644880ff318b0e1d63f4053f54..4d73a5a43ef7d34cc42f0a85fdba2a44a313753e 100644 --- a/testscript/testdata/repo-import.txt +++ b/testscript/testdata/repo-import.txt @@ -1,7 +1,7 @@ # vi: set ft=conf -# convert crlf on windows -[windows] unix2dos repo3.txt +# convert crlf to lf on windows +[windows] dos2unix repo3.txt # import private soft repo import --private repo1 https://github.com/charmbracelet/catwalk.git diff --git a/testscript/testdata/set-username.txtar b/testscript/testdata/set-username.txtar index 0331b228745d00027504b82b78ded160c0b58c6a..03c6e9ce265d302976ac9937db312dd8efca0952 100644 --- a/testscript/testdata/set-username.txtar +++ b/testscript/testdata/set-username.txtar @@ -1,7 +1,7 @@ # vi: set ft=conf -# convert crlf on windows -[windows] unix2dos info1.txt info2.txt +# convert crlf to lf on windows +[windows] dos2unix info1.txt info2.txt # get original username soft info diff --git a/testscript/testdata/user_management.txtar b/testscript/testdata/user_management.txtar index 35d474b29886254071d28a51cefa20c085bdac1b..8eade22e52d8af090cd9501ae8d894901b140b6b 100644 --- a/testscript/testdata/user_management.txtar +++ b/testscript/testdata/user_management.txtar @@ -1,7 +1,7 @@ # vi: set ft=conf -# convert crlf on windows -[windows] unix2dos info.txt admin_key_list1.txt admin_key_list2.txt list1.txt list2.txt foo_info1.txt foo_info2.txt foo_info3.txt foo_info4.txt foo_info5.txt +# convert crlf to lf on windows +[windows] dos2unix info.txt admin_key_list1.txt admin_key_list2.txt list1.txt list2.txt foo_info1.txt foo_info2.txt foo_info3.txt foo_info4.txt foo_info5.txt # add key to admin soft user add-pubkey admin "$ADMIN2_AUTHORIZED_KEY"