-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommit.go
310 lines (261 loc) · 6.76 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package git
import (
"bufio"
"container/list"
"net/http"
"strings"
"time"
"github.com/mechmind/git-go/history"
"github.com/mechmind/git-go/rawgit"
)
type Commit struct {
Tree
ID sha1 // The ID of this commit object
Author *Signature
Committer *Signature
CommitMessage string
//parents []sha1 // SHA1 strings
submoduleCache *objectCache
rawCommit *rawgit.Commit
}
type Signature struct {
Email string
Name string
When time.Time
}
func raw2commit(repo *Repository, raw *rawgit.Commit) (*Commit, error) {
commit := &Commit{
ID: sha1(*raw.GetOID()),
Author: raw2signature(raw.Author),
Committer: raw2signature(raw.Committer),
CommitMessage: raw.Message,
rawCommit: raw,
}
return commit, nil
}
func raw2signature(raw rawgit.UserTime) *Signature {
return &Signature{
Name: raw.Name,
Email: raw.Email,
When: raw.Time,
}
}
// Message returns the commit message. Same as retrieving CommitMessage directly.
func (c *Commit) Message() string {
return c.CommitMessage
}
// Summary returns first line of commit message.
func (c *Commit) Summary() string {
return strings.Split(c.CommitMessage, "\n")[0]
}
// ParentID returns oid of n-th parent (0-based index).
// It returns nil if no such parent exists.
func (c *Commit) ParentID(n int) (sha1, error) {
if n >= len(c.rawCommit.ParentOIDs) {
return sha1{}, ErrNotExist{"", ""}
}
return sha1(*c.rawCommit.ParentOIDs[n]), nil
}
// Parent returns n-th parent (0-based index) of the commit.
func (c *Commit) Parent(n int) (*Commit, error) {
id, err := c.ParentID(n)
if err != nil {
return nil, err
}
parent, err := c.repo.getCommit(id)
if err != nil {
return nil, err
}
return parent, nil
}
// ParentCount returns number of parents of the commit.
// 0 if this is the root commit, otherwise 1,2, etc.
func (c *Commit) ParentCount() int {
return len(c.rawCommit.ParentOIDs)
}
func isImageFile(data []byte) (string, bool) {
contentType := http.DetectContentType(data)
if strings.Index(contentType, "image/") != -1 {
return contentType, true
}
return contentType, false
}
func (c *Commit) IsImageFile(name string) bool {
blob, err := c.GetBlobByPath(name)
if err != nil {
return false
}
dataRc, err := blob.Data()
if err != nil {
return false
}
buf := make([]byte, 1024)
n, _ := dataRc.Read(buf)
if n > 0 {
buf = buf[:n]
}
_, isImage := isImageFile(buf)
return isImage
}
// GetCommitByPath return the commit of relative path object.
func (c *Commit) GetCommitByPath(relpath string) (*Commit, error) {
pathCb := history.MakePathChecker(c.repo.repo, relpath)
pager := history.MakePager(c.repo.repo, pathCb, 0, 1)
cmp := history.MakePathComparator(c.repo.repo, relpath)
hist := history.New(c.repo.repo)
result, err := hist.WalkFilteredHistory(sha2oidp(c.ID), pager, cmp)
if err != nil {
return nil, err
}
if result.Len() == 0 {
// no entries
return nil, nil
}
return raw2commit(c.repo, result.Front().Value.(*rawgit.Commit))
}
// AddAllChanges marks local changes to be ready for commit.
func AddChanges(repoPath string, all bool, files ...string) error {
/*
cmd := NewCommand("add")
if all {
cmd.AddArguments("--all")
}
_, err := cmd.AddArguments(files...).RunInDir(repoPath)
*/
// FIXME
panic("not implemented")
return nil
}
func CommitChanges(repoPath, message string, author *Signature) error {
/*
cmd := NewCommand("commit", "-m", message)
if author != nil {
cmd.AddArguments(fmt.Sprintf("--author='%s <%s>'", author.Name, author.Email))
}
_, err := cmd.RunInDir(repoPath)
// No stderr but exit status 1 means nothing to commit.
if err != nil && err.Error() == "exit status 1" {
return nil
}
*/
// FIXME
panic("not implemented")
return nil
}
// CommitsCount returns number of total commits of until given revision.
func CommitsCount(repoPath, revision string) (int64, error) {
repo, err := OpenRepository(repoPath)
if err != nil {
return 0, err
}
commit, err := repo.GetCommit(revision)
if err != nil {
return 0, err
}
return commit.CommitsCount()
}
func (c *Commit) CommitsCount() (int64, error) {
counter, result := history.MakeCounter(nil)
hist := history.New(c.repo.repo)
_, err := hist.WalkHistory(sha2oidp(c.ID), counter)
if err != nil {
return 0, err
}
return int64(result()), nil
}
var CommitsRangeSize = 50
func (c *Commit) CommitsByRange(page int) (*list.List, error) {
pager := history.MakePager(c.repo.repo, nil, (page-1)*CommitsRangeSize, CommitsRangeSize)
hist := history.New(c.repo.repo)
result, err := hist.WalkHistory(sha2oidp(c.ID), pager)
if err != nil {
return nil, err
}
return c.repo.raw2commitList(result)
}
func (c *Commit) CommitsBefore() (*list.List, error) {
hist := history.New(c.repo.repo)
result, err := hist.WalkHistory(sha2oidp(c.ID), nil)
if err != nil {
return nil, err
}
return c.repo.raw2commitList(result)
}
func (c *Commit) CommitsBeforeLimit(num int) (*list.List, error) {
pager := history.MakePager(c.repo.repo, nil, 0, num)
hist := history.New(c.repo.repo)
result, err := hist.WalkHistory(sha2oidp(c.ID), pager)
if err != nil {
return nil, err
}
return c.repo.raw2commitList(result)
}
func (c *Commit) CommitsBeforeUntil(commitID string) (*list.List, error) {
/*
endCommit, err := c.repo.GetCommit(commitID)
if err != nil {
return nil, err
}
return c.repo.CommitsBetween(c, endCommit)
*/
// FIXME
panic("not implemented!")
return nil, nil
}
func (c *Commit) SearchCommits(keyword string) (*list.List, error) {
searcher, err := history.MakeHistorySearcher(keyword)
if err != nil {
return nil, err
}
hist := history.New(c.repo.repo)
result, err := hist.WalkHistory(sha2oidp(c.ID), searcher)
if err != nil {
return nil, err
}
return c.repo.raw2commitList(result)
}
func (c *Commit) GetSubModules() (*objectCache, error) {
if c.submoduleCache != nil {
return c.submoduleCache, nil
}
entry, err := c.GetTreeEntryByPath(".gitmodules")
if err != nil {
return nil, err
}
rd, err := entry.Blob().Data()
if err != nil {
return nil, err
}
scanner := bufio.NewScanner(rd)
c.submoduleCache = newObjectCache()
var ismodule bool
var path string
for scanner.Scan() {
if strings.HasPrefix(scanner.Text(), "[submodule") {
ismodule = true
continue
}
if ismodule {
fields := strings.Split(scanner.Text(), "=")
k := strings.TrimSpace(fields[0])
if k == "path" {
path = strings.TrimSpace(fields[1])
} else if k == "url" {
c.submoduleCache.Set(path, &SubModule{path, strings.TrimSpace(fields[1])})
ismodule = false
}
}
}
return c.submoduleCache, nil
}
func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
modules, err := c.GetSubModules()
if err != nil {
return nil, err
}
module, has := modules.Get(entryname)
if has {
return module.(*SubModule), nil
}
return nil, nil
}