mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 01:36:48 +00:00 
			
		
		
		
	Backport #35056
This commit is contained in:
		| @@ -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 | ||||
| } | ||||
|   | ||||
| @@ -16,7 +16,7 @@ import ( | ||||
| ) | ||||
|  | ||||
| // GetCommitsInfo gets information of all commits that are corresponding to these entries | ||||
| func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath string) ([]CommitInfo, *Commit, error) { | ||||
| func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit *Commit, treePath string) ([]CommitInfo, *Commit, error) { | ||||
| 	entryPaths := make([]string, len(tes)+1) | ||||
| 	// Get the commit for the treePath itself | ||||
| 	entryPaths[0] = "" | ||||
| @@ -71,22 +71,12 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath | ||||
| 			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(subModuleURL, entry.ID.String()) | ||||
| 			commitsInfo[i].SubmoduleFile = subModuleFile | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -16,7 +16,7 @@ import ( | ||||
| ) | ||||
|  | ||||
| // GetCommitsInfo gets information of all commits that are corresponding to these entries | ||||
| func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath string) ([]CommitInfo, *Commit, error) { | ||||
| func (tes Entries) GetCommitsInfo(ctx context.Context, repoLink string, commit *Commit, treePath string) ([]CommitInfo, *Commit, error) { | ||||
| 	entryPaths := make([]string, len(tes)+1) | ||||
| 	// Get the commit for the treePath itself | ||||
| 	entryPaths[0] = "" | ||||
| @@ -65,22 +65,12 @@ func (tes Entries) GetCommitsInfo(ctx context.Context, commit *Commit, treePath | ||||
| 			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(subModuleURL, entry.ID.String()) | ||||
| 			commitsInfo[i].SubmoduleFile = subModuleFile | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -82,7 +82,7 @@ func testGetCommitsInfo(t *testing.T, repo1 *Repository) { | ||||
| 		} | ||||
|  | ||||
| 		// FIXME: Context.TODO() - if graceful has started we should use its Shutdown context otherwise use install signals in TestMain. | ||||
| 		commitsInfo, treeCommit, err := entries.GetCommitsInfo(t.Context(), commit, testCase.Path) | ||||
| 		commitsInfo, treeCommit, err := entries.GetCommitsInfo(t.Context(), "/any/repo-link", commit, testCase.Path) | ||||
| 		assert.NoError(t, err, "Unable to get commit information for entries of subtree: %s in commit: %s from testcase due to error: %v", testCase.Path, testCase.CommitID, err) | ||||
| 		if err != nil { | ||||
| 			t.FailNow() | ||||
| @@ -159,7 +159,7 @@ func BenchmarkEntries_GetCommitsInfo(b *testing.B) { | ||||
| 		b.ResetTimer() | ||||
| 		b.Run(benchmark.name, func(b *testing.B) { | ||||
| 			for b.Loop() { | ||||
| 				_, _, err := entries.GetCommitsInfo(b.Context(), commit, "") | ||||
| 				_, _, err := entries.GetCommitsInfo(b.Context(), "/any/repo-link", commit, "") | ||||
| 				if err != nil { | ||||
| 					b.Fatal(err) | ||||
| 				} | ||||
|   | ||||
| @@ -6,57 +6,61 @@ package git | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"path" | ||||
| 	"strings" | ||||
|  | ||||
| 	giturl "code.gitea.io/gitea/modules/git/url" | ||||
| 	"code.gitea.io/gitea/modules/util" | ||||
| ) | ||||
|  | ||||
| // CommitSubmoduleFile represents a file with submodule type. | ||||
| type CommitSubmoduleFile struct { | ||||
| 	refURL string | ||||
| 	refID  string | ||||
| 	repoLink string | ||||
| 	fullPath string | ||||
| 	refURL   string | ||||
| 	refID    string | ||||
|  | ||||
| 	parsed         bool | ||||
| 	targetRepoLink string | ||||
| 	parsed           bool | ||||
| 	parsedTargetLink string | ||||
| } | ||||
|  | ||||
| // NewCommitSubmoduleFile create a new submodule file | ||||
| func NewCommitSubmoduleFile(refURL, refID string) *CommitSubmoduleFile { | ||||
| 	return &CommitSubmoduleFile{refURL: refURL, refID: refID} | ||||
| func NewCommitSubmoduleFile(repoLink, fullPath, refURL, refID string) *CommitSubmoduleFile { | ||||
| 	return &CommitSubmoduleFile{repoLink: repoLink, fullPath: fullPath, refURL: refURL, refID: refID} | ||||
| } | ||||
|  | ||||
| func (sf *CommitSubmoduleFile) RefID() string { | ||||
| 	return sf.refID // this function is only used in templates | ||||
| 	return sf.refID | ||||
| } | ||||
|  | ||||
| // SubmoduleWebLink tries to make some web links for a submodule, it also works on "nil" receiver | ||||
| func (sf *CommitSubmoduleFile) SubmoduleWebLink(ctx context.Context, optCommitID ...string) *SubmoduleWebLink { | ||||
| func (sf *CommitSubmoduleFile) getWebLinkInTargetRepo(ctx context.Context, moreLinkPath string) *SubmoduleWebLink { | ||||
| 	if sf == nil { | ||||
| 		return nil | ||||
| 	} | ||||
| 	if strings.HasPrefix(sf.refURL, "../") { | ||||
| 		targetLink := path.Join(sf.repoLink, path.Dir(sf.fullPath), sf.refURL) | ||||
| 		return &SubmoduleWebLink{RepoWebLink: targetLink, CommitWebLink: targetLink + moreLinkPath} | ||||
| 	} | ||||
| 	if !sf.parsed { | ||||
| 		sf.parsed = true | ||||
| 		if strings.HasPrefix(sf.refURL, "../") { | ||||
| 			// FIXME: when handling relative path, this logic is not right. It needs to: | ||||
| 			// 1. Remember the submodule's full path and its commit's repo home link | ||||
| 			// 2. Resolve the relative path: targetRepoLink = path.Join(repoHomeLink, path.Dir(submoduleFullPath), refURL) | ||||
| 			// Not an easy task and need to refactor related code a lot. | ||||
| 			sf.targetRepoLink = sf.refURL | ||||
| 		} else { | ||||
| 			parsedURL, err := giturl.ParseRepositoryURL(ctx, sf.refURL) | ||||
| 			if err != nil { | ||||
| 				return nil | ||||
| 			} | ||||
| 			sf.targetRepoLink = giturl.MakeRepositoryWebLink(parsedURL) | ||||
| 		parsedURL, err := giturl.ParseRepositoryURL(ctx, sf.refURL) | ||||
| 		if err != nil { | ||||
| 			return nil | ||||
| 		} | ||||
| 		sf.parsedTargetLink = giturl.MakeRepositoryWebLink(parsedURL) | ||||
| 	} | ||||
| 	var commitLink string | ||||
| 	if len(optCommitID) == 2 { | ||||
| 		commitLink = sf.targetRepoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1] | ||||
| 	} else if len(optCommitID) == 1 { | ||||
| 		commitLink = sf.targetRepoLink + "/tree/" + optCommitID[0] | ||||
| 	} else { | ||||
| 		commitLink = sf.targetRepoLink + "/tree/" + sf.refID | ||||
| 	} | ||||
| 	return &SubmoduleWebLink{RepoWebLink: sf.targetRepoLink, CommitWebLink: commitLink} | ||||
| 	return &SubmoduleWebLink{RepoWebLink: sf.parsedTargetLink, CommitWebLink: sf.parsedTargetLink + moreLinkPath} | ||||
| } | ||||
|  | ||||
| // SubmoduleWebLinkTree tries to make the submodule's tree link in its own repo, it also works on "nil" receiver | ||||
| func (sf *CommitSubmoduleFile) SubmoduleWebLinkTree(ctx context.Context, optCommitID ...string) *SubmoduleWebLink { | ||||
| 	if sf == nil { | ||||
| 		return nil | ||||
| 	} | ||||
| 	return sf.getWebLinkInTargetRepo(ctx, "/tree/"+util.OptionalArg(optCommitID, sf.refID)) | ||||
| } | ||||
|  | ||||
| // SubmoduleWebLinkCompare tries to make the submodule's compare link in its own repo, it also works on "nil" receiver | ||||
| func (sf *CommitSubmoduleFile) SubmoduleWebLinkCompare(ctx context.Context, commitID1, commitID2 string) *SubmoduleWebLink { | ||||
| 	return sf.getWebLinkInTargetRepo(ctx, "/compare/"+commitID1+"..."+commitID2) | ||||
| } | ||||
|   | ||||
| @@ -10,29 +10,29 @@ import ( | ||||
| ) | ||||
|  | ||||
| func TestCommitSubmoduleLink(t *testing.T) { | ||||
| 	wl := (*CommitSubmoduleFile)(nil).SubmoduleWebLink(t.Context()) | ||||
| 	assert.Nil(t, wl) | ||||
| 	assert.Nil(t, (*CommitSubmoduleFile)(nil).SubmoduleWebLinkTree(t.Context())) | ||||
| 	assert.Nil(t, (*CommitSubmoduleFile)(nil).SubmoduleWebLinkCompare(t.Context(), "", "")) | ||||
|  | ||||
| 	t.Run("GitHubRepo", func(t *testing.T) { | ||||
| 		sf := NewCommitSubmoduleFile("git@github.com:user/repo.git", "aaaa") | ||||
|  | ||||
| 		wl := sf.SubmoduleWebLink(t.Context()) | ||||
| 		sf := NewCommitSubmoduleFile("/any/repo-link", "full-path", "git@github.com:user/repo.git", "aaaa") | ||||
| 		wl := sf.SubmoduleWebLinkTree(t.Context()) | ||||
| 		assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink) | ||||
| 		assert.Equal(t, "https://github.com/user/repo/tree/aaaa", wl.CommitWebLink) | ||||
|  | ||||
| 		wl = sf.SubmoduleWebLink(t.Context(), "1111") | ||||
| 		assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink) | ||||
| 		assert.Equal(t, "https://github.com/user/repo/tree/1111", wl.CommitWebLink) | ||||
|  | ||||
| 		wl = sf.SubmoduleWebLink(t.Context(), "1111", "2222") | ||||
| 		wl = sf.SubmoduleWebLinkCompare(t.Context(), "1111", "2222") | ||||
| 		assert.Equal(t, "https://github.com/user/repo", wl.RepoWebLink) | ||||
| 		assert.Equal(t, "https://github.com/user/repo/compare/1111...2222", wl.CommitWebLink) | ||||
| 	}) | ||||
|  | ||||
| 	t.Run("RelativePath", func(t *testing.T) { | ||||
| 		sf := NewCommitSubmoduleFile("../../user/repo", "aaaa") | ||||
| 		wl := sf.SubmoduleWebLink(t.Context()) | ||||
| 		assert.Equal(t, "../../user/repo", wl.RepoWebLink) | ||||
| 		assert.Equal(t, "../../user/repo/tree/aaaa", wl.CommitWebLink) | ||||
| 		sf := NewCommitSubmoduleFile("/subpath/any/repo-home-link", "full-path", "../../user/repo", "aaaa") | ||||
| 		wl := sf.SubmoduleWebLinkTree(t.Context()) | ||||
| 		assert.Equal(t, "/subpath/user/repo", wl.RepoWebLink) | ||||
| 		assert.Equal(t, "/subpath/user/repo/tree/aaaa", wl.CommitWebLink) | ||||
|  | ||||
| 		sf = NewCommitSubmoduleFile("/subpath/any/repo-home-link", "dir/submodule", "../../../user/repo", "aaaa") | ||||
| 		wl = sf.SubmoduleWebLinkCompare(t.Context(), "1111", "2222") | ||||
| 		assert.Equal(t, "/subpath/user/repo", wl.RepoWebLink) | ||||
| 		assert.Equal(t, "/subpath/user/repo/compare/1111...2222", wl.CommitWebLink) | ||||
| 	}) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user