Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global-revisions flag #158

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cmd/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func GetConsoleCommand() *cobra.Command {
doneChan := make(chan bool)
failed := false
latestFlag, _ := cmd.Flags().GetBool("top")
globalRevisionsFlag, _ := cmd.Flags().GetBool("global-revisions")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
baseFlag, _ := cmd.Flags().GetString("base")
Expand Down Expand Up @@ -205,7 +206,7 @@ func GetConsoleCommand() *cobra.Command {

go listenForUpdates(updateChan, errorChan)

commits, errs := runGitHistoryConsole(args[0], args[1], latestFlag, limitFlag, limitTimeFlag,
commits, errs := runGitHistoryConsole(args[0], args[1], latestFlag, globalRevisionsFlag, limitFlag, limitTimeFlag,
updateChan, errorChan, baseFlag, remoteFlag)

// wait.
Expand Down Expand Up @@ -301,7 +302,7 @@ func runGithubHistoryConsole(username, repo, filePath string, latest bool, limit
return commitHistory, nil
}

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

if gitPath == "" || filePath == "" {
Expand All @@ -315,7 +316,7 @@ func runGitHistoryConsole(gitPath, filePath string, latest bool, limit int, limi
filePath, gitPath), false, updateChan)

// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, globalRevisions, limit, limitTime)

if err != nil {
close(updateChan)
Expand Down
18 changes: 10 additions & 8 deletions cmd/html_report.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@ import (
"encoding/hex"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
"time"

"github.com/google/uuid"
"github.com/pb33f/libopenapi/index"
"github.com/pb33f/openapi-changes/git"
htmlReport "github.com/pb33f/openapi-changes/html-report"
"github.com/pb33f/openapi-changes/model"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)

func GetHTMLReportCommand() *cobra.Command {
Expand All @@ -41,6 +42,7 @@ func GetHTMLReportCommand() *cobra.Command {
noColorFlag, _ := cmd.Flags().GetBool("no-color")
cdnFlag, _ := cmd.Flags().GetBool("use-cdn")
latestFlag, _ := cmd.Flags().GetBool("top")
globalRevisionsFlag, _ := cmd.Flags().GetBool("global-revisions")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
remoteFlag, _ := cmd.Flags().GetBool("remote")
Expand Down Expand Up @@ -226,7 +228,7 @@ func GetHTMLReportCommand() *cobra.Command {
go listenForUpdates(updateChan, errorChan)

report, _, er := RunGitHistoryHTMLReport(args[0], args[1], latestFlag, cdnFlag,
updateChan, errorChan, baseFlag, remoteFlag, limitFlag, limitTimeFlag)
updateChan, errorChan, baseFlag, remoteFlag, globalRevisionsFlag, limitFlag, limitTimeFlag)
<-doneChan
if er != nil {
for x := range er {
Expand Down Expand Up @@ -303,7 +305,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, limit int, limitTime int) ([]byte, []*model.Report, []error) {
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, globalRevisions bool, limit int, limitTime 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 @@ -313,7 +315,7 @@ func RunGitHistoryHTMLReport(gitPath, filePath string, latest, useCDN bool,
}

// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, progressChan, errorChan, limit, limitTime)
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, progressChan, errorChan, globalRevisions, limit, limitTime)
if err != nil {
model.SendFatalError("extraction",
fmt.Sprintf("cannot extract history %s", errors.Join(err...)), errorChan)
Expand Down
7 changes: 4 additions & 3 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func GetReportCommand() *cobra.Command {
doneChan := make(chan bool)
failed := false
latestFlag, _ := cmd.Flags().GetBool("top")
globalRevisionsFlag, _ := cmd.Flags().GetBool("global-revisions")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
baseFlag, _ := cmd.Flags().GetString("base")
Expand Down Expand Up @@ -154,7 +155,7 @@ func GetReportCommand() *cobra.Command {
go listenForUpdates(updateChan, errorChan)

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

<-doneChan

Expand Down Expand Up @@ -225,7 +226,7 @@ func GetReportCommand() *cobra.Command {
}

func runGitHistoryReport(gitPath, filePath string, latest bool,
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, limit int, limitTime int) (*model.HistoricalReport, []error) {
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool, globalRevisions bool, limit int, limitTime 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 @@ -239,7 +240,7 @@ func runGitHistoryReport(gitPath, filePath string, latest bool,
filePath, gitPath), false, updateChan)

// build commit history.
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)
commitHistory, err := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, globalRevisions, limit, limitTime)
if err != nil {
model.SendProgressError("git", fmt.Sprintf("%d errors found building history", len(err)), errorChan)
close(updateChan)
Expand Down
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ package cmd

import (
"fmt"
"os"

"github.com/pterm/pterm"
"github.com/spf13/cobra"
"os"
)

var (
Expand Down Expand Up @@ -61,6 +62,7 @@ func init() {
rootCmd.AddCommand(GetHTMLReportCommand())
rootCmd.PersistentFlags().BoolP("top", "t", false, "Only show latest changes (last git revision against HEAD)")
rootCmd.PersistentFlags().IntP("limit", "l", 5, "Limit history to number of revisions (default is 5)")
rootCmd.PersistentFlags().BoolP("global-revisions", "R", false, "Consider all revisions in limit, not just the ones for the file")
rootCmd.PersistentFlags().IntP("limit-time", "d", -1, "Limit history to number of days. Supersedes limit argument if present.")
rootCmd.PersistentFlags().BoolP("no-logo", "b", false, "Don't print the big purple pb33f banner")
rootCmd.PersistentFlags().StringP("base", "p", "", "Base URL or path to use for resolving relative or remote references")
Expand Down
10 changes: 6 additions & 4 deletions cmd/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/pb33f/openapi-changes/builder"
"io"
"net/http"
"net/url"
Expand All @@ -17,6 +16,8 @@ import (
"strings"
"time"

"github.com/pb33f/openapi-changes/builder"

"github.com/google/uuid"
"github.com/pb33f/libopenapi/what-changed/reports"
"github.com/pb33f/openapi-changes/git"
Expand All @@ -42,6 +43,7 @@ func GetSummaryCommand() *cobra.Command {
baseFlag, _ := cmd.Flags().GetString("base")
latestFlag, _ := cmd.Flags().GetBool("top")
noColorFlag, _ := cmd.Flags().GetBool("no-color")
globalRevisionsFlag, _ := cmd.Flags().GetBool("global-revisions")
limitFlag, _ := cmd.Flags().GetInt("limit")
limitTimeFlag, _ := cmd.Flags().GetInt("limit-time")
remoteFlag, _ := cmd.Flags().GetBool("remote")
Expand Down Expand Up @@ -226,7 +228,7 @@ func GetSummaryCommand() *cobra.Command {
go listenForUpdates(updateChan, errorChan)

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

<-doneChan

Expand Down Expand Up @@ -390,7 +392,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, markdown bool, limit int, limitTime int) error {
updateChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote, markdown bool, globalRevisions bool, limit int, limitTime 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 @@ -402,7 +404,7 @@ func runGitHistorySummary(gitPath, filePath string, latest bool,
filePath, gitPath), false, updateChan)

// build commit history.
commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, limit, limitTime)
commitHistory, errs := git.ExtractHistoryFromFile(gitPath, filePath, updateChan, errorChan, globalRevisions, limit, limitTime)
if errs != nil {
model.SendProgressError("git", fmt.Sprintf("%d errors found extracting history", len(errs)), errorChan)
close(updateChan)
Expand Down
27 changes: 20 additions & 7 deletions git/read_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ import (
"bytes"
"errors"
"fmt"
"github.com/araddon/dateparse"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/openapi-changes/model"
"github.com/pterm/pterm"
"log/slog"
"net/url"
"os"
"os/exec"
"path"
"strconv"
"strings"
"time"

"github.com/araddon/dateparse"
"github.com/pb33f/libopenapi"
"github.com/pb33f/libopenapi/datamodel"
"github.com/pb33f/openapi-changes/model"
"github.com/pterm/pterm"
)

const (
Expand All @@ -29,6 +31,7 @@ const (
TOPLEVEL = "--show-toplevel"
NOPAGER = "--no-pager"
LOGFORMAT = "--pretty=%cD||%h||%s||%an||%ae"
NUMBER = "-n"
DIV = "--"
)

Expand Down Expand Up @@ -57,9 +60,19 @@ func GetTopLevel(dir string) (string, error) {
}

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

args := []string{NOPAGER, LOG, LOGFORMAT}

if limit > 0 && globalRevisions {
args = append(args, fmt.Sprintf("HEAD~%d..HEAD", limit))
} else if limit > 0 {
args = append(args, NUMBER, strconv.Itoa(limit))
}

args = append(args, DIV, filePath)

cmd := exec.Command(GIT, NOPAGER, LOG, LOGFORMAT, DIV, filePath)
cmd := exec.Command(GIT, args...)
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
Expand Down
9 changes: 5 additions & 4 deletions git/read_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package git

import (
"context"
"github.com/pb33f/openapi-changes/model"
"github.com/stretchr/testify/assert"
"testing"
"time"

"github.com/pb33f/openapi-changes/model"
"github.com/stretchr/testify/assert"
)

func TestCheckLocalRepoAvailable(t *testing.T) {
Expand Down Expand Up @@ -38,7 +39,7 @@ func TestExtractHistoryFromFile(t *testing.T) {

// this shit times out in the pipeline (damn you github runners)
ctx, cncl := context.WithTimeout(context.Background(), 5*time.Second)
history, _ := ExtractHistoryFromFile("./", "read_local.go", c, e, 25, -1)
history, _ := ExtractHistoryFromFile("./", "read_local.go", c, e, false, 25, -1)
defer cncl()

select {
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestExtractHistoryFromFile_Fail(t *testing.T) {
}
}()

history, _ := ExtractHistoryFromFile("./", "no_file_nope", c, e, 5, -1)
history, _ := ExtractHistoryFromFile("./", "no_file_nope", c, e, false, 5, -1)
<-d
assert.Len(t, history, 0)
}
Expand Down
Loading