aboutsummaryrefslogtreecommitdiffstats
path: root/repository/common.go
blob: 7fd7ae19a98e2b586ac4c276a2a83bd6bd742ff1 (plain) (blame)
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
package repository

import (
	"io"

	"golang.org/x/crypto/openpgp"
	"golang.org/x/crypto/openpgp/armor"
	"golang.org/x/crypto/openpgp/errors"
)

// nonNativeMerge is an implementation of a branch merge, for the case where
// the underlying git implementation doesn't support it natively.
func nonNativeMerge(repo RepoData, ref string, otherRef string, treeHashFn func() Hash) error {
	commit, err := repo.ResolveRef(ref)
	if err != nil {
		return err
	}

	otherCommit, err := repo.ResolveRef(otherRef)
	if err != nil {
		return err
	}

	if commit == otherCommit {
		// nothing to merge
		return nil
	}

	// fast-forward is possible if otherRef include ref

	otherCommits, err := repo.ListCommits(otherRef)
	if err != nil {
		return err
	}

	fastForwardPossible := false
	for _, hash := range otherCommits {
		if hash == commit {
			fastForwardPossible = true
			break
		}
	}

	if fastForwardPossible {
		return repo.UpdateRef(ref, otherCommit)
	}

	// fast-forward is not possible, we need to create a merge commit

	// we need a Tree to make the commit, an empty Tree will do
	emptyTreeHash, err := repo.StoreTree(nil)
	if err != nil {
		return err
	}

	newHash, err := repo.StoreCommit(emptyTreeHash, commit, otherCommit)
	if err != nil {
		return err
	}

	return repo.UpdateRef(ref, newHash)
}

// nonNativeListCommits is an implementation for ListCommits, for the case where
// the underlying git implementation doesn't support if natively.
func nonNativeListCommits(repo RepoData, ref string) ([]Hash, error) {
	var result []Hash

	stack := make([]Hash, 0, 32)
	visited := make(map[Hash]struct{})

	hash, err := repo.ResolveRef(ref)
	if err != nil {
		return nil, err
	}

	stack = append(stack, hash)

	for len(stack) > 0 {
		// pop
		hash := stack[len(stack)-1]
		stack = stack[:len(stack)-1]

		if _, ok := visited[hash]; ok {
			continue
		}

		// mark as visited
		visited[hash] = struct{}{}
		result = append(result, hash)

		commit, err := repo.ReadCommit(hash)
		if err != nil {
			return nil, err
		}

		for _, parent := range commit.Parents {
			stack = append(stack, parent)
		}
	}

	// reverse
	for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
		result[i], result[j] = result[j], result[i]
	}

	return result, nil
}

// deArmorSignature convert an armored (text serialized) signature into raw binary
func deArmorSignature(armoredSig io.Reader) (io.Reader, error) {
	block, err := armor.Decode(armoredSig)
	if err != nil {
		return nil, err
	}
	if block.Type != openpgp.SignatureType {
		return nil, errors.InvalidArgumentError("expected '" + openpgp.SignatureType + "', got: " + block.Type)
	}
	return block.Body, nil
}