-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
74 lines (68 loc) · 1.34 KB
/
util.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
package main
import (
"io"
"os"
"strconv"
"strings"
)
func FileToString(f *os.File) string {
str := ""
buf := make([]byte, 1024)
for {
n, err := f.Read(buf)
if err != nil && err != io.EOF {
panic(err)
}
if n == 0 {
break
}
str += string(buf[:n])
}
return str
}
func CopyFile(src string, dst string) {
srcFile, err := os.Open(src)
if err != nil {
panic(err)
}
defer srcFile.Close()
dstFile, err := os.Create(dst)
if err != nil {
panic(err)
}
defer dstFile.Close()
io.Copy(dstFile, srcFile)
}
func GetMessageFromCommitFile(commitHash string) string {
commitFile, err := os.OpenFile(".gogit/commits/"+commitHash, os.O_RDONLY, 0644)
if err != nil {
panic(err)
}
commitString := FileToString(commitFile)
lines := strings.Split(commitString, "\n")
return lines[len(lines)-2]
}
func GetTimeFromCommitFile(commitHash string) int64 {
commitFile, err := os.OpenFile(".gogit/commits/"+commitHash, os.O_RDONLY, 0644)
if err != nil {
panic(err)
}
commitString := FileToString(commitFile)
lines := strings.Split(commitString, "\n")
time, err := strconv.ParseInt(lines[len(lines)-3], 10, 64)
if err != nil {
panic(err)
}
return time
}
func AreStringsArraysEqual(a []string, b []string) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}