Skip to content

Commit

Permalink
Introduce readFile function
Browse files Browse the repository at this point in the history
  • Loading branch information
felixjung authored and daveshanley committed Oct 22, 2024
1 parent c448806 commit e103ec0
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 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 @@ -290,3 +285,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
}

0 comments on commit e103ec0

Please sign in to comment.