-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathgit.go
135 lines (126 loc) · 3.82 KB
/
git.go
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
package main
import (
"bufio"
"bytes"
"compress/zlib"
"fmt"
"io"
"io/ioutil"
"os/exec"
"path/filepath"
"strconv"
"strings"
"github.com/pkg/errors"
)
// return the objects reachable from ref excluding the objects reachable from exclude
func gitListObjects(ref string, exclude []string) ([]string, error) {
args := []string{"rev-list", "--objects", ref}
for _, e := range exclude {
args = append(args, "^"+e)
}
revList := exec.Command("git", args...)
// dunno why - sometime git doesnt want to work on the inner repo/.git
if strings.HasSuffix(thisGitRepo, ".git") {
thisGitRepo = filepath.Dir(thisGitRepo)
}
revList.Dir = thisGitRepo // GIT_DIR
out, err := revList.CombinedOutput()
if err != nil {
return nil, errors.Wrapf(err, "rev-list failed: %s\n%q", err, string(out))
}
var objs []string
s := bufio.NewScanner(bytes.NewReader(out))
for s.Scan() {
objs = append(objs, strings.Split(s.Text(), " ")[0])
}
if err := s.Err(); err != nil {
return nil, errors.Wrapf(err, "scanning rev-list output failed: %s", err)
}
return objs, nil
}
func gitFlattenObject(sha1 string) (io.Reader, error) {
kind, err := gitCatKind(sha1)
if err != nil {
return nil, errors.Wrapf(err, "flatten: kind(%s) failed", sha1)
}
size, err := gitCatSize(sha1)
if err != nil {
return nil, errors.Wrapf(err, "flatten: size(%s) failed", sha1)
}
r, err := gitCatData(sha1, kind)
if err != nil {
return nil, errors.Wrapf(err, "flatten: data(%s) failed", sha1)
}
// move to exp/git
pr, pw := io.Pipe()
go func() {
zw := zlib.NewWriter(pw)
if _, err := fmt.Fprintf(zw, "%s %d\x00", kind, size); err != nil {
pw.CloseWithError(errors.Wrapf(err, "writing git format header failed"))
return
}
if _, err := io.Copy(zw, r); err != nil {
pw.CloseWithError(errors.Wrapf(err, "copying git data failed"))
return
}
if err := zw.Close(); err != nil {
pw.CloseWithError(errors.Wrapf(err, "zlib close failed"))
return
}
pw.Close()
}()
return pr, nil
}
func gitCatKind(sha1 string) (string, error) {
catFile := exec.Command("git", "cat-file", "-t", sha1)
catFile.Dir = thisGitRepo // GIT_DIR
out, err := catFile.CombinedOutput()
return strings.TrimSpace(string(out)), err
}
func gitCatSize(sha1 string) (int64, error) {
catFile := exec.Command("git", "cat-file", "-s", sha1)
catFile.Dir = thisGitRepo // GIT_DIR
out, err := catFile.CombinedOutput()
if err != nil {
return -1, errors.Wrapf(err, "catSize(%s): run failed", sha1)
}
return strconv.ParseInt(strings.TrimSpace(string(out)), 10, 64)
}
func gitCatData(sha1, kind string) (io.Reader, error) {
catFile := exec.Command("git", "cat-file", kind, sha1)
catFile.Dir = thisGitRepo // GIT_DIR
stdout, err := catFile.StdoutPipe()
if err != nil {
return nil, errors.Wrapf(err, "catData(%s): stdoutPipe failed", sha1)
}
stderr, err := catFile.StderrPipe()
if err != nil {
return nil, errors.Wrapf(err, "catData(%s): stderrPipe failed", sha1)
}
r := io.MultiReader(stdout, stderr)
if err := catFile.Start(); err != nil {
err = errors.Wrap(err, "catFile.Start failed")
out, readErr := ioutil.ReadAll(r)
if readErr != nil {
readErr = errors.Wrap(readErr, "readAll failed")
return nil, errors.Wrapf(err, "catData(%s) failed during: %s", sha1, readErr)
}
return nil, errors.Wrapf(err, "catData(%s) failed: %q", sha1, out)
}
// todo wait for cmd?!
return r, nil
}
func gitRefHash(ref string) (string, error) {
refParse := exec.Command("git", "rev-parse", ref)
refParse.Dir = thisGitRepo // GIT_DIR
out, err := refParse.CombinedOutput()
return strings.TrimSpace(string(out)), err
}
func gitIsAncestor(a, ref string) error {
mergeBase := exec.Command("git", "merge-base", "--is-ancestor", a, ref)
mergeBase.Dir = thisGitRepo // GIT_DIR
if out, err := mergeBase.CombinedOutput(); err != nil {
return errors.Wrapf(err, "merge-base failed: %q", string(out))
}
return nil
}