-
Notifications
You must be signed in to change notification settings - Fork 0
/
commit.go
139 lines (113 loc) · 3.35 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
package porcelain
import (
"fmt"
"io"
"path/filepath"
"github.com/izhujiang/gogit/common"
"github.com/izhujiang/gogit/core"
"github.com/izhujiang/gogit/core/object"
"github.com/izhujiang/gogit/plumbing"
)
type CommitOption struct {
Message string
}
func Commit(w io.Writer, option *CommitOption) error {
// TODO: do nothing if nothing new in the StagingArea
// if (nothing new){
// return nil
// }
wto := &plumbing.WriteTreeOption{}
treeId, err := plumbing.WriteTree(wto)
if err != nil { // fail to WriteTree, including trees in cache are already valid
// TODO: promote as git status
fmt.Println(err, treeId)
return err
} else {
refs := core.GetReferencs()
lastCommitId, err := refs.LastCommit()
parents := make([]common.Hash, 0)
if err == nil {
parents = append(parents, lastCommitId)
}
// commit changes into repository
ctOption := &plumbing.CommitTreeOption{
Parents: parents,
Message: option.Message,
}
commitId, err := plumbing.CommitTree(treeId, ctOption)
if err == nil {
// save commit id to ref/head/{branch}
ref := core.GetReferencs()
err = ref.SaveCommit(commitId)
headMsg := fmt.Sprintf("[%s %s] %s\n", refs.Head(), commitId, ctOption.Message)
w.Write([]byte(headMsg))
}
// TODO: list all commited files
repo := core.GetRepository()
g, _ := repo.Get(lastCommitId)
var changes *common.Changes
if g != nil {
lastCommit := object.GitObjectToCommit(g)
lastTreeId := lastCommit.Tree()
changes, err = compareTrees(lastTreeId, treeId)
} else {
changes, err = compareTrees(common.ZeroHash, treeId)
}
for _, c := range changes.Create {
line := fmt.Sprintf("create mode %s %s\n", c.To.Mode, c.To.Name)
w.Write([]byte(line))
// TODO: stat lines inserted
}
for _, c := range changes.Remove {
line := fmt.Sprintf("delete mode %s %s\n", c.From.Mode, c.From.Name)
w.Write([]byte(line))
// TODO: stat lines deleted
}
// TODO: stat lines changed
// for _, c := range changes.Modify {
// line := fmt.Sprintf("delete mode %s %s\n", c.From.Mode, c.From.Name)
// w.Write([]byte(line))
// }
}
return err
}
func compareTrees(lastTreeId common.Hash, thisTreeId common.Hash) (*common.Changes, error) {
repo := core.GetRepository()
thisTree, err := repo.LoadTrees(thisTreeId)
if err != nil {
return nil, err
}
thisTrees := object.NewTreeFs(thisTree)
thisTreeCollector := &filesCollector{}
thisTrees.DFWalk(thisTreeCollector.collect, true)
lastTree, err := repo.LoadTrees(lastTreeId)
var changes *common.Changes
if err != nil || lastTree == nil {
lastTreePairs := common.NameHashPairs(make([]*common.NameHashPair, 0))
changes = common.CompareOrderedNameHashPairs(lastTreePairs, thisTreeCollector.pairs)
} else {
lastTrees := object.NewTreeFs(lastTree)
lastTreeCollector := &filesCollector{}
lastTrees.DFWalk(lastTreeCollector.collect, true)
changes = common.CompareOrderedNameHashPairs(lastTreeCollector.pairs, thisTreeCollector.pairs)
}
return changes, nil
}
type filesCollector struct {
pairs common.NameHashPairs
}
func (fc *filesCollector) collect(path string, t *object.Tree) error {
t.ForEach(func(e *object.TreeEntry) error {
if e.Kind == object.Kind_Blob {
fp := filepath.Join(path, e.Name)
p := &common.NameHashPair{
Name: fp,
Oid: e.Oid,
Mode: e.Filemode,
}
fc.pairs = append(fc.pairs, p)
}
return nil
})
return nil
}