diff options
author | Sunny <me@darkowlzz.space> | 2017-10-12 00:31:16 +0530 |
---|---|---|
committer | Sunny <me@darkowlzz.space> | 2017-11-02 18:50:20 +0530 |
commit | 11162e11c830fec51517d86835a884a5547a4f16 (patch) | |
tree | deebf535929ed9d2fe3c14d425d4dbb0d6e66c0e /plumbing/object/commit.go | |
parent | 69b72d30b125b07f69f2b1f53c44b273d7cb4da9 (diff) | |
download | go-git-11162e11c830fec51517d86835a884a5547a4f16.tar.gz |
Add Stats() to Patch and fix diffstat output
Diffstat (limited to 'plumbing/object/commit.go')
-rw-r--r-- | plumbing/object/commit.go | 64 |
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 { |