Skip to content

Commit

Permalink
added limits for git functions and flattened reports
Browse files Browse the repository at this point in the history
The report command is now flat, and much easier to consume. Designed to meet the needs of extracto.

Signed-off-by: quobix <[email protected]>
  • Loading branch information
daveshanley committed Feb 21, 2024
1 parent 43f7f4b commit be2ee83
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 26 deletions.
6 changes: 5 additions & 1 deletion cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,17 @@ func GetConsoleCommand() *cobra.Command {

// boot.
app := tui.BuildApplication(commits, Version)
if app == nil {
return errors.New("console is unable to start")
}
if err := app.Run(); err != nil {
pterm.Error.Println("console is unable to start, are you running this inside a container?")
pterm.Error.Println("the console requires a terminal to run, it cannot run on a headless system.")
fmt.Println()
fmt.Println()
return err
}
return nil

} else {
go listenForUpdates(updateChan, errorChan)
Expand Down Expand Up @@ -310,7 +314,7 @@ func runGitHistoryConsole(gitPath, filePath string, latest bool, limit int,
filePath, gitPath), false, updateChan)

// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan)
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
if err != nil {
close(updateChan)
model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(err)), errorChan)
Expand Down
6 changes: 3 additions & 3 deletions cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func GetHTMLReportCommand() *cobra.Command {
go listenForUpdates(updateChan, errorChan)

report, _, er := RunGitHistoryHTMLReport(args[0], args[1], latestFlag, cdnFlag,
updateChan, errorChan, baseFlag, remoteFlag)
updateChan, errorChan, baseFlag, remoteFlag, limitFlag)
<-doneChan
if er != nil {
for x := range er {
Expand Down Expand Up @@ -302,7 +302,7 @@ func ExtractGithubDetailsFromURL(url *url.URL) (string, string, string, error) {
}

func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) ([]byte, []*model.Report, []error) {
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) ([]byte, []*model.Report, []error) {
if gitPath == "" || filePath == "" {
err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f")
model.SendProgressError("reading paths",
Expand All @@ -312,7 +312,7 @@ func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
}

// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, progressChan, errorChan)
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, progressChan, errorChan, limit)
if err != nil {
model.SendFatalError("extraction",
fmt.Sprintf("cannot extract history %s", errors.Join(err...)), errorChan)
Expand Down
20 changes: 12 additions & 8 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func GetReportCommand() *cobra.Command {
}
return er[0]
}

jsonBytes, _ := json.MarshalIndent(report, "", " ")
flat := FlattenHistoricalReport(report)
jsonBytes, _ := json.MarshalIndent(flat, "", " ")
fmt.Println(string(jsonBytes))
return nil
}
Expand Down Expand Up @@ -152,7 +152,8 @@ func GetReportCommand() *cobra.Command {

go listenForUpdates(updateChan, errorChan)

report, er := runGitHistoryReport(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag, remoteFlag)
report, er := runGitHistoryReport(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag,
remoteFlag, limitFlag)

<-doneChan

Expand All @@ -164,13 +165,14 @@ func GetReportCommand() *cobra.Command {
}

if er != nil {
pterm.Error.Println("errors occurred while processing the git history")
for x := range er {
pterm.Error.Println(er[x].Error())
}
return er[0]
}

jsonBytes, _ := json.MarshalIndent(report, "", " ")
flat := FlattenHistoricalReport(report)
jsonBytes, _ := json.MarshalIndent(flat, "", " ")
fmt.Println(string(jsonBytes))
return nil

Expand Down Expand Up @@ -204,7 +206,9 @@ func GetReportCommand() *cobra.Command {
return nil
}

jsonBytes, _ := json.MarshalIndent(report, "", " ")
// flatten report
flat := FlattenReport(report)
jsonBytes, _ := json.MarshalIndent(flat, "", " ")
fmt.Println(string(jsonBytes))
return nil
}
Expand All @@ -218,7 +222,7 @@ func GetReportCommand() *cobra.Command {
}

func runGitHistoryReport(gitPath, filePath string, latest bool,
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) (*model.HistoricalReport, []error) {
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) (*model.HistoricalReport, []error) {

if gitPath == "" || filePath == "" {
err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f")
Expand All @@ -232,7 +236,7 @@ func runGitHistoryReport(gitPath, filePath string, latest bool,
filePath, gitPath), false, updateChan)

// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan)
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
if err != nil {
model.SendProgressError("git", fmt.Sprintf("%d errors found building history", len(err)), errorChan)
close(updateChan)
Expand Down
31 changes: 17 additions & 14 deletions cmd/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ func GetSummaryCommand() *cobra.Command {
} else {
if !failed {
spinner.Success("completed")
spinner.Stop()
pterm.Println()
pterm.Println()
fmt.Println()
} else {
spinner.Fail("failed to complete. sorry!")
}
Expand Down Expand Up @@ -200,14 +198,16 @@ func GetSummaryCommand() *cobra.Command {

go listenForUpdates(updateChan, errorChan)

err = runGitHistorySummary(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag, remoteFlag)
err = runGitHistorySummary(args[0], args[1], latestFlag, updateChan, errorChan, baseFlag, remoteFlag, limitFlag)

<-doneChan

if err != nil {
pterm.Error.Println(err.Error())
return err
}

return nil
} else {
go listenForUpdates(updateChan, errorChan)

Expand Down Expand Up @@ -321,11 +321,11 @@ func runLeftRightSummary(left, right string, updateChan chan *model.ProgressUpda
model.SendProgressUpdate("extraction",
fmt.Sprintf("extracted %d commits from history", len(commits)), true, updateChan)

close(updateChan)
e := printSummaryDetails(commits)
if e != nil {
return []error{e}
}
close(updateChan)
return nil
}

Expand All @@ -347,7 +347,7 @@ func runGithubHistorySummary(username, repo, filePath string, latest bool, limit
}

func runGitHistorySummary(gitPath, filePath string, latest bool,
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) error {
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int) error {
if gitPath == "" || filePath == "" {
err := errors.New("please supply a path to a git repo via -r, and a path to a file via -f")
model.SendProgressError("git", err.Error(), errorChan)
Expand All @@ -359,7 +359,7 @@ func runGitHistorySummary(gitPath, filePath string, latest bool,
filePath, gitPath), false, updateChan)

// build commit history.
commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan)
commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit)
if errs != nil {
model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(errs)), errorChan)
close(updateChan)
Expand All @@ -372,12 +372,13 @@ func runGitHistorySummary(gitPath, filePath string, latest bool,
if latest {
commitHistory = commitHistory[:1]
}
model.SendProgressUpdate("extraction",
fmt.Sprintf("extracted %d commits from history", len(commitHistory)), true, updateChan)

close(updateChan)
err := printSummaryDetails(commitHistory)

return printSummaryDetails(commitHistory)
model.SendProgressUpdate("extraction",
fmt.Sprintf("extracted %d commits from history\n", len(commitHistory)), true, updateChan)
close(updateChan)
return err
}

func printSummaryDetails(commitHistory []*model.Commit) error {
Expand Down Expand Up @@ -439,12 +440,14 @@ func printSummaryDetails(commitHistory []*model.Commit) error {
}

if c < len(commitHistory) {
pterm.Println()
//pterm.Println()
}
} else {
if tt <= 0 && tb <= 0 {
pterm.Success.Println("No changes detected")
pterm.Println()
if c+1 < len(commitHistory) {
pterm.Print(pterm.Green(fmt.Sprintf("No changes detected between %s and %s\n",
commitHistory[c].Hash, commitHistory[c+1].Hash)))
}
}
}
}
Expand Down

0 comments on commit be2ee83

Please sign in to comment.