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

Fix reading with limit and global-revisions #164

Merged
merged 6 commits into from
Oct 22, 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
Binary file modified bin/openapi-changes
Binary file not shown.
10 changes: 8 additions & 2 deletions cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,14 @@ func GetReportCommand() *cobra.Command {
}

if f.IsDir() {

repo := p
if !path.IsAbs(repo) {
wd, err := os.Getwd()
if err != nil {
return fmt.Errorf("get working dir: %v", err)
}
repo = filepath.Join(wd, repo)
}
p = args[1]
f, err = os.Stat(filepath.Join(repo, p))
if err != nil {
Expand All @@ -154,7 +160,7 @@ func GetReportCommand() *cobra.Command {

go listenForUpdates(updateChan, errorChan)

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

<-doneChan
Expand Down
32 changes: 24 additions & 8 deletions git/read_local.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,11 @@ func PopulateHistoryWithChanges(commitHistory []*model.Commit, limit int, limitT
progressChan chan *model.ProgressUpdate, errorChan chan model.ProgressError, base string, remote bool) ([]*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()
var err []error
commitHistory[c].Data, err = readFile(commitHistory[c].RepoDirectory, commitHistory[c].Hash, commitHistory[c].FilePath)
if err != nil {
return nil, []error{err}
return nil, err
}
commitHistory[c].Data = ou.Bytes()
model.SendProgressUpdate("population",
fmt.Sprintf("Extracting %d bytes extracted from commit '%s'",
len(commitHistory[c].Data)/1024, commitHistory[c].Hash), false, progressChan)
Expand Down Expand Up @@ -220,6 +215,11 @@ func BuildCommitChangelog(commitHistory []*model.Commit,
var oldBits, newBits []byte
if len(commitHistory) == c+1 {
newBits = commitHistory[c].Data

// Obtain data from the previous commit and fail gracefully, if git
// errors. This might happen when the file does not exist in the git
// history.
oldBits, _ = readFile(commitHistory[c].RepoDirectory, fmt.Sprintf("%s~1", commitHistory[c].Hash), commitHistory[c].FilePath)
} else {
oldBits = commitHistory[c+1].Data
commitHistory[c].OldData = oldBits
Expand Down Expand Up @@ -290,3 +290,19 @@ func ExtractPathAndFile(location string) (string, string) {
file := path.Base(location)
return dir, file
}

// readFile reads the specified file at the specified commit hash from the
// specified git repository.
func readFile(repoDir, hash, filePath string) ([]byte, []error) {
cmd := exec.Command(GIT, NOPAGER, SHOW, fmt.Sprintf("%s:%s", hash, filePath))
var ou, er bytes.Buffer
cmd.Stdout = &ou
cmd.Stderr = &er
cmd.Dir = repoDir
err := cmd.Run()
if err != nil {
return nil, []error{fmt.Errorf("read file from git: %v", err)}
}

return ou.Bytes(), nil
}
Empty file added test.text
Empty file.
Loading