-
Notifications
You must be signed in to change notification settings - Fork 1
/
trim.go
97 lines (84 loc) · 2.36 KB
/
trim.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
package main
import (
"errors"
"fmt"
"log"
"time"
)
// Trimmer is an type for describing functions that can be used to
// trim the repository history
type Trimmer func(*Release) bool
// MakeTimeTrimmer creates a trimmer function that reduces the repository
// history to a given window of time
func MakeTimeTrimmer(time.Duration) Trimmer {
return func(commit *Release) (trim bool) {
return false
}
}
// MakeLengthTrimmer creates a trimmer function that reduces the repository
// history to a given number of commits
func MakeLengthTrimmer(commitcount int) Trimmer {
log.Println("Trim requested")
count := commitcount
return func(commit *Release) (trim bool) {
if count >= 0 {
count--
return false
}
return true
}
}
// TrimHistory works through the release history and attempts to apply
// the promvided Trimmer. If the current active release history is longer than
// the trimmer allows, a new release will be created with the number of commits
// after the parent release to trim beyond in the TrimAfter field.
// If the release history is already shorter than the Trimmer allows, or
// the release history has previous been trimmed to a shorter length than would
// be allowed, the original parentid is returned.
func (release *Release) TrimHistory(store Archiver, trimmer Trimmer) error {
curr := release.ParentID
trimCount := int32(0)
activeTrim := int32(0)
for {
if StoreID(curr).String() == store.EmptyFileID().String() {
// We reached an empty commit before we decided to trim
return nil
}
if activeTrim == 1 {
// The current commit is the last commit from previously
// active trimming, the release history is short enough
// already
return nil
}
c, err := store.GetRelease(curr)
if err != nil {
return errors.New("error while trimming, " + err.Error())
}
if c.TrimAfter != 0 {
if c.TrimAfter < activeTrim {
activeTrim = c.TrimAfter
}
}
// Check to see if the trimmer would dispose of this
// commit
if trimmer(c) {
break
} else {
trimCount++
}
if activeTrim > 1 {
activeTrim--
}
curr = c.ParentID
}
if trimCount > 0 {
log.Println("Trim did something")
release.Actions = append(release.Actions,
ReleaseLogAction{
Type: ActionTRIM,
Description: fmt.Sprintf("Release history trimmed to %d revisions", trimCount),
})
release.TrimAfter = trimCount
}
return nil
}