mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 03:25:11 +01:00 
			
		
		
		
	This is a follow-up to https://github.com/go-gitea/gitea/pull/33097. When linking a submodule at a commit in either the repo view, or a diff when adding a new submodule, link to the tree view of that submodules intead of the individual commit. This shows the user the full tree, instead of the diff of the commit. This makes the assumption that the tree for a given SHA is at `<repo_url>/tree/<sha>`. This URL format is supported by both Github & Gitlab, but not Gitea. To fix this, add a redirect from `<username>/<repo>/tree/<ref>` to `<username>/<repo>/src/<ref>`, so that Gitea can support this URL structure.
		
			
				
	
	
		
			55 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // Copyright 2019 The Gitea Authors. All rights reserved.
 | |
| // Copyright 2015 The Gogs Authors. All rights reserved.
 | |
| // SPDX-License-Identifier: MIT
 | |
| 
 | |
| package git
 | |
| 
 | |
| import (
 | |
| 	"context"
 | |
| 
 | |
| 	giturl "code.gitea.io/gitea/modules/git/url"
 | |
| )
 | |
| 
 | |
| // CommitSubmoduleFile represents a file with submodule type.
 | |
| type CommitSubmoduleFile struct {
 | |
| 	refURL    string
 | |
| 	parsedURL *giturl.RepositoryURL
 | |
| 	parsed    bool
 | |
| 	refID     string
 | |
| 	repoLink  string
 | |
| }
 | |
| 
 | |
| // NewCommitSubmoduleFile create a new submodule file
 | |
| func NewCommitSubmoduleFile(refURL, refID string) *CommitSubmoduleFile {
 | |
| 	return &CommitSubmoduleFile{refURL: refURL, refID: refID}
 | |
| }
 | |
| 
 | |
| func (sf *CommitSubmoduleFile) RefID() string {
 | |
| 	return sf.refID // this function is only used in templates
 | |
| }
 | |
| 
 | |
| // 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 {
 | |
| 	if sf == nil {
 | |
| 		return nil
 | |
| 	}
 | |
| 	if !sf.parsed {
 | |
| 		sf.parsed = true
 | |
| 		parsedURL, err := giturl.ParseRepositoryURL(ctx, sf.refURL)
 | |
| 		if err != nil {
 | |
| 			return nil
 | |
| 		}
 | |
| 		sf.parsedURL = parsedURL
 | |
| 		sf.repoLink = giturl.MakeRepositoryWebLink(sf.parsedURL)
 | |
| 	}
 | |
| 	var commitLink string
 | |
| 	if len(optCommitID) == 2 {
 | |
| 		commitLink = sf.repoLink + "/compare/" + optCommitID[0] + "..." + optCommitID[1]
 | |
| 	} else if len(optCommitID) == 1 {
 | |
| 		commitLink = sf.repoLink + "/tree/" + optCommitID[0]
 | |
| 	} else {
 | |
| 		commitLink = sf.repoLink + "/tree/" + sf.refID
 | |
| 	}
 | |
| 	return &SubmoduleWebLink{RepoWebLink: sf.repoLink, CommitWebLink: commitLink}
 | |
| }
 |