Repository navigation
Expand file tree
/
Copy pathcommit.go
More file actions
89 lines (75 loc) · 2.41 KB
/
Copy pathcommit.go
File metadata and controls
89 lines (75 loc) · 2.41 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
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
package src
import "bytes"
// GitCommit ...
type GitCommit struct {
Tree []byte
Parent []byte
Author string
Committer string
Message string
}
// Write - Writes a new Git Commit Object to the filesystem
// We are ignoring ---- PGP SIGNATURE ----
// It's just Tree, Parent (Optional), Author and Commiter (just Default), Message (Optional)
func (commit GitCommit) Write(gitdir string) (string, error) {
// Raw commit data (empty)
raw := []byte{}
// Append tree to record
raw = bytes.Join([][]byte{raw, []byte("tree"), []byte(" "), []byte(commit.Tree), []byte("\n")}, []byte(""))
// Append Parent Record
if len(commit.Parent) > 0 {
raw = bytes.Join([][]byte{raw, []byte("parent"), []byte(" "), []byte(commit.Parent), []byte("\n")}, []byte(""))
}
// Append Author and Commit respectively
raw = bytes.Join([][]byte{raw, []byte("author"), []byte(" "), []byte(commit.Author), []byte("\n")}, []byte(""))
raw = bytes.Join([][]byte{raw, []byte("committer"), []byte(" "), []byte(commit.Committer), []byte("\n")}, []byte(""))
// Append Message to the raw data
if commit.Message != "" {
raw = bytes.Join([][]byte{raw, []byte(commit.Message), []byte("\n")}, []byte(""))
}
// Writetable Object from commit (GitCOmmit)
wObj := GitObject{Kind: "commit", Data: raw}
// Writing the Object
shaStr, err := wObj.Write("git")
return shaStr, err
}
// ParseCommit - Parses GitObject.Data and Return GitCommit Object
func ParseCommit(commitdata []byte) GitCommit {
// Initializing Commit Object
var commit = GitCommit{}
// Split each record (Split by []byte(\n))
entry := bytes.Split(commitdata, []byte("\n"))
// Itetate over the data
for i, chunk := range entry {
// If not Empty line (len(chunk) > 1)
if len(chunk) > 1 {
// Index of space (byte(' '))
sp := bytes.IndexByte(chunk, byte(' '))
key := chunk[:sp] // Key of record
value := chunk[sp+1:] // Value
// Check Tree Type (So Order and Presence Doesn't Matter)
switch string(key) {
case "tree":
commit.Tree = value
continue
case "parent":
commit.Parent = value
continue
case "author":
commit.Author = string(value)
continue
case "committer":
commit.Committer = string(value)
continue
}
// If First 4 main lines are done, then Message there
// Assuming that PGP SIGNATURE doesn't exist
if i > 3 {
commit.Message = string(chunk)
continue
}
}
}
// Return GitCommit
return commit
}