@@ -91,6 +91,22 @@ func (r *Repository) GetObjectFormat() git.ObjectFormat {
9191 return git .ObjectFormatFromName (r .Repository .ObjectFormatName )
9292}
9393
94+ func (r * Repository ) GetRefCommit (ref string , allowedTypes ... git.RefType ) (git.RefName , * git.Commit , error ) {
95+ refName := git .RefNameFromUserInput (ref , allowedTypes ... )
96+ if refName == "" {
97+ return "" , nil , errors .New ("invalid ref" )
98+ }
99+ commitID , err := r .GitRepo .GetRefCommitID (refName .String ())
100+ if err != nil {
101+ return "" , nil , err
102+ }
103+ commit , err := r .GitRepo .GetCommit (commitID )
104+ if err != nil {
105+ return "" , nil , err
106+ }
107+ return refName , commit , nil
108+ }
109+
94110// RepoMustNotBeArchived checks if a repo is archived
95111func RepoMustNotBeArchived () func (ctx * Context ) {
96112 return func (ctx * Context ) {
@@ -780,6 +796,20 @@ type RepoRefByTypeOptions struct {
780796 IgnoreNotExistErr bool
781797}
782798
799+ func repoRefFullName (typ git.RefType , shortName string ) git.RefName {
800+ switch typ {
801+ case git .RefTypeBranch :
802+ return git .RefNameFromBranch (shortName )
803+ case git .RefTypeTag :
804+ return git .RefNameFromTag (shortName )
805+ case git .RefTypeCommit :
806+ return git .RefNameFromCommit (shortName )
807+ default :
808+ setting .PanicInDevOrTesting ("Unknown RepoRefType: %v" , typ )
809+ return git .RefNameFromBranch ("main" ) // just a dummy result, it shouldn't happen
810+ }
811+ }
812+
783813// RepoRefByType handles repository reference name for a specific type
784814// of repository reference
785815func RepoRefByType (detectRefType git.RefType , opts ... RepoRefByTypeOptions ) func (* Context ) {
@@ -842,7 +872,7 @@ func RepoRefByType(detectRefType git.RefType, opts ...RepoRefByTypeOptions) func
842872 } else {
843873 refShortName = getRefName (ctx .Base , ctx .Repo , reqPath , refType )
844874 }
845- ctx .Repo .RefFullName = git . RefNameFromTypeAndShortName (refType , refShortName )
875+ ctx .Repo .RefFullName = repoRefFullName (refType , refShortName )
846876 isRenamedBranch , has := ctx .Data ["IsRenamedBranch" ].(bool )
847877 if isRenamedBranch && has {
848878 renamedBranchName := ctx .Data ["RenamedBranchName" ].(string )
@@ -939,57 +969,6 @@ func RepoRefByType(detectRefType git.RefType, opts ...RepoRefByTypeOptions) func
939969 }
940970}
941971
942- func RepoRefByQueries () func (ctx * Context ) {
943- return func (ctx * Context ) {
944- if ctx .Repo .Repository .IsEmpty {
945- // assume the user is viewing the (non-existent) default branch
946- ctx .Repo .IsViewBranch = true
947- ctx .Repo .BranchName = ctx .Repo .Repository .DefaultBranch
948- ctx .Repo .RefFullName = git .RefNameFromBranch (ctx .Repo .BranchName )
949- return
950- }
951-
952- var err error
953- if ctx .Repo .GitRepo == nil {
954- ctx .Repo .GitRepo , err = gitrepo .RepositoryFromRequestContextOrOpen (ctx , ctx .Repo .Repository )
955- if err != nil {
956- ctx .ServerError (fmt .Sprintf ("Open Repository %v failed" , ctx .Repo .Repository .FullName ()), err )
957- return
958- }
959- }
960-
961- ctx .Repo .RefFullName = git .RefNameFromTypeAndShortName (git .RefType (ctx .FormTrim ("ref_type" )), ctx .FormTrim ("ref_name" ))
962- if ctx .Repo .RefFullName == "" {
963- ctx .Error (http .StatusBadRequest , "invalid ref type or name" )
964- return
965- }
966-
967- switch ctx .Repo .RefFullName .RefType () {
968- case git .RefTypeBranch :
969- ctx .Repo .IsViewBranch = true
970- ctx .Repo .BranchName = ctx .Repo .RefFullName .ShortName ()
971- case git .RefTypeTag :
972- ctx .Repo .IsViewTag = true
973- ctx .Repo .TagName = ctx .Repo .RefFullName .ShortName ()
974- case git .RefTypeCommit :
975- ctx .Repo .IsViewCommit = true
976- ctx .Repo .CommitID = ctx .Repo .RefFullName .ShortName ()
977- }
978-
979- ctx .Repo .Commit , err = ctx .Repo .GitRepo .GetCommit (ctx .Repo .RefFullName .String ())
980- if err != nil {
981- if git .IsErrNotExist (err ) {
982- ctx .NotFound ("GetCommit" , err )
983- } else {
984- ctx .ServerError ("GetCommit" , err )
985- }
986- return
987- }
988- ctx .Repo .CommitID = ctx .Repo .Commit .ID .String ()
989- ctx .Repo .TreePath = ctx .PathParam ("*" )
990- }
991- }
992-
993972// GitHookService checks if repository Git hooks service has been enabled.
994973func GitHookService () func (ctx * Context ) {
995974 return func (ctx * Context ) {
0 commit comments