diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2019-02-03 11:35:17 +0800 |
---|---|---|
committer | techknowlogick <matti@mdranta.net> | 2019-02-02 22:35:17 -0500 |
commit | ecefa9e724460deb70b97dd7c52fc8f4db94be93 (patch) | |
tree | 0d2ea72f0a682b7ab775879b5737782e4c000e69 /vendor/code.gitea.io/git/commit.go | |
parent | 3d91bb2f2dc8584b76a49a1a40c3f688c21380f5 (diff) | |
download | gitea-ecefa9e724460deb70b97dd7c52fc8f4db94be93.tar.gz gitea-ecefa9e724460deb70b97dd7c52fc8f4db94be93.zip |
Add single commit API support (#5843)
* add single commit API support
Diffstat (limited to 'vendor/code.gitea.io/git/commit.go')
-rw-r--r-- | vendor/code.gitea.io/git/commit.go | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/vendor/code.gitea.io/git/commit.go b/vendor/code.gitea.io/git/commit.go index 5e8c91d303..227df09b7d 100644 --- a/vendor/code.gitea.io/git/commit.go +++ b/vendor/code.gitea.io/git/commit.go @@ -1,4 +1,5 @@ // Copyright 2015 The Gogs Authors. All rights reserved. +// Copyright 2018 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. @@ -9,6 +10,7 @@ import ( "bytes" "container/list" "fmt" + "io" "net/http" "strconv" "strings" @@ -279,6 +281,56 @@ func (c *Commit) GetSubModule(entryname string) (*SubModule, error) { return nil, nil } +// CommitFileStatus represents status of files in a commit. +type CommitFileStatus struct { + Added []string + Removed []string + Modified []string +} + +// NewCommitFileStatus creates a CommitFileStatus +func NewCommitFileStatus() *CommitFileStatus { + return &CommitFileStatus{ + []string{}, []string{}, []string{}, + } +} + +// GetCommitFileStatus returns file status of commit in given repository. +func GetCommitFileStatus(repoPath, commitID string) (*CommitFileStatus, error) { + stdout, w := io.Pipe() + done := make(chan struct{}) + fileStatus := NewCommitFileStatus() + go func() { + scanner := bufio.NewScanner(stdout) + for scanner.Scan() { + fields := strings.Fields(scanner.Text()) + if len(fields) < 2 { + continue + } + + switch fields[0][0] { + case 'A': + fileStatus.Added = append(fileStatus.Added, fields[1]) + case 'D': + fileStatus.Removed = append(fileStatus.Removed, fields[1]) + case 'M': + fileStatus.Modified = append(fileStatus.Modified, fields[1]) + } + } + done <- struct{}{} + }() + + stderr := new(bytes.Buffer) + err := NewCommand("show", "--name-status", "--pretty=format:''", commitID).RunInDirPipeline(repoPath, w, stderr) + w.Close() // Close writer to exit parsing goroutine + if err != nil { + return nil, concatenateError(err, stderr.String()) + } + + <-done + return fileStatus, nil +} + // GetFullCommitID returns full length (40) of commit ID by given short SHA in a repository. func GetFullCommitID(repoPath, shortID string) (string, error) { if len(shortID) >= 40 { |