Skip to content
This repository was archived by the owner on Apr 12, 2019. It is now read-only.

Commit b83e0a0

Browse files
lunnytechknowlogick
authored andcommitted
Added commit file status, port from gogs (#137)
1 parent 389d3c8 commit b83e0a0

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

commit.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015 The Gogs Authors. All rights reserved.
2+
// Copyright 2018 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

@@ -9,6 +10,7 @@ import (
910
"bytes"
1011
"container/list"
1112
"fmt"
13+
"io"
1214
"net/http"
1315
"strconv"
1416
"strings"
@@ -279,6 +281,56 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) {
279281
return nil, nil
280282
}
281283

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+
282334
// GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository.
283335
func GetFullCommitID(repoPath, shortID string) (string, error) {
284336
if len(shortID) >= 40 {

0 commit comments

Comments
 (0)