aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/object/commit.go
diff options
context:
space:
mode:
Diffstat (limited to 'plumbing/object/commit.go')
-rw-r--r--plumbing/object/commit.go64
1 files changed, 12 insertions, 52 deletions
diff --git a/plumbing/object/commit.go b/plumbing/object/commit.go
index e5faa38..dfbb0d5 100644
--- a/plumbing/object/commit.go
+++ b/plumbing/object/commit.go
@@ -265,32 +265,22 @@ func (b *Commit) Encode(o plumbing.EncodedObject) error {
return err
}
-// FileStat stores the status of changes in content of a file.
-type FileStat struct {
- Name string
- Addition int
- Deletion int
-}
-
-func (fs FileStat) String() string {
- totalChanges := fs.Addition + fs.Deletion
- adds := strings.Repeat("+", fs.Addition)
- dels := strings.Repeat("-", fs.Deletion)
- return fmt.Sprintf(" %s | %d %s%s", fs.Name, totalChanges, adds, dels)
-}
-
-// FileStats is a collection of FileStat.
-type FileStats []FileStat
-
-// Stats shows the status
+// Stats shows the status of commit.
func (c *Commit) Stats() (FileStats, error) {
- var fileStats FileStats
-
// Get the previous commit.
ci := c.Parents()
parentCommit, err := ci.Next()
if err != nil {
- return nil, err
+ if err == io.EOF {
+ emptyNoder := treeNoder{}
+ parentCommit = &Commit{
+ Hash: emptyNoder.hash,
+ // TreeHash: emptyNoder.parent.Hash,
+ s: c.s,
+ }
+ } else {
+ return nil, err
+ }
}
patch, err := parentCommit.Patch(c)
@@ -298,37 +288,7 @@ func (c *Commit) Stats() (FileStats, error) {
return nil, err
}
- filePatches := patch.FilePatches()
- for _, fp := range filePatches {
- cs := FileStat{}
- from, to := fp.Files()
- if from == nil {
- // New File is created.
- cs.Name = to.Path()
- } else if to == nil {
- // File is deleted.
- cs.Name = from.Path()
- } else if from.Path() != to.Path() {
- // File is renamed.
- cs.Name = fmt.Sprintf("%s => %s", from.Path(), to.Path())
- } else {
- // File is modified.
- cs.Name = from.Path()
- }
-
- for _, chunk := range fp.Chunks() {
- switch chunk.Type() {
- case 1:
- cs.Addition += strings.Count(chunk.Content(), "\n")
- case 2:
- cs.Deletion += strings.Count(chunk.Content(), "\n")
- }
- }
-
- fileStats = append(fileStats, cs)
- }
-
- return fileStats, nil
+ return getFileStatsFromFilePatches(patch.FilePatches()), nil
}
func (c *Commit) String() string {