aboutsummaryrefslogtreecommitdiffstats
path: root/objects.go
blob: 49c0e9e0bb5362998516a9468363d1d0681ba1d8 (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
package git

import (
	"fmt"
	"io"
	"strconv"
	"time"

	"gopkg.in/src-d/go-git.v2/internal"
)

// Blob is used to store file data - it is generally a file.
type Blob struct {
	Hash internal.Hash
	Size int64

	obj internal.Object
}

// Decode transform an internal.Object into a Blob struct
func (b *Blob) Decode(o internal.Object) error {
	b.Hash = o.Hash()
	b.Size = o.Size()
	b.obj = o

	return nil
}

// Reader returns a reader allow the access to the content of the blob
func (b *Blob) Reader() io.Reader {
	return b.obj.Reader()
}

// Signature represents an action signed by a person
type Signature struct {
	Name  string
	Email string
	When  time.Time
}

// Decode decodes a byte slice into a signature
func (s *Signature) Decode(b []byte) {
	if len(b) == 0 {
		return
	}

	from := 0
	state := 'n' // n: name, e: email, t: timestamp, z: timezone
	for i := 0; ; i++ {
		var c byte
		var end bool
		if i < len(b) {
			c = b[i]
		} else {
			end = true
		}

		switch state {
		case 'n':
			if c == '<' || end {
				if i == 0 {
					break
				}
				s.Name = string(b[from : i-1])
				state = 'e'
				from = i + 1
			}
		case 'e':
			if c == '>' || end {
				s.Email = string(b[from:i])
				i++
				state = 't'
				from = i + 1
			}
		case 't':
			if c == ' ' || end {
				t, err := strconv.ParseInt(string(b[from:i]), 10, 64)
				if err == nil {
					s.When = time.Unix(t, 0)
				}
				end = true
			}
		}

		if end {
			break
		}
	}
}

func (s *Signature) String() string {
	return fmt.Sprintf("%s <%s>", s.Name, s.Email)
}