summaryrefslogtreecommitdiff
path: root/fi-prune-empty2/srcrepo.go
blob: c771bc59eab4d6eda7087768bb38a47040b1b52b (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
// Copyright 2019-2021  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 (
	"os/exec"
	"strings"

	"github.com/pkg/errors"
)

type Hash string

const EmptyHash = "0000000000000000000000000000000000000000"

func AsHash(str string) (Hash, error) {
	if len(str) != 40 {
		return "", errors.Errorf("does not look like a sha1: %q", str)
	}
	for _, c := range str {
		switch {
		case '0' <= c && c <= '9', 'a' <= c && c <= 'f':
		default:
			return "", errors.Errorf("does not look like a sha1: %q", str)
		}
	}
	return Hash(str), nil
}

func AsArgs(args ...Hash) []string {
	ret := make([]string, 0, len(args))
	for _, arg := range args {
		ret = append(ret, string(arg))
	}
	return ret
}

type GitRepo struct {
	Dir string

	cache struct {
		parents map[Hash][]Hash
		tree    map[Hash]Hash
	}
}

func (g *GitRepo) init() {
	g.cache.parents = make(map[Hash][]Hash)
	g.cache.tree = make(map[Hash]Hash)
}

func (g *GitRepo) Cmd(args ...string) *exec.Cmd {
	cmd := append([]string{"git", "-C", g.Dir, "--no-replace-objects"}, args...)
	return exec.Command(cmd[0], cmd[1:]...)
}

func LinesAsHashes(hashesBytes []byte) ([]Hash, error) {
	var hashes []Hash
	for _, line := range strings.Split(string(hashesBytes), "\n") {
		if line != "" {
			hash, err := AsHash(line)
			if err != nil {
				return nil, err
			}
			hashes = append(hashes, hash)
		}
	}
	return hashes, nil
}

func (g *GitRepo) Parents(commit Hash) ([]Hash, error) {
	g.init()
	if parents, ok := g.cache.parents[commit]; ok {
		return parents, nil
	}
	parentsBytes, err := g.Cmd("rev-parse", string(commit)+"^@").Output()
	if err != nil {
		return nil, err
	}
	parents, err := LinesAsHashes(parentsBytes)
	if err != nil {
		return nil, err
	}
	g.cache.parents[commit] = parents
	return parents, nil
}

func (g *GitRepo) Tree(commit Hash) (Hash, error) {
	g.init()
	if tree, ok := g.cache.tree[commit]; ok {
		return tree, nil
	}
	treeBytes, err := g.Cmd("rev-parse", string(commit)+"^{tree}").Output()
	if err != nil {
		return "", err
	}
	tree, err := AsHash(strings.TrimSpace(string(treeBytes)))
	if err != nil {
		return "", err
	}
	g.cache.tree[commit] = tree
	return tree, nil
}