-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.go
More file actions
56 lines (48 loc) · 1.37 KB
/
commit.go
File metadata and controls
56 lines (48 loc) · 1.37 KB
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
package gitlib
import (
"fmt"
"strconv"
"strings"
"time"
)
// CommitInfo - данные для создания коммита
type CommitInfo struct {
TreeHash string // хеш дерева
Parent string // хеш родителя (пусто для первого коммита)
Author string // "Name <email>"
Committer string // "Name <email>"
Message string
}
// WriteCommit записывает commit-объект в хранилище и возвращает его хеш
func (s *Storage) WriteCommit(info *CommitInfo) (Hash, error) {
var b strings.Builder
b.WriteString("tree " + info.TreeHash + "\n")
if info.Parent != "" {
b.WriteString("parent " + info.Parent + "\n")
}
t := time.Now().Unix()
tz := formatTimezone(t)
b.WriteString("author " + info.Author + " " + strconv.FormatInt(t, 10) + " " + tz + "\n")
b.WriteString("committer " + info.Committer + " " + strconv.FormatInt(t, 10) + " " + tz + "\n\n")
b.WriteString(info.Message)
if !strings.HasSuffix(info.Message, "\n") {
b.WriteByte('\n')
}
obj := &Object{
Type: ObjectCommit,
Data: []byte(b.String()),
}
return s.WriteLoose(obj)
}
func formatTimezone(unix int64) string {
t := time.Unix(unix, 0)
_, offset := t.Zone()
sign := "+"
if offset < 0 {
sign = "-"
offset = -offset
}
h := offset / 3600
m := (offset % 3600) / 60
return fmt.Sprintf("%s%02d%02d", sign, h, m)
}