|
|
@ -5,10 +5,7 @@ |
|
|
|
package git |
|
|
|
package git |
|
|
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
import ( |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"path" |
|
|
|
|
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"runtime" |
|
|
|
|
|
|
|
"sort" |
|
|
|
"sort" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
"strings" |
|
|
|
"strings" |
|
|
@ -147,112 +144,147 @@ func (tes Entries) Sort() { |
|
|
|
sort.Sort(tes) |
|
|
|
sort.Sort(tes) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type commitInfo struct { |
|
|
|
// getCommitInfoState transient state for getting commit info for entries
|
|
|
|
entryName string |
|
|
|
type getCommitInfoState struct { |
|
|
|
infos []interface{} |
|
|
|
entries map[string]*TreeEntry // map from filepath to entry
|
|
|
|
err error |
|
|
|
commits map[string]*Commit // map from entry name to commit
|
|
|
|
|
|
|
|
lastCommitHash string |
|
|
|
|
|
|
|
lastCommit *Commit |
|
|
|
|
|
|
|
treePath string |
|
|
|
|
|
|
|
headCommit *Commit |
|
|
|
|
|
|
|
nextSearchSize int // next number of commits to search for
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetCommitsInfo takes advantages of concurrency to speed up getting information
|
|
|
|
func initGetCommitInfoState(entries Entries, headCommit *Commit, treePath string) *getCommitInfoState { |
|
|
|
// of all commits that are corresponding to these entries. This method will automatically
|
|
|
|
entriesByPath := make(map[string]*TreeEntry, len(entries)) |
|
|
|
// choose the right number of goroutine (concurrency) to use related of the host CPU.
|
|
|
|
for _, entry := range entries { |
|
|
|
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) { |
|
|
|
entriesByPath[filepath.Join(treePath, entry.Name())] = entry |
|
|
|
return tes.GetCommitsInfoWithCustomConcurrency(commit, treePath, 0) |
|
|
|
} |
|
|
|
|
|
|
|
return &getCommitInfoState{ |
|
|
|
|
|
|
|
entries: entriesByPath, |
|
|
|
|
|
|
|
commits: make(map[string]*Commit, len(entriesByPath)), |
|
|
|
|
|
|
|
treePath: treePath, |
|
|
|
|
|
|
|
headCommit: headCommit, |
|
|
|
|
|
|
|
nextSearchSize: 16, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// GetCommitsInfoWithCustomConcurrency takes advantages of concurrency to speed up getting information
|
|
|
|
// GetCommitsInfo gets information of all commits that are corresponding to these entries
|
|
|
|
// of all commits that are corresponding to these entries. If the given maxConcurrency is negative or
|
|
|
|
func (tes Entries) GetCommitsInfo(commit *Commit, treePath string) ([][]interface{}, error) { |
|
|
|
// equal to zero: the right number of goroutine (concurrency) to use will be chosen related of the
|
|
|
|
state := initGetCommitInfoState(tes, commit, treePath) |
|
|
|
// host CPU.
|
|
|
|
if err := getCommitsInfo(state); err != nil { |
|
|
|
func (tes Entries) GetCommitsInfoWithCustomConcurrency(commit *Commit, treePath string, maxConcurrency int) ([][]interface{}, error) { |
|
|
|
return nil, err |
|
|
|
if len(tes) == 0 { |
|
|
|
|
|
|
|
return nil, nil |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if maxConcurrency <= 0 { |
|
|
|
commitsInfo := make([][]interface{}, len(tes)) |
|
|
|
maxConcurrency = runtime.NumCPU() |
|
|
|
for i, entry := range tes { |
|
|
|
|
|
|
|
commit = state.commits[filepath.Join(treePath, entry.Name())] |
|
|
|
|
|
|
|
switch entry.Type { |
|
|
|
|
|
|
|
case ObjectCommit: |
|
|
|
|
|
|
|
subModuleURL := "" |
|
|
|
|
|
|
|
if subModule, err := state.headCommit.GetSubModule(entry.Name()); err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} else if subModule != nil { |
|
|
|
|
|
|
|
subModuleURL = subModule.URL |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
subModuleFile := NewSubModuleFile(commit, subModuleURL, entry.ID.String()) |
|
|
|
// Length of taskChan determines how many goroutines (subprocesses) can run at the same time.
|
|
|
|
commitsInfo[i] = []interface{}{entry, subModuleFile} |
|
|
|
// The length of revChan should be same as taskChan so goroutines whoever finished job can
|
|
|
|
default: |
|
|
|
// exit as early as possible, only store data inside channel.
|
|
|
|
commitsInfo[i] = []interface{}{entry, commit} |
|
|
|
taskChan := make(chan bool, maxConcurrency) |
|
|
|
|
|
|
|
revChan := make(chan commitInfo, maxConcurrency) |
|
|
|
|
|
|
|
doneChan := make(chan error) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Receive loop will exit when it collects same number of data pieces as tree entries.
|
|
|
|
|
|
|
|
// It notifies doneChan before exits or notify early with possible error.
|
|
|
|
|
|
|
|
infoMap := make(map[string][]interface{}, len(tes)) |
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
|
|
i := 0 |
|
|
|
|
|
|
|
for info := range revChan { |
|
|
|
|
|
|
|
if info.err != nil { |
|
|
|
|
|
|
|
doneChan <- info.err |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
infoMap[info.entryName] = info.infos |
|
|
|
|
|
|
|
i++ |
|
|
|
|
|
|
|
if i == len(tes) { |
|
|
|
|
|
|
|
break |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return commitsInfo, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (state *getCommitInfoState) nextCommit(hash string) { |
|
|
|
|
|
|
|
state.lastCommitHash = hash |
|
|
|
|
|
|
|
state.lastCommit = nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (state *getCommitInfoState) commit() (*Commit, error) { |
|
|
|
|
|
|
|
var err error |
|
|
|
|
|
|
|
if state.lastCommit == nil { |
|
|
|
|
|
|
|
state.lastCommit, err = state.headCommit.repo.GetCommit(state.lastCommitHash) |
|
|
|
} |
|
|
|
} |
|
|
|
doneChan <- nil |
|
|
|
return state.lastCommit, err |
|
|
|
}() |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for i := range tes { |
|
|
|
func (state *getCommitInfoState) update(path string) error { |
|
|
|
// When taskChan is idle (or has empty slots), put operation will not block.
|
|
|
|
relPath, err := filepath.Rel(state.treePath, path) |
|
|
|
// However when taskChan is full, code will block and wait any running goroutines to finish.
|
|
|
|
|
|
|
|
taskChan <- true |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if tes[i].Type != ObjectCommit { |
|
|
|
|
|
|
|
go func(i int) { |
|
|
|
|
|
|
|
cinfo := commitInfo{entryName: tes[i].Name()} |
|
|
|
|
|
|
|
c, err := commit.GetCommitByPath(filepath.Join(treePath, tes[i].Name())) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
cinfo.err = fmt.Errorf("GetCommitByPath (%s/%s): %v", treePath, tes[i].Name(), err) |
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
var entryPath string |
|
|
|
|
|
|
|
if index := strings.IndexRune(relPath, '/'); index >= 0 { |
|
|
|
|
|
|
|
entryPath = filepath.Join(state.treePath, relPath[:index]) |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
cinfo.infos = []interface{}{tes[i], c} |
|
|
|
entryPath = path |
|
|
|
} |
|
|
|
} |
|
|
|
revChan <- cinfo |
|
|
|
if _, ok := state.entries[entryPath]; !ok { |
|
|
|
<-taskChan // Clear one slot from taskChan to allow new goroutines to start.
|
|
|
|
return nil |
|
|
|
}(i) |
|
|
|
} else if _, ok := state.commits[entryPath]; ok { |
|
|
|
continue |
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
state.commits[entryPath], err = state.commit() |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Handle submodule
|
|
|
|
func getCommitsInfo(state *getCommitInfoState) error { |
|
|
|
go func(i int) { |
|
|
|
for len(state.entries) > len(state.commits) { |
|
|
|
cinfo := commitInfo{entryName: tes[i].Name()} |
|
|
|
if err := getNextCommitInfos(state); err != nil { |
|
|
|
sm, err := commit.GetSubModule(path.Join(treePath, tes[i].Name())) |
|
|
|
return err |
|
|
|
if err != nil && !IsErrNotExist(err) { |
|
|
|
|
|
|
|
cinfo.err = fmt.Errorf("GetSubModule (%s/%s): %v", treePath, tes[i].Name(), err) |
|
|
|
|
|
|
|
revChan <- cinfo |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
smURL := "" |
|
|
|
|
|
|
|
if sm != nil { |
|
|
|
|
|
|
|
smURL = sm.URL |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
c, err := commit.GetCommitByPath(filepath.Join(treePath, tes[i].Name())) |
|
|
|
func getNextCommitInfos(state *getCommitInfoState) error { |
|
|
|
|
|
|
|
logOutput, err := logCommand(state.lastCommitHash, state).RunInDir(state.headCommit.repo.Path) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
cinfo.err = fmt.Errorf("GetCommitByPath (%s/%s): %v", treePath, tes[i].Name(), err) |
|
|
|
return err |
|
|
|
} else { |
|
|
|
} |
|
|
|
cinfo.infos = []interface{}{tes[i], NewSubModuleFile(c, smURL, tes[i].ID.String())} |
|
|
|
lines := strings.Split(logOutput, "\n") |
|
|
|
|
|
|
|
i := 0 |
|
|
|
|
|
|
|
for i < len(lines) { |
|
|
|
|
|
|
|
state.nextCommit(lines[i]) |
|
|
|
|
|
|
|
i++ |
|
|
|
|
|
|
|
for ; i < len(lines); i++ { |
|
|
|
|
|
|
|
path := lines[i] |
|
|
|
|
|
|
|
if path == "" { |
|
|
|
|
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
revChan <- cinfo |
|
|
|
state.update(path) |
|
|
|
<-taskChan |
|
|
|
} |
|
|
|
}(i) |
|
|
|
i++ // skip blank line
|
|
|
|
|
|
|
|
if len(state.entries) == len(state.commits) { |
|
|
|
|
|
|
|
break |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err := <-doneChan; err != nil { |
|
|
|
|
|
|
|
return nil, err |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
commitsInfo := make([][]interface{}, len(tes)) |
|
|
|
func logCommand(exclusiveStartHash string, state *getCommitInfoState) *Command { |
|
|
|
for i := 0; i < len(tes); i++ { |
|
|
|
var commitHash string |
|
|
|
commitsInfo[i] = infoMap[tes[i].Name()] |
|
|
|
if len(exclusiveStartHash) == 0 { |
|
|
|
|
|
|
|
commitHash = "HEAD" |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
commitHash = exclusiveStartHash + "^" |
|
|
|
} |
|
|
|
} |
|
|
|
return commitsInfo, nil |
|
|
|
var command *Command |
|
|
|
|
|
|
|
numRemainingEntries := len(state.entries) - len(state.commits) |
|
|
|
|
|
|
|
if numRemainingEntries < 32 { |
|
|
|
|
|
|
|
searchSize := (numRemainingEntries + 1) / 2 |
|
|
|
|
|
|
|
command = NewCommand("log", prettyLogFormat, "--name-only", |
|
|
|
|
|
|
|
"-"+strconv.Itoa(searchSize), commitHash, "--") |
|
|
|
|
|
|
|
for path, entry := range state.entries { |
|
|
|
|
|
|
|
if _, ok := state.commits[entry.Name()]; !ok { |
|
|
|
|
|
|
|
command.AddArguments(path) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
command = NewCommand("log", prettyLogFormat, "--name-only", |
|
|
|
|
|
|
|
"-"+strconv.Itoa(state.nextSearchSize), commitHash, "--", state.treePath) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
state.nextSearchSize += state.nextSearchSize |
|
|
|
|
|
|
|
return command |
|
|
|
} |
|
|
|
} |
|
|
|