diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2020-03-09 23:55:25 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-09 23:55:25 +0100 |
commit | 18a858b38ca4197987a6da7058ab0397255fa82c (patch) | |
tree | e3ed8f892f54ee09af0eb9b829357e2409d4e9fb /repository_plan9_test.go | |
parent | 17dbd886616f82be2a59c0d02fd93d3d69f2392c (diff) | |
parent | cbc339b60d5b25ad2b2685de4303f78d0efa51fe (diff) | |
download | go-git-18a858b38ca4197987a6da7058ab0397255fa82c.tar.gz |
Merge pull request #1 from go-git/pr-1269
*: add Plan 9 support
Diffstat (limited to 'repository_plan9_test.go')
-rw-r--r-- | repository_plan9_test.go | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/repository_plan9_test.go b/repository_plan9_test.go new file mode 100644 index 0000000..00ebeed --- /dev/null +++ b/repository_plan9_test.go @@ -0,0 +1,47 @@ +package git + +import ( + "fmt" + "strings" +) + +// preReceiveHook returns the bytes of a pre-receive hook script +// that prints m before exiting successfully +func preReceiveHook(m string) []byte { + return []byte(fmt.Sprintf("#!/bin/rc\necho -n %s\n", quote(m))) +} + +const quoteChar = '\'' + +func needsQuote(s string) bool { + for i := 0; i < len(s); i++ { + c := s[i] + if c == quoteChar || c <= ' ' { // quote, blanks, or control characters + return true + } + } + return false +} + +// Quote adds single quotes to s in the style of rc(1) if they are needed. +// The behaviour should be identical to Plan 9's quote(3). +func quote(s string) string { + if s == "" { + return "''" + } + if !needsQuote(s) { + return s + } + var b strings.Builder + b.Grow(10 + len(s)) // Enough room for few quotes + b.WriteByte(quoteChar) + for i := 0; i < len(s); i++ { + c := s[i] + if c == quoteChar { + b.WriteByte(quoteChar) + } + b.WriteByte(c) + } + b.WriteByte(quoteChar) + return b.String() +} |