summaryrefslogtreecommitdiff
path: root/fi-filefilter/main.go
blob: 6731c1e65a67511f6b940ba7e55eb7b7d555c12c (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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/>.

// Command fi-filefilter prunes a fast-import stream, removing
// filenames that do not match a regexp.
package main

import (
	"fmt"
	"io"
	"os"
	"regexp"
	"time"

	"git.lukeshu.com/go/libfastimport"
	"github.com/pkg/errors"

	"git.parabola.nu/~lukeshu/fastimport-go-utils/fiutil"
)

func usage(w io.Writer) {
	fmt.Fprintf(w, "Usage: git fast-export --full-tree | %s REGEXP | git fast-import\n", os.Args[0])
	fmt.Fprintf(w, "Prunes a fast-import stream, removing filenames that do not match a regexp\n")
}

func main() {
	if len(os.Args) != 2 {
		usage(os.Stderr)
		os.Exit(2)
	}

	re, err := regexp.Compile(os.Args[1])
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(2)
	}

	frontend := libfastimport.NewFrontend(os.Stdin, os.Stdin, nil)
	backend := libfastimport.NewBackend(os.Stdout, os.Stdout, nil)

	filter := &FileFilter{
		backend: backend,
		re:      re,
		beg:     time.Now(),
	}

	if err := fiutil.RunHandler(frontend, filter); err != nil {
		fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
		os.Exit(1)
	}
}

type FileFilter struct {
	backend *libfastimport.Backend
	re      *regexp.Regexp

	// statistics
	commits  int
	filesIn  int
	filesOut int
	beg      time.Time
}

// file commands ///////////////////////////////////////////////////////////////
func (h *FileFilter) FileModify(cmd libfastimport.FileModify) error {
	h.filesIn++
	if !h.re.MatchString(string(cmd.Path)) {
		return nil
	}
	h.filesOut++
	return h.backend.Do(cmd)
}
func (h *FileFilter) FileModifyInline(cmd libfastimport.FileModifyInline) error {
	h.filesIn++
	if !h.re.MatchString(string(cmd.Path)) {
		return nil
	}
	h.filesOut++
	return h.backend.Do(cmd)
}
func (h *FileFilter) FileDelete(cmd libfastimport.FileDelete) error {
	h.filesIn++
	if !h.re.MatchString(string(cmd.Path)) {
		return nil
	}
	h.filesOut++
	return h.backend.Do(cmd)
}
func (h *FileFilter) FileDeleteAll(cmd libfastimport.FileDeleteAll) error {
	return h.backend.Do(cmd)
}
func (h *FileFilter) FileCopy(cmd libfastimport.FileCopy) error {
	return errors.New("unexpected \"filecopy\" command: this filter requires --full-tree")
}
func (h *FileFilter) FileRename(cmd libfastimport.FileRename) error {
	return errors.New("unexpected \"filerename\" command: this filter requires --full-tree")
}

// statistics //////////////////////////////////////////////////////////////////
func (h *FileFilter) CmdCommitEnd(cmd libfastimport.CmdCommitEnd) error {
	h.commits++
	fmt.Fprintf(os.Stderr,
		"%d commits (%d => %d files) (%.2f commit/s)\r",
		h.commits, h.filesIn, h.filesOut,
		float64(h.commits)/time.Since(h.beg).Seconds())
	return nil
}

// note commands ///////////////////////////////////////////////////////////////
func (h *FileFilter) NoteModify(cmd libfastimport.NoteModify) error {
	return h.backend.Do(cmd)
}
func (h *FileFilter) NoteModifyInline(cmd libfastimport.NoteModifyInline) error {
	return h.backend.Do(cmd)
}

// other commands //////////////////////////////////////////////////////////////
func (h *FileFilter) CmdBlob(cmd libfastimport.CmdBlob) error             { return h.backend.Do(cmd) }
func (h *FileFilter) CmdCheckpoint(cmd libfastimport.CmdCheckpoint) error { return h.backend.Do(cmd) }
func (h *FileFilter) CmdComment(cmd libfastimport.CmdComment) error       { return h.backend.Do(cmd) }
func (h *FileFilter) CmdCommit(cmd libfastimport.CmdCommit) error         { return h.backend.Do(cmd) }
func (h *FileFilter) CmdDone(cmd libfastimport.CmdDone) error             { return h.backend.Do(cmd) }
func (h *FileFilter) CmdOption(cmd libfastimport.CmdOption) error         { return h.backend.Do(cmd) }
func (h *FileFilter) CmdProgress(cmd libfastimport.CmdProgress) error     { return h.backend.Do(cmd) }
func (h *FileFilter) CmdReset(cmd libfastimport.CmdReset) error           { return h.backend.Do(cmd) }
func (h *FileFilter) CmdTag(cmd libfastimport.CmdTag) error               { return h.backend.Do(cmd) }

func (h *FileFilter) CmdCatBlob(cmd libfastimport.CmdCatBlob) (sha1 string, data string, err error) {
	return h.backend.CatBlob(cmd)
}
func (h *FileFilter) CmdGetMark(cmd libfastimport.CmdGetMark) (sha1 string, err error) {
	return h.backend.GetMark(cmd)
}
func (h *FileFilter) CmdLs(cmd libfastimport.CmdLs) (mode libfastimport.Mode, dataref string, path libfastimport.Path, err error) {
	return h.backend.Ls(cmd)
}

func (h *FileFilter) CmdFeature(cmd libfastimport.CmdFeature) error {
	switch cmd.Feature {
	case "date-format":
		if cmd.Argument != "raw" {
			return errors.Errorf("date-format=%q: only supports the %q format", cmd.Argument, "raw")
		}
		return h.backend.Do(cmd)
	case "export-marks", "relative-marks", "no-relative-marks", "force", "import-marks", "import-marks-if-exists", "get-mark", "cat-blob", "ls", "notes", "done":
		return h.backend.Do(cmd)
	default:
		return errors.Errorf("unknown feature %q", cmd.Feature)
	}
}