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
|
// 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-sponge soaks up a fast-import stream, and re-emits it,
// potentially with different flags.
package main
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"syscall"
)
func usage() {
fmt.Printf("Usage: git fast-export | %s [FAST-IMPORT-FLAGS] -- [FAST-EXPORT-FLAGS] | git fast-import\n", os.Args[0])
}
func main() {
var split int
for i := range os.Args {
switch os.Args[i] {
case "--":
split = i
case "-h", "--help":
usage()
os.Exit(0)
}
}
if split == 0 {
fmt.Fprintf(os.Stderr, "%s: must give a '--' argument\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Try '%s --help' for more information\n", os.Args[0])
os.Exit(2)
}
inargs := os.Args[1:split]
outargs := os.Args[split+1:]
err := sponge(inargs, outargs)
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
ws := ee.ProcessState.Sys().(syscall.WaitStatus)
switch {
case ws.Exited():
os.Exit(ws.ExitStatus())
case ws.Signaled():
os.Exit(128 + int(ws.Signal()))
}
} else {
fmt.Fprintf(os.Stderr, "%s: error: %v\n", os.Args[0], err)
os.Exit(255)
}
}
}
func sponge(inargs, outargs []string) error {
tmpdir, err := ioutil.TempDir("", filepath.Base(os.Args[0])+".")
if err != nil {
return err
}
defer os.RemoveAll(tmpdir)
cmd := exec.Command("git", "init", ".")
cmd.Dir = tmpdir
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
cmd = exec.Command("git", append([]string{"fast-import", "--cat-blob-fd=0"}, inargs...)...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
cmd = exec.Command("git", "for-each-ref", "--format=%(refname)")
cmd.Stderr = os.Stderr
b, err := cmd.Output()
if err != nil {
return err
}
refs := strings.Split(strings.TrimSuffix(string(b), "\n"), "\n")
cmd = exec.Command("git", append(append([]string{"fast-export"}, outargs...), refs...)...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
return nil
}
|