1
1
// Copyright 2015 The Gogs Authors. All rights reserved.
2
+ // Copyright 2018 The Gitea Authors. All rights reserved.
2
3
// Use of this source code is governed by a MIT-style
3
4
// license that can be found in the LICENSE file.
4
5
@@ -9,6 +10,7 @@ import (
9
10
"bytes"
10
11
"container/list"
11
12
"fmt"
13
+ "io"
12
14
"net/http"
13
15
"strconv"
14
16
"strings"
@@ -279,6 +281,56 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
279
281
return nil , nil
280
282
}
281
283
284
+ // CommitFileStatus represents status of files in a commit.
285
+ type CommitFileStatus struct {
286
+ Added []string
287
+ Removed []string
288
+ Modified []string
289
+ }
290
+
291
+ // NewCommitFileStatus creates a CommitFileStatus
292
+ func NewCommitFileStatus () * CommitFileStatus {
293
+ return & CommitFileStatus {
294
+ []string {}, []string {}, []string {},
295
+ }
296
+ }
297
+
298
+ // GetCommitFileStatus returns file status of commit in given repository.
299
+ func GetCommitFileStatus (repoPath , commitID string ) (* CommitFileStatus , error ) {
300
+ stdout , w := io .Pipe ()
301
+ done := make (chan struct {})
302
+ fileStatus := NewCommitFileStatus ()
303
+ go func () {
304
+ scanner := bufio .NewScanner (stdout )
305
+ for scanner .Scan () {
306
+ fields := strings .Fields (scanner .Text ())
307
+ if len (fields ) < 2 {
308
+ continue
309
+ }
310
+
311
+ switch fields [0 ][0 ] {
312
+ case 'A' :
313
+ fileStatus .Added = append (fileStatus .Added , fields [1 ])
314
+ case 'D' :
315
+ fileStatus .Removed = append (fileStatus .Removed , fields [1 ])
316
+ case 'M' :
317
+ fileStatus .Modified = append (fileStatus .Modified , fields [1 ])
318
+ }
319
+ }
320
+ done <- struct {}{}
321
+ }()
322
+
323
+ stderr := new (bytes.Buffer )
324
+ err := NewCommand ("show" , "--name-status" , "--pretty=format:''" , commitID ).RunInDirPipeline (repoPath , w , stderr )
325
+ w .Close () // Close writer to exit parsing goroutine
326
+ if err != nil {
327
+ return nil , concatenateError (err , stderr .String ())
328
+ }
329
+
330
+ <- done
331
+ return fileStatus , nil
332
+ }
333
+
282
334
// GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository.
283
335
func GetFullCommitID (repoPath , shortID string ) (string , error ) {
284
336
if len (shortID ) >= 40 {
0 commit comments