summaryrefslogtreecommitdiff
path: root/fiutil
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2019-02-16 20:41:20 -0500
committerLuke Shumaker <lukeshu@lukeshu.com>2019-02-16 20:41:20 -0500
commit97c4b4053ab187a75a1a79854df2941522cdab09 (patch)
treef57c1ee4d8430ed86884bd7b8924cef7c66db349 /fiutil
parent2495e17b6c2d4323b0c989b8ef27248f29eb9548 (diff)
better type checking
Diffstat (limited to 'fiutil')
-rw-r--r--fiutil/.gitignore1
-rw-r--r--fiutil/Makefile40
-rw-r--r--fiutil/cli.go27
-rw-r--r--fiutil/doc.go20
-rwxr-xr-xfiutil/handler.go.gen106
5 files changed, 194 insertions, 0 deletions
diff --git a/fiutil/.gitignore b/fiutil/.gitignore
new file mode 100644
index 0000000..37724f0
--- /dev/null
+++ b/fiutil/.gitignore
@@ -0,0 +1 @@
+/handler.go
diff --git a/fiutil/Makefile b/fiutil/Makefile
new file mode 100644
index 0000000..c74bd8b
--- /dev/null
+++ b/fiutil/Makefile
@@ -0,0 +1,40 @@
+# Copyright 2015-2017, 2019 Luke Shumaker <lukeshu@parabola.nu>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# header
+ifneq ($(topsrcdir),)
+include $(topsrcdir)/build-aux/Makefile.head.mk
+else
+srcdir = .
+.DEFAULT_GOAL = files.generate
+endif
+
+# body
+
+files.src.gen += handler.go
+
+$(srcdir)/%.go: $(srcdir)/%.go.gen
+ cd $(@D) && ./$(^F) > $(@F)
+
+# footer
+ifneq ($(topsrcdir),)
+include $(topsrcdir)/build-aux/Makefile.tail.mk
+else
+files.generate: $(files.src.gen)
+maintainer-clean:
+ rm -f -- $(files.src.gen) $(files.src.int)
+.PHONY: files.generate maintainer-clean
+.DELETE_ON_ERROR:
+endif
diff --git a/fiutil/cli.go b/fiutil/cli.go
new file mode 100644
index 0000000..649aee4
--- /dev/null
+++ b/fiutil/cli.go
@@ -0,0 +1,27 @@
+// Copyright 2019 Luke Shumaker <lukeshu@parabola.nu>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+package fiutil
+
+import (
+ "fmt"
+ "os"
+)
+
+func ErrUsage(msg string) {
+ fmt.Fprintln(os.Stderr, "%s: %s\n", os.Args[0], msg)
+ fmt.Fprintf(os.Stderr, "Try '%s --help' for more information\n", os.Args[0])
+ os.Exit(2)
+}
diff --git a/fiutil/doc.go b/fiutil/doc.go
new file mode 100644
index 0000000..4d6d5f9
--- /dev/null
+++ b/fiutil/doc.go
@@ -0,0 +1,20 @@
+// Copyright 2019 Luke Shumaker <lukeshu@parabola.nu>
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+//go:generate make
+
+// Package fiutil provides utilities for implementing fast-import
+// filters on top of git.lukeshu.com/go/libfastimport.
+package fiutil
diff --git a/fiutil/handler.go.gen b/fiutil/handler.go.gen
new file mode 100755
index 0000000..9f7d054
--- /dev/null
+++ b/fiutil/handler.go.gen
@@ -0,0 +1,106 @@
+#!/usr/bin/env bash
+# Copyright 2019 Luke Shumaker <lukeshu@parabola.nu>
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+cmds=(
+ CmdBlob
+ #CmdCatBlob
+ CmdCheckpoint
+ CmdComment
+ CmdCommit
+ CmdCommitEnd
+ CmdDone
+ CmdFeature
+ #CmdGetMark
+ #CmdLs
+ CmdOption
+ CmdProgress
+ CmdReset
+ CmdTag
+ FileCopy
+ FileDelete
+ FileDeleteAll
+ FileModify
+ FileModifyInline
+ FileRename
+ NoteModify
+ NoteModifyInline
+)
+
+{
+ cat <<-EOT
+ //$(printf ' %q' "$0" "$@")
+ // Code generated by the above command; DO NOT EDIT.
+
+ package fiutil
+
+ import (
+ "io"
+
+ "git.lukeshu.com/go/libfastimport"
+ "github.com/pkg/errors"
+ )
+
+ type Handler interface{
+ $(for cmd in "${cmds[@]}"; do
+ echo "$cmd(libfastimport.$cmd) error"
+ done)
+ CmdCatBlob(libfastimport.CmdCatBlob) (sha1 string, data string, err error)
+ CmdGetMark(libfastimport.CmdGetMark) (sha1 string, err error)
+ CmdLs(libfastimport.CmdLs) (mode libfastimport.Mode, dataref string, path libfastimport.Path, err error)
+ }
+
+ func RunHandler(reader *libfastimport.Frontend, handler Handler) error {
+ for {
+ cmd, err := reader.ReadCmd()
+ if err != nil {
+ if err == io.EOF {
+ return nil
+ }
+ return err
+ }
+
+ switch cmdt := cmd.(type) {
+ $(for cmd in "${cmds[@]}"; do
+ echo "case libfastimport.$cmd:"
+ echo " if err := handler.$cmd(cmdt); err != nil {"
+ echo " return err"
+ echo " }"
+ done)
+ case libfastimport.CmdCatBlob:
+ sha1, data, err := handler.CmdCatBlob(cmdt)
+ if err != nil {
+ return err
+ }
+ return reader.RespondCatBlob(sha1, data)
+ case libfastimport.CmdGetMark:
+ sha1, err := handler.CmdGetMark(cmdt)
+ if err != nil {
+ return err
+ }
+ return reader.RespondGetMark(sha1)
+ case libfastimport.CmdLs:
+ mode, dataref, path, err := handler.CmdLs(cmdt)
+ if err != nil {
+ return err
+ }
+ return reader.RespondLs(mode, dataref, path)
+ default:
+ return errors.Errorf("unexpected command object: %[1]T(%#[1]v)", cmd)
+ }
+ }
+ }
+ EOT
+} | gofmt