Skip to content

Commit

Permalink
Adding in support for corectly processing git.
Browse files Browse the repository at this point in the history
after using this as a library, it needed some tweaking.

Signed-off-by: Dave Shanley <[email protected]>
  • Loading branch information
daveshanley committed Feb 11, 2023
1 parent 141cd67 commit 509292f
Show file tree
Hide file tree
Showing 9 changed files with 1,250 additions and 1,255 deletions.
480 changes: 240 additions & 240 deletions cmd/console.go

Large diffs are not rendered by default.

582 changes: 291 additions & 291 deletions cmd/html_report.go

Large diffs are not rendered by default.

546 changes: 273 additions & 273 deletions cmd/report.go

Large diffs are not rendered by default.

550 changes: 275 additions & 275 deletions cmd/summary.go

Large diffs are not rendered by default.

324 changes: 158 additions & 166 deletions git/read_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,190 +4,182 @@
package git

import (
"bytes"
"fmt"
"github.com/araddon/dateparse"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/resolver"
"github.com/pb33f/openapi-changes/model"
"os/exec"
"path"
"strings"
"bytes"
"fmt"
"github.com/araddon/dateparse"
"github.com/pb33f/libopenapi"
"github.com/pb33f/openapi-changes/model"
"os/exec"
"path"
"strings"
)

const (
GIT = "git"
LOG = "log"
SHOW = "show"
REVPARSE = "rev-parse"
TOPLEVEL = "--show-toplevel"
NOPAGER = "--no-pager"
LOGFORMAT = "--pretty=%cD||%h||%s||%an||%ae"
DIV = "--"
GIT = "git"
LOG = "log"
SHOW = "show"
REVPARSE = "rev-parse"
TOPLEVEL = "--show-toplevel"
NOPAGER = "--no-pager"
LOGFORMAT = "--pretty=%cD||%h||%s||%an||%ae"
DIV = "--"
)

func CheckLocalRepoAvailable(dir string) bool {
cmd := exec.Command(GIT, LOG)
cmd.Dir = dir
err := cmd.Run()
if err != nil {
return false
}
return true
cmd := exec.Command(GIT, LOG)
cmd.Dir = dir
err := cmd.Run()
if err != nil {
return false
}
return true
}

func GetTopLevel(dir string) (string, error) {
cmd := exec.Command(GIT, REVPARSE, TOPLEVEL)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = dir
err := cmd.Run()
if err != nil {
return "", err
}
outStr, _ := string(stdout.Bytes()), string(stderr.Bytes())
return outStr, nil
cmd := exec.Command(GIT, REVPARSE, TOPLEVEL)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = dir
err := cmd.Run()
if err != nil {
return "", err
}
outStr, _ := string(stdout.Bytes()), string(stderr.Bytes())
return outStr, nil
}

func ExtractHistoryFromFile(repoDirectory, filePath string,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError) ([]*model.Commit, []error) {

cmd := exec.Command(GIT, NOPAGER, LOG, LOGFORMAT, DIV, filePath)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = repoDirectory
err := cmd.Run()
var commitHistory []*model.Commit
if err != nil {
model.SendProgressError("git", err.Error(), errorChan)
return nil, []error{err}
}

outStr, _ := string(stdout.Bytes()), string(stderr.Bytes())
lines := strings.Split(outStr, "\n")
for k := range lines {
c := strings.Split(lines[k], "||")
if len(c) == 5 {
date, _ := dateparse.ParseAny(c[0])
commitHistory = append(commitHistory,
&model.Commit{
CommitDate: date,
Hash: c[1],
Message: c[2],
Author: c[3],
AuthorEmail: c[4],
RepoDirectory: repoDirectory,
FilePath: filePath,
})
model.SendProgressUpdate(c[1],
fmt.Sprintf("extacted commit '%s'", c[1]), false, progressChan)
}
}
model.SendProgressUpdate("extraction",
fmt.Sprintf("%d commits extracted", len(commitHistory)), true, progressChan)
return commitHistory, nil
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError) ([]*model.Commit, []error) {

cmd := exec.Command(GIT, NOPAGER, LOG, LOGFORMAT, DIV, filePath)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
cmd.Dir = repoDirectory
err := cmd.Run()
var commitHistory []*model.Commit
if err != nil {
model.SendProgressError("git", err.Error(), errorChan)
return nil, []error{err}
}

outStr, _ := string(stdout.Bytes()), string(stderr.Bytes())
lines := strings.Split(outStr, "\n")
for k := range lines {
c := strings.Split(lines[k], "||")
if len(c) == 5 {
date, _ := dateparse.ParseAny(c[0])
commitHistory = append(commitHistory,
&model.Commit{
CommitDate: date,
Hash: c[1],
Message: c[2],
Author: c[3],
AuthorEmail: c[4],
RepoDirectory: repoDirectory,
FilePath: filePath,
})
model.SendProgressUpdate(c[1],
fmt.Sprintf("extacted commit '%s'", c[1]), false, progressChan)
}
}
model.SendProgressUpdate("extraction",
fmt.Sprintf("%d commits extracted", len(commitHistory)), true, progressChan)
return commitHistory, nil
}

func PopulateHistoryWithChanges(commitHistory []*model.Commit,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError) ([]*model.Commit, []error) {

for c := range commitHistory {
cmd := exec.Command(GIT, NOPAGER, SHOW, fmt.Sprintf("%s:%s", commitHistory[c].Hash, commitHistory[c].FilePath))
var ou, er bytes.Buffer
cmd.Stdout = &ou
cmd.Stderr = &er
cmd.Dir = commitHistory[c].RepoDirectory
err := cmd.Run()
if err != nil {
return nil, []error{err}
}
commitHistory[c].Data = ou.Bytes()
model.SendProgressUpdate("population",
fmt.Sprintf("%d bytes extracted from commit '%s'",
len(commitHistory[c].Data), commitHistory[c].Hash), false, progressChan)

}
cleaned, errors := BuildCommitChangelog(commitHistory)
if len(errors) > 0 {
model.SendProgressError("git",
fmt.Sprintf("%d error(s) found building commit change log", len(errors)), errorChan)

return nil, errors
}
model.SendProgressUpdate("populated",
fmt.Sprintf("%d commits processed and populated", len(cleaned)), true, progressChan)
return cleaned, nil
func PopulateHistoryWithChanges(commitHistory []*model.Commit, limit int,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError) ([]*model.Commit, []error) {

for c := range commitHistory {
cmd := exec.Command(GIT, NOPAGER, SHOW, fmt.Sprintf("%s:%s", commitHistory[c].Hash, commitHistory[c].FilePath))
var ou, er bytes.Buffer
cmd.Stdout = &ou
cmd.Stderr = &er
cmd.Dir = commitHistory[c].RepoDirectory
err := cmd.Run()
if err != nil {
return nil, []error{err}
}
commitHistory[c].Data = ou.Bytes()
model.SendProgressUpdate("population",
fmt.Sprintf("%d bytes extracted from commit '%s'",
len(commitHistory[c].Data), commitHistory[c].Hash), false, progressChan)

}

if limit > 0 && limit+1 < len(commitHistory) {
commitHistory = commitHistory[0 : limit+1]
}

cleaned, errors := BuildCommitChangelog(commitHistory)
if len(errors) > 0 {
model.SendProgressError("git",
fmt.Sprintf("%d error(s) found building commit change log", len(errors)), errorChan)

return cleaned, errors
}
model.SendProgressUpdate("populated",
fmt.Sprintf("%d commits processed and populated", len(cleaned)), true, progressChan)
return cleaned, nil
}

func BuildCommitChangelog(commitHistory []*model.Commit) ([]*model.Commit, []error) {
var errors []error
var cleaned []*model.Commit

for c := len(commitHistory) - 1; c > -1; c-- {
var oldBits, newBits []byte
if len(commitHistory) == c+1 {
newBits = commitHistory[c].Data
} else {
oldBits = commitHistory[c+1].Data
commitHistory[c].OldData = oldBits
newBits = commitHistory[c].Data
}

var oldDoc, newDoc libopenapi.Document

var err error
if len(oldBits) > 0 && len(newBits) > 0 {
oldDoc, err = libopenapi.NewDocument(oldBits)
if err != nil {
errors = append(errors, err)
}
newDoc, err = libopenapi.NewDocument(newBits)
if err != nil {
errors = append(errors, err)
}
if len(errors) > 0 {
for x := range errors {
if _, ok := errors[x].(*resolver.ResolvingError); !ok {
return nil, errors
}
}
}
changes, errs := libopenapi.CompareDocuments(oldDoc, newDoc)
if errs != nil {
errors = append(errors, errs...)
}
if len(errors) > 0 {
for x := range errors {
if _, ok := errors[x].(*resolver.ResolvingError); !ok {
return nil, errors
}
}
}
commitHistory[c].Changes = changes
}
if len(oldBits) == 0 && len(newBits) > 0 {
newDoc, _ = libopenapi.NewDocument(newBits)
}
if newDoc != nil {
commitHistory[c].Document = newDoc
}
if oldDoc != nil {
commitHistory[c].OldDocument = oldDoc
}
if (c == len(commitHistory)-1) || commitHistory[c].Changes != nil {
cleaned = append(cleaned, commitHistory[c])
}
}
for i, j := 0, len(cleaned)-1; i < j; i, j = i+1, j-1 {
cleaned[i], cleaned[j] = cleaned[j], cleaned[i]
}
return cleaned, nil
var errors []error
var cleaned []*model.Commit

for c := len(commitHistory) - 1; c > -1; c-- {
var oldBits, newBits []byte
if len(commitHistory) == c+1 {
newBits = commitHistory[c].Data
} else {
oldBits = commitHistory[c+1].Data
commitHistory[c].OldData = oldBits
newBits = commitHistory[c].Data
}

var oldDoc, newDoc libopenapi.Document

var err error
if len(oldBits) > 0 && len(newBits) > 0 {
oldDoc, err = libopenapi.NewDocument(oldBits)
if err != nil {
errors = append(errors, err)
}
newDoc, err = libopenapi.NewDocument(newBits)
if err != nil {
errors = append(errors, err)
}

changes, errs := libopenapi.CompareDocuments(oldDoc, newDoc)
if errs != nil {
errors = append(errors, errs...)
}
commitHistory[c].Changes = changes
}
if len(oldBits) == 0 && len(newBits) > 0 {
newDoc, _ = libopenapi.NewDocument(newBits)
}
if newDoc != nil {
commitHistory[c].Document = newDoc
}
if oldDoc != nil {
commitHistory[c].OldDocument = oldDoc
}
if (c == len(commitHistory)-1) || commitHistory[c].Changes != nil {
cleaned = append(cleaned, commitHistory[c])
}
}
for i, j := 0, len(cleaned)-1; i < j; i, j = i+1, j-1 {
cleaned[i], cleaned[j] = cleaned[j], cleaned[i]
}
fmt.Println(cleaned)
return cleaned, errors
}

func ExtractPathAndFile(location string) (string, string) {
dir := path.Dir(location)
file := path.Base(location)
return dir, file
dir := path.Dir(location)
file := path.Base(location)
return dir, file
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module github.com/pb33f/openapi-changes
go 1.18

require (
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/pb33f/libopenapi v0.4.16
github.com/pb33f/libopenapi v0.5.1
github.com/pterm/pterm v0.12.51
github.com/rivo/tview v0.0.0-20221117065207-09f052e6ca98
github.com/spf13/cobra v1.6.1
Expand All @@ -16,7 +17,6 @@ require (
require (
atomicgo.dev/cursor v0.1.1 // indirect
atomicgo.dev/keyboard v0.2.8 // indirect
github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/pb33f/libopenapi v0.4.16 h1:1bgiVtM3ZE4XBsqd4ASu9AUfVN531jsTUO944Cmh5TU=
github.com/pb33f/libopenapi v0.4.16/go.mod h1:UcUNPQcwq4ojgQgthV+zbeUs25lhDlD4bM9Da8n2vdU=
github.com/pb33f/libopenapi v0.5.1 h1:nJI91EavQzhminjqaM7Vy2icsLP8F/Wmwxn/IJN//Ls=
github.com/pb33f/libopenapi v0.5.1/go.mod h1:UcUNPQcwq4ojgQgthV+zbeUs25lhDlD4bM9Da8n2vdU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pterm/pterm v0.12.27/go.mod h1:PhQ89w4i95rhgE+xedAoqous6K9X+r6aSOI2eFF7DZI=
Expand Down
5 changes: 4 additions & 1 deletion html-report/templates/header.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@
</style>
{{- if .UseCDN }}
<link rel="stylesheet" href="{{- .CssCDN -}}"/>
<script src="{{- .JsCDN -}}" async></script>
{{- end }}
<script>
let rawData = {{ .Report }}
window.data = JSON.parse(JSON.stringify(rawData));
</script>
</head>
{{ end }}
Loading

0 comments on commit 509292f

Please sign in to comment.