diff --git a/modules/git/commit_info.go b/modules/git/commit_info.go index c046acbb50..a796f4a204 100644 --- a/modules/git/commit_info.go +++ b/modules/git/commit_info.go @@ -3,9 +3,20 @@ package git +import "path" + // CommitInfo describes the first commit with the provided entry type CommitInfo struct { Entry *TreeEntry Commit *Commit SubmoduleFile *CommitSubmoduleFile } + +func getCommitInfoSubmoduleFile(repoLink string, entry *TreeEntry, commit *Commit, treePathDir string) (*CommitSubmoduleFile, error) { + fullPath := path.Join(treePathDir, entry.Name()) + submodule, err := commit.GetSubModule(fullPath) + if err != nil { + return nil, err + } + return NewCommitSubmoduleFile(repoLink, fullPath, submodule.URL, entry.ID.String()), nil +} diff --git a/modules/git/commit_info_gogit.go b/modules/git/commit_info_gogit.go index 7bce1f57fb..7e03e634a0 100644 --- a/modules/git/commit_info_gogit.go +++ b/modules/git/commit_info_gogit.go @@ -71,22 +71,12 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit * commitsInfo[i].Commit = entryCommit } - // If the entry is a submodule add a submodule file for this + // If the entry is a submodule, add a submodule file for this if entry.IsSubModule() { - subModuleURL := "" - var fullPath string - if len(treePath) > 0 { - fullPath = treePath + "/" + entry.Name() - } else { - fullPath = entry.Name() - } - if subModule, err := commit.GetSubModule(fullPath); err != nil { + commitsInfo[i].SubmoduleFile, err = getCommitInfoSubmoduleFile(repoLink, entry, commit, treePath) + if err != nil { return nil, nil, err - } else if subModule != nil { - subModuleURL = subModule.URL } - subModuleFile := NewCommitSubmoduleFile(repoLink, fullPath, subModuleURL, entry.ID.String()) - commitsInfo[i].SubmoduleFile = subModuleFile } } diff --git a/modules/git/commit_info_nogogit.go b/modules/git/commit_info_nogogit.go index 4a1cf3f627..161edb7e96 100644 --- a/modules/git/commit_info_nogogit.go +++ b/modules/git/commit_info_nogogit.go @@ -62,22 +62,12 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit * log.Debug("missing commit for %s", entry.Name()) } - // If the entry is a submodule add a submodule file for this + // If the entry is a submodule, add a submodule file for this if entry.IsSubModule() { - subModuleURL := "" - var fullPath string - if len(treePath) > 0 { - fullPath = treePath + "/" + entry.Name() - } else { - fullPath = entry.Name() - } - if subModule, err := commit.GetSubModule(fullPath); err != nil { + commitsInfo[i].SubmoduleFile, err = getCommitInfoSubmoduleFile(repoLink, entry, commit, treePath) + if err != nil { return nil, nil, err - } else if subModule != nil { - subModuleURL = subModule.URL } - subModuleFile := NewCommitSubmoduleFile(repoLink, fullPath, subModuleURL, entry.ID.String()) - commitsInfo[i].SubmoduleFile = subModuleFile } }