-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit.go
211 lines (194 loc) · 4.87 KB
/
commit.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"strings"
"time"
)
type Commit struct {
User string // User who made the commit
Hash string // Hash of the commit
Objects []Object // Objects (files) that were changed
PrevCommits []*Commit // Previous commit
Time int64 // Time of the commit
Message string // Message of the commit
}
func (c *Commit) Serialize() string {
var prevCommitHashes []string
for _, commit := range c.PrevCommits {
prevCommitHashes = append(prevCommitHashes, commit.Hash)
}
prevCommitHashConcat := strings.Join(prevCommitHashes, ",")
var objectsString string
for _, object := range c.Objects {
objectsString += object.Serialize() + ","
}
return fmt.Sprintf("%s\n%s\n%d\n%s\n%s\n%d\n%s", c.User, c.Hash, len(c.Objects), objectsString, prevCommitHashConcat, c.Time, c.Message)
}
func DeserializeCommit(s string) *Commit {
var user, hash, prevCommitHashConcat string
var objectCount int
var objectsString string
fmt.Sscanf(s, "%s\n%s\n%d\n%s\n%s\n", &user, &hash, &objectCount, &objectsString, &prevCommitHashConcat)
message := GetMessageFromCommitFile(hash)
time := GetTimeFromCommitFile(hash)
objects := strings.Split(objectsString, ",")
var objectsList []Object
for i := 0; i < objectCount; i++ {
objectsList = append(objectsList, *DeserializeObject(objects[i]))
}
return &Commit{user, hash, objectsList, GetMultipleCommits(prevCommitHashConcat), time, message}
}
func GetMultipleCommits(hashesConcat string) []*Commit {
var commits []*Commit
hashes := strings.Split(hashesConcat, ",")
for _, hash := range hashes {
commit := GetCommit(hash)
if commit != nil {
commits = append(commits, commit)
}
}
return commits
}
func GetCommit(hash string) *Commit {
if hash == "" {
return nil
}
f, err := os.OpenFile(".gogit/commits/"+hash, os.O_RDONLY, 0644)
if err != nil {
panic(err)
}
defer f.Close()
commitString := FileToString(f)
return DeserializeCommit(commitString)
}
func SaveCommit(c *Commit) {
err := os.MkdirAll(".gogit/commits", 0755)
if err != nil {
panic(err)
}
f, err := os.OpenFile(".gogit/commits/"+c.Hash, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
defer f.Close()
_, err = fmt.Fprintln(f, c.Serialize())
if err != nil {
panic(err)
}
}
func GetSnapshot() []Object {
var objects []Object
err := filepath.Walk(".",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.Name() == ".gogit" {
return filepath.SkipDir
}
if !info.IsDir() {
f, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
panic(err)
}
defer f.Close()
hash := HashFile(f)
CopyFile(path, ".gogit/objects/"+hash)
objects = append(objects, Object{hash, path})
}
return nil
})
if err != nil {
log.Println(err)
}
return objects
}
func HashObjects(objects []Object, time int64) string {
var hash string
for _, object := range objects {
hash += object.Hash
}
return HashString(hash, time)
}
func CreateCommit(user string, message string, parentCommits []*Commit) *Commit {
objects := GetSnapshot()
//check if the commit is the same as the previous one
if len(parentCommits) == 1 {
same := true
currSnap := map[string]string{} // relative path -> hash
for _, object := range objects {
currSnap[object.RelativePath] = object.Hash
}
if len(objects) != len(parentCommits[0].Objects) {
same = false
}
for _, object := range parentCommits[0].Objects {
if currSnap[object.RelativePath] != object.Hash {
same = false
}
}
if same {
fmt.Println("Nothing to commit")
return nil
}
}
currTime := time.Now().Unix()
commit := Commit{
User: user,
Hash: HashObjects(objects, currTime),
Objects: objects,
PrevCommits: parentCommits,
Time: currTime,
Message: message,
}
SaveCommit(&commit)
return &commit
}
func ApplyCommit(c *Commit) {
err := filepath.Walk(".", func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() && info.Name() == ".gogit" {
return filepath.SkipDir
}
if !info.IsDir() {
err = os.Remove(path)
if err != nil {
panic(err)
}
}
return nil
})
if err != nil {
panic(err)
}
for _, object := range c.Objects {
CopyFile(".gogit/objects/"+object.Hash, object.RelativePath)
}
}
func (c *Commit) LogCommit() {
fmt.Printf("Commit: %s\n", c.Hash)
fmt.Printf("Author: %s\n", c.User)
fmt.Printf("Date: %s\n", time.Unix(c.Time, 0))
fmt.Printf("Message: %s\n\n", c.Message)
}
func (c *Commit) DeleteCommit() {
err := os.Remove(".gogit/commits/" + c.Hash)
if err != nil {
panic(err)
}
// TODO: delete objects
}
func GetHead() string {
head, err := os.OpenFile(".gogit/HEAD", os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
panic(err)
}
defer head.Close()
commitHash := FileToString(head)
return commitHash
}