aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/packfile/object_pack.go
blob: 1563517aef0922a2c870bcfcd264abc532d4b3fa (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
121
122
123
124
package packfile

import (
	"gopkg.in/src-d/go-git.v4/plumbing"
)

// ObjectToPack is a representation of an object that is going to be into a
// pack file.
type ObjectToPack struct {
	// The main object to pack, it could be any object, including deltas
	Object plumbing.EncodedObject
	// Base is the object that a delta is based on (it could be also another delta).
	// If the main object is not a delta, Base will be null
	Base *ObjectToPack
	// Original is the object that we can generate applying the delta to
	// Base, or the same object as Object in the case of a non-delta
	// object.
	Original plumbing.EncodedObject
	// Depth is the amount of deltas needed to resolve to obtain Original
	// (delta based on delta based on ...)
	Depth int

	// offset in pack when object has been already written, or 0 if it
	// has not been written yet
	Offset int64
}

// newObjectToPack creates a correct ObjectToPack based on a non-delta object
func newObjectToPack(o plumbing.EncodedObject) *ObjectToPack {
	return &ObjectToPack{
		Object:   o,
		Original: o,
	}
}

// newDeltaObjectToPack creates a correct ObjectToPack for a delta object, based on
// his base (could be another delta), the delta target (in this case called original),
// and the delta Object itself
func newDeltaObjectToPack(base *ObjectToPack, original, delta plumbing.EncodedObject) *ObjectToPack {
	return &ObjectToPack{
		Object:   delta,
		Base:     base,
		Original: original,
		Depth:    base.Depth + 1,
	}
}

// BackToOriginal converts that ObjectToPack to a non-deltified object if it was one
func (o *ObjectToPack) BackToOriginal() {
	if o.IsDelta() {
		o.Object = o.Original
		o.Base = nil
		o.Depth = 0
	}
}

// IsWritten returns if that ObjectToPack was
// already written into the packfile or not
func (o *ObjectToPack) IsWritten() bool {
	return o.Offset > 1
}

// MarkWantWrite marks this ObjectToPack as WantWrite
// to avoid delta chain loops
func (o *ObjectToPack) MarkWantWrite() {
	o.Offset = 1
}

// WantWrite checks if this ObjectToPack was marked as WantWrite before
func (o *ObjectToPack) WantWrite() bool {
	return o.Offset == 1
}

func (o *ObjectToPack) Type() plumbing.ObjectType {
	if o.Original != nil {
		return o.Original.Type()
	}

	if o.Base != nil {
		return o.Base.Type()
	}

	if o.Object != nil {
		return o.Object.Type()
	}

	panic("cannot get type")
}

func (o *ObjectToPack) Hash() plumbing.Hash {
	if o.Original != nil {
		return o.Original.Hash()
	}

	do, ok := o.Object.(plumbing.DeltaObject)
	if ok {
		return do.ActualHash()
	}

	panic("cannot get hash")
}

func (o *ObjectToPack) Size() int64 {
	if o.Original != nil {
		return o.Original.Size()
	}

	do, ok := o.Object.(plumbing.DeltaObject)
	if ok {
		return do.ActualSize()
	}

	panic("cannot get ObjectToPack size")
}

func (o *ObjectToPack) IsDelta() bool {
	return o.Base != nil
}

func (o *ObjectToPack) SetDelta(base *ObjectToPack, delta plumbing.EncodedObject) {
	o.Object = delta
	o.Base = base
	o.Depth = base.Depth + 1
}