1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
//go:build testtools
// +build testtools
// A simple Git LFS pointer extension that translates lower case characters
// to upper case characters and vise versa. This is used in the Git LFS
// integration tests.
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"unicode"
)
var gitDir = ".git"
func main() {
log := openLog()
if len(os.Args) != 4 || (os.Args[1] != "clean" && os.Args[1] != "smudge") || os.Args[2] != "--" {
logErrorAndExit(log, "invalid arguments: %s", strings.Join(os.Args, " "))
}
stat, err := os.Stat(".git")
if os.IsNotExist(err) {
logErrorAndExit(log, "%q directory not found", gitDir)
} else if err != nil {
logErrorAndExit(log, "unable to check %q directory: %s", gitDir, err)
} else if !stat.Mode().IsDir() {
logErrorAndExit(log, "%q is not a directory", gitDir)
}
if log != nil {
fmt.Fprintf(log, "%s: %s\n", os.Args[1], os.Args[3])
}
reader := bufio.NewReader(os.Stdin)
err = nil
for {
var r rune
r, _, err = reader.ReadRune()
if err != nil {
if err == io.EOF {
err = nil
}
break
}
if unicode.IsLower(r) {
r = unicode.ToUpper(r)
} else if unicode.IsUpper(r) {
r = unicode.ToLower(r)
}
os.Stdout.WriteString(string(r))
}
if err != nil {
logErrorAndExit(log, "unable to read stdin: %s", err)
}
if log != nil {
log.Close()
}
os.Exit(0)
}
func openLog() *os.File {
logPath := os.Getenv("LFSTEST_EXT_LOG")
if logPath == "" {
return nil
}
log, err := os.OpenFile(logPath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
logErrorAndExit(nil, "unable to open log %q: %s", logPath, err)
}
return log
}
func logErrorAndExit(log *os.File, format string, vals ...interface{}) {
msg := fmt.Sprintf(format, vals...)
fmt.Fprintln(os.Stderr, msg)
if log != nil {
fmt.Fprintln(log, msg)
log.Close()
}
os.Exit(1)
}
|