5
5
package git
6
6
7
7
import (
8
+ "bytes"
9
+ "io"
10
+ "io/ioutil"
8
11
"net/http"
9
12
"strings"
10
13
"sync"
@@ -103,9 +106,9 @@ func (c *Commit) CommitsCount(opts ...RevListCountOptions) (int64, error) {
103
106
return c .repo .RevListCount ([]string {c .id .String ()}, opts ... )
104
107
}
105
108
106
- // FilesChangedSince returns a list of files changed since given commit ID.
107
- func (c * Commit ) FilesChangedSince ( commitID string , opts ... DiffNameOnlyOptions ) ([]string , error ) {
108
- return c .repo .DiffNameOnly (commitID , c .id .String (), opts ... )
109
+ // FilesChangedSince returns a list of files changed after given commit ID.
110
+ func (c * Commit ) FilesChangedAfter ( after string , opts ... DiffNameOnlyOptions ) ([]string , error ) {
111
+ return c .repo .DiffNameOnly (after , c .id .String (), opts ... )
109
112
}
110
113
111
114
// CommitsAfter returns a list of commits after given commit ID up to this commit. The returned
@@ -116,28 +119,59 @@ func (c *Commit) CommitsAfter(after string, opts ...RevListOptions) ([]*Commit,
116
119
117
120
// Ancestors returns a list of ancestors of this commit in reverse chronological order.
118
121
func (c * Commit ) Ancestors (opts ... LogOptions ) ([]* Commit , error ) {
119
- return c .repo .Log (c .id .String (), opts ... )
122
+ if c .ParentsCount () == 0 {
123
+ return []* Commit {}, nil
124
+ }
125
+
126
+ var opt LogOptions
127
+ if len (opts ) > 0 {
128
+ opt = opts [0 ]
129
+ }
130
+
131
+ opt .Skip ++
132
+
133
+ return c .repo .Log (c .id .String (), opt )
134
+ }
135
+
136
+ type limitWriter struct {
137
+ W io.Writer
138
+ N int64
120
139
}
121
140
122
- func isImageFile (data []byte ) (string , bool ) {
123
- contentType := http .DetectContentType (data )
124
- if strings .Contains (contentType , "image/" ) {
125
- return contentType , true
141
+ func (w * limitWriter ) Write (p []byte ) (int , error ) {
142
+ if w .N <= 0 {
143
+ return len (p ), nil
126
144
}
127
- return contentType , false
145
+
146
+ limit := int64 (len (p ))
147
+ if limit > w .N {
148
+ limit = w .N
149
+ }
150
+ n , err := w .W .Write (p [:limit ])
151
+ w .N -= int64 (n )
152
+
153
+ // Prevent "short write" error
154
+ return len (p ), err
128
155
}
129
156
130
- // IsImageFile returns true if the commit is a image blob.
131
- func (c * Commit ) IsImageFile (name string ) bool {
157
+ // IsImageFile returns true if the commit is an image blob.
158
+ func (c * Commit ) IsImageFile (name string ) ( bool , error ) {
132
159
blob , err := c .Blob (name )
133
160
if err != nil {
134
- return false
161
+ return false , err
135
162
}
136
163
137
- p , err := blob .Bytes ()
164
+ buf := new (bytes.Buffer )
165
+ buf .Grow (512 )
166
+ stdout := & limitWriter {
167
+ W : buf ,
168
+ N : int64 (buf .Cap ()),
169
+ }
170
+
171
+ err = blob .Pipeline (stdout , ioutil .Discard )
138
172
if err != nil {
139
- return false
173
+ return false , err
140
174
}
141
- _ , isImage := isImageFile ( p )
142
- return isImage
175
+
176
+ return strings . Contains ( http . DetectContentType ( buf . Bytes ()), "image/" ), nil
143
177
}
0 commit comments