diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2019-02-10 22:05:18 -0500 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2019-02-13 22:39:25 -0500 |
commit | f81ca7beef48b350847e7942569b79bcee61d735 (patch) | |
tree | efd88f22f64c2ef572e047022629181576a32e7c | |
parent | c057d061b5575c7666950cc0d335c1d43886c2f5 (diff) |
Add fi-filefilter
-rw-r--r-- | fi-filefilter/main.go | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/fi-filefilter/main.go b/fi-filefilter/main.go new file mode 100644 index 0000000..407f3f6 --- /dev/null +++ b/fi-filefilter/main.go @@ -0,0 +1,128 @@ +// 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 main + +import ( + "fmt" + "io" + "os" + "strings" + "time" + + "git.lukeshu.com/go/libfastimport" +) + +func abort(err error) { + fmt.Fprintln(os.Stderr, "Error:", err) + os.Exit(1) +} + +func accept(path string) bool { + for _, arg := range os.Args[1:] { + if strings.Contains(path, arg) { + return true + } + } + return false +} + +var commit *libfastimport.CmdCommit + +var ( + frontend = libfastimport.NewFrontend(os.Stdin, nil, nil) + backend = libfastimport.NewBackend(os.Stdout, nil, nil) +) + +func ensureCommit() { + if commit == nil { + return + } + backend.Do(*commit) + commit = nil +} + +func main() { + commits := 0 + beg := time.Now() + status := func() { + fmt.Fprintf(os.Stderr, + "%d commits (%.2f commit/s)\r", + commits, + float64(commits)/time.Since(beg).Seconds()) + } + + froms := map[string]string{} + for { + cmd, err := frontend.ReadCmd() + if err != nil { + if err == io.EOF { + break + } + abort(err) + } + + switch cmdt := cmd.(type) { + + case libfastimport.CmdCommit: + commit = &cmdt + if strings.HasPrefix(commit.From, ":") { + if from, ok := froms[commit.From]; ok { + commit.From = from + } + } + for i, m := range commit.Merge { + if strings.HasPrefix(m, ":") { + if from, ok := froms[m]; ok { + commit.Merge[i] = from + } + } + } + + case libfastimport.FileModify: + if accept(string(cmdt.Path)) { + ensureCommit() + backend.Do(cmd) + } + case libfastimport.FileModifyInline: + if accept(string(cmdt.Path)) { + ensureCommit() + backend.Do(cmd) + } + case libfastimport.FileDelete: + if accept(string(cmdt.Path)) { + ensureCommit() + backend.Do(cmd) + } + case libfastimport.FileCopy: + panic("copy") + case libfastimport.FileRename: + panic("rename") + + case libfastimport.CmdCommitEnd: + if commit != nil { + if commit.Mark >= 0 { + froms[fmt.Sprintf(":%d", commit.Mark)] = commit.From + } + commit = nil + } + commits++ + status() + + default: + backend.Do(cmd) + } + } +} |