-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflog.go
151 lines (144 loc) · 3.11 KB
/
reflog.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2021 Wrangle Ltd
package ref
import (
"encoding/hex"
"fmt"
"io"
"strings"
"time"
"github.com/wrgl/core/pkg/encoding"
)
type Reflog struct {
OldOID []byte
NewOID []byte
AuthorName string
AuthorEmail string
Time time.Time
Action string
Message string
}
func (rec *Reflog) WriteTo(w io.Writer) (total int64, err error) {
var n int
if rec.OldOID == nil {
n, err = w.Write([]byte(fmt.Sprintf("%x ", make([]byte, 16))))
} else {
n, err = w.Write([]byte(fmt.Sprintf("%x ", rec.OldOID)))
}
if err != nil {
return 0, err
}
total += int64(n)
n, err = w.Write([]byte(fmt.Sprintf("%x %s ", rec.NewOID, rec.AuthorName)))
if err != nil {
return 0, err
}
total += int64(n)
if rec.AuthorEmail != "" {
n, err = w.Write([]byte(fmt.Sprintf("<%s> ", rec.AuthorEmail)))
if err != nil {
return 0, err
}
total += int64(n)
}
n, err = w.Write([]byte(fmt.Sprintf("%s %s: %s", encoding.EncodeTime(rec.Time), rec.Action, rec.Message)))
if err != nil {
return 0, err
}
total += int64(n)
return total, nil
}
const (
rlStateOldOID int = iota
rlStateNewOID
rlStateName
rlStateEmail
rlStateTime
rlStateAction
rlStateMessage
)
func (rec *Reflog) Read(b []byte) (n int, err error) {
state := rlStateOldOID
off := 0
n = len(b)
line := string(b)
mainLoop:
for {
switch state {
case rlStateOldOID:
if !strings.HasPrefix(line, strings.Repeat("0", 32)) {
rec.OldOID = make([]byte, 16)
_, err = hex.Decode(rec.OldOID, []byte(b[:32]))
if err != nil {
return 0, err
}
}
off += 33
state = rlStateNewOID
case rlStateNewOID:
rec.NewOID = make([]byte, 16)
_, err = hex.Decode(rec.NewOID, []byte(b[off:off+32]))
if err != nil {
return 0, err
}
off += 33
state = rlStateName
case rlStateName:
for i := off + 1; i < n; i++ {
c := b[i]
if c == '<' {
rec.AuthorName = line[off : i-1]
state = rlStateEmail
off = i + 1
break
} else if c >= 48 && c <= 57 {
// c is a numeric rune
rec.AuthorName = line[off : i-1]
state = rlStateTime
off = i
break
}
}
if rec.AuthorName == "" {
return 0, fmt.Errorf("invalid reflog record: couldn't parse author name in record %q", line)
}
case rlStateEmail:
for i := off + 1; i < n; i++ {
c := b[i]
if c == '>' {
rec.AuthorEmail = line[off:i]
state = rlStateTime
off = i + 2
break
}
}
if rec.AuthorEmail == "" {
return 0, fmt.Errorf("invalid reflog record: couldn't parse author email in record %q", line)
}
case rlStateTime:
rec.Time, err = encoding.DecodeTime(line[off:])
if err != nil {
return 0, err
}
state = rlStateAction
off += 17
case rlStateAction:
for i := off + 1; i < n; i++ {
c := b[i]
if c == ':' {
rec.Action = line[off:i]
state = rlStateMessage
off = i + 2
break
}
}
if rec.Action == "" {
return 0, fmt.Errorf("invalid reflog record: couldn't parse action in record %q", line)
}
case rlStateMessage:
rec.Message = line[off:]
break mainLoop
}
}
return n, nil
}